Bug 33360: Add Koha::Notice::Util for mail domain limits
[koha.git] / t / db_dependent / Koha / Plugins / Barcode_transform_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 => 4;
20 use Test::MockModule;
21 use Test::Warn;
22
23 use File::Basename;
24
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
27
28 BEGIN {
29     # Mock pluginsdir before loading Plugins module
30     my $path = dirname(__FILE__) . '/../../../lib/plugins';
31     t::lib::Mocks::mock_config( 'pluginsdir', $path );
32
33     use_ok('Koha::Plugins');
34     use_ok('Koha::Plugins::Handler');
35     use_ok('Koha::Plugin::Test');
36 }
37
38 my $schema  = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
40
41 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
42
43 subtest 'patron_barcode_transform() and item_barcode_transform() hook tests' => sub {
44
45     plan tests => 6;
46
47     $schema->storage->txn_begin;
48
49     # Avoid testing useless warnings
50     my $test_plugin = Test::MockModule->new('Koha::Plugin::Test');
51     $test_plugin->mock( 'after_item_action',   undef );
52     $test_plugin->mock( 'after_biblio_action', undef );
53
54     my $plugins = Koha::Plugins->new;
55
56     warning_is { $plugins->InstallPlugins; } undef;
57
58     C4::Context->dbh->do("DELETE FROM plugin_methods WHERE plugin_class LIKE '%TestBarcodes%'");
59
60     my $plugin = Koha::Plugin::Test->new->enable;
61
62     my $patron = $builder->build_object(
63         { class => 'Koha::Patrons', value => { cardnumber => undef } } );
64
65     t::lib::Mocks::mock_preference( 'autoMemberNum', 1 );
66     warnings_like { $patron->store(); }
67     [
68         qr/patron_barcode_transform called with parameter: /,
69         qr/patron_barcode_transform called with parameter: /
70     ],
71     'Koha::Patron::store calls the patron_barcode_transform hook twice when autoMemberNum is enabled and cardnumber is undefined';
72
73     $patron->cardnumber('TEST');
74     warning_like { $patron->store(); }
75         qr/patron_barcode_transform called with parameter: TEST/,
76         'Koha::Patron::store calls the patron_barcode_transform hook once when autoMemberNum is enabled and cardnumber is set';
77
78     t::lib::Mocks::mock_preference( 'autoMemberNum', 0 );
79     $patron->cardnumber(undef);
80     warning_like { $patron->store(); }
81         qr/patron_barcode_transform called with parameter: /,
82         'Koha::Patron::store calls the patron_barcode_transform hook once when autoMemberNum is disabled and cardnumber is undefined';
83
84     t::lib::Mocks::mock_userenv(
85         {
86             patron     => $patron,
87             branchcode => $patron->branchcode
88         }
89     );
90
91     my $item;
92     warning_like { $item = $builder->build_sample_item(); }
93         qr/Plugin error \(Test Plugin\): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: /,
94         'Koha::Item->store calls the item_barcode_transform hook';
95
96     $item->barcode('THISISATEST');
97
98     warning_like { $item->store(); }
99         qr/Plugin error \(Test Plugin\): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: THISISATEST'/,
100         'Koha::Item->store calls the item_barcode_transform hook';
101
102     $schema->storage->txn_rollback;
103     Koha::Plugins::Methods->delete;
104 };