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

(-)a/Koha/Patron.pm (+35 lines)
Lines 831-836 sub is_child { Link Here
831
    return $self->category->category_type eq 'C' ? 1 : 0;
831
    return $self->category->category_type eq 'C' ? 1 : 0;
832
}
832
}
833
833
834
=head3 has_valid_userid
835
836
my $patron = Koha::Patrons->find(42);
837
$patron->userid( $new_userid );
838
my $has_a_valid_userid = $patron->has_valid_userid
839
840
my $patron = Koha::Patron->new( $params );
841
my $has_a_valid_userid = $patron->has_valid_userid
842
843
Return true if the current userid of this patron is valid/unique, otherwise false.
844
845
Note that this should be done in $self->store instead and raise an exception if needed.
846
847
=cut
848
849
sub has_valid_userid {
850
    my ($self) = @_;
851
852
    return 0 unless $self->userid;
853
854
    return 0 if ( $self->userid eq C4::Context->config('user') );    # DB user
855
856
    my $already_exists = Koha::Patrons->search(
857
        {
858
            userid => $self->userid,
859
            (
860
                $self->in_storage
861
                ? ( borrowernumber => { '!=' => $self->borrowernumber } )
862
                : ()
863
            ),
864
        }
865
    )->count;
866
    return $already_exists ? 0 : 1;
867
}
868
834
=head3 type
869
=head3 type
835
870
836
=cut
871
=cut
(-)a/t/db_dependent/Koha/Patrons.t (-16 / +29 lines)
Lines 1119-1125 subtest 'is_child | is_adult' => sub { Link Here
1119
};
1119
};
1120
1120
1121
subtest 'userid_is_valid' => sub {
1121
subtest 'userid_is_valid' => sub {
1122
    plan tests => 9;
1122
    plan tests => 10;
1123
1123
1124
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1124
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1125
    my $patron_category = $builder->build_object(
1125
    my $patron_category = $builder->build_object(
Lines 1136-1167 subtest 'userid_is_valid' => sub { Link Here
1136
        branchcode   => $library->branchcode,
1136
        branchcode   => $library->branchcode,
1137
    );
1137
    );
1138
1138
1139
    my $expected_userid_patron_1 = 'tomasito.none';
1139
    my $borrowernumber = AddMember(%data);
1140
    my $borrowernumber = AddMember(%data);
1140
    my $patron_1       = Koha::Patrons->find($borrowernumber);
1141
    my $patron_1       = Koha::Patrons->find($borrowernumber);
1142
    is ( $patron_1->userid, $expected_userid_patron_1, 'The userid generated should be the one we expect' );
1141
1143
1142
    is( Check_Userid( 'tomasito.non', $patron_1->borrowernumber ),
1144
    $patron_1->userid( 'tomasito.non' );
1145
    is( $patron_1->has_valid_userid, # FIXME Joubu: What is the difference with the next test?
1143
        1, 'recently created userid -> unique (borrowernumber passed)' );
1146
        1, 'recently created userid -> unique (borrowernumber passed)' );
1144
    is( Check_Userid( 'tomasitoxxx', $patron_1->borrowernumber ),
1147
1148
    $patron_1->userid( 'tomasitoxxx' );
1149
    is( $patron_1->has_valid_userid,
1145
        1, 'non-existent userid -> unique (borrowernumber passed)' );
1150
        1, 'non-existent userid -> unique (borrowernumber passed)' );
1146
    is( Check_Userid( 'tomasito.none', '' ),
1151
    $patron_1->discard_changes; # We compare with the original userid later
1147
        0, 'userid exists (blank borrowernumber)' );
1152
1148
    is( Check_Userid( 'tomasitoxxx', '' ),
1153
    my $patron_not_in_storage = Koha::Patron->new( { userid => '' } );
1149
        1, 'non-existent userid -> unique (blank borrowernumber)' );
1154
    is( $patron_not_in_storage->has_valid_userid,
1155
        0, 'userid exists for another patron, patron is not in storage yet' );
1156
1157
    $patron_not_in_storage = Koha::Patron->new( { userid => 'tomasitoxxx' } );
1158
    is( $patron_not_in_storage->has_valid_userid,
1159
        1, 'non-existent userid, patron is not in storage yet' );
1150
1160
1151
    # Regression tests for BZ12226
1161
    # Regression tests for BZ12226
1152
    is( Check_Userid( C4::Context->config('user'), '' ),
1162
    my $db_patron = Koha::Patron->new( { userid => C4::Context->config('user') } );
1153
        0, 'Check_Userid should return 0 for the DB user (Bug 12226)' );
1163
    is( $db_patron->has_valid_userid,
1164
        0, 'Koha::Patron->has_valid_userid should return 0 for the DB user (Bug 12226)' );
1154
1165
1155
    # Add a new borrower with the same userid but different cardnumber
1166
    # Add a new borrower with the same userid but different cardnumber
1156
    $data{cardnumber} = "987654321";
1167
    $data{cardnumber} = "987654321";
1157
    my $new_borrowernumber = AddMember(%data);
1168
    my $new_borrowernumber = AddMember(%data);
1158
    is( Check_Userid( 'tomasito.none', '' ),
1159
        0, 'userid not unique (blank borrowernumber)' );
1160
    is( Check_Userid( 'tomasito.none', $new_borrowernumber ),
1161
        0, 'userid not unique (second borrowernumber passed)' );
1162
    my $patron_2 = Koha::Patrons->find($new_borrowernumber);
1169
    my $patron_2 = Koha::Patrons->find($new_borrowernumber);
1163
    ok( $patron_2->userid ne 'tomasito',
1170
    $patron_2->userid($patron_1->userid);
1164
        "Borrower with duplicate userid has new userid generated" );
1171
    is( $patron_2->has_valid_userid,
1172
        0, 'The userid is already in used, it cannot be used for another patron' );
1173
1174
    $patron_2 = Koha::Patrons->find($new_borrowernumber);
1175
    isnt( $patron_2->userid, 'tomasito',
1176
        "Patron with duplicate userid has new userid generated" );
1177
    is( $patron_2->userid, $expected_userid_patron_1 . '1', # TODO we could make that configurable
1178
        "Patron with duplicate userid has new userid generated (1 is appened" );
1165
1179
1166
    my $new_userid = 'a_user_id';
1180
    my $new_userid = 'a_user_id';
1167
    $data{cardnumber} = "234567890";
1181
    $data{cardnumber} = "234567890";
1168
- 

Return to bug 19936