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 326-332 subtest 'store() tests' => sub { Link Here
326
};
326
};
327
327
328
subtest 'delete() tests' => sub {
328
subtest 'delete() tests' => sub {
329
    plan tests => 3;
329
    plan tests => 2;
330
330
331
    $schema->storage->txn_begin;
331
    $schema->storage->txn_begin;
332
332
Lines 365-370 subtest 'delete() tests' => sub { Link Here
365
        'Koha::Booking->delete should have deleted the booking'
365
        'Koha::Booking->delete should have deleted the booking'
366
    );
366
    );
367
367
368
    $schema->storage->txn_rollback;
369
};
370
371
subtest 'edit() tests' => sub {
372
    plan tests => 1;
373
374
    $schema->storage->txn_begin;
375
376
    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
377
    my $biblio  = $builder->build_sample_biblio;
378
    my $item_1  = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
379
    my $start_0 = dt_from_string->subtract( days => 2 )->truncate( to => 'day' );
380
    my $end_0   = $start_0->clone->add( days => 6 );
381
382
    $item_1->bookable(1)->store;
383
384
    my $booking = Koha::Booking->new(
385
        {
386
            patron_id         => $patron->borrowernumber,
387
            biblio_id         => $biblio->biblionumber,
388
            item_id           => $item_1->itemnumber,
389
            pickup_library_id => $item_1->homebranch,
390
            start_date        => $start_0,
391
            end_date          => $end_0,
392
        }
393
    )->store;
394
395
    my $booking_to_edit = Koha::Bookings->find( $booking->booking_id );
396
    $booking_to_edit->edit( { status => 'completed' } );
397
398
    is(
399
        $booking_to_edit->unblessed->{status}, 'completed',
400
        'Koha::Booking->edit should edit booking with passed params'
401
    );
402
403
    $schema->storage->txn_rollback;
404
};
405
406
subtest 'cancel() tests' => sub {
407
    plan tests => 1;
408
409
    $schema->storage->txn_begin;
410
411
    my $patron                 = $builder->build_object( { class => 'Koha::Patrons' } );
412
    my $biblio                 = $builder->build_sample_biblio;
413
    my $item_1                 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
414
    my $start_0                = dt_from_string->subtract( days => 2 )->truncate( to => 'day' );
415
    my $end_0                  = $start_0->clone->add( days => 6 );
416
    my $original_notices_count = Koha::Notice::Messages->search(
417
        {
418
            letter_code    => 'BOOKING_CANCELLATION',
419
            borrowernumber => $patron->borrowernumber,
420
        }
421
    )->count;
422
423
    $item_1->bookable(1)->store;
424
425
    my $booking = Koha::Booking->new(
426
        {
427
            patron_id         => $patron->borrowernumber,
428
            biblio_id         => $biblio->biblionumber,
429
            item_id           => $item_1->itemnumber,
430
            pickup_library_id => $item_1->homebranch,
431
            start_date        => $start_0,
432
            end_date          => $end_0,
433
        }
434
    )->store;
435
436
    my $booking_to_cancel = Koha::Bookings->find( $booking->booking_id );
437
    $booking_to_cancel->cancel( { send_letter => 1 } );
438
368
    subtest 'notice trigger' => sub {
439
    subtest 'notice trigger' => sub {
369
        plan tests => 1;
440
        plan tests => 1;
370
441
Lines 377-385 subtest 'delete() tests' => sub { Link Here
377
        is(
448
        is(
378
            $post_notices_count,
449
            $post_notices_count,
379
            $original_notices_count + 1,
450
            $original_notices_count + 1,
380
            'Koha::Booking->delete should have enqueued a BOOKING_CANCELLATION email'
451
            'Koha::Booking->cancel should have enqueued a BOOKING_CANCELLATION email'
381
        );
452
        );
382
    };
453
    };
383
454
384
    $schema->storage->txn_rollback;
455
    $schema->storage->txn_rollback;
385
};
456
};
457
458
subtest 'set_status() tests' => sub {
459
    plan tests => 3;
460
461
    $schema->storage->txn_begin;
462
463
    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
464
    my $biblio  = $builder->build_sample_biblio;
465
    my $item_1  = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
466
    my $start_0 = dt_from_string->subtract( days => 2 )->truncate( to => 'day' );
467
    my $end_0   = $start_0->clone->add( days => 6 );
468
469
    $item_1->bookable(1)->store;
470
471
    my $booking = Koha::Booking->new(
472
        {
473
            patron_id         => $patron->borrowernumber,
474
            biblio_id         => $biblio->biblionumber,
475
            item_id           => $item_1->itemnumber,
476
            pickup_library_id => $item_1->homebranch,
477
            start_date        => $start_0,
478
            end_date          => $end_0,
479
            status            => 'new',
480
        }
481
    )->store;
482
483
    my $booking_with_old_status = Koha::Bookings->find( $booking->booking_id );
484
    $booking_with_old_status->set_status('completed');
485
    is( $booking_with_old_status->unblessed->{status}, 'completed', 'Booking status is now "completed"' );
486
487
    $booking_with_old_status->set_status('cancelled');
488
    is( $booking_with_old_status->unblessed->{status}, 'cancelled', 'Booking status is now "cancelled"' );
489
490
    subtest 'unauthorized status' => sub {
491
        plan tests => 2;
492
493
        eval { $booking_with_old_status->set_status('blah'); };
494
495
        if ($@) {
496
            like(
497
                $@, qr/Invalid status: blah/,
498
                'An error is raised for unauthorized status'
499
            );
500
        } else {
501
            fail('Expected an error but none was raised');
502
        }
503
504
        # Status unchanged
505
        is( $booking_with_old_status->unblessed->{status}, 'cancelled', 'Booking status is still "cancelled"' );
506
    };
507
508
    $schema->storage->txn_rollback;
509
};
(-)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