Bug 29157: Regression tests
[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 => 16;
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");
135
136     $builder->build({ source => "TmpHoldsqueue", value => {
137         itemnumber => $item->itemnumber
138         }
139     });
140     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
141     like($xml,qr{<substatus>Pending hold</substatus>},"Pending status takes precedence over all");
142     my $library_name = $holdinglibrary->branchname;
143     like($xml,qr{<resultbranch>${library_name}</resultbranch>}, "Found resultbranch / holding branch" );
144 };
145
146 $schema->storage->txn_rollback;
147
148 subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
149
150     plan tests => 20;
151
152     $schema->storage->txn_begin;
153
154     my $biblio = $builder->build_sample_biblio;
155
156     # Have two known libraries for testing purposes
157     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
158     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
159     my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
160
161     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_1->id });
162     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_2->id });
163     my $item_3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_3->id });
164
165     my $items_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } });
166
167     ## Test passing items_rs only
168     my $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $items_rs );
169
170     my $library_1_name = $library_1->branchname;
171     my $library_2_name = $library_2->branchname;
172     my $library_3_name = $library_3->branchname;
173
174     like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
175     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
176     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
177     ## Test passing one item in hidden_items and items_rs
178     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
179
180     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
181     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
182     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
183
184     ## Test passing both items in hidden_items and items_rs
185     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber ], $items_rs->reset );
186
187     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
188     unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
189     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
190     is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
191
192     ## Test passing both items in hidden_items and no items_rs
193     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber, $item_3->itemnumber ] );
194
195     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
196     unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
197     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
198     is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
199
200     ## Test passing one item in hidden_items and items_rs
201     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ] );
202
203     unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
204     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
205     like(   $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
206
207     ## Test not passing any param
208     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber );
209
210     like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
211     like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
212     like( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
213
214     $schema->storage->txn_rollback;
215 };