3 # This file is part of Koha.
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.
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.
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>.
21 use File::Path qw/make_path/;
23 use Test::More tests => 4;
25 use t::lib::TestBuilder;
33 use_ok('C4::XSLT', qw( buildKohaItemsNamespace ));
36 my $schema = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
39 subtest 'Tests moved from t' => sub {
41 $schema->storage->txn_begin;
43 my $dir = File::Temp->newdir();
44 my @themes = ('prog', 'test');
45 my @langs = ('en', 'es-ES');
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";
58 my ($dir, $theme, $lang) = @_;
60 my $filename = C4::XSLT::_get_best_default_xslt_filename($dir, $theme, $lang, 'my_file.xslt');
61 open my $fh, '<', $filename;
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');
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');
81 $schema->storage->txn_rollback;
84 subtest 'buildKohaItemsNamespace status tests' => sub {
86 $schema->storage->txn_begin;
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' );
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;
99 my $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
100 like($xml,qr{<status>available</status>},"Item is available when no other status applied");
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");
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;
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");
124 $item->notforloan(1)->store;
125 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
126 like($xml,qr{<status>reference</status>},"reference if positive notforloan value");
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');
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");
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");
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");
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");
155 $builder->build({ source => "Branchtransfer", value => {
156 itemnumber => $item->itemnumber,
157 datearrived => undef,
158 datecancelled => undef,
161 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
162 like($xml,qr{<substatus>In transit</substatus>},"In-transit status takes precedence over Damaged");
164 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
165 biblionumber => $item->biblionumber,
166 itemnumber => $item->itemnumber,
171 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
172 like($xml,qr{<substatus>Hold waiting</substatus>},"Waiting status takes precedence over In transit (holds)");
175 $builder->build({ source => "TmpHoldsqueue", value => {
176 itemnumber => $item->itemnumber
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" );
184 t::lib::Mocks::mock_preference('UseRecalls', 1);
185 my $recall = $builder->build_object(
187 class => 'Koha::Recalls',
189 biblio_id => $item->biblionumber,
190 item_id => $item->itemnumber,
191 pickup_library_id => $item->holdingbranch,
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);
201 $schema->storage->txn_rollback;
204 subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
207 $schema->storage->txn_begin;
209 my $biblio = $builder->build_sample_biblio;
210 my $biblio2 = $builder->build_sample_biblio;
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' });
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 });
221 my $items_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } });
223 ## Test passing items_rs only
224 my $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $items_rs );
226 my $library_1_name = $library_1->branchname;
227 my $library_2_name = $library_2->branchname;
228 my $library_3_name = $library_3->branchname;
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' );
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' );
241 ## Test passing one item in hidden_items and items_rs
242 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
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' );
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 );
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' );
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 ] );
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' );
264 ## Test passing one item in hidden_items and items_rs
265 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ] );
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' );
271 ## Test not passing any param
272 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber );
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' );
278 $schema->storage->txn_rollback;