Bug 25757: Add unit tests for item relation
[koha.git] / t / db_dependent / Koha / Item / Transfer.t
1 #!/usr/bin/perl
2
3 # Copyright 2020 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 Koha::Database;
23
24 use t::lib::TestBuilder;
25
26 use Test::More tests => 2;
27 use Test::Exception;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'item relation tests' => sub {
33     plan tests => 2;
34
35     $schema->storage->txn_begin;
36
37     my $item     = $builder->build_sample_item();
38     my $transfer = $builder->build_object(
39         {
40             class => 'Koha::Item::Transfers',
41             value => {
42                 itemnumber => $item->itemnumber,
43             }
44         }
45     );
46
47     my $transfer_item = $transfer->item;
48     is( ref( $transfer_item ), 'Koha::Item', 'Koha::Item::Transfer->item should return a Koha::Item' );
49     is( $transfer_item->itemnumber, $item->itemnumber, 'Koha::Item::Transfer->item should return the correct item' );
50
51     $schema->storage->txn_rollback;
52 };
53
54 subtest 'transit tests' => sub {
55     plan tests => 7;
56
57     $schema->storage->txn_begin;
58
59     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
60     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
61     my $item     = $builder->build_sample_item(
62         {
63             homebranch    => $library1->branchcode,
64             holdingbranch => $library2->branchcode,
65             datelastseen  => undef
66         }
67     );
68
69     my $transfer = $builder->build_object(
70         {
71             class => 'Koha::Item::Transfers',
72             value => {
73                 itemnumber => $item->itemnumber,
74                 frombranch => $library2->branchcode,
75                 tobranch   => $library1->branchcode,
76                 reason     => 'Manual'
77             }
78         }
79     );
80     is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
81
82     # Item checked out should result in failure
83     my $checkout = $builder->build_object(
84         {
85             class => 'Koha::Checkouts',
86             value => {
87                 itemnumber => $item->itemnumber
88             }
89         }
90     );
91     is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
92
93     throws_ok { $transfer->transit() }
94     'Koha::Exceptions::Item::Transfer::Out',
95       'Exception thrown if item is checked out';
96
97     $checkout->delete;
98
99     # CartToShelf test
100     $item->set({ location => 'CART', permanent_location => 'TEST' })->store();
101     is ( $item->location, 'CART', 'Item location set to CART');
102     $transfer->discard_changes;
103     $transfer->transit();
104     $item->discard_changes;
105     is ( $item->location, 'TEST', 'Item location correctly restored to match permanent location');
106
107     # Transit state set
108     ok( $transfer->datesent, 'Transit set the datesent for the transfer' );
109
110     # Last seen
111     ok ( $item->datelastseen, 'Transit set item datelastseen date');
112
113     $schema->storage->txn_rollback;
114 };