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

(-)a/t/db_dependent/api/v1/patrons.t (-2 / +123 lines)
Lines 707-713 subtest 'update() tests' => sub { Link Here
707
    $schema->storage->txn_rollback;
707
    $schema->storage->txn_rollback;
708
708
709
    subtest 'librarian access tests' => sub {
709
    subtest 'librarian access tests' => sub {
710
        plan tests => 43;
710
711
        plan tests => 44;
711
712
712
        $schema->storage->txn_begin;
713
        $schema->storage->txn_begin;
713
714
Lines 900-905 subtest 'update() tests' => sub { Link Here
900
          ->json_is( '/date_of_birth' => $newpatron->{ date_of_birth }, 'Date field set (Bug 28585)' )
901
          ->json_is( '/date_of_birth' => $newpatron->{ date_of_birth }, 'Date field set (Bug 28585)' )
901
          ->json_is( '/last_seen'     => $newpatron->{ last_seen }, 'Date-time field set (Bug 28585)' );
902
          ->json_is( '/last_seen'     => $newpatron->{ last_seen }, 'Date-time field set (Bug 28585)' );
902
903
904
        subtest "extended_attributes tests" => sub {
905
906
            plan tests => 26;
907
908
            my $attr_type_repeatable = $builder->build_object(
909
                {
910
                    class => 'Koha::Patron::Attribute::Types',
911
                    value => { repeatable => 1, unique_id => 0, mandatory => 0, category_code => undef }
912
                }
913
            );
914
915
            my $attr_type_unique = $builder->build_object(
916
                {
917
                    class => 'Koha::Patron::Attribute::Types',
918
                    value => { repeatable => 0, unique_id => 1, mandatory => 0, category_code => undef }
919
                }
920
            );
921
922
            my $attr_type_mandatory = $builder->build_object(
923
                {
924
                    class => 'Koha::Patron::Attribute::Types',
925
                    value => { repeatable => 0, unique_id => 0, mandatory => 1, category_code => undef }
926
                }
927
            );
928
929
            my $deleted_attr_type = $builder->build_object( { class => 'Koha::Patron::Attribute::Types' } );
930
            my $deleted_attr_code = $deleted_attr_type->code;
931
            $deleted_attr_type->delete;
932
933
            # Make the mandatory attribute mandatory for the patron
934
            $builder->build(
935
                {
936
                    source => 'BorrowerAttributeTypesBranch',
937
                    value  => {
938
                        bat_code     => $attr_type_mandatory->code,
939
                        b_branchcode => undef,
940
                    }
941
                }
942
            );
943
944
            $newpatron->{extended_attributes} = [
945
                { type => $deleted_attr_code, value => 'potato' },
946
            ];
947
948
            $t->put_ok( "//$userid:$password@/api/v1/patrons/"
949
                    . $superlibrarian->borrowernumber => { 'x-koha-embed' => 'extended_attributes' } => json =>
950
                    $newpatron )->status_is(400)
951
                ->json_is( '/error'      => 'Tried to use an invalid attribute type. type=' . $deleted_attr_code )
952
                ->json_is( '/error_code' => 'invalid_attribute_type' );
953
954
            # Add a 'unique' attribute to force failure
955
            my $unique_attr = $builder->build_object(
956
                { class => 'Koha::Patron::Attributes', value => { code => $attr_type_unique->code } } );
957
958
            $newpatron->{extended_attributes} = [
959
                { type => $attr_type_repeatable->code, value => 'a' },
960
                { type => $attr_type_repeatable->code, value => 'b' },
961
                { type => $attr_type_mandatory->code,  value => 'thing' },
962
                { type => $attr_type_unique->code,     value => $unique_attr->attribute }
963
            ];
964
965
            $t->put_ok( "//$userid:$password@/api/v1/patrons/"
966
                    . $superlibrarian->borrowernumber => { 'x-koha-embed' => 'extended_attributes' } => json =>
967
                    $newpatron )->status_is(400)
968
                ->json_is( '/error' => 'Your action breaks a unique constraint on the attribute. type='
969
                    . $attr_type_unique->code
970
                    . ' value='
971
                    . $unique_attr->attribute )->json_is( '/error_code' => 'attribute_not_unique' );
972
973
            $newpatron->{extended_attributes} = [
974
                { type => $attr_type_repeatable->code, value => 'a' },
975
                { type => $attr_type_repeatable->code, value => 'b' },
976
                { type => $attr_type_mandatory->code,  value => 'thing' },
977
                { type => $attr_type_unique->code,     value => $unique_attr->attribute }
978
            ];
979
980
            $t->put_ok( "//$userid:$password@/api/v1/patrons/"
981
                    . $superlibrarian->borrowernumber => { 'x-koha-embed' => 'extended_attributes' } => json =>
982
                    $newpatron )->status_is(400)
983
                ->json_is( '/error' => 'Your action breaks a unique constraint on the attribute. type='
984
                    . $attr_type_unique->code
985
                    . ' value='
986
                    . $unique_attr->attribute )->json_is( '/error_code' => 'attribute_not_unique' );
987
988
            $newpatron->{extended_attributes} = [
989
                { type => $attr_type_repeatable->code, value => 'a' },
990
                { type => $attr_type_mandatory->code,  value => 'ping' },
991
                { type => $attr_type_mandatory->code,  value => 'pong' },
992
            ];
993
994
            $t->put_ok( "//$userid:$password@/api/v1/patrons/"
995
                    . $superlibrarian->borrowernumber => { 'x-koha-embed' => 'extended_attributes' } => json =>
996
                    $newpatron )->status_is(400)
997
                ->json_is( '/error' => 'Tried to add more than one non-repeatable attributes. type='
998
                    . $attr_type_mandatory->code
999
                    . ' value=pong' )->json_is( '/error_code' => 'non_repeatable_attribute' );
1000
1001
            my $unique_value = $unique_attr->attribute;
1002
            $unique_attr->delete;
1003
1004
            $newpatron->{extended_attributes} = [
1005
                { type => $attr_type_repeatable->code, value => 'a' },
1006
                { type => $attr_type_repeatable->code, value => 'b' },
1007
                { type => $attr_type_mandatory->code,  value => 'thing' },
1008
                { type => $attr_type_unique->code,     value => $unique_value }
1009
            ];
1010
1011
            my $extended_attributes =
1012
                $t->put_ok( "//$userid:$password@/api/v1/patrons/"
1013
                    . $superlibrarian->borrowernumber => { 'x-koha-embed' => 'extended_attributes' } => json =>
1014
                    $newpatron )->status_is(200)->tx->res->json->{extended_attributes};
1015
1016
            my @sorted_extended_attributes =
1017
                sort { $a->{extended_attribute_id} <=> $b->{extended_attribute_id} } @{$extended_attributes};
1018
1019
            foreach my $i ( 0 .. 3 ) {
1020
                is( $newpatron->{extended_attributes}->[$i]->{type}, $sorted_extended_attributes[$i]->{type} );
1021
                is( $newpatron->{extended_attributes}->[$i]->{code}, $sorted_extended_attributes[$i]->{code} );
1022
            }
1023
        };
1024
903
        $schema->storage->txn_rollback;
1025
        $schema->storage->txn_rollback;
904
    };
1026
    };
905
};
1027
};
906
- 

Return to bug 36505