Bug 35053: Regression tests
[koha.git] / t / db_dependent / 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 File::Temp;
21 use File::Path qw/make_path/;
22 use MARC::Record;
23 use Test::More tests => 4;
24 use Test::Warn;
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use Koha::Database;
29 use Koha::Libraries;
30 use Koha::ItemTypes;
31
32 BEGIN {
33     use_ok('C4::XSLT', qw( buildKohaItemsNamespace ));
34 }
35
36 my $schema  = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
38
39 subtest 'Tests moved from t' => sub {
40     plan tests => 8;
41     $schema->storage->txn_begin;
42
43     my $dir = File::Temp->newdir();
44     my @themes = ('prog', 'test');
45     my @langs = ('en', 'es-ES');
46
47     # create temporary files to be tested later
48     foreach my $theme (@themes) {
49         foreach my $lang (@langs) {
50             make_path("$dir/$theme/$lang/xslt");
51             open my $fh, '>', "$dir/$theme/$lang/xslt/my_file.xslt";
52             print $fh "Theme $theme, language $lang";
53             close $fh;
54         }
55     }
56
57     sub find_and_slurp {
58         my ($dir, $theme, $lang) = @_;
59
60         my $filename = C4::XSLT::_get_best_default_xslt_filename($dir, $theme, $lang, 'my_file.xslt');
61         open my $fh, '<', $filename;
62         my $str = <$fh>;
63         close $fh;
64         return $str;
65     }
66
67     # These tests verify that we're finding the right XSLT file when present,
68     # and falling back to the right XSLT file when an exact match is not present.
69     is(find_and_slurp($dir, 'test', 'en'   ), 'Theme test, language en',    'Found test/en');
70     is(find_and_slurp($dir, 'test', 'es-ES'), 'Theme test, language es-ES', 'Found test/es-ES');
71     is(find_and_slurp($dir, 'prog', 'en',  ), 'Theme prog, language en',    'Found test/en');
72     is(find_and_slurp($dir, 'prog', 'es-ES'), 'Theme prog, language es-ES', 'Found test/es-ES');
73     is(find_and_slurp($dir, 'test', 'fr-FR'), 'Theme test, language en',    'Fell back to test/en for test/fr-FR');
74     is(find_and_slurp($dir, 'nope', 'es-ES'), 'Theme prog, language es-ES', 'Fell back to prog/es-ES for nope/es-ES');
75     is(find_and_slurp($dir, 'nope', 'fr-FR'), 'Theme prog, language en',    'Fell back to prog/en for nope/fr-FR');
76
77     my $matching_string = q{<syspref name="singleBranchMode">0</syspref>};
78     my $sysprefs_xml = C4::XSLT::get_xslt_sysprefs();
79     ok( $sysprefs_xml =~ m/$matching_string/, 'singleBranchMode has a value of 0');
80
81     $schema->storage->txn_rollback;
82 };
83
84 subtest 'buildKohaItemsNamespace status tests' => sub {
85     plan tests => 18;
86     $schema->storage->txn_begin;
87
88     t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '1|2');
89     t::lib::Mocks::mock_preference( 'OPACResultsLibrary', 'holdingbranch' );
90     t::lib::Mocks::mock_preference( 'OPACResultsMaxItems', '2' );
91
92     my $itype = $builder->build_object({ class => 'Koha::ItemTypes' });
93     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
94     my $holdinglibrary = $builder->build_object({ class => 'Koha::Libraries' });
95     my $item = $builder->build_sample_item({ itype => $itype->itemtype });
96     $item->holdingbranch( $holdinglibrary->branchcode )->store;
97     $item->biblioitem->itemtype($itemtype->itemtype)->store;
98
99     my $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
100     like($xml,qr{<status>available</status>},"Item is available when no other status applied");
101
102     # notforloan
103     {
104         t::lib::Mocks::mock_preference('item-level_itypes', 0);
105         $item->notforloan(0)->store;
106         Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
107         Koha::ItemTypes->find($item->biblioitem->itemtype)->notforloan(1)->store;
108         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
109         like($xml,qr{<status>reference</status>},"reference if positive itype notforloan value");
110
111         t::lib::Mocks::mock_preference('item-level_itypes', 1);
112         Koha::ItemTypes->find($item->itype)->notforloan(1)->store;
113         Koha::ItemTypes->find($item->biblioitem->itemtype)->notforloan(0)->store;
114         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
115         like($xml,qr{<status>reference</status>},"reference if positive itemtype notforloan value");
116         Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
117
118         my $substatus = Koha::AuthorisedValues->search({ category => 'NOT_LOAN', authorised_value => -1 })->next->lib;
119         $item->notforloan(-1)->store;
120         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
121         like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan if negative notforloan value");
122         like($xml,qr{<substatus>$substatus</substatus>},"substatus set if negative notforloan value");
123
124         $item->notforloan(1)->store;
125         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
126         like($xml,qr{<status>reference</status>},"reference if positive notforloan value");
127
128         # But now make status notforloan==1 count under Not available
129         t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '2');
130         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
131         like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan when we change Reference_NFL_Statuses");
132         t::lib::Mocks::mock_preference('Reference_NFL_Statuses', q{}); # empty, same effect
133         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
134         like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan when we empty Reference_NFL_Statuses");
135         t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '1|2');
136     }
137
138     $item->onloan('2001-01-01')->store;
139     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
140     like( $xml, qr/<status>other<\/status>/, "Checked out is part of other statuses" );
141     like($xml,qr{<substatus>Checked out</substatus>},"Checked out status takes precedence over Not for loan");
142
143     $item->withdrawn(1)->store;
144     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
145     like($xml,qr{<substatus>Withdrawn</substatus>},"Withdrawn status takes precedence over Checked out");
146
147     $item->itemlost(1)->store;
148     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
149     like($xml,qr{<substatus>Lost</substatus>},"Lost status takes precedence over Withdrawn");
150
151     $item->damaged(1)->store;
152     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
153     like($xml,qr{<substatus>Damaged</substatus>},"Damaged status takes precedence over Lost");
154
155     $builder->build({ source => "Branchtransfer", value => {
156         itemnumber  => $item->itemnumber,
157         datearrived => undef,
158         datecancelled => undef,
159         }
160     });
161     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
162     like($xml,qr{<substatus>In transit</substatus>},"In-transit status takes precedence over Damaged");
163
164     my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
165         biblionumber => $item->biblionumber,
166         itemnumber   => $item->itemnumber,
167         found        => 'W',
168         priority     => 0,
169         }
170     });
171     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
172     like($xml,qr{<substatus>Hold waiting</substatus>},"Waiting status takes precedence over In transit (holds)");
173     $hold->cancel;
174
175     $builder->build({ source => "TmpHoldsqueue", value => {
176         itemnumber => $item->itemnumber
177         }
178     });
179     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
180     like($xml,qr{<substatus>Pending hold</substatus>},"Pending status takes precedence over all");
181     my $library_name = $holdinglibrary->branchname;
182     like($xml,qr{<resultbranch>${library_name}</resultbranch>}, "Found resultbranch / holding branch" );
183
184     t::lib::Mocks::mock_preference('UseRecalls', 1);
185     my $recall = $builder->build_object(
186         {
187             class => 'Koha::Recalls',
188             value => {
189                 biblio_id         => $item->biblionumber,
190                 item_id           => $item->itemnumber,
191                 pickup_library_id => $item->holdingbranch,
192                 item_level        => 1,
193             }
194         }
195     );
196     $recall->set_waiting;
197     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
198     like($xml,qr{<substatus>Recall waiting</substatus>},"Waiting status takes precedence over In transit (recalls)");
199     t::lib::Mocks::mock_preference('UseRecalls', 0);
200
201     $schema->storage->txn_rollback;
202 };
203
204 subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
205     plan tests => 23;
206
207     $schema->storage->txn_begin;
208
209     my $biblio = $builder->build_sample_biblio;
210     my $biblio2 = $builder->build_sample_biblio;
211
212     # Have two known libraries for testing purposes
213     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
214     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
215     my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
216
217     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_1->id });
218     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_2->id });
219     my $item_3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_3->id });
220
221     my $items_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } });
222
223     ## Test passing items_rs only
224     my $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $items_rs );
225
226     my $library_1_name = $library_1->branchname;
227     my $library_2_name = $library_2->branchname;
228     my $library_3_name = $library_3->branchname;
229
230     like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
231     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
232     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
233
234     t::lib::Mocks::mock_preference('OpacHiddenItems', 'biblionumber: ['.$biblio2->biblionumber.']');
235     my $hid_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } })->filter_by_visible_in_opac();
236     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $hid_rs );
237     like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
238     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
239     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
240
241     ## Test passing one item in hidden_items and items_rs
242     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
243
244     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
245     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
246     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
247
248     ## Test passing both items in hidden_items and items_rs
249     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber ], $items_rs->reset );
250
251     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
252     unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
253     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
254     is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
255
256     ## Test passing both items in hidden_items and no items_rs
257     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber, $item_3->itemnumber ] );
258
259     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
260     unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
261     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
262     is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
263
264     ## Test passing one item in hidden_items and items_rs
265     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ] );
266
267     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
268     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
269     like(   $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
270
271     ## Test not passing any param
272     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber );
273
274     like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
275     like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
276     like( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
277
278     $schema->storage->txn_rollback;
279 };