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

(-)a/Koha/Patron.pm (+35 lines)
Lines 862-867 sub is_child { Link Here
862
    return $self->category->category_type eq 'C' ? 1 : 0;
862
    return $self->category->category_type eq 'C' ? 1 : 0;
863
}
863
}
864
864
865
=head3 has_valid_userid
866
867
my $patron = Koha::Patrons->find(42);
868
$patron->userid( $new_userid );
869
my $has_a_valid_userid = $patron->has_valid_userid
870
871
my $patron = Koha::Patron->new( $params );
872
my $has_a_valid_userid = $patron->has_valid_userid
873
874
Return true if the current userid of this patron is valid/unique, otherwise false.
875
876
Note that this should be done in $self->store instead and raise an exception if needed.
877
878
=cut
879
880
sub has_valid_userid {
881
    my ($self) = @_;
882
883
    return 0 unless $self->userid;
884
885
    return 0 if ( $self->userid eq C4::Context->config('user') );    # DB user
886
887
    my $already_exists = Koha::Patrons->search(
888
        {
889
            userid => $self->userid,
890
            (
891
                $self->in_storage
892
                ? ( borrowernumber => { '!=' => $self->borrowernumber } )
893
                : ()
894
            ),
895
        }
896
    )->count;
897
    return $already_exists ? 0 : 1;
898
}
899
865
=head2 Internal methods
900
=head2 Internal methods
866
901
867
=head3 _type
902
=head3 _type
(-)a/t/db_dependent/Koha/Patrons.t (-16 / +29 lines)
Lines 1198-1204 subtest 'get_overdues' => sub { Link Here
1198
};
1198
};
1199
1199
1200
subtest 'userid_is_valid' => sub {
1200
subtest 'userid_is_valid' => sub {
1201
    plan tests => 9;
1201
    plan tests => 10;
1202
1202
1203
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1203
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1204
    my $patron_category = $builder->build_object(
1204
    my $patron_category = $builder->build_object(
Lines 1215-1246 subtest 'userid_is_valid' => sub { Link Here
1215
        branchcode   => $library->branchcode,
1215
        branchcode   => $library->branchcode,
1216
    );
1216
    );
1217
1217
1218
    my $expected_userid_patron_1 = 'tomasito.none';
1218
    my $borrowernumber = AddMember(%data);
1219
    my $borrowernumber = AddMember(%data);
1219
    my $patron_1       = Koha::Patrons->find($borrowernumber);
1220
    my $patron_1       = Koha::Patrons->find($borrowernumber);
1221
    is ( $patron_1->userid, $expected_userid_patron_1, 'The userid generated should be the one we expect' );
1220
1222
1221
    is( Check_Userid( 'tomasito.non', $patron_1->borrowernumber ),
1223
    $patron_1->userid( 'tomasito.non' );
1224
    is( $patron_1->has_valid_userid, # FIXME Joubu: What is the difference with the next test?
1222
        1, 'recently created userid -> unique (borrowernumber passed)' );
1225
        1, 'recently created userid -> unique (borrowernumber passed)' );
1223
    is( Check_Userid( 'tomasitoxxx', $patron_1->borrowernumber ),
1226
1227
    $patron_1->userid( 'tomasitoxxx' );
1228
    is( $patron_1->has_valid_userid,
1224
        1, 'non-existent userid -> unique (borrowernumber passed)' );
1229
        1, 'non-existent userid -> unique (borrowernumber passed)' );
1225
    is( Check_Userid( 'tomasito.none', '' ),
1230
    $patron_1->discard_changes; # We compare with the original userid later
1226
        0, 'userid exists (blank borrowernumber)' );
1231
1227
    is( Check_Userid( 'tomasitoxxx', '' ),
1232
    my $patron_not_in_storage = Koha::Patron->new( { userid => '' } );
1228
        1, 'non-existent userid -> unique (blank borrowernumber)' );
1233
    is( $patron_not_in_storage->has_valid_userid,
1234
        0, 'userid exists for another patron, patron is not in storage yet' );
1235
1236
    $patron_not_in_storage = Koha::Patron->new( { userid => 'tomasitoxxx' } );
1237
    is( $patron_not_in_storage->has_valid_userid,
1238
        1, 'non-existent userid, patron is not in storage yet' );
1229
1239
1230
    # Regression tests for BZ12226
1240
    # Regression tests for BZ12226
1231
    is( Check_Userid( C4::Context->config('user'), '' ),
1241
    my $db_patron = Koha::Patron->new( { userid => C4::Context->config('user') } );
1232
        0, 'Check_Userid should return 0 for the DB user (Bug 12226)' );
1242
    is( $db_patron->has_valid_userid,
1243
        0, 'Koha::Patron->has_valid_userid should return 0 for the DB user (Bug 12226)' );
1233
1244
1234
    # Add a new borrower with the same userid but different cardnumber
1245
    # Add a new borrower with the same userid but different cardnumber
1235
    $data{cardnumber} = "987654321";
1246
    $data{cardnumber} = "987654321";
1236
    my $new_borrowernumber = AddMember(%data);
1247
    my $new_borrowernumber = AddMember(%data);
1237
    is( Check_Userid( 'tomasito.none', '' ),
1238
        0, 'userid not unique (blank borrowernumber)' );
1239
    is( Check_Userid( 'tomasito.none', $new_borrowernumber ),
1240
        0, 'userid not unique (second borrowernumber passed)' );
1241
    my $patron_2 = Koha::Patrons->find($new_borrowernumber);
1248
    my $patron_2 = Koha::Patrons->find($new_borrowernumber);
1242
    ok( $patron_2->userid ne 'tomasito',
1249
    $patron_2->userid($patron_1->userid);
1243
        "Borrower with duplicate userid has new userid generated" );
1250
    is( $patron_2->has_valid_userid,
1251
        0, 'The userid is already in used, it cannot be used for another patron' );
1252
1253
    $patron_2 = Koha::Patrons->find($new_borrowernumber);
1254
    isnt( $patron_2->userid, 'tomasito',
1255
        "Patron with duplicate userid has new userid generated" );
1256
    is( $patron_2->userid, $expected_userid_patron_1 . '1', # TODO we could make that configurable
1257
        "Patron with duplicate userid has new userid generated (1 is appened" );
1244
1258
1245
    my $new_userid = 'a_user_id';
1259
    my $new_userid = 'a_user_id';
1246
    $data{cardnumber} = "234567890";
1260
    $data{cardnumber} = "234567890";
1247
- 

Return to bug 19936