View | Details | Raw Unified | Return to bug 22709
Collapse All | Expand All

(-)a/t/db_dependent/Biblio_and_Items_plugin_hooks.t (+83 lines)
Line 0 Link Here
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 => 4;
20
use Test::Warn;
21
22
use File::Basename;
23
24
use C4::Items;
25
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
28
29
BEGIN {
30
    # Mock pluginsdir before loading Plugins module
31
    my $path = dirname(__FILE__) . '/../lib';
32
    t::lib::Mocks::mock_config( 'pluginsdir', $path );
33
34
    use_ok('Koha::Plugins');
35
    use_ok('Koha::Plugins::Handler');
36
    use_ok('Koha::Plugin::Test');
37
}
38
39
my $schema  = Koha::Database->new->schema;
40
my $builder = t::lib::TestBuilder->new;
41
42
t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
43
44
subtest 'after_biblio_action() and after_item_action() hooks tests' => sub {
45
46
    plan tests => 6;
47
48
    $schema->storage->txn_begin;
49
50
    my $plugins = Koha::Plugins->new;
51
    $plugins->InstallPlugins;
52
53
    my $plugin = Koha::Plugin::Test->new;
54
55
    my $biblio_id;
56
57
    warning_like { $biblio_id = C4::Biblio::AddBiblio( MARC::Record->new(), '' ); }
58
            qr/after_biblio_action called with action: create, ref: Koha::Biblio/,
59
            'AddBiblio calls the hook with action=create';
60
61
    warning_like { C4::Biblio::ModBiblio( MARC::Record->new(), $biblio_id, '' ); }
62
            qr/after_biblio_action called with action: modify, ref: Koha::Biblio/,
63
            'ModBiblio calls the hook with action=modify';
64
65
    my $item;
66
    warning_like { $item = $builder->build_sample_item({ biblionumber => $biblio_id }); }
67
            qr/after_item_action called with action: create, ref: Koha::Item/,
68
            'AddItem calls the hook with action=create';
69
70
    warning_like { C4::Items::ModItem({ location => 'shelves' }, $biblio_id, $item->itemnumber); }
71
            qr/after_item_action called with action: modify, ref: Koha::Item/,
72
            'ModItem calls the hook with action=modify';
73
74
    warning_like { C4::Items::DelItem({ itemnumber => $item->itemnumber }); }
75
            qr/after_item_action called with action: delete/,
76
            'DelItem calls the hook with action=delete, item_id passed';
77
78
    warning_like { C4::Biblio::DelBiblio( $biblio_id ); }
79
            qr/after_biblio_action called with action: delete/,
80
            'DelBiblio calls the hook with action=delete biblio_id passed';
81
82
    $schema->storage->txn_rollback;
83
};
(-)a/t/lib/Koha/Plugin/Test.pm (-1 / +30 lines)
Lines 3-8 package Koha::Plugin::Test; Link Here
3
## It's good practice to use Modern::Perl
3
## It's good practice to use Modern::Perl
4
use Modern::Perl;
4
use Modern::Perl;
5
5
6
use Koha::Exceptions::Exception;
6
use Mojo::JSON qw(decode_json);
7
use Mojo::JSON qw(decode_json);
7
8
8
## Required for all plugins
9
## Required for all plugins
Lines 124-129 sub api_namespace { Link Here
124
    return "testplugin";
125
    return "testplugin";
125
}
126
}
126
127
128
sub after_biblio_action {
129
    my ( $self, $params ) = @_;
130
    my $action    = $params->{action} // '';
131
    my $biblio    = $params->{biblio};
132
    my $biblio_id = $params->{biblio_id};
133
134
    if ( $action ne 'delete' ) {
135
        Koha::Exceptions::Exception->throw("after_biblio_action called with action: $action, ref: " . ref($biblio) );
136
    }
137
    else {
138
        Koha::Exceptions::Exception->throw("after_biblio_action called with action: $action") if $biblio_id;
139
    }
140
}
141
142
143
sub after_item_action {
144
    my ( $self, $params ) = @_;
145
    my $action  = $params->{action} // '';
146
    my $item    = $params->{item};
147
    my $item_id = $params->{item_id};
148
149
    if ( $action ne 'delete' ) {
150
        Koha::Exceptions::Exception->throw("after_item_action called with action: $action, ref: " . ref($item) );
151
    }
152
    else {
153
        Koha::Exceptions::Exception->throw("after_item_action called with action: $action" ) if $item_id;
154
    }
155
}
156
127
sub api_routes {
157
sub api_routes {
128
    my ( $self, $args ) = @_;
158
    my ( $self, $args ) = @_;
129
159
130
- 

Return to bug 22709