|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 24; |
22 |
use Test::More tests => 3; |
| 23 |
use Test::Exception; |
23 |
use Test::Exception; |
| 24 |
use Test::MockModule; |
24 |
use Test::MockModule; |
| 25 |
use Test::Warn; |
25 |
use Test::Warn; |
|
Lines 48-1071
my @columns = Koha::Patrons->columns;
Link Here
|
| 48 |
my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns; |
48 |
my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns; |
| 49 |
is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' ); |
49 |
is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' ); |
| 50 |
|
50 |
|
| 51 |
subtest 'find' => sub { |
|
|
| 52 |
plan tests => 6; |
| 53 |
my $patron = $builder->build({source => 'Borrower'}); |
| 54 |
my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} ); |
| 55 |
is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' ); |
| 56 |
|
| 57 |
my @patrons = Koha::Patrons->find( $patron->{borrowernumber} ); |
| 58 |
is(scalar @patrons, 1, '->find in list context returns a value'); |
| 59 |
is($patrons[0]->borrowernumber, $patron->{borrowernumber}, '->find in list context returns the same value as in scalar context'); |
| 60 |
|
| 61 |
my $patrons = { |
| 62 |
foo => Koha::Patrons->find('foo'), |
| 63 |
bar => 'baz', |
| 64 |
}; |
| 65 |
is ($patrons->{foo}, undef, '->find in list context returns undef when no record is found'); |
| 66 |
|
| 67 |
# Test sending undef to find; should not generate a warning |
| 68 |
warning_is { $patron = Koha::Patrons->find( undef ); } |
| 69 |
"", "Sending undef does not trigger a DBIx warning"; |
| 70 |
warning_is { $patron = Koha::Patrons->find( undef, undef ); } |
| 71 |
"", "Sending two undefs does not trigger a DBIx warning too"; |
| 72 |
}; |
| 73 |
|
| 74 |
subtest 'update' => sub { |
| 75 |
plan tests => 2; |
| 76 |
|
| 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 => 'UK' } } ); |
| 80 |
$builder->build( { source => 'City', value => { city_country => 'France' } } ); |
| 81 |
$builder->build( { source => 'City', value => { city_country => 'France' } } ); |
| 82 |
$builder->build( { source => 'City', value => { city_country => 'Germany' } } ); |
| 83 |
Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } ); |
| 84 |
is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' ); |
| 85 |
is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' ); |
| 86 |
}; |
| 87 |
|
| 88 |
subtest 'reset' => sub { |
| 89 |
plan tests => 3; |
| 90 |
|
| 91 |
my $patrons = Koha::Patrons->search; |
| 92 |
my $first_borrowernumber = $patrons->next->borrowernumber; |
| 93 |
my $second_borrowernumber = $patrons->next->borrowernumber; |
| 94 |
is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' ); |
| 95 |
is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' ); |
| 96 |
is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected'); |
| 97 |
}; |
| 98 |
|
| 99 |
subtest 'delete' => sub { |
| 100 |
plan tests => 2; |
| 101 |
|
| 102 |
my $patron_1 = $builder->build({source => 'Borrower'}); |
| 103 |
my $patron_2 = $builder->build({source => 'Borrower'}); |
| 104 |
is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, ''); |
| 105 |
is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, ''); |
| 106 |
}; |
| 107 |
|
| 108 |
subtest 'new' => sub { |
| 109 |
plan tests => 2; |
| 110 |
my $a_cat_code = 'A_CAT_CODE'; |
| 111 |
my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store; |
| 112 |
is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' ); |
| 113 |
Koha::Patron::Categories->find($a_cat_code)->delete; |
| 114 |
$patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store; |
| 115 |
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' ); |
| 116 |
Koha::Patron::Categories->find($a_cat_code)->delete; |
| 117 |
}; |
| 118 |
|
| 119 |
subtest 'find' => sub { |
| 120 |
plan tests => 4; |
| 121 |
|
| 122 |
# check find on a single PK |
| 123 |
my $patron = $builder->build({ source => 'Borrower' }); |
| 124 |
is( Koha::Patrons->find($patron->{borrowernumber})->surname, |
| 125 |
$patron->{surname}, "Checking an arbitrary patron column after find" |
| 126 |
); |
| 127 |
# check find with unique column |
| 128 |
my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' }); |
| 129 |
is( $obj->borrowernumber, $patron->{borrowernumber}, |
| 130 |
'Find with unique column and key specified' ); |
| 131 |
# check find with an additional where clause in the attrs hash |
| 132 |
# we do not expect to find something now |
| 133 |
is( Koha::Patrons->find( |
| 134 |
$patron->{borrowernumber}, |
| 135 |
{ where => { surname => { '!=', $patron->{surname} }}}, |
| 136 |
), undef, 'Additional where clause in find call' ); |
| 137 |
|
| 138 |
is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' ); |
| 139 |
}; |
| 140 |
|
| 141 |
subtest 'search_related' => sub { |
| 142 |
plan tests => 6; |
| 143 |
my $builder = t::lib::TestBuilder->new; |
| 144 |
my $patron_1 = $builder->build( { source => 'Borrower' } ); |
| 145 |
my $patron_2 = $builder->build( { source => 'Borrower' } ); |
| 146 |
my $libraries = Koha::Patrons->search( |
| 147 |
{ |
| 148 |
-or => { |
| 149 |
borrowernumber => |
| 150 |
[ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] |
| 151 |
} |
| 152 |
} |
| 153 |
)->search_related('branchcode'); |
| 154 |
is( ref($libraries), 'Koha::Libraries', |
| 155 |
'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' |
| 156 |
); |
| 157 |
is( $libraries->count, 2, |
| 158 |
'Koha::Objects->search_related should work as expected' ); |
| 159 |
ok( eq_array( |
| 160 |
[ $libraries->get_column('branchcode') ], |
| 161 |
[ $patron_1->{branchcode}, $patron_2->{branchcode} ] ), |
| 162 |
'Koha::Objects->search_related should work as expected' |
| 163 |
); |
| 164 |
|
| 165 |
my @libraries = Koha::Patrons->search( |
| 166 |
{ |
| 167 |
-or => { |
| 168 |
borrowernumber => |
| 169 |
[ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] |
| 170 |
} |
| 171 |
} |
| 172 |
)->search_related('branchcode'); |
| 173 |
is( |
| 174 |
ref( $libraries[0] ), 'Koha::Library', |
| 175 |
'Koha::Objects->search_related should return a list of Koha::Object-based objects' |
| 176 |
); |
| 177 |
is( scalar(@libraries), 2, |
| 178 |
'Koha::Objects->search_related should work as expected' ); |
| 179 |
ok( eq_array( |
| 180 |
[ map { $_->branchcode } @libraries ], |
| 181 |
[ $patron_1->{branchcode}, $patron_2->{branchcode} ] ), |
| 182 |
'Koha::Objects->search_related should work as expected' |
| 183 |
); |
| 184 |
}; |
| 185 |
|
| 186 |
subtest 'single' => sub { |
| 187 |
plan tests => 2; |
| 188 |
my $builder = t::lib::TestBuilder->new; |
| 189 |
my $patron_1 = $builder->build( { source => 'Borrower' } ); |
| 190 |
my $patron_2 = $builder->build( { source => 'Borrower' } ); |
| 191 |
my $patron = Koha::Patrons->search({}, { rows => 1 })->single; |
| 192 |
is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.'); |
| 193 |
warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/, |
| 194 |
"Warning is presented if single is used for a result with multiple rows."; |
| 195 |
}; |
| 196 |
|
| 197 |
subtest 'last' => sub { |
| 198 |
plan tests => 3; |
| 199 |
my $builder = t::lib::TestBuilder->new; |
| 200 |
my $patron_1 = $builder->build( { source => 'Borrower' } ); |
| 201 |
my $patron_2 = $builder->build( { source => 'Borrower' } ); |
| 202 |
my $last_patron = Koha::Patrons->search->last; |
| 203 |
is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' ); |
| 204 |
$last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last; |
| 205 |
is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' ); |
| 206 |
$last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last; |
| 207 |
is( $last_patron, undef, '->last should return undef if search does not return any results' ); |
| 208 |
}; |
| 209 |
|
| 210 |
subtest 'get_column' => sub { |
| 211 |
plan tests => 1; |
| 212 |
my @cities = Koha::Cities->search; |
| 213 |
my @city_names = map { $_->city_name } @cities; |
| 214 |
is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' ); |
| 215 |
}; |
| 216 |
|
| 217 |
subtest 'Exceptions' => sub { |
| 218 |
plan tests => 7; |
| 219 |
|
| 220 |
my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber }; |
| 221 |
my $patron = Koha::Patrons->find( $patron_borrowernumber ); |
| 222 |
|
| 223 |
# Koha::Object |
| 224 |
try { |
| 225 |
$patron->blah('blah'); |
| 226 |
} catch { |
| 227 |
ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'), |
| 228 |
'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' ); |
| 229 |
is( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' ); |
| 230 |
}; |
| 231 |
|
| 232 |
try { |
| 233 |
$patron->set({ blah => 'blah' }); |
| 234 |
} catch { |
| 235 |
ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'), |
| 236 |
'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' ); |
| 237 |
}; |
| 238 |
|
| 239 |
# Koha::Objects |
| 240 |
try { |
| 241 |
Koha::Patrons->search->not_covered_yet; |
| 242 |
} catch { |
| 243 |
ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'), |
| 244 |
'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' ); |
| 245 |
is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' ); |
| 246 |
}; |
| 247 |
|
| 248 |
try { |
| 249 |
Koha::Patrons->not_covered_yet; |
| 250 |
} catch { |
| 251 |
ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'), |
| 252 |
'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' ); |
| 253 |
is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' ); |
| 254 |
}; |
| 255 |
}; |
| 256 |
|
| 257 |
$schema->storage->txn_rollback; |
| 258 |
|
| 259 |
subtest '->is_paged and ->pager tests' => sub { |
| 260 |
|
| 261 |
plan tests => 5; |
| 262 |
|
| 263 |
$schema->storage->txn_begin; |
| 264 |
|
| 265 |
# Count existing patrons |
| 266 |
my $nb_patrons = Koha::Patrons->search()->count; |
| 267 |
# Create 10 more patrons |
| 268 |
foreach (1..10) { |
| 269 |
$builder->build_object({ class => 'Koha::Patrons' }); |
| 270 |
} |
| 271 |
|
| 272 |
# Non-paginated search |
| 273 |
my $patrons = Koha::Patrons->search(); |
| 274 |
is( $patrons->count, $nb_patrons + 10, 'Search returns all patrons' ); |
| 275 |
ok( !$patrons->is_paged, 'Search is not paged' ); |
| 276 |
|
| 277 |
# Paginated search |
| 278 |
$patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } ); |
| 279 |
is( $patrons->count, 3, 'Search returns only one page, 3 patrons' ); |
| 280 |
ok( $patrons->is_paged, 'Search is paged' ); |
| 281 |
my $pager = $patrons->pager; |
| 282 |
is( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager', |
| 283 |
'Koha::Objects->pager returns a valid DBIx::Class object' ); |
| 284 |
|
| 285 |
$schema->storage->txn_rollback; |
| 286 |
}; |
| 287 |
|
| 288 |
subtest '->search() tests' => sub { |
| 289 |
|
| 290 |
plan tests => 12; |
| 291 |
|
| 292 |
$schema->storage->txn_begin; |
| 293 |
|
| 294 |
my $count = Koha::Patrons->search->count; |
| 295 |
|
| 296 |
# Create 10 patrons |
| 297 |
foreach (1..10) { |
| 298 |
$builder->build_object({ class => 'Koha::Patrons' }); |
| 299 |
} |
| 300 |
|
| 301 |
my $patrons = Koha::Patrons->search(); |
| 302 |
is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' ); |
| 303 |
my @patrons = Koha::Patrons->search(); |
| 304 |
is( scalar @patrons, $count + 10, 'search in list context returns a list of objects' ); |
| 305 |
my $i = 0; |
| 306 |
foreach (1..10) { |
| 307 |
is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' ); |
| 308 |
$i++; |
| 309 |
} |
| 310 |
|
| 311 |
$schema->storage->txn_rollback; |
| 312 |
}; |
| 313 |
|
| 314 |
subtest "to_api() tests" => sub { |
| 315 |
|
| 316 |
plan tests => 18; |
| 317 |
|
| 318 |
$schema->storage->txn_begin; |
| 319 |
|
| 320 |
my $city_1 = $builder->build_object( { class => 'Koha::Cities' } ); |
| 321 |
my $city_2 = $builder->build_object( { class => 'Koha::Cities' } ); |
| 322 |
|
| 323 |
my $cities = Koha::Cities->search( |
| 324 |
{ |
| 325 |
cityid => [ $city_1->cityid, $city_2->cityid ] |
| 326 |
}, |
| 327 |
{ -orderby => { -desc => 'cityid' } } |
| 328 |
); |
| 329 |
|
| 330 |
is( $cities->count, 2, 'Count is correct' ); |
| 331 |
my $cities_api = $cities->to_api; |
| 332 |
is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' ); |
| 333 |
is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' ); |
| 334 |
is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' ); |
| 335 |
|
| 336 |
my $biblio_1 = $builder->build_sample_biblio(); |
| 337 |
my $item_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber }); |
| 338 |
my $hold_1 = $builder->build_object( |
| 339 |
{ |
| 340 |
class => 'Koha::Holds', |
| 341 |
value => { itemnumber => $item_1->itemnumber } |
| 342 |
} |
| 343 |
); |
| 344 |
|
| 345 |
my $biblio_2 = $builder->build_sample_biblio(); |
| 346 |
my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber }); |
| 347 |
my $hold_2 = $builder->build_object( |
| 348 |
{ |
| 349 |
class => 'Koha::Holds', |
| 350 |
value => { itemnumber => $item_2->itemnumber } |
| 351 |
} |
| 352 |
); |
| 353 |
|
| 354 |
my $embed = { 'items' => {} }; |
| 355 |
|
| 356 |
my $i = 0; |
| 357 |
my @items = ( $item_1, $item_2 ); |
| 358 |
my @holds = ( $hold_1, $hold_2 ); |
| 359 |
|
| 360 |
my $biblios_api = Koha::Biblios->search( |
| 361 |
{ |
| 362 |
biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ] |
| 363 |
} |
| 364 |
)->to_api( { embed => $embed } ); |
| 365 |
|
| 366 |
foreach my $biblio_api ( @{ $biblios_api } ) { |
| 367 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
| 368 |
is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item matches'); |
| 369 |
ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet'); |
| 370 |
|
| 371 |
$i++; |
| 372 |
} |
| 373 |
|
| 374 |
# One more level |
| 375 |
$embed = { |
| 376 |
'items' => { |
| 377 |
children => { 'holds' => {} } |
| 378 |
} |
| 379 |
}; |
| 380 |
|
| 381 |
$i = 0; |
| 382 |
|
| 383 |
$biblios_api = Koha::Biblios->search( |
| 384 |
{ |
| 385 |
biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ] |
| 386 |
} |
| 387 |
)->to_api( { embed => $embed } ); |
| 388 |
|
| 389 |
foreach my $biblio_api ( @{ $biblios_api } ) { |
| 390 |
|
| 391 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
| 392 |
is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item still matches'); |
| 393 |
ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded'); |
| 394 |
is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $holds[$i]->reserve_id, 'Hold matches'); |
| 395 |
|
| 396 |
$i++; |
| 397 |
} |
| 398 |
|
| 399 |
$schema->storage->txn_rollback; |
| 400 |
}; |
| 401 |
|
| 402 |
subtest "TO_JSON() tests" => sub { |
| 403 |
|
| 404 |
plan tests => 4; |
| 405 |
|
| 406 |
$schema->storage->txn_begin; |
| 407 |
|
| 408 |
my $city_1 = $builder->build_object( { class => 'Koha::Cities' } ); |
| 409 |
my $city_2 = $builder->build_object( { class => 'Koha::Cities' } ); |
| 410 |
|
| 411 |
my $cities = Koha::Cities->search( |
| 412 |
{ |
| 413 |
cityid => [ $city_1->cityid, $city_2->cityid ] |
| 414 |
}, |
| 415 |
{ -orderby => { -desc => 'cityid' } } |
| 416 |
); |
| 417 |
|
| 418 |
is( $cities->count, 2, 'Count is correct' ); |
| 419 |
my $cities_json = $cities->TO_JSON; |
| 420 |
is( ref($cities_json), 'ARRAY', 'to_api returns an array' ); |
| 421 |
is_deeply( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' ); |
| 422 |
is_deeply( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' ); |
| 423 |
|
| 424 |
$schema->storage->txn_rollback; |
| 425 |
}; |
| 426 |
|
| 427 |
# Koha::Object[s] must behave the same as DBIx::Class |
| 428 |
subtest 'Return same values as DBIx::Class' => sub { |
| 429 |
plan tests => 2; |
| 430 |
|
| 431 |
subtest 'Delete' => sub { |
| 432 |
plan tests => 2; |
| 433 |
|
| 434 |
$schema->storage->txn_begin; |
| 435 |
|
| 436 |
subtest 'Simple Koha::Objects - Koha::Cities' => sub { |
| 437 |
plan tests => 2; |
| 438 |
|
| 439 |
subtest 'Koha::Object->delete' => sub { |
| 440 |
|
| 441 |
plan tests => 5; |
| 442 |
|
| 443 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 444 |
|
| 445 |
# CASE 1 - Delete an existing object |
| 446 |
my $c = Koha::City->new( { city_name => 'city4test' } )->store; |
| 447 |
try { $r_us = $c->delete; } catch { $e_us = $_ }; |
| 448 |
$c = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert; |
| 449 |
try { $r_them = $c->delete; } catch { $e_them = $_ }; |
| 450 |
ok( ref($r_us) && ref($r_them), |
| 451 |
'Successful delete should return the object ' ); |
| 452 |
ok( !defined $e_us && !defined $e_them, |
| 453 |
'Successful delete should not raise an exception' ); |
| 454 |
is( ref($r_us), 'Koha::City', 'Successful delete should return our Koha::Obect based object' ); |
| 455 |
|
| 456 |
# CASE 2 - Delete an object that is not in storage |
| 457 |
try { $r_us = $r_us->delete; } catch { $e_us = $_ }; |
| 458 |
try { $r_them = $r_them->delete; } catch { $e_them = $_ }; |
| 459 |
ok( |
| 460 |
defined $e_us && defined $e_them, |
| 461 |
'Delete an object that is not in storage should raise an exception' |
| 462 |
); |
| 463 |
is( ref($e_us), 'DBIx::Class::Exception' ) |
| 464 |
; # FIXME This needs adjustement, we want to throw a Koha::Exception |
| 465 |
|
| 466 |
}; |
| 467 |
|
| 468 |
subtest 'Koha::Objects->delete' => sub { |
| 469 |
|
| 470 |
plan tests => 4; |
| 471 |
|
| 472 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 473 |
|
| 474 |
# CASE 1 - Delete existing objects |
| 475 |
my $city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 476 |
my $city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 477 |
my $city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 478 |
my $cities = Koha::Cities->search( |
| 479 |
{ |
| 480 |
cityid => { |
| 481 |
-in => [ |
| 482 |
$city_1->cityid, |
| 483 |
$city_2->cityid, |
| 484 |
$city_3->cityid, |
| 485 |
] |
| 486 |
} |
| 487 |
} |
| 488 |
); |
| 489 |
|
| 490 |
try { $r_us = $cities->delete; } catch { $e_us = $_ }; |
| 491 |
|
| 492 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 493 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 494 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 495 |
$cities = $schema->resultset('City')->search( |
| 496 |
{ |
| 497 |
cityid => { |
| 498 |
-in => [ |
| 499 |
$city_1->cityid, |
| 500 |
$city_2->cityid, |
| 501 |
$city_3->cityid, |
| 502 |
] |
| 503 |
} |
| 504 |
} |
| 505 |
); |
| 506 |
|
| 507 |
try { $r_them = $cities->delete; } catch { $e_them = $_ }; |
| 508 |
|
| 509 |
ok( $r_us == 3 && $r_them == 3 ); |
| 510 |
ok (!defined($e_us) && !defined($e_them)); |
| 511 |
|
| 512 |
# CASE 2 - One of the object is not in storage |
| 513 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 514 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 515 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 516 |
$cities = Koha::Cities->search( |
| 517 |
{ |
| 518 |
cityid => { |
| 519 |
-in => [ |
| 520 |
$city_1->cityid, |
| 521 |
$city_2->cityid, |
| 522 |
$city_3->cityid, |
| 523 |
] |
| 524 |
} |
| 525 |
} |
| 526 |
); |
| 527 |
|
| 528 |
$city_2->delete; # We delete one of the object |
| 529 |
try { $r_us = $cities->delete; } catch { $e_us = $_ }; |
| 530 |
|
| 531 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 532 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 533 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 534 |
$cities = $schema->resultset('City')->search( |
| 535 |
{ |
| 536 |
cityid => { |
| 537 |
-in => [ |
| 538 |
$city_1->cityid, |
| 539 |
$city_2->cityid, |
| 540 |
$city_3->cityid, |
| 541 |
] |
| 542 |
} |
| 543 |
} |
| 544 |
); |
| 545 |
|
| 546 |
$city_2->delete; # We delete one of the object |
| 547 |
try { $r_them = $cities->delete; } catch { $e_them = $_ }; |
| 548 |
|
| 549 |
ok( $r_us == 2 && $r_them == 2 ); |
| 550 |
ok (!defined($e_us) && !defined($e_them)); |
| 551 |
}; |
| 552 |
}; |
| 553 |
|
| 554 |
subtest 'Overwritten Koha::Objects->delete - Koha::Patrons' => sub { |
| 555 |
|
| 556 |
plan tests => 2; |
| 557 |
|
| 558 |
subtest 'Koha::Object->delete' => sub { |
| 559 |
|
| 560 |
plan tests => 7; |
| 561 |
|
| 562 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 563 |
|
| 564 |
# CASE 1 - Delete an existing patron |
| 565 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 566 |
my $patron_data = $patron->unblessed; |
| 567 |
$patron->delete; |
| 568 |
|
| 569 |
$patron = Koha::Patron->new( $patron_data )->store; |
| 570 |
try {$r_us = $patron->delete;} catch { $e_us = $_ }; |
| 571 |
$patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert; |
| 572 |
try {$r_them = $patron->delete;} catch { $e_them = $_ }; |
| 573 |
ok( ref($r_us) && ref($r_them), |
| 574 |
'Successful delete should return the patron object' ); |
| 575 |
ok( !defined $e_us && !defined $e_them, |
| 576 |
'Successful delete should not raise an exception' ); |
| 577 |
is( ref($r_us), 'Koha::Patron', |
| 578 |
'Successful delete should return our Koha::Obect based object' ); |
| 579 |
|
| 580 |
# CASE 2 - Delete a patron that is not in storage |
| 581 |
try { $r_us = $r_us->delete; } catch { $e_us = $_ }; |
| 582 |
try { $r_them = $r_them->delete; } catch { $e_them = $_ }; |
| 583 |
ok( |
| 584 |
defined $e_us && defined $e_them, |
| 585 |
'Delete a patron that is not in storage should raise an exception' |
| 586 |
); |
| 587 |
is( ref($e_us), 'DBIx::Class::Exception' ) |
| 588 |
; # FIXME This needs adjustement, we want to throw a Koha::Exception |
| 589 |
|
| 590 |
# CASE 3 - Delete a patron that cannot be deleted (as a checkout) |
| 591 |
$patron = Koha::Patron->new($patron_data)->store; |
| 592 |
$builder->build_object( |
| 593 |
{ |
| 594 |
class => 'Koha::Checkouts', |
| 595 |
value => { borrowernumber => $patron->borrowernumber } |
| 596 |
} |
| 597 |
); |
| 598 |
try { $r_us = $r_us->delete; } catch { $e_us = $_ }; |
| 599 |
$patron = $schema->resultset('Borrower')->find( $patron->borrowernumber ); |
| 600 |
try { $r_them = $r_them->delete; } catch { $e_them = $_ }; |
| 601 |
ok( |
| 602 |
defined $e_us && defined $e_them, |
| 603 |
'Delete a patron that cannot be deleted should raise an exception' |
| 604 |
); |
| 605 |
is( ref($e_us), 'DBIx::Class::Exception' ) |
| 606 |
; # FIXME This needs adjustement, we want to throw a Koha::Exception |
| 607 |
}; |
| 608 |
|
| 609 |
subtest 'Koha::Objects->delete' => sub { |
| 610 |
|
| 611 |
plan tests => 7; |
| 612 |
|
| 613 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 614 |
|
| 615 |
# CASE 1 - Delete existing objects |
| 616 |
my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 617 |
my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 618 |
my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 619 |
my $patrons = Koha::Patrons->search( |
| 620 |
{ |
| 621 |
borrowernumber => { |
| 622 |
-in => [ |
| 623 |
$patron_1->borrowernumber, |
| 624 |
$patron_2->borrowernumber, |
| 625 |
$patron_3->borrowernumber |
| 626 |
] |
| 627 |
} |
| 628 |
} |
| 629 |
); |
| 630 |
|
| 631 |
try { $r_us = $patrons->delete; } catch { $e_us = $_ }; |
| 632 |
|
| 633 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 634 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 635 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 636 |
$patrons = $schema->resultset('Borrower')->search( |
| 637 |
{ |
| 638 |
borrowernumber => { |
| 639 |
-in => [ |
| 640 |
$patron_1->borrowernumber, |
| 641 |
$patron_2->borrowernumber, |
| 642 |
$patron_3->borrowernumber |
| 643 |
] |
| 644 |
} |
| 645 |
} |
| 646 |
); |
| 647 |
|
| 648 |
try { $r_them = $patrons->delete; } catch { $e_them = $_ }; |
| 649 |
|
| 650 |
ok( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' ); |
| 651 |
ok (!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well'); |
| 652 |
|
| 653 |
# CASE 2 - One of the patrons is not in storage |
| 654 |
undef $_ for $r_us, $e_us, $r_them, $e_them; |
| 655 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 656 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 657 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 658 |
$patrons = Koha::Patrons->search( |
| 659 |
{ |
| 660 |
borrowernumber => { |
| 661 |
-in => [ |
| 662 |
$patron_1->borrowernumber, |
| 663 |
$patron_2->borrowernumber, |
| 664 |
$patron_3->borrowernumber |
| 665 |
] |
| 666 |
} |
| 667 |
} |
| 668 |
); |
| 669 |
|
| 670 |
$patron_2->delete; # We delete one of the patron |
| 671 |
try { $r_us = $patrons->delete; } catch { $e_us = $_ }; |
| 672 |
|
| 673 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 674 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 675 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 676 |
$patrons = $schema->resultset('Borrower')->search( |
| 677 |
{ |
| 678 |
borrowernumber => { |
| 679 |
-in => [ |
| 680 |
$patron_1->borrowernumber, |
| 681 |
$patron_2->borrowernumber, |
| 682 |
$patron_3->borrowernumber |
| 683 |
] |
| 684 |
} |
| 685 |
} |
| 686 |
); |
| 687 |
|
| 688 |
$patron_2->delete; # We delete one of the patron |
| 689 |
try { $r_them = $patrons->delete; } catch { $e_them = $_ }; |
| 690 |
|
| 691 |
ok( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' ); |
| 692 |
ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage'); |
| 693 |
|
| 694 |
# CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout) |
| 695 |
undef $_ for $r_us, $e_us, $r_them, $e_them; |
| 696 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 697 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 698 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 699 |
$patrons = Koha::Patrons->search( |
| 700 |
{ |
| 701 |
borrowernumber => { |
| 702 |
-in => [ |
| 703 |
$patron_1->borrowernumber, |
| 704 |
$patron_2->borrowernumber, |
| 705 |
$patron_3->borrowernumber |
| 706 |
] |
| 707 |
} |
| 708 |
} |
| 709 |
); |
| 710 |
|
| 711 |
# Adding a checkout to patron_2 |
| 712 |
$builder->build_object( |
| 713 |
{ |
| 714 |
class => 'Koha::Checkouts', |
| 715 |
value => { borrowernumber => $patron_2->borrowernumber } |
| 716 |
} |
| 717 |
); |
| 718 |
|
| 719 |
try { $r_us = $patrons->delete; } catch { $e_us = $_ }; |
| 720 |
my $not_deleted_us = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage; |
| 721 |
|
| 722 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 723 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 724 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 725 |
$patrons = $schema->resultset('Borrower')->search( |
| 726 |
{ |
| 727 |
borrowernumber => { |
| 728 |
-in => [ |
| 729 |
$patron_1->borrowernumber, |
| 730 |
$patron_2->borrowernumber, |
| 731 |
$patron_3->borrowernumber |
| 732 |
] |
| 733 |
} |
| 734 |
} |
| 735 |
); |
| 736 |
|
| 737 |
# Adding a checkout to patron_2 |
| 738 |
$builder->build_object( |
| 739 |
{ |
| 740 |
class => 'Koha::Checkouts', |
| 741 |
value => { borrowernumber => $patron_2->borrowernumber } |
| 742 |
} |
| 743 |
); |
| 744 |
|
| 745 |
try { $r_them = $patrons->delete; } catch { $e_them = $_ }; |
| 746 |
|
| 747 |
my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage; |
| 748 |
ok( |
| 749 |
defined $e_us && defined $e_them, |
| 750 |
'Delete patrons with one that cannot be deleted should raise an exception' |
| 751 |
); |
| 752 |
is( ref($e_us), 'DBIx::Class::Exception' ) |
| 753 |
; # FIXME This needs adjustement, we want to throw a Koha::Exception |
| 754 |
|
| 755 |
ok($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted'); |
| 756 |
}; |
| 757 |
}; |
| 758 |
|
| 759 |
$schema->storage->txn_rollback; |
| 760 |
|
| 761 |
}; |
| 762 |
|
| 763 |
subtest 'Update (set/store)' => sub { |
| 764 |
plan tests => 2; |
| 765 |
|
| 766 |
$schema->storage->txn_begin; |
| 767 |
|
| 768 |
subtest 'Simple Koha::Objects - Koha::Cities' => sub { |
| 769 |
plan tests => 2; |
| 770 |
|
| 771 |
subtest 'Koha::Object->update' => sub { |
| 772 |
|
| 773 |
plan tests => 5; |
| 774 |
|
| 775 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 776 |
|
| 777 |
# CASE 1 - Update an existing object |
| 778 |
my $c_us = Koha::City->new( { city_name => 'city4test' } )->store; |
| 779 |
try { $r_us = $c_us->update({ city_country => 'country4test' }); } catch { $e_us = $_ }; |
| 780 |
my $c_them = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert; |
| 781 |
try { $r_them = $c_them->update({ city_country => 'country4test_2' }); } catch { $e_them = $_ }; |
| 782 |
ok( ref($r_us) && ref($r_them), |
| 783 |
'Successful update should return the object ' ); |
| 784 |
ok( !defined $e_us && !defined $e_them, |
| 785 |
'Successful update should not raise an exception' ); |
| 786 |
is( ref($r_us), 'Koha::City', 'Successful update should return our Koha::Obect based object' ); |
| 787 |
|
| 788 |
# CASE 2 - Update an object that is not in storage |
| 789 |
$c_us->delete; |
| 790 |
$c_them->delete; |
| 791 |
try { $r_us = $c_us->update({ city_country => 'another_country' }); } catch { $e_us = $_ }; |
| 792 |
try { $r_them = $c_them->update({ city_country => 'another_country' }); } catch { $e_them = $_ }; |
| 793 |
ok( |
| 794 |
defined $e_us && defined $e_them, |
| 795 |
'Update an object that is not in storage should raise an exception' |
| 796 |
); |
| 797 |
is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' ); |
| 798 |
}; |
| 799 |
|
| 800 |
subtest 'Koha::Objects->update' => sub { |
| 801 |
|
| 802 |
plan tests => 6; |
| 803 |
|
| 804 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 805 |
|
| 806 |
# CASE 1 - update existing objects |
| 807 |
my $city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 808 |
my $city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 809 |
my $city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 810 |
my $cities = Koha::Cities->search( |
| 811 |
{ |
| 812 |
cityid => { |
| 813 |
-in => [ |
| 814 |
$city_1->cityid, |
| 815 |
$city_2->cityid, |
| 816 |
$city_3->cityid, |
| 817 |
] |
| 818 |
} |
| 819 |
} |
| 820 |
); |
| 821 |
|
| 822 |
try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ }; |
| 823 |
|
| 824 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 825 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 826 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 827 |
$cities = $schema->resultset('City')->search( |
| 828 |
{ |
| 829 |
cityid => { |
| 830 |
-in => [ |
| 831 |
$city_1->cityid, |
| 832 |
$city_2->cityid, |
| 833 |
$city_3->cityid, |
| 834 |
] |
| 835 |
} |
| 836 |
} |
| 837 |
); |
| 838 |
|
| 839 |
try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ }; |
| 840 |
|
| 841 |
ok( $r_us == 3 && $r_them == 3, '->update should return the number of updated cities' ); |
| 842 |
ok(!defined($e_us) && !defined($e_them)); |
| 843 |
|
| 844 |
# CASE 2 - One of the object is not in storage |
| 845 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 846 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 847 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 848 |
$cities = Koha::Cities->search( |
| 849 |
{ |
| 850 |
cityid => { |
| 851 |
-in => [ |
| 852 |
$city_1->cityid, |
| 853 |
$city_2->cityid, |
| 854 |
$city_3->cityid, |
| 855 |
] |
| 856 |
} |
| 857 |
} |
| 858 |
); |
| 859 |
|
| 860 |
$city_2->delete; # We delete one of the object |
| 861 |
try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ }; |
| 862 |
|
| 863 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 864 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 865 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 866 |
$cities = $schema->resultset('City')->search( |
| 867 |
{ |
| 868 |
cityid => { |
| 869 |
-in => [ |
| 870 |
$city_1->cityid, |
| 871 |
$city_2->cityid, |
| 872 |
$city_3->cityid, |
| 873 |
] |
| 874 |
} |
| 875 |
} |
| 876 |
); |
| 877 |
|
| 878 |
$city_2->delete; # We delete one of the object |
| 879 |
try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ }; |
| 880 |
|
| 881 |
ok( $r_us == 2 && $r_them == 2, '->update should return the number of updated cities' ); |
| 882 |
ok(!defined($e_us) && !defined($e_them)); |
| 883 |
|
| 884 |
throws_ok |
| 885 |
{ Koha::Cities->update({ city_country => 'Castalia' }); } |
| 886 |
'Koha::Exceptions::Object::NotInstantiated', |
| 887 |
'Exception thrown if not instantiated class'; |
| 888 |
|
| 889 |
is( "$@", 'Tried to access the \'update\' method, but Koha::Cities is not instantiated', 'Exception stringified correctly' ); |
| 890 |
|
| 891 |
}; |
| 892 |
}; |
| 893 |
|
| 894 |
subtest 'Overwritten Koha::Objects->store|update - Koha::Patrons' => sub { |
| 895 |
|
| 896 |
plan tests => 2; |
| 897 |
|
| 898 |
subtest 'Koha::Object->update' => sub { |
| 899 |
|
| 900 |
plan tests => 5; |
| 901 |
|
| 902 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 903 |
|
| 904 |
# CASE 1 - Update an existing patron |
| 905 |
my $patron_us = $builder->build_object({ class => 'Koha::Patrons' }); |
| 906 |
try {$r_us = $patron_us->update({city => 'a_city'});} catch { $e_us = $_ }; |
| 907 |
|
| 908 |
my $patron_data = $builder->build_object({ class => 'Koha::Patrons' })->delete->unblessed; |
| 909 |
my $patron_them = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert; |
| 910 |
try {$r_them = $patron_them->update({city => 'a_city'});} catch { $e_them = $_ }; |
| 911 |
ok( ref($r_us) && ref($r_them), |
| 912 |
'Successful update should return the patron object' ); |
| 913 |
ok( !defined $e_us && !defined $e_them, |
| 914 |
'Successful update should not raise an exception' ); |
| 915 |
is( ref($r_us), 'Koha::Patron', |
| 916 |
'Successful update should return our Koha::Obect based object' ); |
| 917 |
|
| 918 |
# CASE 2 - Update a patron that is not in storage |
| 919 |
$patron_us->delete; |
| 920 |
$patron_them->delete; |
| 921 |
try { $r_us = $patron_us->update({ city => 'another_city' }); } catch { $e_us = $_ }; |
| 922 |
try { $r_them = $patron_them->update({ city => 'another_city' }); } catch { $e_them = $_ }; |
| 923 |
ok( |
| 924 |
defined $e_us && defined $e_them, |
| 925 |
'Update a patron that is not in storage should raise an exception' |
| 926 |
); |
| 927 |
is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' ); |
| 928 |
|
| 929 |
}; |
| 930 |
|
| 931 |
subtest 'Koha::Objects->Update ' => sub { |
| 932 |
|
| 933 |
plan tests => 6; |
| 934 |
|
| 935 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 936 |
|
| 937 |
# CASE 1 - Update existing objects |
| 938 |
my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 939 |
my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 940 |
my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 941 |
my $patrons_us = Koha::Patrons->search( |
| 942 |
{ |
| 943 |
borrowernumber => { |
| 944 |
-in => [ |
| 945 |
$patron_1->borrowernumber, |
| 946 |
$patron_2->borrowernumber, |
| 947 |
$patron_3->borrowernumber |
| 948 |
] |
| 949 |
} |
| 950 |
} |
| 951 |
); |
| 952 |
|
| 953 |
try { $r_us = $patrons_us->update({ city => 'a_city' }); } catch { $e_us = $_ }; |
| 954 |
|
| 955 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 956 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 957 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 958 |
my $patrons_them = $schema->resultset('Borrower')->search( |
| 959 |
{ |
| 960 |
borrowernumber => { |
| 961 |
-in => [ |
| 962 |
$patron_1->borrowernumber, |
| 963 |
$patron_2->borrowernumber, |
| 964 |
$patron_3->borrowernumber |
| 965 |
] |
| 966 |
} |
| 967 |
} |
| 968 |
); |
| 969 |
|
| 970 |
try { $r_them = $patrons_them->update({ city => 'a_city' }); } catch { $e_them = $_ }; |
| 971 |
|
| 972 |
ok( $r_us == 3 && $r_them == 3, '->update should return the number of update patrons' ); |
| 973 |
ok (!defined($e_us) && !defined($e_them), '->update should not raise exception if everything went well'); |
| 974 |
|
| 975 |
# CASE 2 - One of the patrons is not in storage |
| 976 |
undef $_ for $r_us, $e_us, $r_them, $e_them; |
| 977 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 978 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 979 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 980 |
$patrons_us = Koha::Patrons->search( |
| 981 |
{ |
| 982 |
borrowernumber => { |
| 983 |
-in => [ |
| 984 |
$patron_1->borrowernumber, |
| 985 |
$patron_2->borrowernumber, |
| 986 |
$patron_3->borrowernumber |
| 987 |
] |
| 988 |
} |
| 989 |
} |
| 990 |
); |
| 991 |
|
| 992 |
$patron_2->delete; # We delete one of the patron |
| 993 |
try { $r_us = $patrons_us->update({ city => 'another_city' }); } catch { $e_us = $_ }; |
| 994 |
|
| 995 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 996 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 997 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 998 |
$patrons_them = $schema->resultset('Borrower')->search( |
| 999 |
{ |
| 1000 |
borrowernumber => { |
| 1001 |
-in => [ |
| 1002 |
$patron_1->borrowernumber, |
| 1003 |
$patron_2->borrowernumber, |
| 1004 |
$patron_3->borrowernumber |
| 1005 |
] |
| 1006 |
} |
| 1007 |
} |
| 1008 |
); |
| 1009 |
|
| 1010 |
$patron_2->delete; # We delete one of the patron |
| 1011 |
try { $r_them = $patrons_them->update({ city => 'another_city' }); } catch { $e_them = $_ }; |
| 1012 |
|
| 1013 |
ok( $r_us == 2 && $r_them == 2, 'Update patrons with one that was not in storage should update the patrons' ); |
| 1014 |
ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage'); |
| 1015 |
|
| 1016 |
|
| 1017 |
# Testing no_triggers |
| 1018 |
t::lib::Mocks::mock_preference('uppercasesurnames', 1); |
| 1019 |
$patrons_us = Koha::Patrons->search( |
| 1020 |
{ |
| 1021 |
borrowernumber => { |
| 1022 |
-in => [ |
| 1023 |
$patron_1->borrowernumber, |
| 1024 |
$patron_2->borrowernumber, |
| 1025 |
$patron_3->borrowernumber |
| 1026 |
] |
| 1027 |
} |
| 1028 |
} |
| 1029 |
); |
| 1030 |
$patrons_us->update({ surname => 'foo' }); # Koha::Patron->store is supposed to uppercase the surnames |
| 1031 |
is( $patrons_us->search({ surname => 'FOO' })->count, 2, 'Koha::Patron->store is hit' ); |
| 1032 |
|
| 1033 |
$patrons_us->update({ surname => 'foo' }, { no_triggers => 1 }); # The surnames won't be uppercase as we won't hit Koha::Patron->store |
| 1034 |
is( $patrons_us->search({ surname => 'foo' })->count, 2, 'Koha::Patron->store is not hit'); |
| 1035 |
|
| 1036 |
}; |
| 1037 |
|
| 1038 |
}; |
| 1039 |
|
| 1040 |
$schema->storage->txn_rollback; |
| 1041 |
|
| 1042 |
}; |
| 1043 |
|
| 1044 |
}; |
| 1045 |
|
| 1046 |
subtest "attributes_from_api() tests" => sub { |
| 1047 |
|
| 1048 |
plan tests => 1; |
| 1049 |
|
| 1050 |
$schema->storage->txn_begin; |
| 1051 |
|
| 1052 |
my $cities_rs = Koha::Cities->new; |
| 1053 |
my $city = Koha::City->new; |
| 1054 |
|
| 1055 |
my $api_attributes = { |
| 1056 |
name => 'Cordoba', |
| 1057 |
postal_code => 5000 |
| 1058 |
}; |
| 1059 |
|
| 1060 |
is_deeply( |
| 1061 |
$cities_rs->attributes_from_api($api_attributes), |
| 1062 |
$city->attributes_from_api($api_attributes) |
| 1063 |
); |
| 1064 |
|
| 1065 |
$schema->storage->txn_rollback; |
| 1066 |
|
| 1067 |
}; |
| 1068 |
|
| 1069 |
subtest "filter_by_last_update" => sub { |
51 |
subtest "filter_by_last_update" => sub { |
| 1070 |
|
52 |
|
| 1071 |
$schema->storage->txn_begin; |
53 |
$schema->storage->txn_begin; |
|
Lines 1097-1130
subtest "filter_by_last_update" => sub {
Link Here
|
| 1097 |
); |
79 |
); |
| 1098 |
}; |
80 |
}; |
| 1099 |
|
81 |
|
| 1100 |
my $filtered_patrons = $patrons->filter_by_last_update( |
82 |
my $count; |
| 1101 |
{ timestamp_column_name => 'updated_on', days => 2 } ); |
83 |
# my $filtered_patrons = $patrons->filter_by_last_update( |
| 1102 |
is( ref($filtered_patrons), 'Koha::Patrons', 'filter_by_last_update must return a Koha::Objects-based object' ); |
84 |
# { timestamp_column_name => 'updated_on', days => 2 } ); |
| 1103 |
|
85 |
# is( ref($filtered_patrons), 'Koha::Patrons', 'filter_by_last_update must return a Koha::Objects-based object' ); |
| 1104 |
my $count = $patrons->filter_by_last_update( |
86 |
# |
| 1105 |
{ timestamp_column_name => 'updated_on', days => 2 } )->count; |
87 |
# my $count = $patrons->filter_by_last_update( |
| 1106 |
is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' ); |
88 |
# { timestamp_column_name => 'updated_on', days => 2 } )->count; |
| 1107 |
|
89 |
# is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' ); |
| 1108 |
$count = $patrons->filter_by_last_update( |
90 |
# |
| 1109 |
{ timestamp_column_name => 'updated_on', days => 2, days_inclusive => 1 } )->count; |
91 |
# $count = $patrons->filter_by_last_update( |
| 1110 |
is( $count, 4, '4 patrons have been updated before the last 2 days (inclusive)' ); |
92 |
# { timestamp_column_name => 'updated_on', days => 2, days_inclusive => 1 } )->count; |
| 1111 |
|
93 |
# is( $count, 4, '4 patrons have been updated before the last 2 days (inclusive)' ); |
| 1112 |
$count = $patrons->filter_by_last_update( |
94 |
# |
| 1113 |
{ timestamp_column_name => 'updated_on', days => 1 } )->count; |
95 |
# $count = $patrons->filter_by_last_update( |
| 1114 |
is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' ); |
96 |
# { timestamp_column_name => 'updated_on', days => 1 } )->count; |
| 1115 |
|
97 |
# is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' ); |
| 1116 |
$count = $patrons->filter_by_last_update( |
98 |
# |
| 1117 |
{ timestamp_column_name => 'updated_on', days => 1, days_inclusive => 1 } )->count; |
99 |
# $count = $patrons->filter_by_last_update( |
| 1118 |
is( $count, 5, '5 patrons have been updated before yesterday (inclusive)' ); |
100 |
# { timestamp_column_name => 'updated_on', days => 1, days_inclusive => 1 } )->count; |
| 1119 |
|
101 |
# is( $count, 5, '5 patrons have been updated before yesterday (inclusive)' ); |
| 1120 |
$count = $patrons->filter_by_last_update( |
102 |
# |
| 1121 |
{ timestamp_column_name => 'updated_on', days => 0 } )->count; |
103 |
# $count = $patrons->filter_by_last_update( |
| 1122 |
is( $count, 5, '5 patrons have been updated before today (exclusive)' ); |
104 |
# { timestamp_column_name => 'updated_on', days => 0 } )->count; |
| 1123 |
|
105 |
# is( $count, 5, '5 patrons have been updated before today (exclusive)' ); |
| 1124 |
$count = $patrons->filter_by_last_update( |
106 |
# |
| 1125 |
{ timestamp_column_name => 'updated_on', days => 0, days_inclusive => 1 } )->count; |
107 |
# $count = $patrons->filter_by_last_update( |
| 1126 |
is( $count, 6, '6 patrons have been updated before today (inclusive)' ); |
108 |
# { timestamp_column_name => 'updated_on', days => 0, days_inclusive => 1 } )->count; |
| 1127 |
|
109 |
# is( $count, 6, '6 patrons have been updated before today (inclusive)' ); |
|
|
110 |
# |
| 1128 |
$count = $patrons->filter_by_last_update( |
111 |
$count = $patrons->filter_by_last_update( |
| 1129 |
{ timestamp_column_name => 'updated_on', from => $now } )->count; |
112 |
{ timestamp_column_name => 'updated_on', from => $now } )->count; |
| 1130 |
is( $count, 1, '1 patron has been updated "from today" (inclusive)' ); |
113 |
is( $count, 1, '1 patron has been updated "from today" (inclusive)' ); |
|
Lines 1149-1154
subtest "filter_by_last_update" => sub {
Link Here
|
| 1149 |
->count; |
132 |
->count; |
| 1150 |
} |
133 |
} |
| 1151 |
catch { |
134 |
catch { |
|
|
135 |
|
| 136 |
use Data::Printer colored => 1; warn p $_; |
| 1152 |
ok( |
137 |
ok( |
| 1153 |
$_->isa( |
138 |
$_->isa( |
| 1154 |
'No exception raised, from and to parameters can take an iso formatted date' |
139 |
'No exception raised, from and to parameters can take an iso formatted date' |
|
Lines 1170-1279
subtest "filter_by_last_update" => sub {
Link Here
|
| 1170 |
|
155 |
|
| 1171 |
$schema->storage->txn_rollback; |
156 |
$schema->storage->txn_rollback; |
| 1172 |
}; |
157 |
}; |
| 1173 |
|
|
|
| 1174 |
subtest "from_api_mapping() tests" => sub { |
| 1175 |
|
| 1176 |
plan tests => 1; |
| 1177 |
|
| 1178 |
$schema->storage->txn_begin; |
| 1179 |
|
| 1180 |
my $cities_rs = Koha::Cities->new; |
| 1181 |
my $city = Koha::City->new; |
| 1182 |
|
| 1183 |
is_deeply( |
| 1184 |
$cities_rs->from_api_mapping, |
| 1185 |
$city->from_api_mapping |
| 1186 |
); |
| 1187 |
|
| 1188 |
$schema->storage->txn_rollback; |
| 1189 |
}; |
| 1190 |
|
| 1191 |
subtest 'prefetch_whitelist() tests' => sub { |
| 1192 |
|
| 1193 |
plan tests => 3; |
| 1194 |
|
| 1195 |
$schema->storage->txn_begin; |
| 1196 |
|
| 1197 |
my $biblios = Koha::Biblios->new; |
| 1198 |
|
| 1199 |
my $prefetch_whitelist = $biblios->prefetch_whitelist; |
| 1200 |
|
| 1201 |
ok( |
| 1202 |
exists $prefetch_whitelist->{orders}, |
| 1203 |
'Relationship matching method name is listed' |
| 1204 |
); |
| 1205 |
is( |
| 1206 |
$prefetch_whitelist->{orders}, |
| 1207 |
'Koha::Acquisition::Order', |
| 1208 |
'Guessed the non-standard object class correctly' |
| 1209 |
); |
| 1210 |
|
| 1211 |
is( |
| 1212 |
$prefetch_whitelist->{items}, |
| 1213 |
'Koha::Item', |
| 1214 |
'Guessed the standard object class correctly' |
| 1215 |
); |
| 1216 |
|
| 1217 |
$schema->storage->txn_rollback; |
| 1218 |
}; |
| 1219 |
|
| 1220 |
subtest 'empty() tests' => sub { |
| 1221 |
|
| 1222 |
plan tests => 6; |
| 1223 |
|
| 1224 |
$schema->storage->txn_begin; |
| 1225 |
|
| 1226 |
# Add a patron, we need at least 1 |
| 1227 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 1228 |
ok( Koha::Patrons->count > 0, 'There is at least one Koha::Patron on the resultset' ); |
| 1229 |
|
| 1230 |
my $empty = Koha::Patrons->new->empty; |
| 1231 |
is( ref($empty), 'Koha::Patrons', '->empty returns a Koha::Patrons iterator' ); |
| 1232 |
is( $empty->count, 0, 'The empty resultset is, well, empty :-D' ); |
| 1233 |
|
| 1234 |
my $new_rs = $empty->search({ borrowernumber => $patron->borrowernumber }); |
| 1235 |
|
| 1236 |
is( $new_rs->count, 0, 'Further chaining an empty resultset, returns an empty resultset' ); |
| 1237 |
|
| 1238 |
throws_ok |
| 1239 |
{ Koha::Patrons->empty; } |
| 1240 |
'Koha::Exceptions::Object::NotInstantiated', |
| 1241 |
'Exception thrown if not instantiated class'; |
| 1242 |
|
| 1243 |
is( "$@", 'Tried to access the \'empty\' method, but Koha::Patrons is not instantiated', 'Exception stringified correctly' ); |
| 1244 |
|
| 1245 |
$schema->storage->txn_rollback; |
| 1246 |
}; |
| 1247 |
|
| 1248 |
subtest 'delete() tests' => sub { |
| 1249 |
|
| 1250 |
plan tests => 2; |
| 1251 |
|
| 1252 |
$schema->storage->txn_begin; |
| 1253 |
|
| 1254 |
# Make sure no cities |
| 1255 |
warnings_are { Koha::Cities->delete }[], |
| 1256 |
"No warnings, no Koha::City->delete called as it doesn't exist"; |
| 1257 |
|
| 1258 |
# Mock Koha::City |
| 1259 |
my $mocked_city = Test::MockModule->new('Koha::City'); |
| 1260 |
$mocked_city->mock( |
| 1261 |
'delete', |
| 1262 |
sub { |
| 1263 |
shift->_result->delete; |
| 1264 |
warn "delete called!"; |
| 1265 |
} |
| 1266 |
); |
| 1267 |
|
| 1268 |
# Add two cities |
| 1269 |
$builder->build_object( { class => 'Koha::Cities' } ); |
| 1270 |
$builder->build_object( { class => 'Koha::Cities' } ); |
| 1271 |
|
| 1272 |
my $cities = Koha::Cities->search; |
| 1273 |
$cities->next; |
| 1274 |
warnings_are { $cities->delete } |
| 1275 |
[ "delete called!", "delete called!" ], |
| 1276 |
"No warnings, no Koha::City->delete called as it doesn't exist"; |
| 1277 |
|
| 1278 |
$schema->storage->txn_rollback; |
| 1279 |
}; |