Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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 => 4;
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( transformMARCXML4XSLT getAuthorisedValues4MARCSubfields 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 'transformMARCXML4XSLT tests' => sub {
40     plan tests => 1;
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";
48 };
49
50 subtest 'buildKohaItemsNamespace status tests' => sub {
51     plan tests => 17;
52
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' );
56
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;
63
64     my $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
65     like($xml,qr{<status>available</status>},"Item is available when no other status applied");
66
67     # notforloan
68     {
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");
75
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;
82
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");
88
89         $item->notforloan(1)->store;
90         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
91         like($xml,qr{<status>reference</status>},"reference if positive notforloan value");
92
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');
98     }
99
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");
104
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");
108
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");
112
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");
116
117     $builder->build({ source => "Branchtransfer", value => {
118         itemnumber  => $item->itemnumber,
119         datearrived => undef,
120         datecancelled => undef,
121         }
122     });
123     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
124     like($xml,qr{<substatus>In transit</substatus>},"In-transit status takes precedence over Damaged");
125
126     my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
127         biblionumber => $item->biblionumber,
128         itemnumber   => $item->itemnumber,
129         found        => 'W',
130         priority     => 0,
131         }
132     });
133     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
134     like($xml,qr{<substatus>Waiting</substatus>},"Waiting status takes precedence over In transit (holds)");
135     $hold->cancel;
136
137     $builder->build({ source => "TmpHoldsqueue", value => {
138         itemnumber => $item->itemnumber
139         }
140     });
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" );
145
146     my $recall = $builder->build_object({ class => 'Koha::Recalls', value => {
147         biblionumber    => $item->biblionumber,
148         itemnumber      => $item->itemnumber,
149         branchcode      => $item->holdingbranch,
150         status          => 'R',
151     }});
152     $recall->set_waiting;
153     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
154     like($xml,qr{<status>Waiting</status>},"Waiting status takes precedence over In transit (recalls)");
155
156 };
157
158 $schema->storage->txn_rollback;
159
160 subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
161
162     plan tests => 20;
163
164     $schema->storage->txn_begin;
165
166     my $biblio = $builder->build_sample_biblio;
167
168     # Have two known libraries for testing purposes
169     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
170     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
171     my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
172
173     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_1->id });
174     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_2->id });
175     my $item_3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_3->id });
176
177     my $items_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } });
178
179     ## Test passing items_rs only
180     my $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $items_rs );
181
182     my $library_1_name = $library_1->branchname;
183     my $library_2_name = $library_2->branchname;
184     my $library_3_name = $library_3->branchname;
185
186     like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
187     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
188     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
189     ## Test passing one item in hidden_items and items_rs
190     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
191
192     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
193     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
194     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
195
196     ## Test passing both items in hidden_items and items_rs
197     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber ], $items_rs->reset );
198
199     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
200     unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
201     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
202     is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
203
204     ## Test passing both items in hidden_items and no items_rs
205     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber, $item_3->itemnumber ] );
206
207     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
208     unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
209     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
210     is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
211
212     ## Test passing one item in hidden_items and items_rs
213     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ] );
214
215     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
216     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
217     like(   $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
218
219     ## Test not passing any param
220     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber );
221
222     like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
223     like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
224     like( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
225
226     $schema->storage->txn_rollback;
227 };