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