Bug 31421: Add tests
[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 => 6;
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 my $builder = t::lib::TestBuilder->new;
34 my $nb_categories = Koha::Patron::Categories->count;
35
36 # Create sample categories
37 my $category_1 = $builder->build_object( { class => 'Koha::Patron::Categories' } );
38 my @categories = Koha::Template::Plugin::Categories->new->all->as_list;
39 is( scalar(@categories), 1 + $nb_categories, '->all returns all defined categories' );
40
41 my $category_2 = $builder->build_object( { class => 'Koha::Patron::Categories' } );
42 @categories = Koha::Template::Plugin::Categories->new->all->as_list;
43 is( scalar(@categories), 2 + $nb_categories, '->all returns all defined categories' );
44
45 is( Koha::Template::Plugin::Categories->GetName(
46         $category_1->categorycode
47     ),
48     $category_1->description,
49     '->GetName returns the right description'
50 );
51
52 my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
53 my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
54 $category_1->library_limits( [ $library_1->branchcode ] );
55 $category_2->library_limits( [ $library_2->branchcode ] );
56 t::lib::Mocks::mock_userenv( { branchcode => $library_1->branchcode } );
57 my $limited = Koha::Template::Plugin::Categories->limited;
58 is( $limited->search( { 'me.categorycode' => $category_1->categorycode } )->count,
59     1, 'Category 1 is available from library 1' );
60 is( $limited->search( { 'me.categorycode' => $category_2->categorycode } )->count,
61     0, 'Category 2 is not available from library 1' );
62
63 $schema->storage->txn_rollback;
64
65 subtest 'can_any_reset_password() tests' => sub {
66
67     plan tests => 3;
68
69     $schema->storage->txn_begin;
70
71     # Make sure all existing categories have reset_password set to 0
72     Koha::Patron::Categories->search->update({ reset_password => 0 });
73
74     ok( !Koha::Template::Plugin::Categories->new->can_any_reset_password, 'No category is allowed to reset password' );
75
76     t::lib::Mocks::mock_preference( 'OpacResetPassword', 0 );
77
78     my $category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reset_password => 1 } });
79
80     ok( Koha::Template::Plugin::Categories->new->can_any_reset_password, 'There\'s at least a category that is allowed to reset password' );
81
82     $category->reset_password( undef )->store;
83
84     ok( !Koha::Template::Plugin::Categories->new->can_any_reset_password, 'No category is allowed to reset password' );
85
86     $schema->storage->txn_rollback;
87 };