Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / Koha / Exceptions.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 10;
21 use Test::MockObject;
22 use Test::Exception;
23
24 subtest 'Koha::Exceptions::Hold tests' => sub {
25
26     plan tests => 5;
27
28     use_ok('Koha::Exceptions::Hold');
29
30     throws_ok
31         { Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'W' ); }
32         'Koha::Exceptions::Hold::CannotSuspendFound',
33         'Exception is thrown :-D';
34
35     # stringify the exception
36     is( "$@", 'Found hold cannot be suspended. Status=W', 'Exception stringified correctly' );
37
38     throws_ok
39         { Koha::Exceptions::Hold::CannotSuspendFound->throw( "Manual message exception" ) }
40         'Koha::Exceptions::Hold::CannotSuspendFound',
41         'Exception is thrown :-D';
42     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
43 };
44
45 subtest 'Koha::Exceptions::Object::FKConstraint tests' => sub {
46
47     plan tests => 9;
48
49     use_ok('Koha::Exceptions::Object');
50
51     throws_ok
52         { Koha::Exceptions::Object::FKConstraint->throw( broken_fk => 'nasty', value => 'fk' ); }
53         'Koha::Exceptions::Object::FKConstraint',
54         'Exception is thrown :-D';
55
56     # stringify the exception
57     is( "$@", 'Invalid parameter passed, nasty=fk does not exist', 'Exception stringified correctly' );
58
59     throws_ok
60         { Koha::Exceptions::Object::FKConstraint->throw( "Manual message exception" ) }
61         'Koha::Exceptions::Object::FKConstraint',
62         'Exception is thrown :-D';
63     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
64
65     throws_ok {
66         Koha::Exceptions::Object::BadValue->throw(
67             type     => 'datetime',
68             property => 'a_property',
69             value    => 'a_value'
70         );
71     }
72     'Koha::Exceptions::Object::BadValue',
73         'Koha::Exceptions::Object::BadValue exception is thrown :-D';
74
75     # stringify the exception
76     is( "$@", 'Invalid value passed, a_property=a_value expected type is datetime', 'Koha::Exceptions::Object::BadValue stringified correctly' );
77
78     throws_ok
79         { Koha::Exceptions::Object::BadValue->throw( "Manual message exception" ) }
80         'Koha::Exceptions::Object::BadValue',
81         'Koha::Exceptions::Object::BadValue is thrown :-D';
82     is( "$@", 'Manual message exception', 'Koha::Exceptions::Object::BadValue not stringified if manually passed' );
83 };
84
85 subtest 'Koha::Exceptions::Password tests' => sub {
86
87     plan tests => 5;
88
89     use_ok('Koha::Exceptions::Password');
90
91     throws_ok
92         { Koha::Exceptions::Password::TooShort->throw( length => 4, min_length => 5 ); }
93         'Koha::Exceptions::Password::TooShort',
94         'Exception is thrown :-D';
95
96     # stringify the exception
97     is( "$@", 'Password length (4) is shorter than required (5)', 'Exception stringified correctly' );
98
99     throws_ok
100         { Koha::Exceptions::Password::TooShort->throw( "Manual message exception" ) }
101         'Koha::Exceptions::Password::TooShort',
102         'Exception is thrown :-D';
103     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
104 };
105
106 subtest 'Koha::Exceptions::Metadata tests' => sub {
107
108     plan tests => 5;
109
110     use_ok('Koha::Exceptions::Metadata');
111
112     my $object = Test::MockObject->new;
113     $object->mock( 'id', 'an_id' );
114     $object->mock( 'biblionumber', 'a_biblionumber' );
115     $object->mock( 'format', 'a_format' );
116     $object->mock( 'schema', 'a_schema' );
117
118     throws_ok
119         { Koha::Exceptions::Metadata::Invalid->throw(
120             id => 'an_id', biblionumber => 'a_biblionumber', format => 'a_format',
121             schema => 'a_schema', decoding_error => 'a_nasty_error' ); }
122         'Koha::Exceptions::Metadata::Invalid',
123         'Exception is thrown :-D';
124
125     # stringify the exception
126     is( "$@", 'Invalid data, cannot decode metadata object (biblio_metadata.id=an_id, biblionumber=a_biblionumber, format=a_format, schema=a_schema, decoding_error=\'a_nasty_error\')', 'Exception stringified correctly' );
127
128     throws_ok
129         { Koha::Exceptions::Metadata::Invalid->throw( "Manual message exception" ) }
130         'Koha::Exceptions::Metadata::Invalid',
131         'Exception is thrown :-D';
132     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
133 };
134
135 subtest 'Koha::Exceptions::Patron::Relationship tests' => sub {
136
137     plan tests => 9;
138
139     use_ok('Koha::Exceptions::Patron::Relationship');
140
141     throws_ok
142         { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( no_relationship => 1 ); }
143         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
144         'Exception is thrown :-D';
145
146     # stringify the exception
147     is( "$@", 'No relationship passed.', 'Exception stringified correctly' );
148
149     throws_ok
150         { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( relationship => 'some' ); }
151         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
152         'Exception is thrown :-D';
153
154     # stringify the exception
155     is( "$@", "Invalid relationship passed, 'some' is not defined.", 'Exception stringified correctly' );
156
157     my $guarantor_id = 1;
158     my $guarantee_id = 2;
159
160     throws_ok {
161         Koha::Exceptions::Patron::Relationship::DuplicateRelationship->throw(
162             guarantor_id => $guarantor_id,
163             guarantee_id => $guarantee_id
164         );
165     }
166     'Koha::Exceptions::Patron::Relationship::DuplicateRelationship', 'Exception is thrown :-D';
167
168     # stringify the exception
169     is( "$@",
170         "There already exists a relationship for the same guarantor ($guarantor_id) and guarantee ($guarantee_id) combination",
171         'Exception stringified correctly'
172     );
173
174     throws_ok
175         { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( "Manual message exception" ) }
176         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
177         'Exception is thrown :-D';
178     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
179 };
180
181 subtest 'Koha::Exceptions::Object::NotInstantiated tests' => sub {
182
183     plan tests => 4;
184
185     throws_ok
186         { Koha::Exceptions::Object::NotInstantiated->throw(
187             method => 'brain_explode', class => 'Koha::JD' ); }
188         'Koha::Exceptions::Object::NotInstantiated',
189         'Exception is thrown :-D';
190
191     # stringify the exception
192     is( "$@", 'Tried to access the \'brain_explode\' method, but Koha::JD is not instantiated', 'Exception stringified correctly' );
193
194     throws_ok
195         { Koha::Exceptions::Object::NotInstantiated->throw( "Manual message exception" ) }
196         'Koha::Exceptions::Object::NotInstantiated',
197         'Exception is thrown :-D';
198     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
199 };
200
201 subtest 'Koha::Exceptions::Patron::Attribute::* tests' => sub {
202
203     plan tests => 13;
204
205     use_ok("Koha::Exceptions::Patron::Attribute");
206
207     my $code      = 'CODE';
208     my $attribute = 'ATTRIBUTE';
209
210     my $mocked_attribute = Test::MockObject->new();
211     $mocked_attribute->mock('code', sub { return $code } );
212     $mocked_attribute->mock('attribute', sub { return $attribute } );
213
214     throws_ok
215         { Koha::Exceptions::Patron::Attribute::NonRepeatable->throw(
216             attribute => $mocked_attribute ); }
217         'Koha::Exceptions::Patron::Attribute::NonRepeatable',
218         'Exception is thrown :-D';
219
220     # stringify the exception
221     is(
222         "$@",
223         "Tried to add more than one non-repeatable attributes. type=$code value=$attribute",
224         'Exception stringified correctly'
225     );
226
227     throws_ok
228         { Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( "Manual message exception" ) }
229         'Koha::Exceptions::Patron::Attribute::NonRepeatable',
230         'Exception is thrown :-D';
231
232     is(
233         "$@",
234         'Manual message exception',
235         'Exception not stringified if manually passed'
236     );
237
238     throws_ok
239         { Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw(
240             attribute => $mocked_attribute ); }
241         'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint',
242         'Exception is thrown :-D';
243
244     # stringify the exception
245     is(
246         "$@",
247         "Your action breaks a unique constraint on the attribute. type=$code value=$attribute",
248         'Exception stringified correctly'
249     );
250
251     throws_ok
252         { Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( "Manual message exception" ) }
253         'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint',
254         'Exception is thrown :-D';
255
256     is(
257         "$@",
258         'Manual message exception',
259         'Exception not stringified if manually passed'
260     );
261
262     my $type = "SOME_TYPE";
263
264     throws_ok
265         { Koha::Exceptions::Patron::Attribute::InvalidType->throw(
266             type => $type ); }
267         'Koha::Exceptions::Patron::Attribute::InvalidType',
268         'Exception is thrown :-D';
269
270     # stringify the exception
271     is(
272         "$@",
273         "Tried to use an invalid attribute type. type=$type",
274         'Exception stringified correctly'
275     );
276
277     throws_ok
278         { Koha::Exceptions::Patron::Attribute::InvalidType->throw( "Manual message exception" ) }
279         'Koha::Exceptions::Patron::Attribute::InvalidType',
280         'Exception is thrown :-D';
281
282     is(
283         "$@",
284         'Manual message exception',
285         'Exception not stringified if manually passed'
286     );
287 };
288
289 subtest 'Koha::Exceptions::Patron tests' => sub {
290
291     plan tests => 5;
292
293     use_ok("Koha::Exceptions::Patron");
294
295     my $type = 'yahey';
296
297     throws_ok
298         { Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute->throw(
299             type => $type ); }
300         'Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute',
301         'Exception is thrown :-D';
302
303     # stringify the exception
304     is( "$@", "Missing mandatory extended attribute (type=$type)", 'Exception stringified correctly' );
305
306     throws_ok
307         { Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute->throw( "Manual message exception" ) }
308         'Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute',
309         'Exception is thrown :-D';
310     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
311 };
312
313 subtest 'Koha::Exceptions::Plugin tests' => sub {
314
315     plan tests => 5;
316
317     use_ok("Koha::Exceptions::Plugin");
318
319     my $plugin_class = 'yahey';
320
321     throws_ok
322         { Koha::Exceptions::Plugin::InstallDied->throw(
323             plugin_class => $plugin_class ); }
324         'Koha::Exceptions::Plugin::InstallDied',
325         'Exception is thrown :-D';
326
327     # stringify the exception
328     is( "$@", "Calling 'install' died for plugin $plugin_class", 'Exception stringified correctly' );
329
330     throws_ok
331         { Koha::Exceptions::Plugin::UpgradeDied->throw(
332             plugin_class => $plugin_class ); }
333         'Koha::Exceptions::Plugin::UpgradeDied',
334         'Exception is thrown :-D';
335
336     # stringify the exception
337     is( "$@", "Calling 'upgrade' died for plugin $plugin_class", 'Exception stringified correctly' );
338 };
339
340 subtest 'Koha::Exception tests' => sub {
341
342     plan tests => 8;
343
344     use Koha::Exception;
345
346     use Exception::Class (
347         'Koha::Exceptions::Weird' => {
348             isa         => 'Koha::Exception',
349             description => 'Weird exception!',
350             fields      => [ 'a', 'b' ]
351         }
352     );
353
354     my $exception_message = "This is a message";
355
356     throws_ok
357         { Koha::Exceptions::Weird->throw( $exception_message ) }
358         'Koha::Exception',
359         'Exception is thrown :-D';
360
361     is(
362         "$@",
363         "Exception 'Koha::Exceptions::Weird' thrown '$exception_message'\n",
364         'Exception not stringified if manually passed'
365     );
366
367     throws_ok
368         { Koha::Exceptions::Weird->throw( a => "A", b => "B" ) }
369         'Koha::Exception',
370         'Exception is thrown :-D';
371
372     is(
373         "$@",
374         "Exception 'Koha::Exceptions::Weird' thrown 'Weird exception!' with a => A, b => B\n",
375         'Exception stringified correctly'
376     );
377
378     throws_ok
379         { Koha::Exceptions::Weird->throw( a => "A" ) }
380         'Koha::Exception',
381         'Exception is thrown :-D';
382
383     is(
384         "$@",
385         "Exception 'Koha::Exceptions::Weird' thrown 'Weird exception!' with a => A\n",
386         'Exception stringified correctly, b skipped entirely'
387     );
388
389     throws_ok
390         { Koha::Exceptions::Weird->throw() }
391         'Koha::Exception',
392         'Exception is thrown :-D';
393
394     is(
395         "$@",
396         "Exception 'Koha::Exceptions::Weird' thrown 'Weird exception!'\n",
397         'Exception stringified correctly'
398     );
399 };