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

(-)a/t/db_dependent/Circulation.t (-2 / +126 lines)
Lines 19-25 use Modern::Perl; Link Here
19
use utf8;
19
use utf8;
20
20
21
use Test::NoWarnings;
21
use Test::NoWarnings;
22
use Test::More tests => 76;
22
use Test::More tests => 77;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Deep qw( cmp_deeply );
25
use Test::Deep qw( cmp_deeply );
Lines 6882-6887 subtest 'AddIssue records staff who checked out item if appropriate' => sub { Link Here
6882
    );
6882
    );
6883
};
6883
};
6884
6884
6885
subtest 'AddIssue | booking status handling' => sub {
6886
    plan tests => 5;
6887
6888
    my $schema = Koha::Database->schema;
6889
    $schema->storage->txn_begin;
6890
6891
    my $patron1        = $builder->build_object( { class => 'Koha::Patrons' } );
6892
    my $patron2        = $builder->build_object( { class => 'Koha::Patrons' } );
6893
    my $pickup_library = $builder->build_object( { class => 'Koha::Libraries' } );
6894
    my $item           = $builder->build_sample_item( { bookable => 1 } );
6895
6896
    # Test 1: Patron checks out item with their own booking - should mark as completed
6897
    my $booking1 = Koha::Booking->new(
6898
        {
6899
            patron_id         => $patron1->borrowernumber,
6900
            pickup_library_id => $pickup_library->branchcode,
6901
            item_id           => $item->itemnumber,
6902
            biblio_id         => $item->biblio->biblionumber,
6903
            start_date        => dt_from_string(),
6904
            end_date          => dt_from_string()->add( days => 7 ),
6905
        }
6906
    )->store();
6907
6908
    my $issue1 = AddIssue( $patron1, $item->barcode, dt_from_string()->add( days => 7 ) );
6909
    $booking1->discard_changes();
6910
6911
    is( $booking1->status, 'completed', "Patron's own booking marked as completed when item checked out" );
6912
6913
    # Clean up for next test
6914
    AddReturn( $item->barcode, $pickup_library->branchcode );
6915
    $booking1->delete();
6916
6917
    # Test 2: Another patron checks out item with different patron's booking overlapping actual booking period - should cancel booking
6918
    my $booking2 = Koha::Booking->new(
6919
        {
6920
            patron_id         => $patron1->borrowernumber,
6921
            pickup_library_id => $pickup_library->branchcode,
6922
            item_id           => $item->itemnumber,
6923
            biblio_id         => $item->biblio->biblionumber,
6924
            start_date        => dt_from_string()->add( days => 1 ),
6925
            end_date          => dt_from_string()->add( days => 5 ),
6926
        }
6927
    )->store();
6928
6929
    my $issue2 = AddIssue( $patron2, $item->barcode, dt_from_string()->add( days => 3 ) );
6930
    $booking2->discard_changes();
6931
6932
    is(
6933
        $booking2->status, 'cancelled',
6934
        "Another patron's booking cancelled when checkout overlaps actual booking period"
6935
    );
6936
6937
    # Clean up for next test
6938
    AddReturn( $item->barcode, $pickup_library->branchcode );
6939
    $booking2->delete();
6940
6941
    # Test 3: Another patron checks out item - checkout period only in lead period - should NOT cancel booking
6942
    # First set up a lead period rule
6943
    Koha::CirculationRules->set_rules(
6944
        {
6945
            branchcode => '*',
6946
            itemtype   => $item->effective_itemtype,
6947
            rules      => {
6948
                bookings_lead_period => 3,
6949
            },
6950
        }
6951
    );
6952
6953
    my $booking3 = Koha::Booking->new(
6954
        {
6955
            patron_id         => $patron1->borrowernumber,
6956
            pickup_library_id => $pickup_library->branchcode,
6957
            item_id           => $item->itemnumber,
6958
            biblio_id         => $item->biblio->biblionumber,
6959
            start_date        => dt_from_string()->add( days => 5 ),
6960
            end_date          => dt_from_string()->add( days => 10 ),
6961
        }
6962
    )->store();
6963
6964
    # Issue that would return before booking starts (only conflicts due to lead period)
6965
    my $issue3 = AddIssue( $patron2, $item->barcode, dt_from_string()->add( days => 4 ) );
6966
    $booking3->discard_changes();
6967
6968
    is(
6969
        $booking3->status, 'new',
6970
        "Another patron's booking NOT cancelled when checkout only conflicts with lead period"
6971
    );
6972
6973
    # Clean up for next test
6974
    AddReturn( $item->barcode, $pickup_library->branchcode );
6975
    $booking3->delete();
6976
6977
    # Test 4: Edge case - checkout period starts before booking and ends after booking (encompasses booking)
6978
    my $booking4 = Koha::Booking->new(
6979
        {
6980
            patron_id         => $patron1->borrowernumber,
6981
            pickup_library_id => $pickup_library->branchcode,
6982
            item_id           => $item->itemnumber,
6983
            biblio_id         => $item->biblio->biblionumber,
6984
            start_date        => dt_from_string()->add( days => 2 ),
6985
            end_date          => dt_from_string()->add( days => 4 ),
6986
        }
6987
    )->store();
6988
6989
    # Issue that encompasses the entire booking period
6990
    my $issue4 = AddIssue( $patron2, $item->barcode, dt_from_string()->add( days => 6 ) );
6991
    $booking4->discard_changes();
6992
6993
    is(
6994
        $booking4->status, 'cancelled',
6995
        "Another patron's booking cancelled when checkout encompasses entire booking period"
6996
    );
6997
6998
    # Clean up for next test
6999
    AddReturn( $item->barcode, $pickup_library->branchcode );
7000
    $booking4->delete();
7001
7002
    # Test 5: No booking found - should not cause errors
7003
    my $issue5 = AddIssue( $patron1, $item->barcode, dt_from_string()->add( days => 7 ) );
7004
7005
    ok( $issue5, "AddIssue works normally when no booking is found" );
7006
7007
    $schema->storage->txn_rollback;
7008
};
7009
6885
subtest "Item's onloan value should be set if checked out item is checked out to a different patron" => sub {
7010
subtest "Item's onloan value should be set if checked out item is checked out to a different patron" => sub {
6886
    plan tests => 2;
7011
    plan tests => 2;
6887
7012
6888
- 

Return to bug 40296