Trog-Themes.t 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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::Fatal qw{exception};
  7. use Test::MockModule qw{strict};
  8. use Path::Tiny();
  9. use FindBin::libs;
  10. require_ok('Trog::Themes') or BAIL_OUT("Can't find SUT");
  11. # A document root with a stock copy of every file, and a theme which overrides
  12. # some of them. The helpers answer paths relative to the tCMS root, so the
  13. # test runs from the root of the fixture.
  14. my $root = Path::Tiny->tempdir();
  15. foreach my $file (qw{img/a.png img/b.png scripts/a.js styles/a.css styles/b.css}) {
  16. $root->child("www/$file")->touchpath;
  17. }
  18. $root->child("www/themes/spec/$_")->touchpath foreach qw{img/a.png scripts/a.js styles/a.css};
  19. chdir("$root") or BAIL_OUT("Cannot enter the fixture: $!");
  20. my $themedir = '';
  21. my $mock = Test::MockModule->new('Trog::Themes');
  22. $mock->redefine( 'get_dir', sub { $themedir } );
  23. subtest 'url_path' => sub {
  24. is( Trog::Themes::url_path('www/img/a.png'), '/img/a.png', "a file under www/ is served from the root of the site" );
  25. is( Trog::Themes::url_path('www/themes/spec/img/a.png'), '/themes/spec/img/a.png', "and so is one in a theme" );
  26. like( exception { Trog::Themes::url_path('lib/Trog/Themes.pm') }, qr/not[ ]under[ ]www/, "a file outside the document root has no URL" );
  27. like( exception { Trog::Themes::url_path('wwwx/img/a.png') }, qr/not[ ]under[ ]www/, "nor does one in a directory whose name only starts with www" );
  28. like( exception { Trog::Themes::url_path('/www/img/a.png') }, qr/not[ ]under[ ]www/, "nor a URL path, which is not a file" );
  29. };
  30. subtest 'with no theme' => sub {
  31. $themedir = '';
  32. is( Trog::Themes::td(), '', "td() is empty" );
  33. is( Trog::Themes::themed('img/a.png'), 'www/img/a.png', "themed() answers the stock file" );
  34. is( Trog::Themes::themed_script('a.js'), 'www/scripts/a.js', "and so does themed_script()" );
  35. is_deeply( [ Trog::Themes::themed_style('a.css') ], ['/styles/a.css'], "themed_style() links the stock sheet alone" );
  36. };
  37. subtest 'with a theme' => sub {
  38. $themedir = 'www/themes/spec';
  39. is( Trog::Themes::td(), '/themes/spec', "td() is the URL path of the theme, not its directory" );
  40. is( Trog::Themes::themed('img/a.png'), 'www/themes/spec/img/a.png', "themed() answers the theme's copy" );
  41. is( Trog::Themes::themed('img/b.png'), 'www/img/b.png', "and the stock file where the theme has no copy" );
  42. is( Trog::Themes::themed_script('a.js'), 'www/themes/spec/scripts/a.js', "themed_script() answers the theme's copy" );
  43. ok( Path::Tiny::path( Trog::Themes::themed("img/b.png") )->is_file, "what themed() answers is a file" );
  44. is_deeply( [ Trog::Themes::themed_style('a.css') ], [ '/styles/a.css', '/themes/spec/styles/a.css' ], "themed_style() links the stock sheet, then the theme's" );
  45. is_deeply( [ Trog::Themes::themed_style('b.css') ], ['/styles/b.css'], "and the stock sheet alone where the theme has none" );
  46. };
  47. done_testing();