Bug 30921: Replace C4::XSLT::transformMARCXML4XSLT with RecordProcessor
[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 MARC::Record;
21 use Test::More tests => 3;
22 use Test::Warn;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::Database;
27 use Koha::Libraries;
28 use Koha::ItemTypes;
29
30 BEGIN {
31     use_ok('C4::XSLT', qw( buildKohaItemsNamespace ));
32 }
33
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 $schema->storage->txn_begin;
38
39 subtest 'buildKohaItemsNamespace status tests' => sub {
40     plan tests => 17;
41
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' );
45
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;
52
53     my $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
54     like($xml,qr{<status>available</status>},"Item is available when no other status applied");
55
56     # notforloan
57     {
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");
64
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;
71
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");
77
78         $item->notforloan(1)->store;
79         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
80         like($xml,qr{<status>reference</status>},"reference if positive notforloan value");
81
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');
87     }
88
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");
93
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");
97
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");
101
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");
105
106     $builder->build({ source => "Branchtransfer", value => {
107         itemnumber  => $item->itemnumber,
108         datearrived => undef,
109         datecancelled => undef,
110         }
111     });
112     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
113     like($xml,qr{<substatus>In transit</substatus>},"In-transit status takes precedence over Damaged");
114
115     my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
116         biblionumber => $item->biblionumber,
117         itemnumber   => $item->itemnumber,
118         found        => 'W',
119         priority     => 0,
120         }
121     });
122     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
123     like($xml,qr{<substatus>Hold waiting</substatus>},"Waiting status takes precedence over In transit (holds)");
124     $hold->cancel;
125
126     $builder->build({ source => "TmpHoldsqueue", value => {
127         itemnumber => $item->itemnumber
128         }
129     });
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" );
134
135     t::lib::Mocks::mock_preference('UseRecalls', 1);
136     my $recall = $builder->build_object({ class => 'Koha::Recalls', value => {
137         biblio_id         => $item->biblionumber,
138         item_id           => $item->itemnumber,
139         pickup_library_id => $item->holdingbranch,
140     }});
141     $recall->set_waiting;
142     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
143     like($xml,qr{<substatus>Recall waiting</substatus>},"Waiting status takes precedence over In transit (recalls)");
144     t::lib::Mocks::mock_preference('UseRecalls', 0);
145
146 };
147
148 $schema->storage->txn_rollback;
149
150 subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
151
152     plan tests => 23;
153
154     $schema->storage->txn_begin;
155
156     my $biblio = $builder->build_sample_biblio;
157     my $biblio2 = $builder->build_sample_biblio;
158
159     # Have two known libraries for testing purposes
160     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
161     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
162     my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
163
164     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_1->id });
165     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_2->id });
166     my $item_3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_3->id });
167
168     my $items_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } });
169
170     ## Test passing items_rs only
171     my $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $items_rs );
172
173     my $library_1_name = $library_1->branchname;
174     my $library_2_name = $library_2->branchname;
175     my $library_3_name = $library_3->branchname;
176
177     like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
178     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
179     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
180
181     t::lib::Mocks::mock_preference('OpacHiddenItems', 'biblionumber: ['.$biblio2->biblionumber.']');
182     my $hid_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } })->filter_by_visible_in_opac();
183     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $hid_rs );
184     like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
185     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
186     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
187
188     ## Test passing one item in hidden_items and items_rs
189     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
190
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' );
194
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 );
197
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' );
202
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 ] );
205
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' );
210
211     ## Test passing one item in hidden_items and items_rs
212     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ] );
213
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' );
217
218     ## Test not passing any param
219     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber );
220
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' );
224
225     $schema->storage->txn_rollback;
226 };