Bug 22694: Unit tests
[koha-equinox.git] / t / db_dependent / Koha / Patron / Category.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 3;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Database;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'effective_reset_password() tests' => sub {
33
34     plan tests => 2;
35
36     subtest 'specific overrides global' => sub {
37
38         plan tests => 4;
39
40         $schema->storage->txn_begin;
41
42         my $category = $builder->build_object({
43             class => 'Koha::Patron::Categories',
44             value => {
45                 reset_password => 1
46             }
47         });
48
49         t::lib::Mocks::mock_preference( 'OpacResetPassword', 0 );
50         ok( $category->effective_reset_password, 'OpacResetPassword unset, but category has the flag set to 1' );
51
52         t::lib::Mocks::mock_preference( 'OpacResetPassword', 1 );
53         ok( $category->effective_reset_password, 'OpacResetPassword set and category has the flag set to 1' );
54
55         # disable
56         $category->reset_password( 0 )->store->discard_changes;
57
58         t::lib::Mocks::mock_preference( 'OpacResetPassword', 0 );
59         ok( !$category->effective_reset_password, 'OpacResetPassword unset, but category has the flag set to 0' );
60
61         t::lib::Mocks::mock_preference( 'OpacResetPassword', 1 );
62         ok( !$category->effective_reset_password, 'OpacResetPassword set and category has the flag set to 0' );
63
64         $schema->storage->txn_rollback;
65     };
66
67     subtest 'no specific rule, global applies' => sub {
68
69         plan tests => 2;
70
71         $schema->storage->txn_begin;
72
73         my $category = $builder->build_object({
74             class => 'Koha::Patron::Categories',
75             value => {
76                 reset_password => undef
77             }
78         });
79
80         t::lib::Mocks::mock_preference( 'OpacResetPassword', 0 );
81         ok( !$category->effective_reset_password, 'OpacResetPassword set to 0 used' );
82
83         t::lib::Mocks::mock_preference( 'OpacResetPassword', 1 );
84         ok( $category->effective_reset_password, 'OpacResetPassword set to 1 used' );
85
86         $schema->storage->txn_rollback;
87     };
88 };
89
90 subtest 'effective_change_password() tests' => sub {
91
92     plan tests => 2;
93
94     subtest 'specific overrides global' => sub {
95
96         plan tests => 4;
97
98         $schema->storage->txn_begin;
99
100         my $category = $builder->build_object({
101             class => 'Koha::Patron::Categories',
102             value => {
103                 change_password => 1
104             }
105         });
106
107         t::lib::Mocks::mock_preference( 'OpacPasswordChange', 0 );
108         ok( $category->effective_change_password, 'OpacPasswordChange unset, but category has the flag set to 1' );
109
110         t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 );
111         ok( $category->effective_change_password, 'OpacPasswordChange set and category has the flag set to 1' );
112
113         # disable
114         $category->change_password( 0 )->store->discard_changes;
115
116         t::lib::Mocks::mock_preference( 'OpacPasswordChange', 0 );
117         ok( !$category->effective_change_password, 'OpacPasswordChange unset, but category has the flag set to 0' );
118
119         t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 );
120         ok( !$category->effective_change_password, 'OpacPasswordChange set and category has the flag set to 0' );
121
122         $schema->storage->txn_rollback;
123     };
124
125     subtest 'no specific rule, global applies' => sub {
126
127         plan tests => 2;
128
129         $schema->storage->txn_begin;
130
131         my $category = $builder->build_object({
132             class => 'Koha::Patron::Categories',
133             value => {
134                 change_password => undef
135             }
136         });
137
138         t::lib::Mocks::mock_preference( 'OpacPasswordChange', 0 );
139         ok( !$category->effective_change_password, 'OpacPasswordChange set to 0 used' );
140
141         t::lib::Mocks::mock_preference( 'OpacPasswordChange', 1 );
142         ok( $category->effective_change_password, 'OpacPasswordChange set to 1 used' );
143
144         $schema->storage->txn_rollback;
145     };
146 };
147
148 subtest 'override_hidden_items() tests' => sub {
149
150     plan tests => 4;
151
152     $schema->storage->txn_begin;
153
154     my $category_1 = $builder->build_object({ class => 'Koha::Patron::Categories' });
155     my $category_2 = $builder->build_object({ class => 'Koha::Patron::Categories' });
156
157     t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', $category_1->categorycode . '|' . $category_2->categorycode . '|RANDOM' );
158
159     ok( $category_1->override_hidden_items, 'Category configured to override' );
160     ok( $category_2->override_hidden_items, 'Category configured to override' );
161
162     t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', 'RANDOM|' . $category_2->categorycode );
163
164     ok( !$category_1->override_hidden_items, 'Category not configured to override' );
165     ok( $category_2->override_hidden_items, 'Category configured to override' );
166
167     $schema->storage->txn_rollback;
168 };