Bug 9302: (QA follow-up) Consistency follow-up
[koha.git] / t / db_dependent / Koha / Items.t
blob862f8328c74c37656f09afde1e8b7898c4af4c43
1 #!/usr/bin/perl
3 # Copyright 2016 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
20 use Modern::Perl;
22 use Test::More tests => 8;
24 use C4::Circulation;
25 use Koha::Item;
26 use Koha::Items;
27 use Koha::Database;
29 use t::lib::TestBuilder;
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
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',
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_bc_for_t",
53 itype => 'BK',
55 )->store;
57 C4::Context->_new_userenv('xxx');
58 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
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' );
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' );
66 subtest 'get_transfer' => sub {
67 plan tests => 3;
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' );
72 my $library_to = $builder->build( { source => 'Branch' } );
74 C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
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' );
79 is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
82 subtest 'biblio' => sub {
83 plan tests => 2;
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' );
90 subtest 'biblioitem' => sub {
91 plan tests => 2;
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' );
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' );
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' );
113 # Do the return
114 C4::Circulation::AddReturn( $item->barcode );
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' );
121 $retrieved_item_1->delete;
122 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
124 $schema->storage->txn_rollback;