Bug 31894: (QA follow-up) Move rollbacks to the end
[koha.git] / t / db_dependent / Koha / Plugins / Holds_hooks.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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 use Modern::Perl;
18
19 use Test::More tests => 6;
20 use Test::MockModule;
21 use Test::Warn;
22
23 use File::Basename;
24
25 use C4::Reserves qw( AddReserve );
26
27 use t::lib::Mocks;
28 use t::lib::TestBuilder;
29
30 BEGIN {
31     # Mock pluginsdir before loading Plugins module
32     my $path = dirname(__FILE__) . '/../../../lib/plugins';
33     t::lib::Mocks::mock_config( 'pluginsdir', $path );
34
35     use_ok('Koha::Plugins');
36     use_ok('Koha::Plugins::Handler');
37     use_ok('Koha::Plugin::Test');
38 }
39
40 my $schema  = Koha::Database->new->schema;
41 my $builder = t::lib::TestBuilder->new;
42
43 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
44
45 subtest 'after_hold_create() hook tests' => sub {
46
47     plan tests => 1;
48
49     $schema->storage->txn_begin;
50
51     my $plugins = Koha::Plugins->new;
52     $plugins->InstallPlugins;
53
54     my $plugin = Koha::Plugin::Test->new->enable;
55
56     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
57
58     t::lib::Mocks::mock_userenv(
59         {
60             patron     => $patron,
61             branchcode => $patron->branchcode
62         }
63     );
64
65     # Avoid testing useless warnings
66     my $test_plugin = Test::MockModule->new('Koha::Plugin::Test');
67     $test_plugin->mock( 'after_item_action',   undef );
68     $test_plugin->mock( 'after_biblio_action', undef );
69     $test_plugin->mock( 'item_barcode_transform', undef );
70     $test_plugin->mock( 'after_hold_action', undef );
71
72     my $biblio = $builder->build_sample_biblio();
73     my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
74
75     warnings_like { AddReserve({
76                         branchcode     => $patron->branchcode,
77                         borrowernumber => $patron->borrowernumber,
78                         biblionumber   => $item_1->biblionumber }); }
79         [ qr/after_hold_create is deprecated and will be removed soon/,
80           qr/after_hold_create called with parameter Koha::Hold/ ],
81           'AddReserve calls the after_hold_create hook, deprecation warning found';
82
83     Koha::Plugins::Methods->delete;
84     $schema->storage->txn_rollback;
85 };
86
87 subtest 'after_hold_action (placed) hook tests' => sub {
88
89     plan tests => 1;
90
91     $schema->storage->txn_begin;
92
93     my $plugins = Koha::Plugins->new;
94     $plugins->InstallPlugins;
95
96     my $plugin = Koha::Plugin::Test->new->enable;
97
98     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
99
100     t::lib::Mocks::mock_userenv(
101         {
102             patron     => $patron,
103             branchcode => $patron->branchcode
104         }
105     );
106
107     # Avoid testing useless warnings
108     my $test_plugin = Test::MockModule->new('Koha::Plugin::Test');
109     $test_plugin->mock( 'after_item_action',   undef );
110     $test_plugin->mock( 'after_biblio_action', undef );
111     $test_plugin->mock( 'item_barcode_transform', undef );
112     $test_plugin->mock( 'after_hold_create', undef );
113
114     my $biblio = $builder->build_sample_biblio();
115     my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
116
117     warnings_like {
118         AddReserve(
119             {   branchcode     => $patron->branchcode,
120                 borrowernumber => $patron->borrowernumber,
121                 biblionumber   => $item_1->biblionumber
122             }
123         );
124     }
125     [ qr/after_hold_create is deprecated and will be removed soon/, qr/after_hold_action called with action: place, ref: Koha::Hold/,  ], 'AddReserve calls the after_hold_action hook';
126
127     Koha::Plugins::Methods->delete;
128     $schema->storage->txn_rollback;
129 };
130
131 subtest 'Koha::Hold tests' => sub {
132
133     plan tests => 7;
134
135     $schema->storage->txn_begin;
136
137     my $plugins = Koha::Plugins->new;
138     $plugins->InstallPlugins;
139
140     my $plugin = Koha::Plugin::Test->new->enable;
141
142     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
143
144     # Reduce noise
145     t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
146     t::lib::Mocks::mock_preference( 'HoldsLog',    0 );
147
148     my $hold = $builder->build_object(
149         {
150             class => 'Koha::Holds',
151             value => {
152                 borrowernumber => $patron->id, found => undef,
153             }
154         }
155     );
156
157     warning_like {
158         $hold->fill;
159     }
160     qr/after_hold_action called with action: fill, ref: Koha::Old::Hold/,
161       '->fill calls the after_hold_action hook';
162
163     $hold = $builder->build_object(
164         {
165             class => 'Koha::Holds',
166             value => {
167                 borrowernumber => $patron->id, found => undef,
168             }
169         }
170     );
171
172     warning_like {
173         $hold->suspend_hold;
174     }
175     qr/after_hold_action called with action: suspend, ref: Koha::Hold/,
176       '->suspend_hold calls the after_hold_action hook';
177
178     warning_like {
179         $hold->resume;
180     }
181     qr/after_hold_action called with action: resume, ref: Koha::Hold/,
182       '->resume calls the after_hold_action hook';
183
184     warning_like {
185         $hold->set_transfer;
186     }
187     qr/after_hold_action called with action: transfer, ref: Koha::Hold/,
188       '->set_transfer calls the after_hold_action hook';
189
190     warning_like {
191         $hold->set_processing;
192     }
193     qr/after_hold_action called with action: processing, ref: Koha::Hold/,
194       '->set_processing calls the after_hold_action hook';
195
196     warning_like {
197         $hold->set_waiting;
198     }
199     qr/after_hold_action called with action: waiting, ref: Koha::Hold/,
200       '->set_waiting calls the after_hold_action hook';
201
202     warning_like {
203         $hold->cancel;
204     }
205     qr/after_hold_action called with action: cancel, ref: Koha::Old::Hold/,
206       '->cancel calls the after_hold_action hook';
207
208     Koha::Plugins::Methods->delete;
209     $schema->storage->txn_rollback;
210 };