Bug 27117: Only place_holds permission is needed to adjust pickup locations
[koha.git] / t / db_dependent / Koha / Template / Plugin / Registers.t
1 #!/usr/bin/perl
2
3 # Copyright PTFS Europe 2020
4
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20 use Test::More tests => 4;
21
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
24
25 use C4::Context;
26 use Koha::Database;
27
28 BEGIN {
29     use_ok('Koha::Template::Plugin::Registers');
30 }
31
32 my $schema  = Koha::Database->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 subtest 'session_register_id' => sub {
36
37     plan tests => 3;
38
39     my $plugin = Koha::Template::Plugin::Registers->new();
40     ok( $plugin, "Plugin initialized" );
41     is( $plugin->session_register_id,
42         '', "Returns empty string if no userenv is set" );
43     t::lib::Mocks::mock_userenv( { register_id => '1' } );
44     is( $plugin->session_register_id,
45         '1', "Returns the register id when set in the userenv" );
46
47     # Unset the userenv
48     C4::Context->_new_userenv(undef);
49 };
50
51 subtest 'session_register_name' => sub {
52
53     plan tests => 3;
54
55     my $plugin = Koha::Template::Plugin::Registers->new();
56     ok( $plugin, "Plugin initialized" );
57     is( $plugin->session_register_name,
58         '', "Returns empty string if no userenv is set" );
59     t::lib::Mocks::mock_userenv( { register_name => 'Register One' } );
60     is( $plugin->session_register_name,
61         'Register One', "Returns the register name when set in the userenv" );
62
63     # Unset the userenv
64     C4::Context->_new_userenv(undef);
65 };
66
67 subtest 'all() tests' => sub {
68
69     plan tests => 25;
70
71     $schema->storage->txn_begin;
72
73     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
74
75     my $count = Koha::Cash::Registers->search({ archived => 0 })->count;
76     my $max_register = Koha::Cash::Registers->search( {},
77         { order_by => { '-desc' => 'id' }, rows => 1 } )->single;
78     my $max_id = $max_register ? $max_register->id : 0;
79
80     my $library1 = $builder->build_object(
81         {
82             class => 'Koha::Libraries'
83         }
84     );
85     my $register1 = $builder->build_object(
86         {
87             class => 'Koha::Cash::Registers',
88             value => {
89                 branch         => $library1->branchcode,
90                 branch_default => 0,
91                 archived       => 0
92             }
93         }
94     );
95     my $register2 = $builder->build_object(
96         {
97             class => 'Koha::Cash::Registers',
98             value => {
99                 branch         => $library1->branchcode,
100                 branch_default => 1,
101                 archived       => 0
102             }
103         }
104     );
105
106     my $library2 = $builder->build_object(
107         {
108             class => 'Koha::Libraries'
109         }
110     );
111     my $register3 = $builder->build_object(
112         {
113             class => 'Koha::Cash::Registers',
114             value => {
115                 branch   => $library2->branchcode,
116                 archived => 0
117             }
118         }
119     );
120
121     my $plugin = Koha::Template::Plugin::Registers->new();
122     ok( $plugin, "Plugin initialized" );
123
124     my $result = $plugin->all;
125     is( ref($result), 'ARRAY', "Return arrayref (no userenv, no filters)" );
126     is( scalar( @{$result} ),
127         3 + $count, "Array contains all test registers (no userenv, no filters)" );
128     for my $register ( @{$result} ) {
129         next if $register->{id} <= $max_id;
130         is( $register->{selected}, 0, "Register is not selected (no userenv)" );
131     }
132
133     $result = $plugin->all( { filters => { current_branch => 1 } } );
134     is( ref($result), 'ARRAY',
135         "Return arrayref (no userenv, filters: current_branch)" );
136
137     t::lib::Mocks::mock_userenv( { branchcode => $library1->branchcode } );
138     $result = $plugin->all;
139     is( ref($result), 'ARRAY',
140         "Return arrayref (userenv: branchcode, no filters)" );
141     is( scalar( @{$result} ),
142         3 + $count, "Array contains all test registers (userenv: branchcode, no filters)" );
143     for my $register ( @{$result} ) {
144         next if $register->{id} <= $max_id;
145         is( $register->{selected}, 0,
146             "Register is not selected (userenv: branchcode, no filters)" );
147     }
148
149     $result = $plugin->all( { filters => { current_branch => 1 } } );
150     is( ref($result), 'ARRAY',
151         "Return arrayref (userenv: branchcode, filters: current_branch)" );
152     is(
153         scalar( @{$result} ),
154         2,
155 "Array contains 2 branch registers (userenv: branchcode, filters: current_branch)"
156     );
157     for my $register ( @{$result} ) {
158         is( $register->{selected}, 0,
159 "Register is not selected (userenv: branchcode, filters: current_branch)"
160         );
161     }
162
163     t::lib::Mocks::mock_userenv(
164         { branchcode => $library1->branchcode, register_id => $register2->id }
165     );
166     $result = $plugin->all( { filters => { current_branch => 1 } } );
167     is( ref($result), 'ARRAY',
168 "Return arrayref (userenv: branchcode + register_id, filters: current_branch)"
169     );
170     is(
171         scalar( @{$result} ),
172         2,
173 "Array contains 2 branch registers (userenv: branchcode + register_id, filters: current_branch)"
174     );
175     for my $register ( @{$result} ) {
176         my $selected = ( $register->{id} == $register2->id ) ? 1 : 0;
177         is( $register->{selected}, $selected,
178 "Register is selected $selected (userenv: brancode, filters: current_branch)"
179         );
180     }
181
182     $result = $plugin->all( { filters => { current_branch => 1 }, selected => $register1->id } );
183     is( ref($result), 'ARRAY',
184 "Return arrayref (userenv: branchcode + register_id, filters: current_branch, selected: register 1)"
185     );
186     is(
187         scalar( @{$result} ),
188         2,
189 "Array contains 2 branch registers (userenv: branchcode + register_id, filters: current_branch, selected: register 1)"
190     );
191     for my $register ( @{$result} ) {
192         my $selected = ( $register->{id} == $register1->id ) ? 1 : 0;
193         is( $register->{selected}, $selected,
194 "Register is selected $selected (userenv: brancode, filters: current_branch, selected: register 1)"
195         );
196     }
197
198     t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
199     $result = $plugin->all();
200     is( $result, undef, "Return undef when UseCashRegisters is disabled" );
201
202     $schema->storage->txn_rollback;
203 };
204
205 1;