Bug 13276: t/XSLT.t shouldn't depend on the DB
[koha.git] / t / XSLT.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 8;
21 use File::Temp;
22 use File::Path qw/make_path/;
23 use Test::MockModule;
24 use DBD::Mock;
25
26 # Mock the DB connexion and C4::Context
27 my $context = new Test::MockModule('C4::Context');
28 $context->mock( '_new_dbh', sub {
29         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
30           || die "Cannot create handle: $DBI::errstr\n";
31         return $dbh;
32 });
33
34 use_ok('C4::XSLT');
35
36 my $dir = File::Temp->newdir();
37 my @themes = ('prog', 'test');
38 my @langs = ('en', 'es-ES');
39
40 # create temporary files to be tested later
41 foreach my $theme (@themes) {
42     foreach my $lang (@langs) {
43         make_path("$dir/$theme/$lang/xslt");
44         open my $fh, '>', "$dir/$theme/$lang/xslt/my_file.xslt";
45         print $fh "Theme $theme, language $lang";
46         close $fh;
47     }
48 }
49
50 sub find_and_slurp {
51     my ($dir, $theme, $lang) = @_;
52
53     my $filename = C4::XSLT::_get_best_default_xslt_filename($dir, $theme, $lang, 'my_file.xslt');
54     open my $fh, '<', $filename;
55     my $str = <$fh>;
56     close $fh;
57     return $str;
58 }
59
60 # These tests verify that we're finding the right XSLT file when present,
61 # and falling back to the right XSLT file when an exact match is not present.
62 is(find_and_slurp($dir, 'test', 'en'   ), 'Theme test, language en',    'Found test/en');
63 is(find_and_slurp($dir, 'test', 'es-ES'), 'Theme test, language es-ES', 'Found test/es-ES');
64 is(find_and_slurp($dir, 'prog', 'en',  ), 'Theme prog, language en',    'Found test/en');
65 is(find_and_slurp($dir, 'prog', 'es-ES'), 'Theme prog, language es-ES', 'Found test/es-ES');
66 is(find_and_slurp($dir, 'test', 'fr-FR'), 'Theme test, language en',    'Fell back to test/en for test/fr-FR');
67 is(find_and_slurp($dir, 'nope', 'es-ES'), 'Theme prog, language es-ES', 'Fell back to prog/es-ES for nope/es-ES');
68 is(find_and_slurp($dir, 'nope', 'fr-FR'), 'Theme prog, language en',    'Fell back to prog/en for nope/fr-FR');
69
70 1;