Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[koha.git] / t / db_dependent / Koha / Plugins / Recall_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 use File::Basename;
19 use Test::More tests => 4;
20 use Test::MockModule;
21 use Test::Warn;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use Koha::Database;
27 use C4::Circulation qw();
28 use Koha::CirculationRules;
29 use Koha::Plugins::Methods;
30 use Koha::Recalls;
31
32 BEGIN {
33     # Mock pluginsdir before loading Plugins module
34     my $path = dirname(__FILE__) . '/../../../lib/plugins';
35     t::lib::Mocks::mock_config( 'pluginsdir', $path );
36
37     use_ok('Koha::Plugins');
38     use_ok('Koha::Plugins::Handler');
39     use_ok('Koha::Plugin::Test');
40 }
41
42 my $schema  = Koha::Database->new->schema;
43 my $builder = t::lib::TestBuilder->new;
44
45 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
46
47 subtest 'after_recall_action hook' => sub {
48
49     plan tests => 1;
50
51     $schema->storage->txn_begin;
52
53     my $plugins = Koha::Plugins->new;
54     $plugins->InstallPlugins;
55
56     my $plugin = Koha::Plugin::Test->new->enable;
57     # Avoid testing useless warnings
58     my $test_plugin = Test::MockModule->new('Koha::Plugin::Test');
59     $test_plugin->mock( 'after_item_action',   undef );
60     $test_plugin->mock( 'after_circ_action', undef );
61     $test_plugin->mock( 'after_biblio_action', undef );
62     $test_plugin->mock( 'patron_barcode_transform', undef );
63     $test_plugin->mock( 'item_barcode_transform', undef );
64
65     my $item = $builder->build_sample_item();
66     my $biblio = $item->biblio;
67     my $branch = $item->holdingbranch;
68     my $category = $builder->build({ source => 'Category' })->{ categorycode };
69     my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category, branchcode => $branch } });
70     my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category, branchcode => $branch } });
71     t::lib::Mocks::mock_userenv({ patron => $patron1 });
72
73     Koha::CirculationRules->set_rules({
74         branchcode => undef,
75         categorycode => undef,
76         itemtype => undef,
77         rules => {
78             'recall_due_date_interval' => undef,
79             'recalls_allowed' => 10,
80         }
81     });
82
83     C4::Circulation::AddIssue( $patron2->unblessed, $item->barcode );
84
85     warning_like {
86       Koha::Recalls->add_recall({
87           patron => $patron1,
88           biblio => $biblio,
89           branchcode => $branch,
90           item => undef,
91           expirationdate => undef,
92           interface => 'COMMANDLINE',
93       });
94     }
95     qr/after_recall_action called with action: add, ref: Koha::Recall/,
96       '->add_recall calls the after_recall_action hook with action add';
97
98     Koha::Plugins::Methods->delete;
99     $schema->storage->txn_rollback;
100 };