Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Koha / Plugins / Patron.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 => 5;
20 use Test::Exception;
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     use_ok('Koha::Patron');
38 }
39
40 my $schema  = Koha::Database->new->schema;
41 my $builder = t::lib::TestBuilder->new;
42
43 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
44
45 subtest 'check_password hook tests' => sub {
46
47     plan tests => 6;
48
49     $schema->storage->txn_begin;
50
51     my $plugins = Koha::Plugins->new;
52     $plugins->InstallPlugins;
53
54     # Test Plugin enforces a 4 digit numeric pin for passwords
55     my $plugin = Koha::Plugin::Test->new->enable;
56
57     my $library  = $builder->build( { source => 'Branch' } );
58     my $category = $builder->build( { source => 'Category' } );
59     my $patron   = Koha::Patron->new(
60         {
61             cardnumber   => 'test_cn_1',
62             branchcode   => $library->{branchcode},
63             categorycode => $category->{categorycode},
64             surname      => 'surname for patron1',
65             firstname    => 'firstname for patron1',
66             userid       => 'a_nonexistent_userid_1',
67         }
68     );
69
70     # store hook (add action)
71     $patron->password('exploder');
72     throws_ok { $patron->store } 'Koha::Exceptions::Password::Plugin',
73       'Plugin Exception raised for adding patron with bad password';
74     $patron->password('1234');
75     ok( $patron->store, 'Patron created with good password' );
76
77     $patron->discard_changes;
78     $patron->password('87654321');
79     $patron->store;
80     isnt($patron->password, '87654321', 'Koha::Patron->store silently drops changes to password');
81
82     # set_password hook (update action)
83     t::lib::Mocks::mock_preference( 'RequireStrongPassword', '0' );
84     t::lib::Mocks::mock_preference( 'minPasswordLength', '4' ); # Testing Plugin validation, not internal validation
85     throws_ok { $patron->set_password({ password => 'explosion' }) } 'Koha::Exceptions::Password::Plugin',
86       'Exception raised for update patron password with bad string';
87     ok( $patron->set_password({ password => '4321' }), 'Patron password updated with good string' );
88     ok( $patron->set_password({ password => 'explosion', skip_validation => 1}), 'Patron password updated via skip validation');
89
90     $schema->storage->txn_rollback;
91     Koha::Plugins::Methods->delete;
92 };
93
94 1;