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>.
22 use Test::More tests => 6;
24 use t::lib::TestBuilder;
30 use Koha::Patron::Attribute::Type;
31 use Koha::Patron::Attribute::Types;
33 my $schema = Koha::Database->new->schema;
34 my $dbh = C4::Context->dbh;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'new() tests' => sub {
41 $schema->storage->txn_begin;
43 # Cleanup before running the tests
44 Koha::Patron::Attribute::Types->search()->delete();
46 my $attribute_type = Koha::Patron::Attribute::Type->new(
48 description => 'description',
53 is( Koha::Patron::Attribute::Types->search()->count,
54 1, 'Only one object created' );
57 = $builder->build( { source => 'Category' } )->{categorycode};
59 my $attribute_type_with_category = Koha::Patron::Attribute::Type->new(
61 description => 'description',
62 category_code => $cateogory_code,
67 is( Koha::Patron::Attribute::Types->search()->count,
68 2, 'Two objects created' );
70 $schema->storage->txn_rollback;
73 subtest 'library_limits() tests' => sub {
77 $schema->storage->txn_begin;
79 # Cleanup before running the tests
80 Koha::Patron::Attribute::Types->search()->delete();
82 my $attribute_type = Koha::Patron::Attribute::Type->new(
84 description => 'description',
89 my $library = $builder->build( { source => 'Branch' } )->{branchcode};
91 my $library_limits = $attribute_type->library_limits();
92 is( $library_limits, undef,
93 'No branch limitations defined for attribute type' );
95 my $print_error = $dbh->{PrintError};
96 $dbh->{PrintError} = 0;
99 $attribute_type->library_limits( ['fake'] );
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' );
108 $attribute_type->library_limits( [ 'fake', $library ] );
110 'Koha::Exceptions::CannotAddLibraryLimit',
111 'Exception thrown on invalid branchcode present';
113 $library_limits = $attribute_type->library_limits();
114 is( $library_limits, undef,
115 'No branch limitations defined for attribute type' );
117 $dbh->{PrintError} = $print_error;
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)'
126 is( $limit_library->branchcode,
127 $library, 'Library limits correctly set (value)' );
130 = $builder->build( { source => 'Branch' } )->{branchcode};
131 my @branchcodes_list = ( $library, $another_library );
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)' );
137 while ( $limit_library = $library_limits->next ) {
138 ok( $limit_library->isa('Koha::Library'),
139 'Library limits correctly set (type)'
142 grep { $limit_library->branchcode eq $_ } @branchcodes_list;
144 'Library limits correctly set (values)'
148 $schema->storage->txn_rollback;
151 subtest 'add_library_limit() tests' => sub {
155 $schema->storage->txn_begin;
157 # Cleanup before running the tests
158 Koha::Patron::Attribute::Types->search()->delete();
160 my $attribute_type = Koha::Patron::Attribute::Type->new(
162 description => 'description',
167 throws_ok { $attribute_type->add_library_limit() }
168 'Koha::Exceptions::MissingParameter', 'branchcode parameter is mandatory';
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)' );
178 my $print_error = $dbh->{PrintError};
179 $dbh->{PrintError} = 0;
182 $attribute_type->add_library_limit('fake');
184 'Koha::Exceptions::CannotAddLibraryLimit',
185 'Exception thrown on invalid branchcode';
187 $dbh->{PrintError} = $print_error;
189 $schema->storage->txn_rollback;
192 subtest 'del_library_limit() tests' => sub {
196 $schema->storage->txn_begin;
198 # Cleanup before running the tests
199 Koha::Patron::Attribute::Types->search()->delete();
201 my $attribute_type = Koha::Patron::Attribute::Type->new(
203 description => 'description',
208 throws_ok { $attribute_type->del_library_limit() }
209 'Koha::Exceptions::MissingParameter',
210 'branchcode parameter is mandatory';
212 my $library = $builder->build( { source => 'Branch' } )->{branchcode};
213 $attribute_type->add_library_limit($library);
215 is( $attribute_type->del_library_limit($library),
216 1, 'Library limit successfully deleted' );
218 my $print_error = $dbh->{PrintError};
219 $dbh->{PrintError} = 0;
222 $attribute_type->del_library_limit($library);
224 'Koha::Exceptions::ObjectNotFound',
225 'Exception thrown on non-existent library limit';
228 $attribute_type->del_library_limit('fake');
230 'Koha::Exceptions::ObjectNotFound',
231 'Exception thrown on non-existent library limit';
233 $dbh->{PrintError} = $print_error;
235 $schema->storage->txn_rollback;
238 subtest 'replace_library_limits() tests' => sub {
242 $schema->storage->txn_begin;
244 # Cleanup before running the tests
245 Koha::Patron::Attribute::Types->search()->delete();
247 my $attribute_type = Koha::Patron::Attribute::Type->new(
249 description => 'description',
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' );
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};
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' );
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' );
276 while ( my $limit_library = $library_limits->next ) {
277 ok( $limit_library->isa('Koha::Library'),
278 'Library limits correctly set (type)'
281 grep { $limit_library->branchcode eq $_ } @branchcodes_list;
283 'Library limits correctly set (values)'
287 $schema->storage->txn_rollback;
290 subtest 'search() with branch limits tests' => sub {
294 $schema->storage->txn_begin;
296 # Cleanup before running the tests
297 Koha::Patron::Attribute::Types->search()->delete();
300 = Koha::Patron::Attribute::Type->new( { code => 'code_1', } )
304 = Koha::Patron::Attribute::Type->new( { code => 'code_2', } )
308 = Koha::Patron::Attribute::Type->new( { code => 'code_3', } )
312 = Koha::Patron::Attribute::Type->new( { code => 'code_4', } )
315 is( Koha::Patron::Attribute::Types->search()->count,
316 4, 'Three objects created' );
318 my $branch_1 = $builder->build( { source => 'Branch' } )->{branchcode};
319 my $branch_2 = $builder->build( { source => 'Branch' } )->{branchcode};
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 ] );
325 is( Koha::Patron::Attribute::Types->search( { branchcode => $branch_1 } )
328 '3 attribute types are available for the specified branch'
330 is( Koha::Patron::Attribute::Types->search( { branchcode => $branch_2 } )
333 '3 attribute types are available for the specified branch'
336 $schema->storage->txn_rollback;