Bug 30047: (follow-up) Fix failing tests
[koha.git] / t / db_dependent / Koha / Plugins / authority_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 MARC::Record;
20 use Test::More tests => 4;
21 use Test::Warn;
22
23 use File::Basename;
24
25 use C4::AuthoritiesMarc ();
26 use Koha::Database;
27
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
30
31 BEGIN {
32     # Mock pluginsdir before loading Plugins module
33     my $path = dirname(__FILE__) . '/../../../lib/plugins';
34     t::lib::Mocks::mock_config( 'pluginsdir', $path );
35
36     use_ok('Koha::Plugins');
37     use_ok('Koha::Plugins::Handler');
38     use_ok('Koha::Plugin::Test');
39 }
40
41 my $schema  = Koha::Database->new->schema;
42 my $builder = t::lib::TestBuilder->new;
43
44 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
45
46 subtest 'after_authority_action hook' => sub {
47     plan tests => 3;
48
49     $schema->storage->txn_begin;
50
51     my $plugins = Koha::Plugins->new;
52     $plugins->InstallPlugins;
53     my $plugin = Koha::Plugin::Test->new->enable;
54     my $id;
55
56     my $record = MARC::Record->new;
57     $record->append_fields( MARC::Field->new( '100', '1', '2', a => 'Name' ) );
58     my $type = $builder->build( { source => 'AuthType', value => { auth_tag_to_report => '100' } } );
59
60     warnings_exist { ( $id ) = C4::AuthoritiesMarc::AddAuthority( $record, undef, $type->{authtypecode} ); }
61             qr/after_authority_action called with action: create, id: \d+/,
62             'AddAuthority calls the hook with action=create, id passed';
63
64     warnings_exist { C4::AuthoritiesMarc::ModAuthority( $id, $record, $type->{authtypecode}, { skip_merge => 1 } ); }
65             qr/after_authority_action called with action: modify, id: $id/,
66             'ModAuthority calls the hook with action=modify, id passed';
67
68     warnings_exist { C4::AuthoritiesMarc::DelAuthority({ authid => $id, skip_merge => 1 }); }
69             qr/after_authority_action called with action: delete, id: $id/,
70             'DelAuthority calls the hook with action=delete, id passed';
71
72     Koha::Plugins->RemovePlugins;
73     $schema->storage->txn_rollback;
74 };