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 => 4;
23 use t::lib::TestBuilder;
31 use_ok('C4::XSLT', qw( transformMARCXML4XSLT getAuthorisedValues4MARCSubfields buildKohaItemsNamespace ));
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 $schema->storage->txn_begin;
39 subtest 'transformMARCXML4XSLT tests' => sub {
41 my $mock_xslt = Test::MockModule->new("C4::XSLT");
42 $mock_xslt->mock( getAuthorisedValues4MARCSubfields => sub { return { 942 => { 'n' => 1 } } } );
43 $mock_xslt->mock( GetAuthorisedValueDesc => sub { warn "called"; });
44 my $record = MARC::Record->new();
45 my $suppress_field = MARC::Field->new( 942, ' ', ' ', n => '1' );
46 $record->append_fields($suppress_field);
47 warning_is { C4::XSLT::transformMARCXML4XSLT( 3,$record ) } undef, "942n auth value not translated";
50 subtest 'buildKohaItemsNamespace status tests' => sub {
53 t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '1|2');
54 t::lib::Mocks::mock_preference( 'OPACResultsLibrary', 'holdingbranch' );
55 t::lib::Mocks::mock_preference( 'OPACResultsMaxItems', '2' );
57 my $itype = $builder->build_object({ class => 'Koha::ItemTypes' });
58 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
59 my $holdinglibrary = $builder->build_object({ class => 'Koha::Libraries' });
60 my $item = $builder->build_sample_item({ itype => $itype->itemtype });
61 $item->holdingbranch( $holdinglibrary->branchcode )->store;
62 $item->biblioitem->itemtype($itemtype->itemtype)->store;
64 my $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
65 like($xml,qr{<status>available</status>},"Item is available when no other status applied");
69 t::lib::Mocks::mock_preference('item-level_itypes', 0);
70 $item->notforloan(0)->store;
71 Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
72 Koha::ItemTypes->find($item->biblioitem->itemtype)->notforloan(1)->store;
73 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
74 like($xml,qr{<status>reference</status>},"reference if positive itype notforloan value");
76 t::lib::Mocks::mock_preference('item-level_itypes', 1);
77 Koha::ItemTypes->find($item->itype)->notforloan(1)->store;
78 Koha::ItemTypes->find($item->biblioitem->itemtype)->notforloan(0)->store;
79 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
80 like($xml,qr{<status>reference</status>},"reference if positive itemtype notforloan value");
81 Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
83 my $substatus = Koha::AuthorisedValues->search({ category => 'NOT_LOAN', authorised_value => -1 })->next->lib;
84 $item->notforloan(-1)->store;
85 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
86 like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan if negative notforloan value");
87 like($xml,qr{<substatus>$substatus</substatus>},"substatus set if negative notforloan value");
89 $item->notforloan(1)->store;
90 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
91 like($xml,qr{<status>reference</status>},"reference if positive notforloan value");
93 # But now make status notforloan==1 count under Not available
94 t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '2');
95 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
96 like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan when we change Reference_NFL_Statuses");
97 t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '1|2');
100 $item->onloan('2001-01-01')->store;
101 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
102 like( $xml, qr/<status>other<\/status>/, "Checked out is part of other statuses" );
103 like($xml,qr{<substatus>Checked out</substatus>},"Checked out status takes precedence over Not for loan");
105 $item->withdrawn(1)->store;
106 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
107 like($xml,qr{<substatus>Withdrawn</substatus>},"Withdrawn status takes precedence over Checked out");
109 $item->itemlost(1)->store;
110 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
111 like($xml,qr{<substatus>Lost</substatus>},"Lost status takes precedence over Withdrawn");
113 $item->damaged(1)->store;
114 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
115 like($xml,qr{<substatus>Damaged</substatus>},"Damaged status takes precedence over Lost");
117 $builder->build({ source => "Branchtransfer", value => {
118 itemnumber => $item->itemnumber,
119 datearrived => undef,
120 datecancelled => undef,
123 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
124 like($xml,qr{<substatus>In transit</substatus>},"In-transit status takes precedence over Damaged");
126 my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
127 biblionumber => $item->biblionumber,
128 itemnumber => $item->itemnumber,
133 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
134 like($xml,qr{<substatus>Hold waiting</substatus>},"Waiting status takes precedence over In transit (holds)");
137 $builder->build({ source => "TmpHoldsqueue", value => {
138 itemnumber => $item->itemnumber
141 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
142 like($xml,qr{<substatus>Pending hold</substatus>},"Pending status takes precedence over all");
143 my $library_name = $holdinglibrary->branchname;
144 like($xml,qr{<resultbranch>${library_name}</resultbranch>}, "Found resultbranch / holding branch" );
146 my $recall = $builder->build_object({ class => 'Koha::Recalls', value => {
147 biblionumber => $item->biblionumber,
148 itemnumber => $item->itemnumber,
149 branchcode => $item->holdingbranch,
151 $recall->set_waiting;
152 $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
153 like($xml,qr{<substatus>Recall waiting</substatus>},"Waiting status takes precedence over In transit (recalls)");
157 $schema->storage->txn_rollback;
159 subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
163 $schema->storage->txn_begin;
165 my $biblio = $builder->build_sample_biblio;
167 # Have two known libraries for testing purposes
168 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
169 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
170 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
172 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_1->id });
173 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_2->id });
174 my $item_3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_3->id });
176 my $items_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } });
178 ## Test passing items_rs only
179 my $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $items_rs );
181 my $library_1_name = $library_1->branchname;
182 my $library_2_name = $library_2->branchname;
183 my $library_3_name = $library_3->branchname;
185 like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
186 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
187 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
188 ## Test passing one item in hidden_items and items_rs
189 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
191 unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
192 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
193 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
195 ## Test passing both items in hidden_items and items_rs
196 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber ], $items_rs->reset );
198 unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
199 unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
200 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
201 is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
203 ## Test passing both items in hidden_items and no items_rs
204 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber, $item_3->itemnumber ] );
206 unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
207 unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
208 unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
209 is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
211 ## Test passing one item in hidden_items and items_rs
212 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ] );
214 unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
215 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
216 like( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
218 ## Test not passing any param
219 $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber );
221 like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
222 like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
223 like( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
225 $schema->storage->txn_rollback;