Bug 20997: (QA follow-up) POD fixes and offset type consistency
[koha.git] / t / db_dependent / Koha / Items.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 9;
23 use Test::Exception;
24
25 use C4::Circulation;
26 use Koha::Item;
27 use Koha::Item::Transfer::Limits;
28 use Koha::Items;
29 use Koha::Database;
30
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 my $builder     = t::lib::TestBuilder->new;
38 my $biblioitem  = $builder->build( { source => 'Biblioitem' } );
39 my $library     = $builder->build( { source => 'Branch' } );
40 my $nb_of_items = Koha::Items->search->count;
41 my $new_item_1  = Koha::Item->new(
42     {   biblionumber     => $biblioitem->{biblionumber},
43         biblioitemnumber => $biblioitem->{biblioitemnumber},
44         homebranch       => $library->{branchcode},
45         holdingbranch    => $library->{branchcode},
46         barcode          => "a_barcode_for_t",
47         itype            => 'BK',
48     }
49 )->store;
50 my $new_item_2 = Koha::Item->new(
51     {   biblionumber     => $biblioitem->{biblionumber},
52         biblioitemnumber => $biblioitem->{biblioitemnumber},
53         homebranch       => $library->{branchcode},
54         holdingbranch    => $library->{branchcode},
55         barcode          => "another_bc_for_t",
56         itype            => 'BK',
57     }
58 )->store;
59
60 C4::Context->_new_userenv('xxx');
61 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
62
63 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
64 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
65
66 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
67 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
68
69 subtest 'get_transfer' => sub {
70     plan tests => 3;
71
72     my $transfer = $new_item_1->get_transfer();
73     is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
74
75     my $library_to = $builder->build( { source => 'Branch' } );
76
77     C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
78
79     $transfer = $new_item_1->get_transfer();
80     is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
81
82     is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
83 };
84
85 subtest 'biblio' => sub {
86     plan tests => 2;
87
88     my $biblio = $retrieved_item_1->biblio;
89     is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
90     is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
91 };
92
93 subtest 'biblioitem' => sub {
94     plan tests => 2;
95
96     my $biblioitem = $retrieved_item_1->biblioitem;
97     is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
98     is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
99 };
100
101 subtest 'checkout' => sub {
102     plan tests => 5;
103     my $item = Koha::Items->find( $new_item_1->itemnumber );
104     # No checkout yet
105     my $checkout = $item->checkout;
106     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
107
108     # Add a checkout
109     my $patron = $builder->build({ source => 'Borrower' });
110     C4::Circulation::AddIssue( $patron, $item->barcode );
111     $checkout = $retrieved_item_1->checkout;
112     is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
113     is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
114     is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
115
116     # Do the return
117     C4::Circulation::AddReturn( $item->barcode );
118
119     # There is no more checkout on this item, making sure it will not return old checkouts
120     $checkout = $item->checkout;
121     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
122 };
123
124 subtest 'can_be_transferred' => sub {
125     plan tests => 5;
126
127     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
128     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
129
130     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
131     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
132     my $item  = Koha::Item->new({
133         biblionumber     => $biblioitem->{biblionumber},
134         biblioitemnumber => $biblioitem->{biblioitemnumber},
135         homebranch       => $library1->branchcode,
136         holdingbranch    => $library1->branchcode,
137         itype            => 'test',
138         barcode          => "newbarcode",
139     })->store;
140     $nb_of_items++;
141
142     is(Koha::Item::Transfer::Limits->search({
143         fromBranch => $library1->branchcode,
144         toBranch => $library2->branchcode,
145     })->count, 0, 'There are no transfer limits between libraries.');
146     ok($item->can_be_transferred({ to => $library2 }),
147        'Item can be transferred between libraries.');
148
149     my $limit = Koha::Item::Transfer::Limit->new({
150         fromBranch => $library1->branchcode,
151         toBranch => $library2->branchcode,
152         itemtype => $item->effective_itemtype,
153     })->store;
154     is(Koha::Item::Transfer::Limits->search({
155         fromBranch => $library1->branchcode,
156         toBranch => $library2->branchcode,
157     })->count, 1, 'Given we have added a transfer limit,');
158     is($item->can_be_transferred({ to => $library2 }), 0,
159        'Item can no longer be transferred between libraries.');
160     is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
161        'We get the same result also if we pass the from-library parameter.');
162 };
163
164 $retrieved_item_1->delete;
165 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
166
167 $schema->storage->txn_rollback;
168