View | Details | Raw Unified | Return to bug 38175
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Booking.t (-3 / +127 lines)
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 => 3;
23
use Test::More tests => 6;
24
24
25
use Test::Exception;
25
use Test::Exception;
26
26
Lines 460-466 subtest 'store() tests' => sub { Link Here
460
};
460
};
461
461
462
subtest 'delete() tests' => sub {
462
subtest 'delete() tests' => sub {
463
    plan tests => 3;
463
    plan tests => 2;
464
464
465
    $schema->storage->txn_begin;
465
    $schema->storage->txn_begin;
466
466
Lines 500-505 subtest 'delete() tests' => sub { Link Here
500
        'Koha::Booking->delete should have deleted the booking'
500
        'Koha::Booking->delete should have deleted the booking'
501
    );
501
    );
502
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
533
    is(
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
503
    subtest 'notice trigger' => sub {
574
    subtest 'notice trigger' => sub {
504
        plan tests => 1;
575
        plan tests => 1;
505
576
Lines 512-520 subtest 'delete() tests' => sub { Link Here
512
        is(
583
        is(
513
            $post_notices_count,
584
            $post_notices_count,
514
            $original_notices_count + 1,
585
            $original_notices_count + 1,
515
            'Koha::Booking->delete should have enqueued a BOOKING_CANCELLATION email'
586
            'Koha::Booking->cancel should have enqueued a BOOKING_CANCELLATION email'
516
        );
587
        );
517
    };
588
    };
518
589
519
    $schema->storage->txn_rollback;
590
    $schema->storage->txn_rollback;
520
};
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;
627
628
        eval { $booking_with_old_status->set_status('blah'); };
629
630
        if ($@) {
631
            like(
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
639
        # Status unchanged
640
        is( $booking_with_old_status->unblessed->{status}, 'cancelled', 'Booking status is still "cancelled"' );
641
    };
642
643
    $schema->storage->txn_rollback;
644
};
(-)a/t/db_dependent/api/v1/bookings.t (-2 / +66 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 5;
20
use Test::More tests => 6;
21
use Test::Mojo;
21
use Test::Mojo;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 437-439 subtest 'delete() tests' => sub { Link Here
437
437
438
    $schema->storage->txn_rollback;
438
    $schema->storage->txn_rollback;
439
};
439
};
440
- 
440
441
subtest 'patch() tests' => sub {
442
443
    plan tests => 5;
444
445
    $schema->storage->txn_begin;
446
447
    my $librarian = $builder->build_object(
448
        {
449
            class => 'Koha::Patrons',
450
            value => { flags => 0 }     # no additional permissions
451
        }
452
    );
453
    $builder->build(
454
        {
455
            source => 'UserPermission',
456
            value  => {
457
                borrowernumber => $librarian->borrowernumber,
458
                module_bit     => 1,
459
                code           => 'manage_bookings',
460
            },
461
        }
462
    );
463
    my $password = 'thePassword123';
464
    $librarian->set_password( { password => $password, skip_validation => 1 } );
465
    my $userid = $librarian->userid;
466
467
    my $patron = $builder->build_object(
468
        {
469
            class => 'Koha::Patrons',
470
            value => { flags => 0 }
471
        }
472
    );
473
474
    $patron->set_password( { password => $password, skip_validation => 1 } );
475
    my $unauth_userid = $patron->userid;
476
477
    my $booking_id = $builder->build_object( { class => 'Koha::Bookings' } )->id;
478
479
    # Unauthorized attempt to partial update via PATCH
480
    $t->patch_ok(
481
        "//$unauth_userid:$password@/api/v1/bookings/$booking_id" => json => { status => 'cancelled' }
482
    )->status_is(403);
483
484
    my $biblio         = $builder->build_sample_biblio;
485
    my $item           = $builder->build_sample_item( { bookable => 1, biblionumber => $biblio->id } );
486
    my $pickup_library = $builder->build_object( { class => 'Koha::Libraries' } );
487
488
    # Authorized attempt to write invalid data
489
    my $booking_with_invalid_field = {
490
        blah => "Booking Blah",
491
    };
492
493
    $t->patch_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_invalid_field )
494
        ->status_is(400)->json_is(
495
        "/errors" => [
496
            {
497
                message => "Properties not allowed: blah.",
498
                path    => "/body"
499
            }
500
        ]
501
        );
502
503
    $schema->storage->txn_rollback;
504
};

Return to bug 38175