Trog-Themes-themed-templates.t 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use Test::More;
  6. use Test::MockModule qw{strict};
  7. use Path::Tiny();
  8. use FindBin::libs;
  9. require_ok('Trog::Themes') or BAIL_OUT("Can't find SUT");
  10. # A stock template tree and a theme which only partially overrides it.
  11. my $root = Path::Tiny->tempdir();
  12. my $stock = $root->child('www/templates/html/components/forms');
  13. my $theme = $root->child('www/themes/spec/templates/html/components/forms');
  14. $_->mkpath foreach ( $stock, $theme );
  15. $stock->child($_)->spew_utf8("stock $_") foreach qw{blog.tx series.tx profile.tx blog.json series.json};
  16. $theme->child($_)->spew_utf8("themed $_") foreach qw{blog.tx gallery.tx blog.json};
  17. my $themedir = '';
  18. my $mock = Test::MockModule->new('Trog::Themes');
  19. $mock->redefine( 'get_dir', sub { $themedir } );
  20. # $template_dir is what the stock tree hangs off; point it at the fixture.
  21. {
  22. no warnings qw{once};
  23. $Trog::Themes::template_dir = "$root/www/templates";
  24. }
  25. subtest 'with no theme, nothing changes' => sub {
  26. $themedir = '';
  27. is_deeply(
  28. [ Trog::Themes::template_dirs( 'text/html', 1 ) ],
  29. ["$root/www/templates/html/components"],
  30. "only the stock dir is searched"
  31. );
  32. my $listed = [ sort @{ Trog::Themes::themed_templates_in_dir( 'forms', 'text/html', 1 ) } ];
  33. is_deeply( $listed, [qw{blog.tx profile.tx series.tx}], "the stock templates are listed" );
  34. };
  35. subtest 'with a partial theme, the stock templates show through' => sub {
  36. $themedir = "$root/www/themes/spec";
  37. is_deeply(
  38. [ Trog::Themes::template_dirs( 'text/html', 1 ) ],
  39. [ "$root/www/themes/spec/templates/html/components", "$root/www/templates/html/components" ],
  40. "the theme is searched first, then the stock tree"
  41. );
  42. is_deeply(
  43. [ sort @{ Trog::Themes::themed_templates_in_dir( 'forms', 'text/html', 1 ) } ],
  44. [qw{blog.tx gallery.tx profile.tx series.tx}],
  45. "themed_templates_in_dir() adds back the ones the theme didn't override"
  46. );
  47. my $listed = Trog::Themes::themed_templates_in_dir( 'forms', 'text/html', 1 );
  48. is( scalar( grep { $_ eq 'blog.tx' } @$listed ), 1, "an overridden template is listed once, not twice" );
  49. is( $listed->[0], 'blog.tx', "and the theme's copies come first" );
  50. };
  51. subtest 'themed_file_in_dir prefers the theme, falls back to stock' => sub {
  52. $themedir = "$root/www/themes/spec";
  53. is(
  54. Trog::Themes::themed_file_in_dir( qw{forms blog.json text/html}, 1 ),
  55. "$theme/blog.json", "the theme's copy wins"
  56. );
  57. is(
  58. Trog::Themes::themed_file_in_dir( qw{forms series.json text/html}, 1 ),
  59. "$stock/series.json", "one the theme lacks falls back"
  60. );
  61. is(
  62. Trog::Themes::themed_file_in_dir( qw{forms nope.json text/html}, 1 ),
  63. undef, "and one nobody has is undef, not a path that doesn't exist"
  64. );
  65. };
  66. subtest 'routes: a theme ships routes.pm under a dir relative to the tCMS root' => sub {
  67. my $site = Path::Tiny->tempdir();
  68. $site->child('www/themes/routed')->mkpath;
  69. $site->child('www/themes/routed/routes.pm')->spew_utf8(<<'PM');
  70. package Theme;
  71. our %routes = ( '/spec-themed' => { method => 'GET' } );
  72. 1;
  73. PM
  74. my $was = Path::Tiny->cwd;
  75. chdir $site or die "chdir $site: $!";
  76. $themedir = 'www/themes/routed';
  77. my %routes = eval { Trog::Themes::routes() };
  78. my $err = $@;
  79. chdir $was or die "chdir $was: $!";
  80. is( $err, '', 'it loads' );
  81. is_deeply( [ keys %routes ], ['/spec-themed'], 'and the routes are the ones it declared' );
  82. };
  83. subtest 'an empty or missing dir is not fatal' => sub {
  84. $themedir = "$root/www/themes/spec";
  85. is_deeply( Trog::Themes::themed_templates_in_dir( 'nosuchdir', 'text/html', 1 ), [], "just an empty list" );
  86. };
  87. done_testing();