3 # This file is part of Koha.
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
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.
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>.
19 use Test::More tests => 4;
25 use C4::Circulation qw( AddIssue AddRenewal AddReturn );
28 use t::lib::TestBuilder;
31 # Mock pluginsdir before loading Plugins module
32 my $path = dirname(__FILE__) . '/../../../lib';
33 t::lib::Mocks::mock_config( 'pluginsdir', $path );
35 use_ok('Koha::Plugins');
36 use_ok('Koha::Plugins::Handler');
37 use_ok('Koha::Plugin::Test');
40 my $schema = Koha::Database->new->schema;
41 my $builder = t::lib::TestBuilder->new;
43 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
45 subtest 'after_circ_action() hook tests' => sub {
49 $schema->storage->txn_begin;
51 my $plugins = Koha::Plugins->new;
52 $plugins->InstallPlugins;
54 my $plugin = Koha::Plugin::Test->new->enable;
56 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
58 t::lib::Mocks::mock_userenv(
61 branchcode => $patron->branchcode
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 );
70 my $biblio = $builder->build_sample_biblio();
71 my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
72 my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
74 subtest 'AddIssue' => sub {
77 warning_like { AddIssue( $patron->unblessed, $item_1->barcode ); }
78 qr/after_circ_action called with action: checkout, ref: Koha::Checkout type: issue/,
79 'AddIssue calls the after_circ_action hook';
81 warning_like { AddIssue( $patron->unblessed, $item_2->barcode, undef, undef, undef, undef, { onsite_checkout => 1 } ); }
82 qr/after_circ_action called with action: checkout, ref: Koha::Checkout type: onsite_checkout/,
83 'AddIssue calls the after_circ_action hook (onsite_checkout case)';
86 subtest 'AddRenewal' => sub {
89 warning_like { AddRenewal( $patron->borrowernumber, $item_1->id, $patron->branchcode ); }
90 qr/after_circ_action called with action: renewal, ref: Koha::Checkout/,
91 'AddRenewal calls the after_circ_action hook';
94 subtest 'AddReturn' => sub {
97 t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems', 1);
98 $item_1->set({ withdrawn => 1 })->store;
101 AddReturn( $item_1->barcode, $patron->branchcode );
102 } undef, 'No hook called because no return happened';
104 t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems', 0);
105 $item_1->set({ withdrawn => 0 })->store;
108 AddReturn( $item_1->barcode, $patron->branchcode );
110 qr/after_circ_action called with action: checkin, ref: Koha::Old::Checkout/,
111 'AddReturn calls the after_circ_action hook';
114 $schema->storage->txn_rollback;
115 Koha::Plugins::Methods->delete;