Bug 20443: Move C4::Members::AttributeTypes::GetAttributeTypes to Koha::Patron::Attri...
[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::Types;
31
32 my $schema  = Koha::Database->new->schema;
33 my $dbh     = C4::Context->dbh;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'new() tests' => sub {
37
38     plan tests => 2;
39
40     $schema->storage->txn_begin;
41
42     # Cleanup before running the tests
43     Koha::Patron::Attribute::Types->search()->delete();
44
45     my $attribute_type = Koha::Patron::Attribute::Type->new(
46         {   code        => 'code',
47             description => 'description',
48             repeatable  => 0
49         }
50     )->store();
51
52     is( Koha::Patron::Attribute::Types->search()->count,
53         1, 'Only one object created' );
54
55     my $cateogory_code
56         = $builder->build( { source => 'Category' } )->{categorycode};
57
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
63         }
64     )->store();
65
66     is( Koha::Patron::Attribute::Types->search()->count,
67         2, 'Two objects created' );
68
69     $schema->storage->txn_rollback;
70 };
71
72 subtest 'library_limits() tests' => sub {
73
74     plan tests => 13;
75
76     $schema->storage->txn_begin;
77
78     # Cleanup before running the tests
79     Koha::Patron::Attribute::Types->search()->delete();
80
81     my $attribute_type = Koha::Patron::Attribute::Type->new(
82         {   code        => 'code',
83             description => 'description',
84             repeatable  => 0
85         }
86     )->store();
87
88     my $library = $builder->build( { source => 'Branch' } )->{branchcode};
89
90     my $library_limits = $attribute_type->library_limits();
91     is( $library_limits, undef,
92         'No branch limitations defined for attribute type' );
93
94     my $print_error = $dbh->{PrintError};
95     $dbh->{PrintError} = 0;
96
97     throws_ok {
98         $attribute_type->library_limits( ['fake'] );
99     }
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' );
105
106     throws_ok {
107         $attribute_type->library_limits( [ 'fake', $library ] );
108     }
109     'Koha::Exceptions::CannotAddLibraryLimit',
110         'Exception thrown on invalid branchcode present';
111
112     $library_limits = $attribute_type->library_limits();
113     is( $library_limits, undef,
114         'No branch limitations defined for attribute type' );
115
116     $dbh->{PrintError} = $print_error;
117
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)'
124     );
125     is( $limit_library->branchcode,
126         $library, 'Library limits correctly set (value)' );
127
128     my $another_library
129         = $builder->build( { source => 'Branch' } )->{branchcode};
130     my @branchcodes_list = ( $library, $another_library );
131
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)' );
135
136     while ( $limit_library = $library_limits->next ) {
137         ok( $limit_library->isa('Koha::Library'),
138             'Library limits correctly set (type)'
139         );
140         ok( eval {
141                 grep { $limit_library->branchcode eq $_ } @branchcodes_list;
142             },
143             'Library limits correctly set (values)'
144         );
145     }
146
147     $schema->storage->txn_rollback;
148 };
149
150 subtest 'add_library_limit() tests' => sub {
151
152     plan tests => 4;
153
154     $schema->storage->txn_begin;
155
156     # Cleanup before running the tests
157     Koha::Patron::Attribute::Types->search()->delete();
158
159     my $attribute_type = Koha::Patron::Attribute::Type->new(
160         {   code        => 'code',
161             description => 'description',
162             repeatable  => 0
163         }
164     )->store();
165
166     throws_ok { $attribute_type->add_library_limit() }
167     'Koha::Exceptions::MissingParameter', 'branchcode parameter is mandatory';
168
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)' );
176
177     my $print_error = $dbh->{PrintError};
178     $dbh->{PrintError} = 0;
179
180     throws_ok {
181         $attribute_type->add_library_limit('fake');
182     }
183     'Koha::Exceptions::CannotAddLibraryLimit',
184         'Exception thrown on invalid branchcode';
185
186     $dbh->{PrintError} = $print_error;
187
188     $schema->storage->txn_rollback;
189 };
190
191 subtest 'del_library_limit() tests' => sub {
192
193     plan tests => 4;
194
195     $schema->storage->txn_begin;
196
197     # Cleanup before running the tests
198     Koha::Patron::Attribute::Types->search()->delete();
199
200     my $attribute_type = Koha::Patron::Attribute::Type->new(
201         {   code        => 'code',
202             description => 'description',
203             repeatable  => 0
204         }
205     )->store();
206
207     throws_ok { $attribute_type->del_library_limit() }
208     'Koha::Exceptions::MissingParameter',
209         'branchcode parameter is mandatory';
210
211     my $library = $builder->build( { source => 'Branch' } )->{branchcode};
212     $attribute_type->add_library_limit($library);
213
214     is( $attribute_type->del_library_limit($library),
215         1, 'Library limit successfully deleted' );
216
217     my $print_error = $dbh->{PrintError};
218     $dbh->{PrintError} = 0;
219
220     throws_ok {
221         $attribute_type->del_library_limit($library);
222     }
223     'Koha::Exceptions::ObjectNotFound',
224         'Exception thrown on non-existent library limit';
225
226     throws_ok {
227         $attribute_type->del_library_limit('fake');
228     }
229     'Koha::Exceptions::ObjectNotFound',
230         'Exception thrown on non-existent library limit';
231
232     $dbh->{PrintError} = $print_error;
233
234     $schema->storage->txn_rollback;
235 };
236
237 subtest 'replace_library_limits() tests' => sub {
238
239     plan tests => 10;
240
241     $schema->storage->txn_begin;
242
243     # Cleanup before running the tests
244     Koha::Patron::Attribute::Types->search()->delete();
245
246     my $attribute_type = Koha::Patron::Attribute::Type->new(
247         {   code        => 'code',
248             description => 'description',
249             repeatable  => 0
250         }
251     )->store();
252
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' );
256
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};
260
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' );
268
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' );
274
275     while ( my $limit_library = $library_limits->next ) {
276         ok( $limit_library->isa('Koha::Library'),
277             'Library limits correctly set (type)'
278         );
279         ok( eval {
280                 grep { $limit_library->branchcode eq $_ } @branchcodes_list;
281             },
282             'Library limits correctly set (values)'
283         );
284     }
285
286     $schema->storage->txn_rollback;
287 };
288
289 subtest 'search_with_library_limits() tests' => sub {
290
291     plan tests => 4;
292
293     $schema->storage->txn_begin;
294
295     # Cleanup before running the tests
296     Koha::Patron::Attribute::Types->search()->delete();
297
298     my $object_code_1
299         = Koha::Patron::Attribute::Type->new( { code => 'code_1', description => 'a description for code_1' } )
300         ->store();
301
302     my $object_code_2
303         = Koha::Patron::Attribute::Type->new( { code => 'code_2', description => 'a description for code_2' } )
304         ->store();
305
306     my $object_code_3
307         = Koha::Patron::Attribute::Type->new( { code => 'code_3', description => 'a description for code_3' } )
308         ->store();
309
310     my $object_code_4
311         = Koha::Patron::Attribute::Type->new( { code => 'code_4', description => 'a description for code_4' } )
312         ->store();
313
314     is( Koha::Patron::Attribute::Types->search()->count,
315         4, 'Three objects created' );
316
317     my $branch_1 = $builder->build( { source => 'Branch' } )->{branchcode};
318     my $branch_2 = $builder->build( { source => 'Branch' } )->{branchcode};
319
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 ] );
323
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'
330     );
331
332     # TODO test if filter_by_branch_limitations restriced on logged-in-user's branch
333
334     $schema->storage->txn_rollback;
335 };
336