Bug 21890: Add can_any_reset_password() to the Categories TT plugin
[koha.git] / t / db_dependent / Template / Plugin / Categories.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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 5;
21 use t::lib::Mocks;
22 use t::lib::TestBuilder;
23
24 use Koha::Patron::Categories;
25 use Koha::Checkouts;
26 use Koha::Patrons;
27 use Koha::Database;
28 use Koha::Template::Plugin::Categories;
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 # Delete all categories
34 Koha::Checkouts->search->delete;
35 Koha::Patrons->search->delete;
36 Koha::Patron::Categories->search->delete;
37
38 my $builder = t::lib::TestBuilder->new;
39
40 is( Koha::Template::Plugin::Categories->new->all->count,
41     0, '->all returns 0 results if no categories defined' );
42
43 # Create sample categories
44 my $category_1 = $builder->build( { source => 'Category' } );
45 my @categories = Koha::Template::Plugin::Categories->new->all;
46 is( scalar(@categories), 1, '->all returns all defined categories' );
47
48 my $category_2 = $builder->build( { source => 'Category' } );
49 @categories = Koha::Template::Plugin::Categories->new->all;
50 is( scalar(@categories), 2, '->all returns all defined categories' );
51
52 is( Koha::Template::Plugin::Categories->GetName(
53         $category_1->{categorycode}
54     ),
55     $category_1->{description},
56     '->GetName returns the right description'
57 );
58
59 $schema->storage->txn_rollback;
60
61 subtest 'can_any_reset_password() tests' => sub {
62
63     plan tests => 3;
64
65     $schema->storage->txn_begin;
66
67     # Make sure all existing categories have reset_password set to 0
68     Koha::Patron::Categories->update({ reset_password => 0 });
69
70     ok( !Koha::Template::Plugin::Categories->new->can_any_reset_password, 'No category is allowed to reset password' );
71
72     t::lib::Mocks::mock_preference( 'OpacResetPassword', 0 );
73
74     my $category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reset_password => 1 } });
75
76     ok( Koha::Template::Plugin::Categories->new->can_any_reset_password, 'There\'s at least a category that is allowed to reset password' );
77
78     $category->reset_password( undef )->store;
79
80     ok( !Koha::Template::Plugin::Categories->new->can_any_reset_password, 'No category is allowed to reset password' );
81
82     $schema->storage->txn_rollback;
83 };