3 # Copyright 2015 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
=> 23;
27 use Koha
::Authority
::Types
;
30 use Koha
::Patron
::Category
;
31 use Koha
::Patron
::Categories
;
35 use t
::lib
::TestBuilder
;
40 my $schema = Koha
::Database
->new->schema;
41 $schema->storage->txn_begin;
42 my $builder = t
::lib
::TestBuilder
->new;
44 is
( ref(Koha
::Authority
::Types
->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
46 my @columns = Koha
::Patrons
->columns;
47 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
48 is
( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
50 subtest
'find' => sub {
52 my $patron = $builder->build({source
=> 'Borrower'});
53 my $patron_object = Koha
::Patrons
->find( $patron->{borrowernumber
} );
54 is
( $patron_object->borrowernumber, $patron->{borrowernumber
}, '->find should return the correct object' );
56 my @patrons = Koha
::Patrons
->find( $patron->{borrowernumber
} );
57 is
(scalar @patrons, 1, '->find in list context returns a value');
58 is
($patrons[0]->borrowernumber, $patron->{borrowernumber
}, '->find in list context returns the same value as in scalar context');
61 foo
=> Koha
::Patrons
->find('foo'),
64 is
($patrons->{foo
}, undef, '->find in list context returns undef when no record is found');
66 # Test sending undef to find; should not generate a warning
67 warning_is
{ $patron = Koha
::Patrons
->find( undef ); }
68 "", "Sending undef does not trigger a DBIx warning";
69 warning_is
{ $patron = Koha
::Patrons
->find( undef, undef ); }
70 "", "Sending two undefs does not trigger a DBIx warning too";
73 subtest
'update' => sub {
76 $builder->build( { source
=> 'City', value
=> { city_country
=> 'UK' } } );
77 $builder->build( { source
=> 'City', value
=> { city_country
=> 'UK' } } );
78 $builder->build( { source
=> 'City', value
=> { city_country
=> 'UK' } } );
79 $builder->build( { source
=> 'City', value
=> { city_country
=> 'France' } } );
80 $builder->build( { source
=> 'City', value
=> { city_country
=> 'France' } } );
81 $builder->build( { source
=> 'City', value
=> { city_country
=> 'Germany' } } );
82 Koha
::Cities
->search( { city_country
=> 'UK' } )->update( { city_country
=> 'EU' } );
83 is
( Koha
::Cities
->search( { city_country
=> 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
84 is
( Koha
::Cities
->search( { city_country
=> 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
87 subtest
'reset' => sub {
90 my $patrons = Koha
::Patrons
->search;
91 my $first_borrowernumber = $patrons->next->borrowernumber;
92 my $second_borrowernumber = $patrons->next->borrowernumber;
93 is
( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
94 is
( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
95 is
( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
98 subtest
'delete' => sub {
101 my $patron_1 = $builder->build({source
=> 'Borrower'});
102 my $patron_2 = $builder->build({source
=> 'Borrower'});
103 is
( Koha
::Patrons
->search({ -or => { borrowernumber
=> [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
}]}})->delete, 2, '');
104 is
( Koha
::Patrons
->search({ -or => { borrowernumber
=> [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
}]}})->count, 0, '');
107 subtest
'new' => sub {
109 my $a_cat_code = 'A_CAT_CODE';
110 my $patron_category = Koha
::Patron
::Category
->new( { categorycode
=> $a_cat_code } )->store;
111 is
( Koha
::Patron
::Categories
->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
112 Koha
::Patron
::Categories
->find($a_cat_code)->delete;
113 $patron_category = Koha
::Patron
::Category
->new( { categorycode
=> $a_cat_code, category_type
=> undef } )->store;
114 is
( Koha
::Patron
::Categories
->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value even if the argument exists but is not defined' );
115 Koha
::Patron
::Categories
->find($a_cat_code)->delete;
118 subtest
'find' => sub {
121 # check find on a single PK
122 my $patron = $builder->build({ source
=> 'Borrower' });
123 is
( Koha
::Patrons
->find($patron->{borrowernumber
})->surname,
124 $patron->{surname
}, "Checking an arbitrary patron column after find"
126 # check find with unique column
127 my $obj = Koha
::Patrons
->find($patron->{cardnumber
}, { key
=> 'cardnumber' });
128 is
( $obj->borrowernumber, $patron->{borrowernumber
},
129 'Find with unique column and key specified' );
130 # check find with an additional where clause in the attrs hash
131 # we do not expect to find something now
132 is
( Koha
::Patrons
->find(
133 $patron->{borrowernumber
},
134 { where
=> { surname
=> { '!=', $patron->{surname
} }}},
135 ), undef, 'Additional where clause in find call' );
137 is
( Koha
::Patrons
->find(), undef, 'Find returns undef if no params passed' );
140 subtest
'search_related' => sub {
142 my $builder = t
::lib
::TestBuilder
->new;
143 my $patron_1 = $builder->build( { source
=> 'Borrower' } );
144 my $patron_2 = $builder->build( { source
=> 'Borrower' } );
145 my $libraries = Koha
::Patrons
->search( { -or => { borrowernumber
=> [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
} ] } } )->search_related('branchcode');
146 is
( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
147 is
( $libraries->count, 2, 'Koha::Objects->search_related should work as expected' );
148 is
( $libraries->next->branchcode, $patron_1->{branchcode
}, 'Koha::Objects->search_related should work as expected' );
149 is
( $libraries->next->branchcode, $patron_2->{branchcode
}, 'Koha::Objects->search_related should work as expected' );
151 my @libraries = Koha
::Patrons
->search( { -or => { borrowernumber
=> [ $patron_1->{borrowernumber
}, $patron_2->{borrowernumber
} ] } } )->search_related('branchcode');
152 is
( ref( $libraries[0] ), 'Koha::Library', 'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
153 is
( scalar(@libraries), 2, 'Koha::Objects->search_related should work as expected' );
154 is
( $libraries[0]->branchcode, $patron_1->{branchcode
}, 'Koha::Objects->search_related should work as expected' );
155 is
( $libraries[1]->branchcode, $patron_2->{branchcode
}, 'Koha::Objects->search_related should work as expected' );
158 subtest
'single' => sub {
160 my $builder = t
::lib
::TestBuilder
->new;
161 my $patron_1 = $builder->build( { source
=> 'Borrower' } );
162 my $patron_2 = $builder->build( { source
=> 'Borrower' } );
163 my $patron = Koha
::Patrons
->search({}, { rows
=> 1 })->single;
164 is
(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
165 warning_like
{ Koha
::Patrons
->search->single } qr/SQL that returns multiple rows/,
166 "Warning is presented if single is used for a result with multiple rows.";
169 subtest
'last' => sub {
171 my $builder = t
::lib
::TestBuilder
->new;
172 my $patron_1 = $builder->build( { source
=> 'Borrower' } );
173 my $patron_2 = $builder->build( { source
=> 'Borrower' } );
174 my $last_patron = Koha
::Patrons
->search->last;
175 is
( $last_patron->borrowernumber, $patron_2->{borrowernumber
}, '->last should return the last inserted patron' );
176 $last_patron = Koha
::Patrons
->search({ borrowernumber
=> $patron_1->{borrowernumber
} })->last;
177 is
( $last_patron->borrowernumber, $patron_1->{borrowernumber
}, '->last should work even if there is only 1 result' );
178 $last_patron = Koha
::Patrons
->search({ surname
=> 'should_not_exist' })->last;
179 is
( $last_patron, undef, '->last should return undef if search does not return any results' );
182 subtest
'get_column' => sub {
184 my @cities = Koha
::Cities
->search;
185 my @city_names = map { $_->city_name } @cities;
186 is_deeply
( [ Koha
::Cities
->search->get_column('city_name') ], \
@city_names, 'Koha::Objects->get_column should be allowed' );
189 subtest
'Exceptions' => sub {
192 my $patron_borrowernumber = $builder->build({ source
=> 'Borrower' })->{ borrowernumber
};
193 my $patron = Koha
::Patrons
->find( $patron_borrowernumber );
197 $patron->blah('blah');
199 ok
( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
200 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
201 is
( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
205 $patron->set({ blah
=> 'blah' });
207 ok
( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
208 'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
213 Koha
::Patrons
->search->not_covered_yet;
215 ok
( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
216 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
217 is
( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
221 Koha
::Patrons
->not_covered_yet;
223 ok
( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
224 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
225 is
( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
229 $schema->storage->txn_rollback;
231 subtest
'->is_paged and ->pager tests' => sub {
235 $schema->storage->txn_begin;
237 # Delete existing patrons
238 Koha
::Checkouts
->delete;
239 Koha
::Patrons
->delete;
242 $builder->build_object({ class => 'Koha::Patrons' });
245 # Non-paginated search
246 my $patrons = Koha
::Patrons
->search();
247 is
( $patrons->count, 10, 'Search returns all patrons' );
248 ok
( !$patrons->is_paged, 'Search is not paged' );
251 $patrons = Koha
::Patrons
->search( undef, { 'page' => 1, 'rows' => 3 } );
252 is
( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
253 ok
( $patrons->is_paged, 'Search is paged' );
254 my $pager = $patrons->pager;
255 is
( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
256 'Koha::Objects->pager returns a valid DBIx::Class object' );
258 $schema->storage->txn_rollback;
261 subtest
'->search() tests' => sub {
265 $schema->storage->txn_begin;
267 my $count = Koha
::Patrons
->search->count;
271 $builder->build_object({ class => 'Koha::Patrons' });
274 my $patrons = Koha
::Patrons
->search();
275 is
( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
276 my @patrons = Koha
::Patrons
->search();
277 is
( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
280 is
( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
284 $schema->storage->txn_rollback;
287 subtest
"to_api() tests" => sub {
291 $schema->storage->txn_begin;
293 my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
294 my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
296 my $cities = Koha
::Cities
->search(
298 cityid
=> [ $city_1->cityid, $city_2->cityid ]
300 { -orderby
=> { -desc
=> 'cityid' } }
303 is
( $cities->count, 2, 'Count is correct' );
304 my $cities_api = $cities->to_api;
305 is
( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
306 is_deeply
( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
307 is_deeply
( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
309 my $biblio_1 = $builder->build_sample_biblio();
310 my $item_1 = $builder->build_sample_item({ biblionumber
=> $biblio_1->biblionumber });
311 my $hold_1 = $builder->build_object(
313 class => 'Koha::Holds',
314 value
=> { itemnumber
=> $item_1->itemnumber }
318 my $biblio_2 = $builder->build_sample_biblio();
319 my $item_2 = $builder->build_sample_item({ biblionumber
=> $biblio_2->biblionumber });
320 my $hold_2 = $builder->build_object(
322 class => 'Koha::Holds',
323 value
=> { itemnumber
=> $item_2->itemnumber }
327 my $embed = { 'items' => {} };
330 my @items = ( $item_1, $item_2 );
331 my @holds = ( $hold_1, $hold_2 );
333 my $biblios_api = Koha
::Biblios
->search(
335 biblionumber
=> [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
337 )->to_api( { embed
=> $embed } );
339 foreach my $biblio_api ( @
{ $biblios_api } ) {
340 ok
(exists $biblio_api->{items
}, 'Items where embedded in biblio results');
341 is
($biblio_api->{items
}->[0]->{item_id
}, $items[$i]->itemnumber, 'Item matches');
342 ok
(!exists $biblio_api->{items
}->[0]->{holds
}, 'No holds info should be embedded yet');
350 children
=> { 'holds' => {} }
356 $biblios_api = Koha
::Biblios
->search(
358 biblionumber
=> [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
360 )->to_api( { embed
=> $embed } );
362 foreach my $biblio_api ( @
{ $biblios_api } ) {
364 ok
(exists $biblio_api->{items
}, 'Items where embedded in biblio results');
365 is
($biblio_api->{items
}->[0]->{item_id
}, $items[$i]->itemnumber, 'Item still matches');
366 ok
(exists $biblio_api->{items
}->[0]->{holds
}, 'Holds info should be embedded');
367 is
($biblio_api->{items
}->[0]->{holds
}->[0]->{hold_id
}, $holds[$i]->reserve_id, 'Hold matches');
372 $schema->storage->txn_rollback;
375 subtest
"TO_JSON() tests" => sub {
379 $schema->storage->txn_begin;
381 my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
382 my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
384 my $cities = Koha
::Cities
->search(
386 cityid
=> [ $city_1->cityid, $city_2->cityid ]
388 { -orderby
=> { -desc
=> 'cityid' } }
391 is
( $cities->count, 2, 'Count is correct' );
392 my $cities_json = $cities->TO_JSON;
393 is
( ref($cities_json), 'ARRAY', 'to_api returns an array' );
394 is_deeply
( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' );
395 is_deeply
( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' );
397 $schema->storage->txn_rollback;
400 # Koha::Object[s] must behave the same as DBIx::Class
401 subtest
'Return same values as DBIx::Class' => sub {
404 subtest
'Delete' => sub {
407 $schema->storage->txn_begin;
409 subtest
'Simple Koha::Objects - Koha::Cities' => sub {
412 subtest
'Koha::Object->delete' => sub {
416 my ( $r_us, $e_us, $r_them, $e_them );
418 # CASE 1 - Delete an existing object
419 my $c = Koha
::City
->new( { city_name
=> 'city4test' } )->store;
420 try
{ $r_us = $c->delete; } catch
{ $e_us = $_ };
421 $c = $schema->resultset('City')->new( { city_name
=> 'city4test_2' } )->update_or_insert;
422 try
{ $r_them = $c->delete; } catch
{ $e_them = $_ };
423 ok
( ref($r_us) && ref($r_them),
424 'Successful delete should return the object ' );
425 ok
( !defined $e_us && !defined $e_them,
426 'Successful delete should not raise an exception' );
427 is
( ref($r_us), 'Koha::City', 'Successful delete should return our Koha::Obect based object' );
429 # CASE 2 - Delete an object that is not in storage
430 try
{ $r_us = $r_us->delete; } catch
{ $e_us = $_ };
431 try
{ $r_them = $r_them->delete; } catch
{ $e_them = $_ };
433 defined $e_us && defined $e_them,
434 'Delete an object that is not in storage should raise an exception'
436 is
( ref($e_us), 'DBIx::Class::Exception' )
437 ; # FIXME This needs adjustement, we want to throw a Koha::Exception
441 subtest
'Koha::Objects->delete' => sub {
445 my ( $r_us, $e_us, $r_them, $e_them );
447 # CASE 1 - Delete existing objects
448 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
449 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
450 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
451 my $cities = Koha
::Cities
->search(
463 try
{ $r_us = $cities->delete; } catch
{ $e_us = $_ };
465 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
466 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
467 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
468 $cities = $schema->resultset('City')->search(
480 try
{ $r_them = $cities->delete; } catch
{ $e_them = $_ };
482 ok
( $r_us == 3 && $r_them == 3 );
483 ok
(!defined($e_us) && !defined($e_them));
485 # CASE 2 - One of the object is not in storage
486 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
487 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
488 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
489 $cities = Koha
::Cities
->search(
501 $city_2->delete; # We delete one of the object
502 try
{ $r_us = $cities->delete; } catch
{ $e_us = $_ };
504 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
505 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
506 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
507 $cities = $schema->resultset('City')->search(
519 $city_2->delete; # We delete one of the object
520 try
{ $r_them = $cities->delete; } catch
{ $e_them = $_ };
522 ok
( $r_us == 2 && $r_them == 2 );
523 ok
(!defined($e_us) && !defined($e_them));
527 subtest
'Overwritten Koha::Objects->delete - Koha::Patrons' => sub {
531 subtest
'Koha::Object->delete' => sub {
535 my ( $r_us, $e_us, $r_them, $e_them );
537 # CASE 1 - Delete an existing patron
538 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
539 my $patron_data = $patron->unblessed;
542 $patron = Koha
::Patron
->new( $patron_data )->store;
543 try
{$r_us = $patron->delete;} catch
{ $e_us = $_ };
544 $patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
545 try
{$r_them = $patron->delete;} catch
{ $e_them = $_ };
546 ok
( ref($r_us) && ref($r_them),
547 'Successful delete should return the patron object' );
548 ok
( !defined $e_us && !defined $e_them,
549 'Successful delete should not raise an exception' );
550 is
( ref($r_us), 'Koha::Patron',
551 'Successful delete should return our Koha::Obect based object' );
553 # CASE 2 - Delete a patron that is not in storage
554 try
{ $r_us = $r_us->delete; } catch
{ $e_us = $_ };
555 try
{ $r_them = $r_them->delete; } catch
{ $e_them = $_ };
557 defined $e_us && defined $e_them,
558 'Delete a patron that is not in storage should raise an exception'
560 is
( ref($e_us), 'DBIx::Class::Exception' )
561 ; # FIXME This needs adjustement, we want to throw a Koha::Exception
563 # CASE 3 - Delete a patron that cannot be deleted (as a checkout)
564 $patron = Koha
::Patron
->new($patron_data)->store;
565 $builder->build_object(
567 class => 'Koha::Checkouts',
568 value
=> { borrowernumber
=> $patron->borrowernumber }
571 try
{ $r_us = $r_us->delete; } catch
{ $e_us = $_ };
572 $patron = $schema->resultset('Borrower')->find( $patron->borrowernumber );
573 try
{ $r_them = $r_them->delete; } catch
{ $e_them = $_ };
575 defined $e_us && defined $e_them,
576 'Delete a patron that cannot be deleted should raise an exception'
578 is
( ref($e_us), 'DBIx::Class::Exception' )
579 ; # FIXME This needs adjustement, we want to throw a Koha::Exception
582 subtest
'Koha::Objects->delete' => sub {
586 my ( $r_us, $e_us, $r_them, $e_them );
588 # CASE 1 - Delete existing objects
589 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
590 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
591 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
592 my $patrons = Koha
::Patrons
->search(
596 $patron_1->borrowernumber,
597 $patron_2->borrowernumber,
598 $patron_3->borrowernumber
604 try
{ $r_us = $patrons->delete; } catch
{ $e_us = $_ };
606 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
607 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
608 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
609 $patrons = $schema->resultset('Borrower')->search(
613 $patron_1->borrowernumber,
614 $patron_2->borrowernumber,
615 $patron_3->borrowernumber
621 try
{ $r_them = $patrons->delete; } catch
{ $e_them = $_ };
623 ok
( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' );
624 ok
(!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well');
626 # CASE 2 - One of the patrons is not in storage
627 undef $_ for $r_us, $e_us, $r_them, $e_them;
628 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
629 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
630 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
631 $patrons = Koha
::Patrons
->search(
635 $patron_1->borrowernumber,
636 $patron_2->borrowernumber,
637 $patron_3->borrowernumber
643 $patron_2->delete; # We delete one of the patron
644 try
{ $r_us = $patrons->delete; } catch
{ $e_us = $_ };
646 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
647 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
648 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
649 $patrons = $schema->resultset('Borrower')->search(
653 $patron_1->borrowernumber,
654 $patron_2->borrowernumber,
655 $patron_3->borrowernumber
661 $patron_2->delete; # We delete one of the patron
662 try
{ $r_them = $patrons->delete; } catch
{ $e_them = $_ };
664 ok
( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' );
665 ok
(!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
667 # CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout)
668 undef $_ for $r_us, $e_us, $r_them, $e_them;
669 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
670 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
671 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
672 $patrons = Koha
::Patrons
->search(
676 $patron_1->borrowernumber,
677 $patron_2->borrowernumber,
678 $patron_3->borrowernumber
684 # Adding a checkout to patron_2
685 $builder->build_object(
687 class => 'Koha::Checkouts',
688 value
=> { borrowernumber
=> $patron_2->borrowernumber }
692 try
{ $r_us = $patrons->delete; } catch
{ $e_us = $_ };
693 my $not_deleted_us = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
695 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
696 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
697 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
698 $patrons = $schema->resultset('Borrower')->search(
702 $patron_1->borrowernumber,
703 $patron_2->borrowernumber,
704 $patron_3->borrowernumber
710 # Adding a checkout to patron_2
711 $builder->build_object(
713 class => 'Koha::Checkouts',
714 value
=> { borrowernumber
=> $patron_2->borrowernumber }
718 try
{ $r_them = $patrons->delete; } catch
{ $e_them = $_ };
720 my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
722 defined $e_us && defined $e_them,
723 'Delete patrons with one that cannot be deleted should raise an exception'
725 is
( ref($e_us), 'DBIx::Class::Exception' )
726 ; # FIXME This needs adjustement, we want to throw a Koha::Exception
728 ok
($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted');
732 $schema->storage->txn_rollback;
736 subtest
'Update (set/store)' => sub {
739 $schema->storage->txn_begin;
741 subtest
'Simple Koha::Objects - Koha::Cities' => sub {
744 subtest
'Koha::Object->update' => sub {
748 my ( $r_us, $e_us, $r_them, $e_them );
750 # CASE 1 - Update an existing object
751 my $c_us = Koha
::City
->new( { city_name
=> 'city4test' } )->store;
752 try
{ $r_us = $c_us->update({ city_country
=> 'country4test' }); } catch
{ $e_us = $_ };
753 my $c_them = $schema->resultset('City')->new( { city_name
=> 'city4test_2' } )->update_or_insert;
754 try
{ $r_them = $c_them->update({ city_country
=> 'country4test_2' }); } catch
{ $e_them = $_ };
755 ok
( ref($r_us) && ref($r_them),
756 'Successful update should return the object ' );
757 ok
( !defined $e_us && !defined $e_them,
758 'Successful update should not raise an exception' );
759 is
( ref($r_us), 'Koha::City', 'Successful update should return our Koha::Obect based object' );
761 # CASE 2 - Update an object that is not in storage
764 try
{ $r_us = $c_us->update({ city_country
=> 'another_country' }); } catch
{ $e_us = $_ };
765 try
{ $r_them = $c_them->update({ city_country
=> 'another_country' }); } catch
{ $e_them = $_ };
767 defined $e_us && defined $e_them,
768 'Update an object that is not in storage should raise an exception'
770 is
( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
773 subtest
'Koha::Objects->update' => sub {
777 my ( $r_us, $e_us, $r_them, $e_them );
779 # CASE 1 - update existing objects
780 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
781 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
782 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
783 my $cities = Koha
::Cities
->search(
795 try
{ $r_us = $cities->update({ city_country
=> 'country4test' }); } catch
{ $e_us = $_ };
797 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
798 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
799 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
800 $cities = $schema->resultset('City')->search(
812 try
{ $r_them = $cities->update({ city_country
=> 'country4test' }); } catch
{ $e_them = $_ };
814 ok
( $r_us == 3 && $r_them == 3, '->update should return the number of updated cities' );
815 ok
(!defined($e_us) && !defined($e_them));
817 # CASE 2 - One of the object is not in storage
818 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
819 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
820 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
821 $cities = Koha
::Cities
->search(
833 $city_2->delete; # We delete one of the object
834 try
{ $r_us = $cities->update({ city_country
=> 'country4test' }); } catch
{ $e_us = $_ };
836 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
837 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
838 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
839 $cities = $schema->resultset('City')->search(
851 $city_2->delete; # We delete one of the object
852 try
{ $r_them = $cities->update({ city_country
=> 'country4test' }); } catch
{ $e_them = $_ };
854 ok
( $r_us == 2 && $r_them == 2, '->update should return the number of updated cities' );
855 ok
(!defined($e_us) && !defined($e_them));
859 subtest
'Overwritten Koha::Objects->store|update - Koha::Patrons' => sub {
863 subtest
'Koha::Object->update' => sub {
867 my ( $r_us, $e_us, $r_them, $e_them );
869 # CASE 1 - Update an existing patron
870 my $patron_us = $builder->build_object({ class => 'Koha::Patrons' });
871 try
{$r_us = $patron_us->update({city
=> 'a_city'});} catch
{ $e_us = $_ };
873 my $patron_data = $builder->build_object({ class => 'Koha::Patrons' })->delete->unblessed;
874 my $patron_them = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
875 try
{$r_them = $patron_them->update({city
=> 'a_city'});} catch
{ $e_them = $_ };
876 ok
( ref($r_us) && ref($r_them),
877 'Successful update should return the patron object' );
878 ok
( !defined $e_us && !defined $e_them,
879 'Successful update should not raise an exception' );
880 is
( ref($r_us), 'Koha::Patron',
881 'Successful update should return our Koha::Obect based object' );
883 # CASE 2 - Update a patron that is not in storage
885 $patron_them->delete;
886 try
{ $r_us = $patron_us->update({ city
=> 'another_city' }); } catch
{ $e_us = $_ };
887 try
{ $r_them = $patron_them->update({ city
=> 'another_city' }); } catch
{ $e_them = $_ };
889 defined $e_us && defined $e_them,
890 'Update a patron that is not in storage should raise an exception'
892 is
( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
896 subtest
'Koha::Objects->Update ' => sub {
900 my ( $r_us, $e_us, $r_them, $e_them );
902 # CASE 1 - Update existing objects
903 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
904 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
905 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
906 my $patrons_us = Koha
::Patrons
->search(
910 $patron_1->borrowernumber,
911 $patron_2->borrowernumber,
912 $patron_3->borrowernumber
918 try
{ $r_us = $patrons_us->update({ city
=> 'a_city' }); } catch
{ $e_us = $_ };
920 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
921 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
922 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
923 my $patrons_them = $schema->resultset('Borrower')->search(
927 $patron_1->borrowernumber,
928 $patron_2->borrowernumber,
929 $patron_3->borrowernumber
935 try
{ $r_them = $patrons_them->update({ city
=> 'a_city' }); } catch
{ $e_them = $_ };
937 ok
( $r_us == 3 && $r_them == 3, '->update should return the number of update patrons' );
938 ok
(!defined($e_us) && !defined($e_them), '->update should not raise exception if everything went well');
940 # CASE 2 - One of the patrons is not in storage
941 undef $_ for $r_us, $e_us, $r_them, $e_them;
942 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
943 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
944 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
945 $patrons_us = Koha
::Patrons
->search(
949 $patron_1->borrowernumber,
950 $patron_2->borrowernumber,
951 $patron_3->borrowernumber
957 $patron_2->delete; # We delete one of the patron
958 try
{ $r_us = $patrons_us->update({ city
=> 'another_city' }); } catch
{ $e_us = $_ };
960 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
961 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
962 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
963 $patrons_them = $schema->resultset('Borrower')->search(
967 $patron_1->borrowernumber,
968 $patron_2->borrowernumber,
969 $patron_3->borrowernumber
975 $patron_2->delete; # We delete one of the patron
976 try
{ $r_them = $patrons_them->update({ city
=> 'another_city' }); } catch
{ $e_them = $_ };
978 ok
( $r_us == 2 && $r_them == 2, 'Update patrons with one that was not in storage should update the patrons' );
979 ok
(!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
982 # Testing no_triggers
983 t
::lib
::Mocks
::mock_preference
('uppercasesurnames', 1);
984 $patrons_us = Koha
::Patrons
->search(
988 $patron_1->borrowernumber,
989 $patron_2->borrowernumber,
990 $patron_3->borrowernumber
995 $patrons_us->update({ surname
=> 'foo' }); # Koha::Patron->store is supposed to uppercase the surnames
996 is
( $patrons_us->search({ surname
=> 'FOO' })->count, 2, 'Koha::Patron->store is hit' );
998 $patrons_us->update({ surname
=> 'foo', no_triggers
=> 1 }); # The surnames won't be uppercase as we won't hit Koha::Patron->store
999 is
( $patrons_us->search({ surname
=> 'foo' })->count, 2, 'Koha::Patron->store is not hit');
1005 $schema->storage->txn_rollback;
1011 subtest
"attributes_from_api() tests" => sub {
1015 $schema->storage->txn_begin;
1017 my $cities_rs = Koha
::Cities
->new;
1018 my $city = Koha
::City
->new;
1020 my $api_attributes = {
1026 $cities_rs->attributes_from_api($api_attributes),
1027 $city->attributes_from_api($api_attributes)
1030 $schema->storage->txn_rollback;
1033 subtest
"from_api_mapping() tests" => sub {
1037 $schema->storage->txn_begin;
1039 my $cities_rs = Koha
::Cities
->new;
1040 my $city = Koha
::City
->new;
1043 $cities_rs->from_api_mapping,
1044 $city->from_api_mapping
1047 $schema->storage->txn_rollback;
1050 subtest
'prefetch_whitelist() tests' => sub {
1054 $schema->storage->txn_begin;
1056 my $biblios = Koha
::Biblios
->new;
1058 my $prefetch_whitelist = $biblios->prefetch_whitelist;
1061 exists $prefetch_whitelist->{orders
},
1062 'Relationship matching method name is listed'
1065 $prefetch_whitelist->{orders
},
1066 'Koha::Acquisition::Order',
1067 'Guessed the non-standard object class correctly'
1071 $prefetch_whitelist->{items
},
1073 'Guessed the standard object class correctly'
1076 $schema->storage->txn_rollback;
1079 subtest
'empty() tests' => sub {
1083 $schema->storage->txn_begin;
1085 # Add a patron, we need more than 0
1086 $builder->build_object({ class => 'Koha::Patrons' });
1087 ok
( Koha
::Patrons
->count > 0, 'There is more than one Koha::Patron on the resultset' );
1089 my $empty = Koha
::Patrons
->new->empty;
1090 is
( ref($empty), 'Koha::Patrons', '->empty returns a Koha::Patrons iterator' );
1091 is
( $empty->count, 0, 'The empty resultset is, well, empty :-D' );
1093 $empty = Koha
::Patrons
->empty;
1094 is
( ref($empty), 'Koha::Patrons', 'without being instantiated, ->empty still returns a Koha::Patrons iterator' );
1095 is
( $empty->count, 0, 'The empty resultset is, well, empty :-D' );
1097 $schema->storage->txn_rollback;
1100 subtest
'delete() tests' => sub {
1104 $schema->storage->txn_begin;
1106 # Make sure no cities
1107 warnings_are
{ Koha
::Cities
->delete }[],
1108 "No warnings, no Koha::City->delete called as it doesn't exist";
1111 my $mocked_city = Test
::MockModule
->new('Koha::City');
1115 shift->_result->delete;
1116 warn "delete called!";
1121 $builder->build_object( { class => 'Koha::Cities' } );
1122 $builder->build_object( { class => 'Koha::Cities' } );
1124 my $cities = Koha
::Cities
->search;
1126 warnings_are
{ $cities->delete }
1127 [ "delete called!", "delete called!" ],
1128 "No warnings, no Koha::City->delete called as it doesn't exist";
1130 $schema->storage->txn_rollback;