Bug 18292: Remove return 1 statements in tests
[koha.git] / t / db_dependent / Koha / Patron / Attribute / Types.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 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 => 6;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26 use Test::Exception;
27
28 use C4::Context;
29 use Koha::Database;
30 use Koha::Patron::Attribute::Type;
31 use Koha::Patron::Attribute::Types;
32
33 my $schema  = Koha::Database->new->schema;
34 my $dbh     = C4::Context->dbh;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'new() tests' => sub {
38
39     plan tests => 2;
40
41     $schema->storage->txn_begin;
42
43     # Cleanup before running the tests
44     Koha::Patron::Attribute::Types->search()->delete();
45
46     my $attribute_type = Koha::Patron::Attribute::Type->new(
47         {   code        => 'code',
48             description => 'description',
49             repeatable  => 0
50         }
51     )->store();
52
53     is( Koha::Patron::Attribute::Types->search()->count,
54         1, 'Only one object created' );
55
56     my $cateogory_code
57         = $builder->build( { source => 'Category' } )->{categorycode};
58
59     my $attribute_type_with_category = Koha::Patron::Attribute::Type->new(
60         {   code          => 'code_2',
61             description   => 'description',
62             category_code => $cateogory_code,
63             repeatable    => 0
64         }
65     )->store();
66
67     is( Koha::Patron::Attribute::Types->search()->count,
68         2, 'Two objects created' );
69
70     $schema->storage->txn_rollback;
71 };
72
73 subtest 'library_limits() tests' => sub {
74
75     plan tests => 13;
76
77     $schema->storage->txn_begin;
78
79     # Cleanup before running the tests
80     Koha::Patron::Attribute::Types->search()->delete();
81
82     my $attribute_type = Koha::Patron::Attribute::Type->new(
83         {   code        => 'code',
84             description => 'description',
85             repeatable  => 0
86         }
87     )->store();
88
89     my $library = $builder->build( { source => 'Branch' } )->{branchcode};
90
91     my $library_limits = $attribute_type->library_limits();
92     is( $library_limits, undef,
93         'No branch limitations defined for attribute type' );
94
95     my $print_error = $dbh->{PrintError};
96     $dbh->{PrintError} = 0;
97
98     throws_ok {
99         $attribute_type->library_limits( ['fake'] );
100     }
101     'Koha::Exceptions::CannotAddLibraryLimit',
102         'Exception thrown on single invalid branchcode';
103     $library_limits = $attribute_type->library_limits();
104     is( $library_limits, undef,
105         'No branch limitations defined for attribute type' );
106
107     throws_ok {
108         $attribute_type->library_limits( [ 'fake', $library ] );
109     }
110     'Koha::Exceptions::CannotAddLibraryLimit',
111         'Exception thrown on invalid branchcode present';
112
113     $library_limits = $attribute_type->library_limits();
114     is( $library_limits, undef,
115         'No branch limitations defined for attribute type' );
116
117     $dbh->{PrintError} = $print_error;
118
119     $attribute_type->library_limits( [$library] );
120     $library_limits = $attribute_type->library_limits;
121     is( $library_limits->count, 1, 'Library limits correctly set (count)' );
122     my $limit_library = $library_limits->next;
123     ok( $limit_library->isa('Koha::Library'),
124         'Library limits correctly set (type)'
125     );
126     is( $limit_library->branchcode,
127         $library, 'Library limits correctly set (value)' );
128
129     my $another_library
130         = $builder->build( { source => 'Branch' } )->{branchcode};
131     my @branchcodes_list = ( $library, $another_library );
132
133     $attribute_type->library_limits( \@branchcodes_list );
134     $library_limits = $attribute_type->library_limits;
135     is( $library_limits->count, 2, 'Library limits correctly set (count)' );
136
137     while ( $limit_library = $library_limits->next ) {
138         ok( $limit_library->isa('Koha::Library'),
139             'Library limits correctly set (type)'
140         );
141         ok( eval {
142                 grep { $limit_library->branchcode eq $_ } @branchcodes_list;
143             },
144             'Library limits correctly set (values)'
145         );
146     }
147
148     $schema->storage->txn_rollback;
149 };
150
151 subtest 'add_library_limit() tests' => sub {
152
153     plan tests => 4;
154
155     $schema->storage->txn_begin;
156
157     # Cleanup before running the tests
158     Koha::Patron::Attribute::Types->search()->delete();
159
160     my $attribute_type = Koha::Patron::Attribute::Type->new(
161         {   code        => 'code',
162             description => 'description',
163             repeatable  => 0
164         }
165     )->store();
166
167     throws_ok { $attribute_type->add_library_limit() }
168     'Koha::Exceptions::MissingParameter', 'branchcode parameter is mandatory';
169
170     my $library = $builder->build( { source => 'Branch' } )->{branchcode};
171     $attribute_type->add_library_limit($library);
172     my $rs = Koha::Database->schema->resultset('BorrowerAttributeTypesBranch')
173         ->search( { bat_code => 'code' } );
174     is( $rs->count, 1, 'Library limit successfully added (count)' );
175     is( $rs->next->b_branchcode->branchcode,
176         $library, 'Library limit successfully added (value)' );
177
178     my $print_error = $dbh->{PrintError};
179     $dbh->{PrintError} = 0;
180
181     throws_ok {
182         $attribute_type->add_library_limit('fake');
183     }
184     'Koha::Exceptions::CannotAddLibraryLimit',
185         'Exception thrown on invalid branchcode';
186
187     $dbh->{PrintError} = $print_error;
188
189     $schema->storage->txn_rollback;
190 };
191
192 subtest 'del_library_limit() tests' => sub {
193
194     plan tests => 4;
195
196     $schema->storage->txn_begin;
197
198     # Cleanup before running the tests
199     Koha::Patron::Attribute::Types->search()->delete();
200
201     my $attribute_type = Koha::Patron::Attribute::Type->new(
202         {   code        => 'code',
203             description => 'description',
204             repeatable  => 0
205         }
206     )->store();
207
208     throws_ok { $attribute_type->del_library_limit() }
209     'Koha::Exceptions::MissingParameter',
210         'branchcode parameter is mandatory';
211
212     my $library = $builder->build( { source => 'Branch' } )->{branchcode};
213     $attribute_type->add_library_limit($library);
214
215     is( $attribute_type->del_library_limit($library),
216         1, 'Library limit successfully deleted' );
217
218     my $print_error = $dbh->{PrintError};
219     $dbh->{PrintError} = 0;
220
221     throws_ok {
222         $attribute_type->del_library_limit($library);
223     }
224     'Koha::Exceptions::ObjectNotFound',
225         'Exception thrown on non-existent library limit';
226
227     throws_ok {
228         $attribute_type->del_library_limit('fake');
229     }
230     'Koha::Exceptions::ObjectNotFound',
231         'Exception thrown on non-existent library limit';
232
233     $dbh->{PrintError} = $print_error;
234
235     $schema->storage->txn_rollback;
236 };
237
238 subtest 'replace_library_limits() tests' => sub {
239
240     plan tests => 10;
241
242     $schema->storage->txn_begin;
243
244     # Cleanup before running the tests
245     Koha::Patron::Attribute::Types->search()->delete();
246
247     my $attribute_type = Koha::Patron::Attribute::Type->new(
248         {   code        => 'code',
249             description => 'description',
250             repeatable  => 0
251         }
252     )->store();
253
254     $attribute_type->replace_library_limits( [] );
255     my $library_limits = $attribute_type->library_limits;
256     is( $library_limits, undef, 'Replacing with empty array yields no library limits' );
257
258     my $library_1 = $builder->build({ source => 'Branch' })->{branchcode};
259     my $library_2 = $builder->build({ source => 'Branch' })->{branchcode};
260     my $library_3 = $builder->build({ source => 'Branch' })->{branchcode};
261
262     $attribute_type->replace_library_limits( [$library_1] );
263     $library_limits = $attribute_type->library_limits;
264     is( $library_limits->count, 1,
265         'Successfully adds a single library limit' );
266     my $library_limit = $library_limits->next;
267     is( $library_limit->branchcode,
268         $library_1, 'Library limit correctly set' );
269
270     my @branchcodes_list = ( $library_1, $library_2, $library_3 );
271     $attribute_type->replace_library_limits(
272         [ $library_1, $library_2, $library_3 ] );
273     $library_limits = $attribute_type->library_limits;
274     is( $library_limits->count, 3, 'Successfully adds two library limit' );
275
276     while ( my $limit_library = $library_limits->next ) {
277         ok( $limit_library->isa('Koha::Library'),
278             'Library limits correctly set (type)'
279         );
280         ok( eval {
281                 grep { $limit_library->branchcode eq $_ } @branchcodes_list;
282             },
283             'Library limits correctly set (values)'
284         );
285     }
286
287     $schema->storage->txn_rollback;
288 };
289
290 subtest 'search() with branch limits tests' => sub {
291
292     plan tests => 3;
293
294     $schema->storage->txn_begin;
295
296     # Cleanup before running the tests
297     Koha::Patron::Attribute::Types->search()->delete();
298
299     my $object_code_1
300         = Koha::Patron::Attribute::Type->new( { code => 'code_1', } )
301         ->store();
302
303     my $object_code_2
304         = Koha::Patron::Attribute::Type->new( { code => 'code_2', } )
305         ->store();
306
307     my $object_code_3
308         = Koha::Patron::Attribute::Type->new( { code => 'code_3', } )
309         ->store();
310
311     my $object_code_4
312         = Koha::Patron::Attribute::Type->new( { code => 'code_4', } )
313         ->store();
314
315     is( Koha::Patron::Attribute::Types->search()->count,
316         4, 'Three objects created' );
317
318     my $branch_1 = $builder->build( { source => 'Branch' } )->{branchcode};
319     my $branch_2 = $builder->build( { source => 'Branch' } )->{branchcode};
320
321     $object_code_1->library_limits( [$branch_1] );
322     $object_code_2->library_limits( [$branch_2] );
323     $object_code_3->library_limits( [ $branch_1, $branch_2 ] );
324
325     is( Koha::Patron::Attribute::Types->search( { branchcode => $branch_1 } )
326             ->count,
327         3,
328         '3 attribute types are available for the specified branch'
329     );
330     is( Koha::Patron::Attribute::Types->search( { branchcode => $branch_2 } )
331             ->count,
332         3,
333         '3 attribute types are available for the specified branch'
334     );
335
336     $schema->storage->txn_rollback;
337 };
338