Bug 18403: Add tests for Koha::Patrons
[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 => 8;
23
24 use C4::Circulation;
25 use Koha::Item;
26 use Koha::Items;
27 use Koha::Database;
28
29 use t::lib::TestBuilder;
30
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $builder     = t::lib::TestBuilder->new;
35 my $biblioitem  = $builder->build( { source => 'Biblioitem' } );
36 my $library     = $builder->build( { source => 'Branch' } );
37 my $nb_of_items = Koha::Items->search->count;
38 my $new_item_1  = Koha::Item->new(
39     {   biblionumber     => $biblioitem->{biblionumber},
40         biblioitemnumber => $biblioitem->{biblioitemnumber},
41         homebranch       => $library->{branchcode},
42         holdingbranch    => $library->{branchcode},
43         barcode          => "a_barcode_for_t",
44         itype            => 'BK',
45     }
46 )->store;
47 my $new_item_2 = Koha::Item->new(
48     {   biblionumber     => $biblioitem->{biblionumber},
49         biblioitemnumber => $biblioitem->{biblioitemnumber},
50         homebranch       => $library->{branchcode},
51         holdingbranch    => $library->{branchcode},
52         barcode          => "another_barcode_for_t",
53         itype            => 'BK',
54     }
55 )->store;
56
57 C4::Context->_new_userenv('xxx');
58 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
59
60 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
61 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
62
63 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
64 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
65
66 subtest 'get_transfer' => sub {
67     plan tests => 3;
68
69     my $transfer = $new_item_1->get_transfer();
70     is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
71
72     my $library_to = $builder->build( { source => 'Branch' } );
73
74     C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
75
76     $transfer = $new_item_1->get_transfer();
77     is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
78
79     is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
80 };
81
82 subtest 'biblio' => sub {
83     plan tests => 2;
84
85     my $biblio = $retrieved_item_1->biblio;
86     is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
87     is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
88 };
89
90 subtest 'biblioitem' => sub {
91     plan tests => 2;
92
93     my $biblioitem = $retrieved_item_1->biblioitem;
94     is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
95     is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
96 };
97
98 subtest 'checkout' => sub {
99     plan tests => 5;
100     my $item = Koha::Items->find( $new_item_1->itemnumber );
101     # No checkout yet
102     my $checkout = $item->checkout;
103     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
104
105     # Add a checkout
106     my $patron = $builder->build({ source => 'Borrower' });
107     C4::Circulation::AddIssue( $patron, $item->barcode );
108     $checkout = $retrieved_item_1->checkout;
109     is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
110     is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
111     is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
112
113     # Do the return
114     C4::Circulation::AddReturn( $item->barcode );
115
116     # There is no more checkout on this item, making sure it will not return old checkouts
117     $checkout = $item->checkout;
118     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
119 };
120
121 $retrieved_item_1->delete;
122 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
123
124 $schema->storage->txn_rollback;
125