Bug 34529: (QA follow-up) Tidy OfflineCirculation.t
[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 => 2;
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 ProcessOfflineIssue );
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 34529: Offline circulation should be able to accept userid as well as cardnumber" => sub {
59
60     plan tests => 2;
61
62     $dbh->do("DELETE FROM pending_offline_operations");
63
64     $branch = $builder->build( { source => 'Branch' } )->{branchcode};
65
66     my $borrower1 = $builder->build(
67         {
68             source => 'Borrower',
69             value  => { branchcode => $branch }
70         }
71     );
72
73     my $biblio = t::lib::TestBuilder->new->build_sample_biblio;
74     my $item1  = $builder->build_sample_item(
75         {
76             biblionumber => $biblio->id,
77             library      => $branch,
78         }
79     );
80     my $item2 = $builder->build_sample_item(
81         {
82             biblionumber => $biblio->id,
83             library      => $branch,
84         }
85     );
86
87     is(
88         ProcessOfflineIssue(
89             {
90                 cardnumber => $borrower1->{cardnumber},
91                 barcode    => $item1->barcode
92             }
93         ),
94         "Success.",
95         "ProcessOfflineIssue succeeds with cardnumber"
96     );
97     is(
98         ProcessOfflineIssue( { cardnumber => $borrower1->{userid}, barcode => $item2->barcode } ),
99         "Success.",
100         "ProcessOfflineIssue succeeds with user id"
101     );
102
103 };
104
105 subtest "Bug 30114 - Koha offline circulation will always cancel the next hold when issuing item to a patron" => sub {
106
107     plan tests => 3;
108
109     $dbh->do("DELETE FROM pending_offline_operations");
110
111     # Set item-level item types
112     t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
113
114     # Create a branch
115     $branch = $builder->build( { source => 'Branch' } )->{branchcode};
116
117     # Create a borrower
118     my $borrower1 = $builder->build(
119         {
120             source => 'Borrower',
121             value  => { branchcode => $branch }
122         }
123     );
124
125     my $borrower2 = $builder->build(
126         {
127             source => 'Borrower',
128             value  => { branchcode => $branch }
129         }
130     );
131
132     my $borrower3 = $builder->build(
133         {
134             source => 'Borrower',
135             value  => { branchcode => $branch }
136         }
137     );
138
139     # Look for the defined MARC field for biblio-level itemtype
140     my $rs = $schema->resultset('MarcSubfieldStructure')->search(
141         {
142             frameworkcode => '',
143             kohafield     => 'biblioitems.itemtype'
144         }
145     );
146     my $tagfield    = $rs->first->tagfield;
147     my $tagsubfield = $rs->first->tagsubfield;
148
149     # Create a biblio record with biblio-level itemtype
150     my $record = MARC::Record->new();
151     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
152     my $itype = $builder->build( { source => 'Itemtype' } );
153     my $item  = $builder->build_sample_item(
154         {
155             biblionumber => $biblionumber,
156             library      => $branch,
157             itype        => $itype->{itemtype},
158         }
159     );
160
161     AddReserve(
162         {
163             branchcode     => $branch,
164             borrowernumber => $borrower1->{borrowernumber},
165             biblionumber   => $biblionumber,
166             priority       => 1,
167             itemnumber     => $item->id,
168         }
169     );
170
171     AddReserve(
172         {
173             branchcode     => $branch,
174             borrowernumber => $borrower2->{borrowernumber},
175             biblionumber   => $biblionumber,
176             priority       => 2,
177             itemnumber     => $item->id,
178         }
179     );
180
181     my $now = dt_from_string->truncate( to => 'minute' );
182     AddOfflineOperation(
183         $borrower3->{borrowernumber}, $borrower3->{branchcode}, $now, 'issue', $item->barcode,
184         $borrower3->{cardnumber}
185     );
186
187     my $offline_rs = Koha::Database->new()->schema()->resultset('PendingOfflineOperation')->search();
188     is( $offline_rs->count, 1, "Found one pending offline operation" );
189
190     is( Koha::Holds->search( { biblionumber => $biblionumber } )->count, 2, "Found two holds for the record" );
191
192     my $op = GetOfflineOperation( $offline_rs->next->id );
193
194     my $ret = ProcessOfflineOperation($op);
195
196     is( Koha::Holds->search( { biblionumber => $biblionumber } )->count, 2, "Still found two holds for the record" );
197 };
198
199 $schema->storage->txn_rollback;