Trog-Themes-themed-templates.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use 5.040;
  2. use re '/aasx';
  3. use warnings FATAL => 'all';
  4. use Test::More;
  5. use Test::MockModule qw{strict};
  6. use Path::Tiny();
  7. use FindBin;
  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 $old = [ sort @{ Trog::Themes::templates_in_dir( 'forms', 'text/html', 1 ) } ];
  33. my $new = [ sort @{ Trog::Themes::themed_templates_in_dir( 'forms', 'text/html', 1 ) } ];
  34. is_deeply( $new, $old, "the themed helper agrees with the old one" );
  35. is_deeply( $new, [qw{blog.tx profile.tx series.tx}], "...and lists the stock templates" );
  36. };
  37. subtest 'with a partial theme, the two disagree' => sub {
  38. $themedir = "$root/www/themes/spec";
  39. is_deeply(
  40. [ Trog::Themes::template_dirs( 'text/html', 1 ) ],
  41. [ "$root/www/themes/spec/templates/html/components", "$root/www/templates/html/components" ],
  42. "the theme is searched first, then the stock tree"
  43. );
  44. # This is the shortcoming: one forms/ dir in a theme hides every stock
  45. # post type the theme didn't happen to copy.
  46. is_deeply(
  47. [ sort @{ Trog::Themes::templates_in_dir( 'forms', 'text/html', 1 ) } ],
  48. [qw{blog.tx gallery.tx}],
  49. "templates_in_dir() sees only the theme's"
  50. );
  51. is_deeply(
  52. [ sort @{ Trog::Themes::themed_templates_in_dir( 'forms', 'text/html', 1 ) } ],
  53. [qw{blog.tx gallery.tx profile.tx series.tx}],
  54. "themed_templates_in_dir() adds back the ones the theme didn't override"
  55. );
  56. my $listed = Trog::Themes::themed_templates_in_dir( 'forms', 'text/html', 1 );
  57. is( scalar( grep { $_ eq 'blog.tx' } @$listed ), 1, "an overridden template is listed once, not twice" );
  58. is( $listed->[0], 'blog.tx', "and the theme's copies come first" );
  59. };
  60. subtest 'themed_file_in_dir prefers the theme, falls back to stock' => sub {
  61. $themedir = "$root/www/themes/spec";
  62. is(
  63. Trog::Themes::themed_file_in_dir( qw{forms blog.json text/html}, 1 ),
  64. "$theme/blog.json", "the theme's copy wins"
  65. );
  66. is(
  67. Trog::Themes::themed_file_in_dir( qw{forms series.json text/html}, 1 ),
  68. "$stock/series.json", "one the theme lacks falls back"
  69. );
  70. is(
  71. Trog::Themes::themed_file_in_dir( qw{forms nope.json text/html}, 1 ),
  72. undef, "and one nobody has is undef, not a path that doesn't exist"
  73. );
  74. };
  75. subtest 'routes: a theme ships routes.pm under a dir relative to the tCMS root' => sub {
  76. my $site = Path::Tiny->tempdir();
  77. $site->child('www/themes/routed')->mkpath;
  78. $site->child('www/themes/routed/routes.pm')->spew_utf8(<<'PM');
  79. package Theme;
  80. our %routes = ( '/spec-themed' => { method => 'GET' } );
  81. 1;
  82. PM
  83. my $was = Path::Tiny->cwd;
  84. chdir $site or die "chdir $site: $!";
  85. $themedir = 'www/themes/routed';
  86. my %routes = eval { Trog::Themes::routes() };
  87. my $err = $@;
  88. chdir $was or die "chdir $was: $!";
  89. is( $err, '', 'it loads' );
  90. is_deeply( [ keys %routes ], ['/spec-themed'], 'and the routes are the ones it declared' );
  91. };
  92. subtest 'an empty or missing dir is not fatal' => sub {
  93. $themedir = "$root/www/themes/spec";
  94. is_deeply( Trog::Themes::themed_templates_in_dir( 'nosuchdir', 'text/html', 1 ), [], "just an empty list" );
  95. };
  96. done_testing();