Bug 23406: Unit test
[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::ItemTypes;
27
28 BEGIN {
29     use_ok('C4::XSLT');
30 }
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 $schema->storage->txn_begin;
36
37 subtest 'transformMARCXML4XSLT tests' => sub {
38     plan tests => 1;
39     my $mock_xslt =  Test::MockModule->new("C4::XSLT");
40     $mock_xslt->mock( getAuthorisedValues4MARCSubfields => sub { return { 942 => { 'n' => 1 } } } );
41     $mock_xslt->mock( GetAuthorisedValueDesc => sub { warn "called"; });
42     my $record = MARC::Record->new();
43     my $suppress_field = MARC::Field->new( 942, ' ', ' ', n => '1' );
44     $record->append_fields($suppress_field);
45     warning_is { C4::XSLT::transformMARCXML4XSLT( 3,$record ) } undef, "942n auth value not translated";
46 };
47
48 subtest 'buildKohaItemsNamespace status tests' => sub {
49     plan tests => 13;
50     my $itype = $builder->build_object({ class => 'Koha::ItemTypes' });
51     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
52     my $item  = $builder->build_sample_item({ itype => $itype->itemtype });
53     $item->biblioitem->itemtype($itemtype->itemtype)->store;
54
55     my $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
56     like($xml,qr{<status>available</status>},"Item is available when no other status applied");
57
58     # notforloan
59     {
60
61         t::lib::Mocks::mock_preference('item-level_itypes', 0);
62         $item->notforloan(0)->store;
63         Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
64         Koha::ItemTypes->find($item->biblioitem->itemtype)->notforloan(1)->store;
65         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
66         like($xml,qr{<status>reference</status>},"reference if positive itype notforloan value");
67
68         t::lib::Mocks::mock_preference('item-level_itypes', 1);
69         Koha::ItemTypes->find($item->itype)->notforloan(1)->store;
70         Koha::ItemTypes->find($item->biblioitem->itemtype)->notforloan(0)->store;
71         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
72         like($xml,qr{<status>reference</status>},"reference if positive itemtype notforloan value");
73         Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
74
75         my $substatus = Koha::AuthorisedValues->search({ category => 'NOT_LOAN', authorised_value => -1 })->next->lib;
76         $item->notforloan(-1)->store;
77         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
78         like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan if negative notforloan value");
79         like($xml,qr{<substatus>$substatus</substatus>},"substatus set if negative notforloan value");
80
81         $item->notforloan(1)->store;
82         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
83         like($xml,qr{<status>reference</status>},"reference if positive notforloan value");
84     }
85
86     $item->onloan('2001-01-01')->store;
87     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
88     like($xml,qr{<status>Checked out</status>},"Checked out status takes precedence over Not for loan");
89
90     $item->withdrawn(1)->store;
91     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
92     like($xml,qr{<status>Withdrawn</status>},"Withdrawn status takes precedence over Checked out");
93
94     $item->itemlost(1)->store;
95     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
96     like($xml,qr{<status>Lost</status>},"Lost status takes precedence over Withdrawn");
97
98     $item->damaged(1)->store;
99     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
100     like($xml,qr{<status>Damaged</status>},"Damaged status takes precedence over Lost");
101
102     $builder->build({ source => "Branchtransfer", value => {
103         itemnumber  => $item->itemnumber,
104         datearrived => undef,
105         }
106     });
107     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
108     like($xml,qr{<status>In transit</status>},"In-transit status takes precedence over Damaged");
109
110     my $hold = $builder->build_object({ class => 'Koha::Holds', value => {
111         biblionumber => $item->biblionumber,
112         itemnumber   => $item->itemnumber,
113         found        => 'W',
114         priority     => 0,
115         }
116     });
117     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
118     like($xml,qr{<status>Waiting</status>},"Waiting status takes precedence over In transit");
119
120     $builder->build({ source => "TmpHoldsqueue", value => {
121         itemnumber => $item->itemnumber
122         }
123     });
124     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
125     like($xml,qr{<status>Pending hold</status>},"Pending status takes precedence over all");
126
127
128 };
129
130 $schema->storage->txn_rollback;