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;
26 use t::lib::TestBuilder;
29 # Mock pluginsdir before loading Plugins module
30 my $path = dirname(__FILE__) . '/../../../lib/plugins';
31 t::lib::Mocks::mock_config( 'pluginsdir', $path );
33 use_ok('Koha::Plugins');
34 use_ok('Koha::Plugins::Handler');
35 use_ok('Koha::Plugin::Test');
38 my $schema = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
41 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
43 subtest 'patron_barcode_transform() and item_barcode_transform() hook tests' => sub {
47 $schema->storage->txn_begin;
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 );
54 my $plugins = Koha::Plugins->new;
56 warning_is { $plugins->InstallPlugins; } undef;
58 C4::Context->dbh->do("DELETE FROM plugin_methods WHERE plugin_class LIKE '%TestBarcodes%'");
60 my $plugin = Koha::Plugin::Test->new->enable;
62 my $patron = $builder->build_object(
63 { class => 'Koha::Patrons', value => { cardnumber => undef } } );
65 t::lib::Mocks::mock_preference( 'autoMemberNum', 1 );
66 warnings_like { $patron->store(); }
68 qr/patron_barcode_transform called with parameter: /,
69 qr/patron_barcode_transform called with parameter: /
71 'Koha::Patron::store calls the patron_barcode_transform hook twice when autoMemberNum is enabled and cardnumber is undefined';
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';
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';
84 t::lib::Mocks::mock_userenv(
87 branchcode => $patron->branchcode
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';
96 $item->barcode('THISISATEST');
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';
102 $schema->storage->txn_rollback;
103 Koha::Plugins::Methods->delete;