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 Test::More tests => 3;
23 use t::lib::TestBuilder;
31 use_ok('C4::XSLT', qw( buildKohaItemsNamespace ));
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 $schema->storage->txn_begin;
39 subtest 'buildKohaItemsNamespace status tests' => sub {
42 t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '1|2');
43 t::lib::Mocks::mock_preference( 'OPACResultsLibrary', 'holdingbranch' );
44 t::lib::Mocks::mock_preference( 'OPACResultsMaxItems', '2' );
46 my $itype = $builder->build_object({ class => 'Koha::ItemTypes' });
47 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
48 my $holdinglibrary = $builder->build_object({ class => 'Koha::Libraries' });
49 my $item = $builder->build_sample_item({ itype => $itype->itemtype });
50 $item->holdingbranch( $holdinglibrary->branchcode )->store;
51 $item->biblioitem->itemtype($itemtype->itemtype)->store;
53 my $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
54 like($xml,qr{<status>available</status>},"Item is available when no other status applied");
58 t::lib::Mocks::mock_preference('item-level_itypes', 0);
59 $item->notforloan(0)->store;
60 Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
61 Koha::ItemTypes->find($item->biblioitem->itemtype)->notforloan(1)->store;
62 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
63 like($xml,qr{<status>reference</status>},"reference if positive itype notforloan value");
65 t::lib::Mocks::mock_preference('item-level_itypes', 1);
66 Koha::ItemTypes->find($item->itype)->notforloan(1)->store;
67 Koha::ItemTypes->find($item->biblioitem->itemtype)->notforloan(0)->store;
68 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
69 like($xml,qr{<status>reference</status>},"reference if positive itemtype notforloan value");
70 Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
72 my $substatus = Koha::AuthorisedValues->search({ category => 'NOT_LOAN', authorised_value => -1 })->next->lib;
73 $item->notforloan(-1)->store;
74 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
75 like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan if negative notforloan value");
76 like($xml,qr{<substatus>$substatus</substatus>},"substatus set if negative notforloan value");
78 $item->notforloan(1)->store;
79 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
80 like($xml,qr{<status>reference</status>},"reference if positive notforloan value");
82 # But now make status notforloan==1 count under Not available
83 t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '2');
84 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
85 like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan when we change Reference_NFL_Statuses");
86 t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '1|2');
89 $item->onloan('2001-01-01')->store;
90 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
91 like( $xml, qr/<status>other<\/status>/, "Checked out is part of other statuses" );
92 like($xml,qr{<substatus>Checked out</substatus>},"Checked out status takes precedence over Not for loan");
94 $item->withdrawn(1)->store;
95 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
96 like($xml,qr{<substatus>Withdrawn</substatus>},"Withdrawn status takes precedence over Checked out");
98 $item->itemlost(1)->store;
99 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
100 like($xml,qr{<substatus>Lost</substatus>},"Lost status takes precedence over Withdrawn");
102 $item->damaged(1)->store;
103 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
104 like($xml,qr{<substatus>Damaged</substatus>},"Damaged status takes precedence over Lost");
106 $builder->build({ source => "Branchtransfer", value => {
107 itemnumber => $item->itemnumber,
108 datearrived => undef,
109 datecancelled => undef,
112 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
113 like($xml,qr{<substatus>In transit</substatus>},"In-transit status takes precedence over Damaged");
115 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
116 biblionumber => $item->biblionumber,
117 itemnumber => $item->itemnumber,
122 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
123 like($xml,qr{<substatus>Hold waiting</substatus>},"Waiting status takes precedence over In transit (holds)");
126 $builder->build({ source => "TmpHoldsqueue", value => {
127 itemnumber => $item->itemnumber
130 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
131 like($xml,qr{<substatus>Pending hold</substatus>},"Pending status takes precedence over all");
132 my $library_name = $holdinglibrary->branchname;
133 like($xml,qr{<resultbranch>${library_name}</resultbranch>}, "Found resultbranch / holding branch" );
135 t::lib::Mocks::mock_preference('UseRecalls', 1);
136 my $recall = $builder->build_object(
138 class => 'Koha::Recalls',
140 biblio_id => $item->biblionumber,
141 item_id => $item->itemnumber,
142 pickup_library_id => $item->holdingbranch,
147 $recall->set_waiting;
148 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
149 like($xml,qr{<substatus>Recall waiting</substatus>},"Waiting status takes precedence over In transit (recalls)");
150 t::lib::Mocks::mock_preference('UseRecalls', 0);
154 $schema->storage->txn_rollback;
156 subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
160 $schema->storage->txn_begin;
162 my $biblio = $builder->build_sample_biblio;
163 my $biblio2 = $builder->build_sample_biblio;
165 # Have two known libraries for testing purposes
166 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
167 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
168 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
170 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_1->id });
171 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_2->id });
172 my $item_3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_3->id });
174 my $items_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } });
176 ## Test passing items_rs only
177 my $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $items_rs );
179 my $library_1_name = $library_1->branchname;
180 my $library_2_name = $library_2->branchname;
181 my $library_3_name = $library_3->branchname;
183 like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
184 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
185 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
187 t::lib::Mocks::mock_preference('OpacHiddenItems', 'biblionumber: ['.$biblio2->biblionumber.']');
188 my $hid_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } })->filter_by_visible_in_opac();
189 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $hid_rs );
190 like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
191 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
192 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
194 ## Test passing one item in hidden_items and items_rs
195 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
197 unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
198 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
199 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
201 ## Test passing both items in hidden_items and items_rs
202 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber ], $items_rs->reset );
204 unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
205 unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
206 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
207 is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
209 ## Test passing both items in hidden_items and no items_rs
210 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber, $item_3->itemnumber ] );
212 unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
213 unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
214 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
215 is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
217 ## Test passing one item in hidden_items and items_rs
218 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ] );
220 unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
221 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
222 like( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
224 ## Test not passing any param
225 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber );
227 like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
228 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
229 like( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
231 $schema->storage->txn_rollback;