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

(-)a/t/db_dependent/api/v1/holds.t (-31 / +213 lines)
Lines 37-42 use Koha::Biblios; Link Here
37
use Koha::Biblioitems;
37
use Koha::Biblioitems;
38
use Koha::Items;
38
use Koha::Items;
39
use Koha::CirculationRules;
39
use Koha::CirculationRules;
40
use Koha::Patron::Debarments qw(AddDebarment);
40
41
41
my $schema  = Koha::Database->new->schema;
42
my $schema  = Koha::Database->new->schema;
42
my $builder = t::lib::TestBuilder->new();
43
my $builder = t::lib::TestBuilder->new();
Lines 723-784 subtest 'add() tests (maxreserves behaviour)' => sub { Link Here
723
724
724
subtest 'add() + can_place_holds() tests' => sub {
725
subtest 'add() + can_place_holds() tests' => sub {
725
726
726
    plan tests => 30;
727
    plan tests => 7;
727
728
728
    $schema->storage->txn_begin;
729
    $schema->storage->txn_begin;
729
730
730
    my $password = 'AbcdEFG123';
731
    my $password = 'AbcdEFG123';
731
732
732
    my $library = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
733
    my $library = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
733
    my $patron =
734
    my $staff   = $builder->build_object( { class => 'Koha::Patrons',   value => { flags           => 1 } } );
734
        $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library->id, flags => 1 } } );
735
    $staff->set_password( { password => $password, skip_validation => 1 } );
735
    $patron->set_password( { password => $password, skip_validation => 1 } );
736
    my $userid = $staff->userid;
736
    my $userid = $patron->userid;
737
737
738
    my $current_message;
738
    subtest "'expired' tests" => sub {
739
    my $patron_mock = Test::MockModule->new('Koha::Patron');
740
739
741
    $patron_mock->mock(
740
        plan tests => 5;
742
        'can_place_holds',
741
743
        sub {
742
        t::lib::Mocks::mock_preference( 'BlockExpiredPatronOpacActions', 'hold' );
744
            return Koha::Result::Boolean->new(0)->add_message( { type => 'error', message => $current_message } );
743
745
        }
744
        my $patron = $builder->build_object(
746
    );
745
            {
746
                class => 'Koha::Patrons',
747
                value => { dateexpiry => \'DATE_ADD(NOW(), INTERVAL -1 DAY)' }
748
            }
749
        );
750
751
        my $item = $builder->build_sample_item( { library => $library->id } );
752
753
        my $post_data = {
754
            patron_id         => $patron->id,
755
            pickup_library_id => $item->holdingbranch,
756
            item_id           => $item->id
757
        };
758
759
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
760
            ->status_is( 409, "Expected error related to 'expired'" )
761
            ->json_is( { error => "Hold cannot be placed. Reason: expired", error_code => 'expired' } );
747
762
748
    my $item = $builder->build_sample_item( { library => $library->id } );
763
        $t->post_ok( "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => 'expired' } => json => $post_data )
764
            ->status_is( 201, "Override works for 'expired'" );
765
    };
766
767
    subtest "'debt_limit' tests" => sub {
749
768
750
    my @messages = qw(
769
        plan tests => 7;
751
        expired
752
        debt_limit
753
        bad_address
754
        card_lost
755
        restricted
756
        hold_limit);
757
770
758
    foreach my $message (@messages) {
771
        # Add a patron, making sure it is not (yet) expired
772
        my $patron = $builder->build_object( { class => 'Koha::Patrons', } );
773
        $patron->account->add_debit( { amount => 10, interface => 'opac', type => 'ACCOUNT' } );
759
774
760
        $current_message = $message;
775
        my $item = $builder->build_sample_item( { library => $library->id } );
761
776
762
        my $post_data = {
777
        my $post_data = {
763
            patron_id         => $patron->id,
778
            patron_id         => $patron->id,
764
            biblio_id         => $item->biblio->id,
765
            pickup_library_id => $item->holdingbranch,
779
            pickup_library_id => $item->holdingbranch,
766
            item_id           => $item->id
780
            item_id           => $item->id
767
        };
781
        };
768
782
783
        t::lib::Mocks::mock_preference( 'maxoutstanding', '5' );
784
769
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
785
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
770
            ->status_is( 409, "Expected error related to '$message'" )
786
            ->status_is( 409, "Expected error related to 'debt_limit'" )
771
            ->json_is( { error => "Hold cannot be placed. Reason: $message", error_code => $message } );
787
            ->json_is( { error => "Hold cannot be placed. Reason: debt_limit", error_code => 'debt_limit' } );
772
788
773
        my $hold_id =
789
        my $hold_id =
774
            $t->post_ok(
790
            $t->post_ok(
775
            "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => $message } => json => $post_data )
791
            "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => 'debt_limit' } => json => $post_data )
776
            ->status_is( 201, "Override works for '$message'" )->tx->res->json->{hold_id};
792
            ->status_is( 201, "Override works for 'debt_limit'" )->tx->res->json->{hold_id};
777
793
778
        Koha::Holds->find($hold_id)->delete();
794
        Koha::Holds->find($hold_id)->delete();
779
    }
780
795
781
    $schema->storage->txn_rollback;
796
        t::lib::Mocks::mock_preference( 'maxoutstanding', undef );
797
798
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
799
            ->status_is( 201, "No 'maxoutstanding', can place holds" );
800
    };
801
802
    subtest "'bad_address' tests" => sub {
803
804
        plan tests => 5;
805
806
        # Add a patron, making sure it is not (yet) expired
807
        my $patron = $builder->build_object(
808
            {
809
                class => 'Koha::Patrons',
810
                value => {
811
                    gonenoaddress => 1,
812
                }
813
            }
814
        );
815
816
        my $item = $builder->build_sample_item( { library => $library->id } );
817
818
        my $post_data = {
819
            patron_id         => $patron->id,
820
            pickup_library_id => $item->holdingbranch,
821
            item_id           => $item->id
822
        };
823
824
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
825
            ->status_is( 409, "Expected error related to 'bad_address'" )
826
            ->json_is( { error => "Hold cannot be placed. Reason: bad_address", error_code => 'bad_address' } );
827
828
        $t->post_ok(
829
            "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => 'bad_address' } => json => $post_data )
830
            ->status_is( 201, "Override works for 'bad_address'" );
831
    };
832
833
    subtest "'card_lost' tests" => sub {
834
835
        plan tests => 5;
836
837
        # Add a patron, making sure it is not (yet) expired
838
        my $patron = $builder->build_object(
839
            {
840
                class => 'Koha::Patrons',
841
                value => {
842
                    lost => 1,
843
                }
844
            }
845
        );
846
847
        my $item = $builder->build_sample_item( { library => $library->id } );
848
849
        my $post_data = {
850
            patron_id         => $patron->id,
851
            pickup_library_id => $item->holdingbranch,
852
            item_id           => $item->id
853
        };
854
855
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
856
            ->status_is( 409, "Expected error related to 'card_lost'" )
857
            ->json_is( { error => "Hold cannot be placed. Reason: card_lost", error_code => 'card_lost' } );
858
859
        $t->post_ok( "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => 'card_lost' } => json => $post_data )
860
            ->status_is( 201, "Override works for 'card_lost'" );
861
    };
862
863
    subtest "'restricted' tests" => sub {
864
865
        plan tests => 5;
866
867
        # Add a patron, making sure it is not (yet) expired
868
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
869
        AddDebarment( { borrowernumber => $patron->borrowernumber } );
870
        $patron->discard_changes();
871
872
        my $item = $builder->build_sample_item( { library => $library->id } );
873
874
        my $post_data = {
875
            patron_id         => $patron->id,
876
            pickup_library_id => $item->holdingbranch,
877
            item_id           => $item->id
878
        };
879
880
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
881
            ->status_is( 409, "Expected error related to 'restricted'" )
882
            ->json_is( { error => "Hold cannot be placed. Reason: restricted", error_code => 'restricted' } );
883
884
        $t->post_ok(
885
            "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => 'restricted' } => json => $post_data )
886
            ->status_is( 201, "Override works for 'restricted'" );
887
    };
888
889
    subtest "'hold_limit' tests" => sub {
890
891
        plan tests => 7;
892
893
        # Add a patron, making sure it is not (yet) expired
894
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
895
896
        # Add a hold
897
        $builder->build_object( { class => 'Koha::Holds', value => { borrowernumber => $patron->borrowernumber } } );
898
899
        t::lib::Mocks::mock_preference( 'maxreserves', 1 );
900
901
        my $item = $builder->build_sample_item( { library => $library->id } );
902
903
        my $post_data = {
904
            patron_id         => $patron->id,
905
            pickup_library_id => $item->holdingbranch,
906
            item_id           => $item->id
907
        };
908
909
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
910
            ->status_is( 409, "Expected error related to 'hold_limit'" )
911
            ->json_is( { error => "Hold cannot be placed. Reason: hold_limit", error_code => 'hold_limit' } );
912
913
        my $hold_id = $t->post_ok(
914
            "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => 'hold_limit' } => json => $post_data )
915
            ->status_is( 201, "Override works for 'hold_limit'" )->tx->res->json->{hold_id};
916
917
        Koha::Holds->find($hold_id)->delete();
918
919
        t::lib::Mocks::mock_preference( 'maxreserves', undef );
920
921
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
922
            ->status_is( 201, "No 'max_reserves', can place holds" );
923
    };
924
925
    subtest "Multiple blocking conditions tests" => sub {
926
927
        plan tests => 8;
928
929
        t::lib::Mocks::mock_preference( 'BlockExpiredPatronOpacActions', 'hold' );
930
931
        my $patron = $builder->build_object(
932
            {
933
                class => 'Koha::Patrons',
934
                value => { dateexpiry => \'DATE_ADD(NOW(), INTERVAL -1 DAY)' }
935
            }
936
        );
937
938
        t::lib::Mocks::mock_preference( 'maxoutstanding', '5' );
939
        $patron->account->add_debit( { amount => 10, interface => 'opac', type => 'ACCOUNT' } );
940
941
        my $item = $builder->build_sample_item( { library => $library->id } );
942
943
        my $post_data = {
944
            patron_id         => $patron->id,
945
            pickup_library_id => $item->holdingbranch,
946
            item_id           => $item->id
947
        };
948
949
        ## NOTICE: this tests rely on 'expired' being checked before 'debt_limit' in Koha::Patron->can_place_holds
950
951
        # patron meets 'expired' and 'debt_limit'
952
        $t->post_ok( "//$userid:$password@/api/v1/holds" => json => $post_data )
953
            ->status_is( 409, "Expected error related to 'expired'" )
954
            ->json_is( { error => "Hold cannot be placed. Reason: expired", error_code => 'expired' } );
955
956
        # patron meets 'expired' AND 'debt_limit'
957
        $t->post_ok( "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => 'expired' } => json => $post_data )
958
            ->status_is( 409, "Expected error related to 'debt_limit'" )
959
            ->json_is( { error => "Hold cannot be placed. Reason: debt_limit", error_code => 'debt_limit' } );
960
961
        $t->post_ok(
962
            "//$userid:$password@/api/v1/holds" => { 'x-koha-override' => 'expired,debt_limit' } => json => $post_data )
963
            ->status_is( 201, "Override works for both 'expired' and 'debt_limit'" );
964
    };
782
};
965
};
783
966
784
subtest 'pickup_locations() tests' => sub {
967
subtest 'pickup_locations() tests' => sub {
785
- 

Return to bug 40254