Lines 6-12
Link Here
|
6 |
use strict; |
6 |
use strict; |
7 |
use warnings; |
7 |
use warnings; |
8 |
|
8 |
|
9 |
use Test::More tests => 8; |
9 |
use C4::Context; |
|
|
10 |
use C4::Templates; |
11 |
use Test::More tests => 13; |
12 |
use Test::MockModule; |
13 |
use File::Basename qw/dirname/; |
10 |
use File::Temp; |
14 |
use File::Temp; |
11 |
use File::Path qw/make_path/; |
15 |
use File::Path qw/make_path/; |
12 |
|
16 |
|
Lines 14-49
BEGIN {
Link Here
|
14 |
use_ok('C4::XSLT'); |
18 |
use_ok('C4::XSLT'); |
15 |
} |
19 |
} |
16 |
|
20 |
|
17 |
my $dir = File::Temp->newdir(); |
21 |
my $opacdir = File::Temp->newdir(); |
|
|
22 |
my $staffdir = File::Temp->newdir(); |
18 |
my @themes = ('prog', 'test'); |
23 |
my @themes = ('prog', 'test'); |
19 |
my @langs = ('en', 'es-ES'); |
24 |
my @langs = ('en', 'es-ES'); |
20 |
|
25 |
|
|
|
26 |
sub make_test_file { |
27 |
my ( $filename, $contents ) = @_; |
28 |
|
29 |
make_path(dirname($filename)); |
30 |
open my $fh, '>', $filename or die "Could not create test file: $filename"; |
31 |
print $fh $contents; |
32 |
close $fh; |
33 |
} |
34 |
|
21 |
# create temporary files to be tested later |
35 |
# create temporary files to be tested later |
22 |
foreach my $theme (@themes) { |
36 |
foreach my $theme (@themes) { |
23 |
foreach my $lang (@langs) { |
37 |
foreach my $lang (@langs) { |
24 |
make_path("$dir/$theme/$lang/xslt"); |
38 |
foreach my $dir ($opacdir, $staffdir) { |
25 |
open my $fh, '>', "$dir/$theme/$lang/xslt/my_file.xslt"; |
39 |
make_test_file( "$dir/$theme/$lang/xslt/my_file.xslt", "Theme $theme, language $lang" ); |
26 |
print $fh "Theme $theme, language $lang"; |
40 |
make_test_file( "$dir/$theme/$lang/xslt/MARC21slim2intranetDetail.xsl", "Theme $theme, language $lang, MARC21slim2intranetDetail" ); |
27 |
close $fh; |
41 |
make_test_file( "$dir/$theme/$lang/xslt/test_en.xsl", "Theme $theme, language $lang, test_en" ); |
|
|
42 |
make_test_file( "$dir/$theme/$lang/xslt/UNIMARCslim2OPACDetail.xsl", "Theme $theme, language $lang, UNIMARCslim2OPACDetail" ); |
43 |
make_test_file( "$dir/$theme/$lang/xslt/nondefault_test.xsl", "Theme $theme, language $lang, nondefault_test" ); |
44 |
make_test_file( "$dir/$theme/$lang/xslt/MARC21slim2OPACResults.xsl", "Theme $theme, language $lang, MARC21slim2OPACResults" ); |
45 |
} |
28 |
} |
46 |
} |
29 |
} |
47 |
} |
30 |
|
48 |
|
31 |
sub find_and_slurp { |
49 |
sub find_and_slurp_default { |
32 |
my ($dir, $theme, $lang) = @_; |
50 |
my ($dir, $theme, $lang) = @_; |
33 |
|
51 |
|
34 |
my $filename = C4::XSLT::_get_best_default_xslt_filename($dir, $theme, $lang, 'my_file.xslt'); |
52 |
my $filename = C4::XSLT::_get_best_default_xslt_filename($dir, $theme, $lang, 'my_file.xslt'); |
35 |
open my $fh, '<', $filename; |
53 |
open my $fh, '<', $filename or return "Could not open: $filename"; |
36 |
my $str = <$fh>; |
54 |
my $str = <$fh>; |
37 |
close $fh; |
55 |
close $fh; |
38 |
return $str; |
56 |
return $str; |
39 |
} |
57 |
} |
40 |
|
58 |
|
|
|
59 |
sub find_and_slurp { |
60 |
my ($marcflavour, $xslsyspref) = @_; |
61 |
|
62 |
my $filename = C4::XSLT::XSLTGetFilename( $marcflavour, $xslsyspref ); |
63 |
open my $fh, '<', $filename or return "Could not open: $filename"; |
64 |
my $str = <$fh>; |
65 |
close $fh; |
66 |
return $str; |
67 |
} |
68 |
|
69 |
my $module_context = new Test::MockModule('C4::Context'); |
70 |
$module_context->mock( |
71 |
'config', |
72 |
sub { |
73 |
my ( $self, $var ) = @_; |
74 |
my %predefs = ( |
75 |
opachtdocs => $opacdir, |
76 |
intrahtdocs => $staffdir, |
77 |
); |
78 |
|
79 |
return $predefs{$var} || $module_context->original('config')->(@_); |
80 |
} |
81 |
); |
82 |
$module_context->mock( |
83 |
'preference', |
84 |
sub { |
85 |
my ( $self, $var ) = @_; |
86 |
my %predefs = ( |
87 |
template => 'prog', |
88 |
marcflavour => 'MARC21', |
89 |
opacthemes => 'test', |
90 |
XSLTDetailsDisplay => 'default', |
91 |
XSLTResultsDisplay => "$staffdir/prog/en/xslt/test_en.xsl", |
92 |
OPACXSLTDetailsDisplay => "$opacdir/test/en/xslt/nondefault_test.xsl", |
93 |
OPACXSLTResultsDisplay => '"default"', |
94 |
); |
95 |
|
96 |
return $predefs{$var} || $module_context->original('preference')->(@_); |
97 |
} |
98 |
); |
99 |
|
41 |
# These tests verify that we're finding the right XSLT file when present, |
100 |
# These tests verify that we're finding the right XSLT file when present, |
42 |
# and falling back to the right XSLT file when an exact match is not present. |
101 |
# and falling back to the right XSLT file when an exact match is not present. |
43 |
is(find_and_slurp($dir, 'test', 'en' ), 'Theme test, language en', 'Found test/en'); |
102 |
is(find_and_slurp_default($opacdir, 'test', 'en' ), 'Theme test, language en', 'Found test/en'); |
44 |
is(find_and_slurp($dir, 'test', 'es-ES'), 'Theme test, language es-ES', 'Found test/es-ES'); |
103 |
is(find_and_slurp_default($opacdir, 'test', 'es-ES'), 'Theme test, language es-ES', 'Found test/es-ES'); |
45 |
is(find_and_slurp($dir, 'prog', 'en', ), 'Theme prog, language en', 'Found test/en'); |
104 |
is(find_and_slurp_default($opacdir, 'prog', 'en', ), 'Theme prog, language en', 'Found test/en'); |
46 |
is(find_and_slurp($dir, 'prog', 'es-ES'), 'Theme prog, language es-ES', 'Found test/es-ES'); |
105 |
is(find_and_slurp_default($opacdir, 'prog', 'es-ES'), 'Theme prog, language es-ES', 'Found test/es-ES'); |
47 |
is(find_and_slurp($dir, 'test', 'fr-FR'), 'Theme test, language en', 'Fell back to test/en for test/fr-FR'); |
106 |
is(find_and_slurp_default($opacdir, 'test', 'fr-FR'), 'Theme test, language en', 'Fell back to test/en for test/fr-FR'); |
48 |
is(find_and_slurp($dir, 'nope', 'es-ES'), 'Theme prog, language es-ES', 'Fell back to prog/es-ES for nope/es-ES'); |
107 |
is(find_and_slurp_default($opacdir, 'nope', 'es-ES'), 'Theme prog, language es-ES', 'Fell back to prog/es-ES for nope/es-ES'); |
49 |
is(find_and_slurp($dir, 'nope', 'fr-FR'), 'Theme prog, language en', 'Fell back to prog/en for nope/fr-FR'); |
108 |
is(find_and_slurp_default($opacdir, 'nope', 'fr-FR'), 'Theme prog, language en', 'Fell back to prog/en for nope/fr-FR'); |
|
|
109 |
is(find_and_slurp('MARC21', 'XSLTDetailsDisplay'), 'Theme prog, language en, MARC21slim2intranetDetail', 'Used default for staff/details'); |
110 |
is(find_and_slurp('MARC21', 'XSLTResultsDisplay'), 'Theme prog, language en, test_en', 'Used non-default with langcode for staff/results'); |
111 |
is(find_and_slurp('MARC21', 'OPACXSLTDetailsDisplay'), 'Theme test, language en, nondefault_test', 'not-prog: Used non-default for local marcflavour and opac/details'); |
112 |
is(find_and_slurp('UNIMARC', 'OPACXSLTDetailsDisplay'), 'Theme test, language en, UNIMARCslim2OPACDetail', 'not-prog: Used default for non-local marcflavour and opac/details'); |
113 |
is(find_and_slurp('MARC21', 'OPACXSLTResultsDisplay'), 'Theme test, language en, MARC21slim2OPACResults', 'not-prog: Used "default" for opac/results'); |
50 |
- |
|
|