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

(-)a/Koha/Patron.pm (+35 lines)
Lines 806-811 sub has_permission { Link Here
806
    return C4::Auth::haspermission( $self->userid, $flagsrequired );
806
    return C4::Auth::haspermission( $self->userid, $flagsrequired );
807
}
807
}
808
808
809
=head3 has_valid_userid
810
811
my $patron = Koha::Patrons->find(42);
812
$patron->userid( $new_userid );
813
my $has_a_valid_userid = $patron->has_valid_userid
814
815
my $patron = Koha::Patron->new( $params );
816
my $has_a_valid_userid = $patron->has_valid_userid
817
818
Return true if the current userid of this patron is valid/unique, otherwise false.
819
820
Note that this should be done in $self->store instead and raise an exception if needed.
821
822
=cut
823
824
sub has_valid_userid {
825
    my ($self) = @_;
826
827
    return 0 unless $self->userid;
828
829
    return 0 if ( $self->userid eq C4::Context->config('user') );    # DB user
830
831
    my $already_exists = Koha::Patrons->search(
832
        {
833
            userid => $self->userid,
834
            (
835
                $self->in_storage
836
                ? ( borrowernumber => { '!=' => $self->borrowernumber } )
837
                : ()
838
            ),
839
        }
840
    )->count;
841
    return $already_exists ? 0 : 1;
842
}
843
809
=head3 type
844
=head3 type
810
845
811
=cut
846
=cut
(-)a/t/db_dependent/Koha/Patrons.t (-16 / +29 lines)
Lines 1056-1062 subtest 'account_locked' => sub { Link Here
1056
};
1056
};
1057
1057
1058
subtest 'userid_is_valid' => sub {
1058
subtest 'userid_is_valid' => sub {
1059
    plan tests => 9;
1059
    plan tests => 10;
1060
1060
1061
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1061
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1062
    my $patron_category = $builder->build_object(
1062
    my $patron_category = $builder->build_object(
Lines 1073-1104 subtest 'userid_is_valid' => sub { Link Here
1073
        branchcode   => $library->branchcode,
1073
        branchcode   => $library->branchcode,
1074
    );
1074
    );
1075
1075
1076
    my $expected_userid_patron_1 = 'tomasito.none';
1076
    my $borrowernumber = AddMember(%data);
1077
    my $borrowernumber = AddMember(%data);
1077
    my $patron_1       = Koha::Patrons->find($borrowernumber);
1078
    my $patron_1       = Koha::Patrons->find($borrowernumber);
1079
    is ( $patron_1->userid, $expected_userid_patron_1, 'The userid generated should be the one we expect' );
1078
1080
1079
    is( Check_Userid( 'tomasito.non', $patron_1->borrowernumber ),
1081
    $patron_1->userid( 'tomasito.non' );
1082
    is( $patron_1->has_valid_userid, # FIXME Joubu: What is the difference with the next test?
1080
        1, 'recently created userid -> unique (borrowernumber passed)' );
1083
        1, 'recently created userid -> unique (borrowernumber passed)' );
1081
    is( Check_Userid( 'tomasitoxxx', $patron_1->borrowernumber ),
1084
1085
    $patron_1->userid( 'tomasitoxxx' );
1086
    is( $patron_1->has_valid_userid,
1082
        1, 'non-existent userid -> unique (borrowernumber passed)' );
1087
        1, 'non-existent userid -> unique (borrowernumber passed)' );
1083
    is( Check_Userid( 'tomasito.none', '' ),
1088
    $patron_1->discard_changes; # We compare with the original userid later
1084
        0, 'userid exists (blank borrowernumber)' );
1089
1085
    is( Check_Userid( 'tomasitoxxx', '' ),
1090
    my $patron_not_in_storage = Koha::Patron->new( { userid => '' } );
1086
        1, 'non-existent userid -> unique (blank borrowernumber)' );
1091
    is( $patron_not_in_storage->has_valid_userid,
1092
        0, 'userid exists for another patron, patron is not in storage yet' );
1093
1094
    $patron_not_in_storage = Koha::Patron->new( { userid => 'tomasitoxxx' } );
1095
    is( $patron_not_in_storage->has_valid_userid,
1096
        1, 'non-existent userid, patron is not in storage yet' );
1087
1097
1088
    # Regression tests for BZ12226
1098
    # Regression tests for BZ12226
1089
    is( Check_Userid( C4::Context->config('user'), '' ),
1099
    my $db_patron = Koha::Patron->new( { userid => C4::Context->config('user') } );
1090
        0, 'Check_Userid should return 0 for the DB user (Bug 12226)' );
1100
    is( $db_patron->has_valid_userid,
1101
        0, 'Koha::Patron->has_valid_userid should return 0 for the DB user (Bug 12226)' );
1091
1102
1092
    # Add a new borrower with the same userid but different cardnumber
1103
    # Add a new borrower with the same userid but different cardnumber
1093
    $data{cardnumber} = "987654321";
1104
    $data{cardnumber} = "987654321";
1094
    my $new_borrowernumber = AddMember(%data);
1105
    my $new_borrowernumber = AddMember(%data);
1095
    is( Check_Userid( 'tomasito.none', '' ),
1096
        0, 'userid not unique (blank borrowernumber)' );
1097
    is( Check_Userid( 'tomasito.none', $new_borrowernumber ),
1098
        0, 'userid not unique (second borrowernumber passed)' );
1099
    my $patron_2 = Koha::Patrons->find($new_borrowernumber);
1106
    my $patron_2 = Koha::Patrons->find($new_borrowernumber);
1100
    ok( $patron_2->userid ne 'tomasito',
1107
    $patron_2->userid($patron_1->userid);
1101
        "Borrower with duplicate userid has new userid generated" );
1108
    is( $patron_2->has_valid_userid,
1109
        0, 'The userid is already in used, it cannot be used for another patron' );
1110
1111
    $patron_2 = Koha::Patrons->find($new_borrowernumber);
1112
    isnt( $patron_2->userid, 'tomasito',
1113
        "Patron with duplicate userid has new userid generated" );
1114
    is( $patron_2->userid, $expected_userid_patron_1 . '1', # TODO we could make that configurable
1115
        "Patron with duplicate userid has new userid generated (1 is appened" );
1102
1116
1103
    my $new_userid = 'a_user_id';
1117
    my $new_userid = 'a_user_id';
1104
    $data{cardnumber} = "234567890";
1118
    $data{cardnumber} = "234567890";
1105
- 

Return to bug 19936