Lines 20-26
Link Here
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
use utf8; |
21 |
use utf8; |
22 |
|
22 |
|
23 |
use Test::More tests => 6; |
23 |
use Test::More tests => 2; |
24 |
|
24 |
|
25 |
use Test::Exception; |
25 |
use Test::Exception; |
26 |
|
26 |
|
Lines 123-129
subtest 'Relation accessor tests' => sub {
Link Here
|
123 |
}; |
123 |
}; |
124 |
|
124 |
|
125 |
subtest 'store() tests' => sub { |
125 |
subtest 'store() tests' => sub { |
126 |
plan tests => 15; |
126 |
plan tests => 16; |
127 |
$schema->storage->txn_begin; |
127 |
$schema->storage->txn_begin; |
128 |
|
128 |
|
129 |
my $patron = $builder->build_object( { class => "Koha::Patrons" } ); |
129 |
my $patron = $builder->build_object( { class => "Koha::Patrons" } ); |
Lines 323-337
subtest 'store() tests' => sub {
Link Here
|
323 |
# ✓ Any (1) |--| |
323 |
# ✓ Any (1) |--| |
324 |
}; |
324 |
}; |
325 |
|
325 |
|
326 |
subtest 'modification notice trigger' => sub { |
326 |
subtest 'confirmation notice trigger' => sub { |
327 |
plan tests => 3; |
327 |
plan tests => 2; |
328 |
|
328 |
|
329 |
my $original_notices_count = Koha::Notice::Messages->search( |
329 |
my $original_notices_count = Koha::Notice::Messages->search( |
|
|
330 |
{ |
331 |
letter_code => 'BOOKING_CONFIRMATION', |
332 |
borrowernumber => $patron->borrowernumber, |
333 |
} |
334 |
)->count; |
335 |
|
336 |
# Reuse previous booking to produce a clash |
337 |
eval { $booking = Koha::Booking->new( $booking->unblessed )->store }; |
338 |
|
339 |
my $post_notices_count = Koha::Notice::Messages->search( |
340 |
{ |
341 |
letter_code => 'BOOKING_CONFIRMATION', |
342 |
borrowernumber => $patron->borrowernumber, |
343 |
} |
344 |
)->count; |
345 |
is( |
346 |
$post_notices_count, |
347 |
$original_notices_count, |
348 |
'Koha::Booking->store should not have enqueued a BOOKING_CONFIRMATION email if booking creation fails' |
349 |
); |
350 |
|
351 |
$start_1 = dt_from_string->add( months => 1 )->truncate( to => 'day' ); |
352 |
$end_1 = $start_1->clone()->add( days => 1 ); |
353 |
|
354 |
$booking = Koha::Booking->new( |
355 |
{ |
356 |
patron_id => $patron->borrowernumber, |
357 |
biblio_id => $biblio->biblionumber, |
358 |
pickup_library_id => $item_2->homebranch, |
359 |
start_date => $start_1->datetime(q{ }), |
360 |
end_date => $end_1->datetime(q{ }), |
361 |
} |
362 |
)->store; |
363 |
|
364 |
$post_notices_count = Koha::Notice::Messages->search( |
365 |
{ |
366 |
letter_code => 'BOOKING_CONFIRMATION', |
367 |
borrowernumber => $patron->borrowernumber, |
368 |
} |
369 |
)->count; |
370 |
is( |
371 |
$post_notices_count, |
372 |
$original_notices_count + 1, |
373 |
'Koha::Booking->store should have enqueued a BOOKING_CONFIRMATION email for a new booking' |
374 |
); |
375 |
}; |
376 |
|
377 |
subtest 'modification/cancellation notice triggers' => sub { |
378 |
plan tests => 5; |
379 |
|
380 |
my $original_modification_notices_count = Koha::Notice::Messages->search( |
330 |
{ |
381 |
{ |
331 |
letter_code => 'BOOKING_MODIFICATION', |
382 |
letter_code => 'BOOKING_MODIFICATION', |
332 |
borrowernumber => $patron->borrowernumber, |
383 |
borrowernumber => $patron->borrowernumber, |
333 |
} |
384 |
} |
334 |
)->count; |
385 |
)->count; |
|
|
386 |
my $original_cancellation_notices_count = Koha::Notice::Messages->search( |
387 |
{ |
388 |
letter_code => 'BOOKING_CANCELLATION', |
389 |
borrowernumber => $patron->borrowernumber, |
390 |
} |
391 |
)->count; |
335 |
|
392 |
|
336 |
$start_1 = dt_from_string->add( months => 1 )->truncate( to => 'day' ); |
393 |
$start_1 = dt_from_string->add( months => 1 )->truncate( to => 'day' ); |
337 |
$end_1 = $start_1->clone()->add( days => 1 ); |
394 |
$end_1 = $start_1->clone()->add( days => 1 ); |
Lines 347-361
subtest 'store() tests' => sub {
Link Here
|
347 |
} |
404 |
} |
348 |
)->store; |
405 |
)->store; |
349 |
|
406 |
|
350 |
my $post_notices_count = Koha::Notice::Messages->search( |
407 |
my $post_modification_notices_count = Koha::Notice::Messages->search( |
351 |
{ |
408 |
{ |
352 |
letter_code => 'BOOKING_MODIFICATION', |
409 |
letter_code => 'BOOKING_MODIFICATION', |
353 |
borrowernumber => $patron->borrowernumber, |
410 |
borrowernumber => $patron->borrowernumber, |
354 |
} |
411 |
} |
355 |
)->count; |
412 |
)->count; |
356 |
is( |
413 |
is( |
357 |
$post_notices_count, |
414 |
$post_modification_notices_count, |
358 |
$original_notices_count, |
415 |
$original_modification_notices_count, |
359 |
'Koha::Booking->store should not have enqueued a BOOKING_MODIFICATION email for a new booking' |
416 |
'Koha::Booking->store should not have enqueued a BOOKING_MODIFICATION email for a new booking' |
360 |
); |
417 |
); |
361 |
|
418 |
|
Lines 367-381
subtest 'store() tests' => sub {
Link Here
|
367 |
} |
424 |
} |
368 |
); |
425 |
); |
369 |
|
426 |
|
370 |
$post_notices_count = Koha::Notice::Messages->search( |
427 |
$post_modification_notices_count = Koha::Notice::Messages->search( |
371 |
{ |
428 |
{ |
372 |
letter_code => 'BOOKING_MODIFICATION', |
429 |
letter_code => 'BOOKING_MODIFICATION', |
373 |
borrowernumber => $patron->borrowernumber, |
430 |
borrowernumber => $patron->borrowernumber, |
374 |
} |
431 |
} |
375 |
)->count; |
432 |
)->count; |
376 |
is( |
433 |
is( |
377 |
$post_notices_count, |
434 |
$post_modification_notices_count, |
378 |
$original_notices_count, |
435 |
$original_modification_notices_count, |
379 |
'Koha::Booking->store should not have enqueued a BOOKING_MODIFICATION email for a booking with modified item_id' |
436 |
'Koha::Booking->store should not have enqueued a BOOKING_MODIFICATION email for a booking with modified item_id' |
380 |
); |
437 |
); |
381 |
|
438 |
|
Lines 386-400
subtest 'store() tests' => sub {
Link Here
|
386 |
); |
443 |
); |
387 |
|
444 |
|
388 |
# start_date, end_date and pickup_library_id should behave identical |
445 |
# start_date, end_date and pickup_library_id should behave identical |
389 |
$post_notices_count = Koha::Notice::Messages->search( |
446 |
$post_modification_notices_count = Koha::Notice::Messages->search( |
390 |
{ |
447 |
{ |
391 |
letter_code => 'BOOKING_MODIFICATION', |
448 |
letter_code => 'BOOKING_MODIFICATION', |
392 |
borrowernumber => $patron->borrowernumber, |
449 |
borrowernumber => $patron->borrowernumber, |
393 |
} |
450 |
} |
394 |
)->count; |
451 |
)->count; |
395 |
is( |
452 |
is( |
396 |
$post_notices_count, |
453 |
$post_modification_notices_count, |
397 |
$original_notices_count + 1, |
454 |
$original_modification_notices_count + 1, |
398 |
'Koha::Booking->store should have enqueued a BOOKING_MODIFICATION email for a booking with modified start_date' |
455 |
'Koha::Booking->store should have enqueued a BOOKING_MODIFICATION email for a booking with modified start_date' |
399 |
); |
456 |
); |
400 |
|
457 |
|
Lines 403-643
subtest 'store() tests' => sub {
Link Here
|
403 |
end_date => $end_1->clone()->add( days => 1 )->datetime(q{ }), |
460 |
end_date => $end_1->clone()->add( days => 1 )->datetime(q{ }), |
404 |
} |
461 |
} |
405 |
); |
462 |
); |
406 |
}; |
|
|
407 |
|
408 |
subtest 'confirmation notice trigger' => sub { |
409 |
plan tests => 2; |
410 |
|
463 |
|
411 |
my $original_notices_count = Koha::Notice::Messages->search( |
464 |
$booking->update( |
412 |
{ |
465 |
{ |
413 |
letter_code => 'BOOKING_CONFIRMATION', |
466 |
status => 'completed', |
414 |
borrowernumber => $patron->borrowernumber, |
|
|
415 |
} |
467 |
} |
416 |
)->count; |
468 |
); |
417 |
|
|
|
418 |
# Reuse previous booking to produce a clash |
419 |
eval { $booking = Koha::Booking->new( $booking->unblessed )->store }; |
420 |
|
469 |
|
421 |
my $post_notices_count = Koha::Notice::Messages->search( |
470 |
my $post_cancellation_notices_count = Koha::Notice::Messages->search( |
422 |
{ |
471 |
{ |
423 |
letter_code => 'BOOKING_CONFIRMATION', |
472 |
letter_code => 'BOOKING_CANCELLATION', |
424 |
borrowernumber => $patron->borrowernumber, |
473 |
borrowernumber => $patron->borrowernumber, |
425 |
} |
474 |
} |
426 |
)->count; |
475 |
)->count; |
427 |
is( |
476 |
is( |
428 |
$post_notices_count, |
477 |
$post_cancellation_notices_count, |
429 |
$original_notices_count, |
478 |
$original_cancellation_notices_count, |
430 |
'Koha::Booking->store should not have enqueued a BOOKING_CONFIRMATION email if booking creation fails' |
479 |
'Koha::Booking->store should NOT have enqueued a BOOKING_CANCELLATION email for a booking status change that is not a "cancellation"' |
431 |
); |
480 |
); |
432 |
|
481 |
|
433 |
$start_1 = dt_from_string->add( months => 1 )->truncate( to => 'day' ); |
482 |
$booking->update( |
434 |
$end_1 = $start_1->clone()->add( days => 1 ); |
|
|
435 |
|
436 |
$booking = Koha::Booking->new( |
437 |
{ |
438 |
patron_id => $patron->borrowernumber, |
439 |
biblio_id => $biblio->biblionumber, |
440 |
pickup_library_id => $item_2->homebranch, |
441 |
start_date => $start_1->datetime(q{ }), |
442 |
end_date => $end_1->datetime(q{ }), |
443 |
} |
444 |
)->store; |
445 |
|
446 |
$post_notices_count = Koha::Notice::Messages->search( |
447 |
{ |
483 |
{ |
448 |
letter_code => 'BOOKING_CONFIRMATION', |
484 |
status => 'cancelled', |
449 |
borrowernumber => $patron->borrowernumber, |
|
|
450 |
} |
485 |
} |
451 |
)->count; |
|
|
452 |
is( |
453 |
$post_notices_count, |
454 |
$original_notices_count + 1, |
455 |
'Koha::Booking->store should have enqueued a BOOKING_CONFIRMATION email for a new booking' |
456 |
); |
486 |
); |
457 |
}; |
|
|
458 |
|
459 |
$schema->storage->txn_rollback; |
460 |
}; |
461 |
|
462 |
subtest 'delete() tests' => sub { |
463 |
plan tests => 2; |
464 |
|
465 |
$schema->storage->txn_begin; |
466 |
|
467 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
468 |
t::lib::Mocks::mock_userenv( { patron => $patron } ); |
469 |
my $biblio = $builder->build_sample_biblio; |
470 |
my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
471 |
my $start_0 = dt_from_string->subtract( days => 2 )->truncate( to => 'day' ); |
472 |
my $end_0 = $start_0->clone->add( days => 6 ); |
473 |
my $original_notices_count = Koha::Notice::Messages->search( |
474 |
{ |
475 |
letter_code => 'BOOKING_CANCELLATION', |
476 |
borrowernumber => $patron->borrowernumber, |
477 |
} |
478 |
)->count; |
479 |
|
480 |
$item_1->bookable(1)->store; |
481 |
|
482 |
my $booking = Koha::Booking->new( |
483 |
{ |
484 |
patron_id => $patron->borrowernumber, |
485 |
biblio_id => $biblio->biblionumber, |
486 |
item_id => $item_1->itemnumber, |
487 |
pickup_library_id => $item_1->homebranch, |
488 |
start_date => $start_0, |
489 |
end_date => $end_0 |
490 |
} |
491 |
)->store; |
492 |
|
493 |
my $deleted = $booking->delete; |
494 |
is( |
495 |
ref($deleted), 'Koha::Booking', |
496 |
'Koha::Booking->delete should return the Koha::Booking object if the booking has been correctly deleted' |
497 |
); |
498 |
is( |
499 |
Koha::Bookings->search( { booking_id => $booking->booking_id } )->count, 0, |
500 |
'Koha::Booking->delete should have deleted the booking' |
501 |
); |
502 |
|
503 |
$schema->storage->txn_rollback; |
504 |
}; |
505 |
|
506 |
subtest 'edit() tests' => sub { |
507 |
plan tests => 1; |
508 |
|
509 |
$schema->storage->txn_begin; |
510 |
|
511 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
512 |
my $biblio = $builder->build_sample_biblio; |
513 |
my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
514 |
my $start_0 = dt_from_string->subtract( days => 2 )->truncate( to => 'day' ); |
515 |
my $end_0 = $start_0->clone->add( days => 6 ); |
516 |
|
517 |
$item_1->bookable(1)->store; |
518 |
|
519 |
my $booking = Koha::Booking->new( |
520 |
{ |
521 |
patron_id => $patron->borrowernumber, |
522 |
biblio_id => $biblio->biblionumber, |
523 |
item_id => $item_1->itemnumber, |
524 |
pickup_library_id => $item_1->homebranch, |
525 |
start_date => $start_0, |
526 |
end_date => $end_0, |
527 |
} |
528 |
)->store; |
529 |
|
530 |
my $booking_to_edit = Koha::Bookings->find( $booking->booking_id ); |
531 |
$booking_to_edit->edit( { status => 'completed' } ); |
532 |
|
487 |
|
533 |
is( |
488 |
$post_cancellation_notices_count = Koha::Notice::Messages->search( |
534 |
$booking_to_edit->unblessed->{status}, 'completed', |
|
|
535 |
'Koha::Booking->edit should edit booking with passed params' |
536 |
); |
537 |
|
538 |
$schema->storage->txn_rollback; |
539 |
}; |
540 |
|
541 |
subtest 'cancel() tests' => sub { |
542 |
plan tests => 1; |
543 |
|
544 |
$schema->storage->txn_begin; |
545 |
|
546 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
547 |
my $biblio = $builder->build_sample_biblio; |
548 |
my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
549 |
my $start_0 = dt_from_string->subtract( days => 2 )->truncate( to => 'day' ); |
550 |
my $end_0 = $start_0->clone->add( days => 6 ); |
551 |
my $original_notices_count = Koha::Notice::Messages->search( |
552 |
{ |
553 |
letter_code => 'BOOKING_CANCELLATION', |
554 |
borrowernumber => $patron->borrowernumber, |
555 |
} |
556 |
)->count; |
557 |
|
558 |
$item_1->bookable(1)->store; |
559 |
|
560 |
my $booking = Koha::Booking->new( |
561 |
{ |
562 |
patron_id => $patron->borrowernumber, |
563 |
biblio_id => $biblio->biblionumber, |
564 |
item_id => $item_1->itemnumber, |
565 |
pickup_library_id => $item_1->homebranch, |
566 |
start_date => $start_0, |
567 |
end_date => $end_0, |
568 |
} |
569 |
)->store; |
570 |
|
571 |
my $booking_to_cancel = Koha::Bookings->find( $booking->booking_id ); |
572 |
$booking_to_cancel->cancel( { send_letter => 1 } ); |
573 |
|
574 |
subtest 'notice trigger' => sub { |
575 |
plan tests => 1; |
576 |
|
577 |
my $post_notices_count = Koha::Notice::Messages->search( |
578 |
{ |
489 |
{ |
579 |
letter_code => 'BOOKING_CANCELLATION', |
490 |
letter_code => 'BOOKING_CANCELLATION', |
580 |
borrowernumber => $patron->borrowernumber, |
491 |
borrowernumber => $patron->borrowernumber, |
581 |
} |
492 |
} |
582 |
)->count; |
493 |
)->count; |
583 |
is( |
494 |
is( |
584 |
$post_notices_count, |
495 |
$post_cancellation_notices_count, |
585 |
$original_notices_count + 1, |
496 |
$original_cancellation_notices_count + 1, |
586 |
'Koha::Booking->cancel should have enqueued a BOOKING_CANCELLATION email' |
497 |
'Koha::Booking->store should have enqueued a BOOKING_CANCELLATION email for a booking status change that is a "cancellation"' |
587 |
); |
498 |
); |
588 |
}; |
499 |
}; |
589 |
|
500 |
|
590 |
$schema->storage->txn_rollback; |
501 |
subtest 'status change exception' => sub { |
591 |
}; |
|
|
592 |
|
593 |
subtest 'set_status() tests' => sub { |
594 |
plan tests => 3; |
595 |
|
596 |
$schema->storage->txn_begin; |
597 |
|
598 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
599 |
my $biblio = $builder->build_sample_biblio; |
600 |
my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
601 |
my $start_0 = dt_from_string->subtract( days => 2 )->truncate( to => 'day' ); |
602 |
my $end_0 = $start_0->clone->add( days => 6 ); |
603 |
|
604 |
$item_1->bookable(1)->store; |
605 |
|
606 |
my $booking = Koha::Booking->new( |
607 |
{ |
608 |
patron_id => $patron->borrowernumber, |
609 |
biblio_id => $biblio->biblionumber, |
610 |
item_id => $item_1->itemnumber, |
611 |
pickup_library_id => $item_1->homebranch, |
612 |
start_date => $start_0, |
613 |
end_date => $end_0, |
614 |
status => 'new', |
615 |
} |
616 |
)->store; |
617 |
|
618 |
my $booking_with_old_status = Koha::Bookings->find( $booking->booking_id ); |
619 |
$booking_with_old_status->_set_status('completed'); |
620 |
is( $booking_with_old_status->unblessed->{status}, 'completed', 'Booking status is now "completed"' ); |
621 |
|
622 |
$booking_with_old_status->_set_status('cancelled'); |
623 |
is( $booking_with_old_status->unblessed->{status}, 'cancelled', 'Booking status is now "cancelled"' ); |
624 |
|
625 |
subtest 'unauthorized status' => sub { |
626 |
plan tests => 2; |
502 |
plan tests => 2; |
627 |
|
503 |
|
628 |
eval { $booking_with_old_status->_set_status('blah'); }; |
504 |
$booking->discard_changes; |
629 |
|
505 |
my $status = $booking->status; |
630 |
if ($@) { |
506 |
throws_ok { $booking->update( { status => 'blah' } ) } 'Koha::Exceptions::Object::BadValue', |
631 |
like( |
507 |
'Throws exception when passed booking status would fail enum constraint'; |
632 |
$@, qr/Invalid status: blah/, |
|
|
633 |
'An error is raised for unauthorized status' |
634 |
); |
635 |
} else { |
636 |
fail('Expected an error but none was raised'); |
637 |
} |
638 |
|
508 |
|
639 |
# Status unchanged |
509 |
# Status unchanged |
640 |
is( $booking_with_old_status->unblessed->{status}, 'cancelled', 'Booking status is still "cancelled"' ); |
510 |
$booking->discard_changes; |
|
|
511 |
is( $booking->status, $status, 'Booking status is unchanged' ); |
641 |
}; |
512 |
}; |
642 |
|
513 |
|
643 |
$schema->storage->txn_rollback; |
514 |
$schema->storage->txn_rollback; |