Bug 23271: Prevent crash if called without parameters
[koha.git] / t / db_dependent / Koha / Patron / Attribute.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 => 2;
23
24 use t::lib::TestBuilder;
25 use Test::Exception;
26
27 use Koha::Database;
28 use Koha::Patron::Attribute;
29 use Koha::Patron::Attributes;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 subtest 'store() tests' => sub {
35
36     plan tests => 4;
37
38     subtest 'repeatable attributes tests' => sub {
39
40         plan tests => 5;
41
42         $schema->storage->txn_begin;
43
44         my $patron = $builder->build( { source => 'Borrower' } )->{borrowernumber};
45         my $attribute_type_1 = $builder->build(
46             {   source => 'BorrowerAttributeType',
47                 value  => { repeatable => 1 }
48             }
49         );
50         Koha::Patron::Attribute->new(
51             {   borrowernumber => $patron,
52                 code           => $attribute_type_1->{code},
53                 attribute      => 'Foo'
54             }
55         )->store;
56         Koha::Patron::Attribute->new(
57             {   borrowernumber => $patron,
58                 code           => $attribute_type_1->{code},
59                 attribute      => 'Bar'
60             }
61         )->store;
62         my $attr_count
63             = Koha::Patron::Attributes->search(
64             { borrowernumber => $patron, code => $attribute_type_1->{code} } )
65             ->count;
66         is( $attr_count, 2,
67             '2 repeatable attributes stored and retrieved correcctly' );
68
69         my $attribute_type_2 = $builder->build(
70             {   source => 'BorrowerAttributeType',
71                 value  => { repeatable => 0 }
72             }
73         );
74
75         Koha::Patron::Attribute->new(
76             {   borrowernumber => $patron,
77                 code           => $attribute_type_2->{code},
78                 attribute      => 'Foo'
79             }
80         )->store;
81         throws_ok {
82             Koha::Patron::Attribute->new(
83                 {   borrowernumber => $patron,
84                     code           => $attribute_type_2->{code},
85                     attribute      => 'Bar'
86                 }
87             )->store;
88         }
89         'Koha::Exceptions::Patron::Attribute::NonRepeatable',
90             'Exception thrown trying to store more than one non-repeatable attribute';
91
92         is(
93             "$@",
94             "Tried to add more than one non-repeatable attributes. type="
95             . $attribute_type_2->{code}
96             . " value=Bar",
97             'Exception stringified correctly, attribute passed correctly'
98         );
99
100         my $attributes = Koha::Patron::Attributes->search(
101             { borrowernumber => $patron, code => $attribute_type_2->{code} } );
102         is( $attributes->count, 1, '1 non-repeatable attribute stored' );
103         is( $attributes->next->attribute,
104             'Foo', 'Non-repeatable attribute remains unchanged' );
105
106         $schema->storage->txn_rollback;
107     };
108
109     subtest 'unique_id attributes tests' => sub {
110
111         plan tests => 5;
112
113         $schema->storage->txn_begin;
114
115         my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
116         my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
117
118         my $attribute_type_1 = $builder->build(
119             {   source => 'BorrowerAttributeType',
120                 value  => { unique_id => 0 }
121             }
122         );
123         Koha::Patron::Attribute->new(
124             {   borrowernumber => $patron_1,
125                 code           => $attribute_type_1->{code},
126                 attribute      => 'Foo'
127             }
128         )->store;
129         Koha::Patron::Attribute->new(
130             {   borrowernumber => $patron_2,
131                 code           => $attribute_type_1->{code},
132                 attribute      => 'Bar'
133             }
134         )->store;
135         my $attr_count
136             = Koha::Patron::Attributes->search(
137             { code => $attribute_type_1->{code} } )
138             ->count;
139         is( $attr_count, 2,
140             '2 non-unique attributes stored and retrieved correcctly' );
141
142         my $attribute_type_2 = $builder->build(
143             {   source => 'BorrowerAttributeType',
144                 value  => { unique_id => 1 }
145             }
146         );
147
148         Koha::Patron::Attribute->new(
149             {   borrowernumber => $patron_1,
150                 code           => $attribute_type_2->{code},
151                 attribute      => 'Foo'
152             }
153         )->store;
154         throws_ok {
155             Koha::Patron::Attribute->new(
156                 {   borrowernumber => $patron_2,
157                     code           => $attribute_type_2->{code},
158                     attribute      => 'Foo'
159                 }
160             )->store;
161         }
162         'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint',
163             'Exception thrown trying to store more than one unique attribute';
164
165         is(
166             "$@",
167             "Your action breaks a unique constraint on the attribute. type="
168             . $attribute_type_2->{code}
169             . " value=Foo",
170             'Exception stringified correctly, attribute passed correctly'
171         );
172
173         my $attributes = Koha::Patron::Attributes->search(
174             { borrowernumber => $patron_1, code => $attribute_type_2->{code} } );
175         is( $attributes->count, 1, '1 unique attribute stored' );
176         is( $attributes->next->attribute,
177             'Foo', 'unique attribute remains unchanged' );
178
179         $schema->storage->txn_rollback;
180     };
181
182     subtest 'invalid type tests' => sub {
183
184         plan tests => 2;
185
186         $schema->storage->txn_begin;
187
188         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
189         my $attribute_type = $builder->build_object(
190             {
191                 class => 'Koha::Patron::Attribute::Types',
192                 value => {
193                     unique_id  => 0,
194                     repeatable => 0
195                 }
196             }
197         );
198
199         my $code = $attribute_type->code;
200         $attribute_type->delete;
201
202         throws_ok
203             { Koha::Patron::Attribute->new(
204                 {
205                     borrowernumber => $patron->borrowernumber,
206                     code           => $code,
207                     attribute      => 'Who knows'
208
209                 })->store;
210             }
211             'Koha::Exceptions::Patron::Attribute::InvalidType',
212             'Exception thrown on invalid attribute code';
213
214         is( $@->type, $code, 'type exception parameter passed'  );
215
216         $schema->storage->txn_rollback;
217     };
218
219     subtest 'Edit attribute tests for non-repeatable tests (Bug 28031)' => sub {
220
221         plan tests => 1;
222
223         $schema->storage->txn_begin;
224
225         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
226         my $non_repeatable_type = $builder->build_object(
227             {
228                 class => 'Koha::Patron::Attribute::Types',
229                 value => {
230                     mandatory     => 0,
231                     repeatable    => 0,
232                     unique_id     => 1,
233                     category_code => undef
234                 }
235             }
236         );
237
238         # Here we test the case of editing an already stored attribute
239         my $non_repeatable_attr = $patron->add_extended_attribute(
240             {
241                 code      => $non_repeatable_type->code,
242                 attribute => 'WOW'
243             }
244         );
245
246         $non_repeatable_attr->set({ attribute => 'HEY' })
247                         ->store
248                         ->discard_changes;
249
250         is( $non_repeatable_attr->attribute, 'HEY', 'Value stored correctly' );
251
252         $schema->storage->txn_rollback;
253     };
254 };
255
256 subtest 'type() tests' => sub {
257
258     plan tests => 4;
259
260     $schema->storage->txn_begin;
261
262     my $patron
263         = $builder->build( { source => 'Borrower' } )->{borrowernumber};
264     my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
265     my $attribute = Koha::Patron::Attribute->new(
266         {   borrowernumber => $patron,
267             code           => $attr_type->{code},
268             attribute      => $patron
269         }
270     );
271
272     my $attribute_type = $attribute->type;
273
274     is( ref($attribute_type),
275         'Koha::Patron::Attribute::Type',
276         '->type returns a Koha::Patron::Attribute::Type object'
277     );
278
279     is( $attribute_type->code,
280         $attr_type->{code},
281         '->type returns the right Koha::Patron::Attribute::Type object' );
282
283     is( $attribute_type->opac_editable,
284         $attr_type->{opac_editable},
285         '->type returns the right Koha::Patron::Attribute::Type object'
286     );
287
288     is( $attribute_type->opac_display,
289         $attr_type->{opac_display},
290         '->type returns the right Koha::Patron::Attribute::Type object'
291     );
292
293     $schema->storage->txn_rollback;
294 };