Bug 23599: Regression tests
[koha.git] / t / db_dependent / Koha / Patron / Attribute / Types.t
blobebb31a9512e9de9c2a9b643834434113d0931197
1 #!/usr/bin/perl
3 # Copyright 2016 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
20 use Modern::Perl;
22 use Test::More tests => 6;
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26 use Test::Exception;
28 use C4::Context;
29 use Koha::Database;
30 use Koha::Patron::Attribute::Types;
32 my $schema = Koha::Database->new->schema;
33 my $dbh = C4::Context->dbh;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'new() tests' => sub {
38 plan tests => 2;
40 $schema->storage->txn_begin;
42 # Cleanup before running the tests
43 Koha::Patron::Attribute::Types->search()->delete();
45 my $attribute_type = Koha::Patron::Attribute::Type->new(
46 { code => 'code',
47 description => 'description',
48 repeatable => 0
50 )->store();
52 is( Koha::Patron::Attribute::Types->search()->count,
53 1, 'Only one object created' );
55 my $cateogory_code
56 = $builder->build( { source => 'Category' } )->{categorycode};
58 my $attribute_type_with_category = Koha::Patron::Attribute::Type->new(
59 { code => 'code_2',
60 description => 'description',
61 category_code => $cateogory_code,
62 repeatable => 0
64 )->store();
66 is( Koha::Patron::Attribute::Types->search()->count,
67 2, 'Two objects created' );
69 $schema->storage->txn_rollback;
72 subtest 'library_limits() tests' => sub {
74 plan tests => 13;
76 $schema->storage->txn_begin;
78 # Cleanup before running the tests
79 Koha::Patron::Attribute::Types->search()->delete();
81 my $attribute_type = Koha::Patron::Attribute::Type->new(
82 { code => 'code',
83 description => 'description',
84 repeatable => 0
86 )->store();
88 my $library = $builder->build( { source => 'Branch' } )->{branchcode};
90 my $library_limits = $attribute_type->library_limits();
91 is( $library_limits, undef,
92 'No branch limitations defined for attribute type' );
94 my $print_error = $dbh->{PrintError};
95 $dbh->{PrintError} = 0;
97 throws_ok {
98 $attribute_type->library_limits( ['fake'] );
100 'Koha::Exceptions::CannotAddLibraryLimit',
101 'Exception thrown on single invalid branchcode';
102 $library_limits = $attribute_type->library_limits();
103 is( $library_limits, undef,
104 'No branch limitations defined for attribute type' );
106 throws_ok {
107 $attribute_type->library_limits( [ 'fake', $library ] );
109 'Koha::Exceptions::CannotAddLibraryLimit',
110 'Exception thrown on invalid branchcode present';
112 $library_limits = $attribute_type->library_limits();
113 is( $library_limits, undef,
114 'No branch limitations defined for attribute type' );
116 $dbh->{PrintError} = $print_error;
118 $attribute_type->library_limits( [$library] );
119 $library_limits = $attribute_type->library_limits;
120 is( $library_limits->count, 1, 'Library limits correctly set (count)' );
121 my $limit_library = $library_limits->next;
122 ok( $limit_library->isa('Koha::Library'),
123 'Library limits correctly set (type)'
125 is( $limit_library->branchcode,
126 $library, 'Library limits correctly set (value)' );
128 my $another_library
129 = $builder->build( { source => 'Branch' } )->{branchcode};
130 my @branchcodes_list = ( $library, $another_library );
132 $attribute_type->library_limits( \@branchcodes_list );
133 $library_limits = $attribute_type->library_limits;
134 is( $library_limits->count, 2, 'Library limits correctly set (count)' );
136 while ( $limit_library = $library_limits->next ) {
137 ok( $limit_library->isa('Koha::Library'),
138 'Library limits correctly set (type)'
140 ok( eval {
141 grep { $limit_library->branchcode eq $_ } @branchcodes_list;
143 'Library limits correctly set (values)'
147 $schema->storage->txn_rollback;
150 subtest 'add_library_limit() tests' => sub {
152 plan tests => 4;
154 $schema->storage->txn_begin;
156 # Cleanup before running the tests
157 Koha::Patron::Attribute::Types->search()->delete();
159 my $attribute_type = Koha::Patron::Attribute::Type->new(
160 { code => 'code',
161 description => 'description',
162 repeatable => 0
164 )->store();
166 throws_ok { $attribute_type->add_library_limit() }
167 'Koha::Exceptions::MissingParameter', 'branchcode parameter is mandatory';
169 my $library = $builder->build( { source => 'Branch' } )->{branchcode};
170 $attribute_type->add_library_limit($library);
171 my $rs = Koha::Database->schema->resultset('BorrowerAttributeTypesBranch')
172 ->search( { bat_code => 'code' } );
173 is( $rs->count, 1, 'Library limit successfully added (count)' );
174 is( $rs->next->b_branchcode->branchcode,
175 $library, 'Library limit successfully added (value)' );
177 my $print_error = $dbh->{PrintError};
178 $dbh->{PrintError} = 0;
180 throws_ok {
181 $attribute_type->add_library_limit('fake');
183 'Koha::Exceptions::CannotAddLibraryLimit',
184 'Exception thrown on invalid branchcode';
186 $dbh->{PrintError} = $print_error;
188 $schema->storage->txn_rollback;
191 subtest 'del_library_limit() tests' => sub {
193 plan tests => 4;
195 $schema->storage->txn_begin;
197 # Cleanup before running the tests
198 Koha::Patron::Attribute::Types->search()->delete();
200 my $attribute_type = Koha::Patron::Attribute::Type->new(
201 { code => 'code',
202 description => 'description',
203 repeatable => 0
205 )->store();
207 throws_ok { $attribute_type->del_library_limit() }
208 'Koha::Exceptions::MissingParameter',
209 'branchcode parameter is mandatory';
211 my $library = $builder->build( { source => 'Branch' } )->{branchcode};
212 $attribute_type->add_library_limit($library);
214 is( $attribute_type->del_library_limit($library),
215 1, 'Library limit successfully deleted' );
217 my $print_error = $dbh->{PrintError};
218 $dbh->{PrintError} = 0;
220 throws_ok {
221 $attribute_type->del_library_limit($library);
223 'Koha::Exceptions::ObjectNotFound',
224 'Exception thrown on non-existent library limit';
226 throws_ok {
227 $attribute_type->del_library_limit('fake');
229 'Koha::Exceptions::ObjectNotFound',
230 'Exception thrown on non-existent library limit';
232 $dbh->{PrintError} = $print_error;
234 $schema->storage->txn_rollback;
237 subtest 'replace_library_limits() tests' => sub {
239 plan tests => 10;
241 $schema->storage->txn_begin;
243 # Cleanup before running the tests
244 Koha::Patron::Attribute::Types->search()->delete();
246 my $attribute_type = Koha::Patron::Attribute::Type->new(
247 { code => 'code',
248 description => 'description',
249 repeatable => 0
251 )->store();
253 $attribute_type->replace_library_limits( [] );
254 my $library_limits = $attribute_type->library_limits;
255 is( $library_limits, undef, 'Replacing with empty array yields no library limits' );
257 my $library_1 = $builder->build({ source => 'Branch' })->{branchcode};
258 my $library_2 = $builder->build({ source => 'Branch' })->{branchcode};
259 my $library_3 = $builder->build({ source => 'Branch' })->{branchcode};
261 $attribute_type->replace_library_limits( [$library_1] );
262 $library_limits = $attribute_type->library_limits;
263 is( $library_limits->count, 1,
264 'Successfully adds a single library limit' );
265 my $library_limit = $library_limits->next;
266 is( $library_limit->branchcode,
267 $library_1, 'Library limit correctly set' );
269 my @branchcodes_list = ( $library_1, $library_2, $library_3 );
270 $attribute_type->replace_library_limits(
271 [ $library_1, $library_2, $library_3 ] );
272 $library_limits = $attribute_type->library_limits;
273 is( $library_limits->count, 3, 'Successfully adds two library limit' );
275 while ( my $limit_library = $library_limits->next ) {
276 ok( $limit_library->isa('Koha::Library'),
277 'Library limits correctly set (type)'
279 ok( eval {
280 grep { $limit_library->branchcode eq $_ } @branchcodes_list;
282 'Library limits correctly set (values)'
286 $schema->storage->txn_rollback;
289 subtest 'search_with_library_limits() tests' => sub {
291 plan tests => 4;
293 $schema->storage->txn_begin;
295 # Cleanup before running the tests
296 Koha::Patron::Attribute::Types->search()->delete();
298 my $object_code_1
299 = Koha::Patron::Attribute::Type->new( { code => 'code_1', description => 'a description for code_1' } )
300 ->store();
302 my $object_code_2
303 = Koha::Patron::Attribute::Type->new( { code => 'code_2', description => 'a description for code_2' } )
304 ->store();
306 my $object_code_3
307 = Koha::Patron::Attribute::Type->new( { code => 'code_3', description => 'a description for code_3' } )
308 ->store();
310 my $object_code_4
311 = Koha::Patron::Attribute::Type->new( { code => 'code_4', description => 'a description for code_4' } )
312 ->store();
314 is( Koha::Patron::Attribute::Types->search()->count,
315 4, 'Three objects created' );
317 my $branch_1 = $builder->build( { source => 'Branch' } )->{branchcode};
318 my $branch_2 = $builder->build( { source => 'Branch' } )->{branchcode};
320 $object_code_1->library_limits( [$branch_1] );
321 $object_code_2->library_limits( [$branch_2] );
322 $object_code_3->library_limits( [ $branch_1, $branch_2 ] );
324 is( Koha::Patron::Attribute::Types->search_with_library_limits( {}, {}, $branch_1 )->count,
325 3, '3 attribute types are available for the specified branch' );
326 is( Koha::Patron::Attribute::Types->search_with_library_limits( {}, {}, $branch_2 )->count,
327 3, '3 attribute types are available for the specified branch' );
328 is( Koha::Patron::Attribute::Types->search_with_library_limits( {}, {}, undef )->count,
329 4, '4 attribute types are available with no library passed'
332 $schema->storage->txn_rollback;