3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 6;
22 use t::lib::TestBuilder;
24 use Koha::Patron::Categories;
28 use Koha::Template::Plugin::Categories;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new;
34 my $nb_categories = Koha::Patron::Categories->count;
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' );
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' );
45 is( Koha::Template::Plugin::Categories->GetName(
46 $category_1->categorycode
48 $category_1->description,
49 '->GetName returns the right description'
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' );
63 $schema->storage->txn_rollback;
65 subtest 'can_any_reset_password() tests' => sub {
69 $schema->storage->txn_begin;
71 # Make sure all existing categories have reset_password set to 0
72 Koha::Patron::Categories->search->update({ reset_password => 0 });
74 ok( !Koha::Template::Plugin::Categories->new->can_any_reset_password, 'No category is allowed to reset password' );
76 t::lib::Mocks::mock_preference( 'OpacResetPassword', 0 );
78 my $category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reset_password => 1 } });
80 ok( Koha::Template::Plugin::Categories->new->can_any_reset_password, 'There\'s at least a category that is allowed to reset password' );
82 $category->reset_password( undef )->store;
84 ok( !Koha::Template::Plugin::Categories->new->can_any_reset_password, 'No category is allowed to reset password' );
86 $schema->storage->txn_rollback;