Bug 20262: (QA follow-up) Update unit tests for changes to payin_amount
[koha.git] / t / db_dependent / Circulation / OfflineCirculation.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 => 1;
21 use Test::MockModule;
22 use Test::Warn;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Biblio qw( AddBiblio );
28 use C4::Circulation qw( AddOfflineOperation ProcessOfflineOperation GetOfflineOperation );
29 use C4::Context;
30 use C4::Reserves qw( AddReserve );
31 use Koha::DateUtils qw( dt_from_string );
32
33 use MARC::Record;
34
35 # Mock userenv, used by AddIssue
36 my $branch;
37 my $manager_id;
38 my $context = Test::MockModule->new('C4::Context');
39 $context->mock(
40     'userenv',
41     sub {
42         return {
43             branch    => $branch,
44             number    => $manager_id,
45             firstname => "Adam",
46             surname   => "Smaith"
47         };
48     }
49 );
50
51 my $schema = Koha::Database->schema;
52 $schema->storage->txn_begin;
53
54 my $dbh = C4::Context->dbh;
55
56 my $builder = t::lib::TestBuilder->new();
57
58 subtest "Bug 30114 - Koha offline circulation will always cancel the next hold when issuing item to a patron" => sub {
59
60     plan tests => 3;
61
62     $dbh->do("DELETE FROM pending_offline_operations");
63
64     # Set item-level item types
65     t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
66
67     # Create a branch
68     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
69     # Create a borrower
70     my $borrower1 = $builder->build({
71         source => 'Borrower',
72         value => { branchcode => $branch }
73     });
74
75     my $borrower2 = $builder->build({
76         source => 'Borrower',
77         value => { branchcode => $branch }
78     });
79
80     my $borrower3 = $builder->build({
81         source => 'Borrower',
82         value => { branchcode => $branch }
83     });
84
85     # Look for the defined MARC field for biblio-level itemtype
86     my $rs = $schema->resultset('MarcSubfieldStructure')->search({
87         frameworkcode => '',
88         kohafield     => 'biblioitems.itemtype'
89     });
90     my $tagfield    = $rs->first->tagfield;
91     my $tagsubfield = $rs->first->tagsubfield;
92
93     # Create a biblio record with biblio-level itemtype
94     my $record = MARC::Record->new();
95     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
96     my $itype = $builder->build({ source => 'Itemtype' });
97     my $item = $builder->build_sample_item(
98         {
99             biblionumber => $biblionumber,
100             library      => $branch,
101             itype        => $itype->{itemtype},
102         }
103     );
104
105     AddReserve(
106         {
107             branchcode       => $branch,
108             borrowernumber   => $borrower1->{borrowernumber},
109             biblionumber     => $biblionumber,
110             priority         => 1,
111             itemnumber       => $item->id,
112         }
113     );
114
115     AddReserve(
116         {
117             branchcode       => $branch,
118             borrowernumber   => $borrower2->{borrowernumber},
119             biblionumber     => $biblionumber,
120             priority         => 2,
121             itemnumber       => $item->id,
122         }
123     );
124
125     my $now = dt_from_string->truncate( to => 'minute' );
126     AddOfflineOperation( $borrower3->{borrowernumber}, $borrower3->{branchcode}, $now, 'issue', $item->barcode, $borrower3->{cardnumber} );
127
128     my $offline_rs = Koha::Database->new()->schema()->resultset('PendingOfflineOperation')->search();
129     is( $offline_rs->count, 1, "Found one pending offline operation" );
130
131     is( Koha::Holds->search({ biblionumber => $biblionumber })->count, 2, "Found two holds for the record" );
132
133     my $op = GetOfflineOperation( $offline_rs->next->id );
134
135     my $ret = ProcessOfflineOperation( $op );
136
137     is( Koha::Holds->search({ biblionumber => $biblionumber })->count, 2, "Still found two holds for the record" );
138 };
139
140 $schema->storage->txn_rollback;