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

(-)a/Koha/Patron/Modifications.pm (-14 / +35 lines)
Lines 52-68 sub pending_count { Link Here
52
        AND borrower_modifications.borrowernumber = borrowers.borrowernumber
52
        AND borrower_modifications.borrowernumber = borrowers.borrowernumber
53
    ";
53
    ";
54
54
55
    my @params;
55
    my $userenv = C4::Context->userenv;
56
    if ($branchcode) {
56
    my @branchcodes;
57
        $query .= " AND borrowers.branchcode = ? ";
57
    if ( $userenv ) {
58
        push( @params, $branchcode );
58
        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
59
        if ($branchcode) {
60
            return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
61
            @branchcodes = ( $branchcode );
62
        }
63
        else {
64
            @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
65
        }
66
    }
67
    my @sql_params;
68
    if ( @branchcodes ) {
69
        $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
70
        push( @sql_params, @branchcodes );
59
    }
71
    }
60
72
61
    my $sth = $dbh->prepare($query);
73
    my ( $count ) = $dbh->selectrow_array( $query, undef, @sql_params );
62
    $sth->execute(@params);
74
    return $count;
63
    my $result = $sth->fetchrow_hashref();
64
65
    return $result->{count};
66
}
75
}
67
76
68
=head2 pending
77
=head2 pending
Lines 84-97 sub pending { Link Here
84
        AND borrower_modifications.borrowernumber = borrowers.borrowernumber
93
        AND borrower_modifications.borrowernumber = borrowers.borrowernumber
85
    ";
94
    ";
86
95
87
    my @params;
96
    my $userenv = C4::Context->userenv;
88
    if ($branchcode) {
97
    my @branchcodes;
89
        $query .= " AND borrowers.branchcode = ? ";
98
    if ( $userenv ) {
90
        push( @params, $branchcode );
99
        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
100
        if ($branchcode) {
101
            return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
102
            @branchcodes = ( $branchcode );
103
        }
104
        else {
105
            @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
106
        }
107
    }
108
    my @sql_params;
109
    if ( @branchcodes ) {
110
        $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
111
        push( @sql_params, @branchcodes );
91
    }
112
    }
92
    $query .= " ORDER BY borrowers.surname, borrowers.firstname";
113
    $query .= " ORDER BY borrowers.surname, borrowers.firstname";
93
    my $sth = $dbh->prepare($query);
114
    my $sth = $dbh->prepare($query);
94
    $sth->execute(@params);
115
    $sth->execute(@sql_params);
95
116
96
    my @m;
117
    my @m;
97
    while ( my $row = $sth->fetchrow_hashref() ) {
118
    while ( my $row = $sth->fetchrow_hashref() ) {
(-)a/t/db_dependent/Koha/Patron/Modifications.t (-14 / +26 lines)
Lines 272-296 subtest 'pending_count() and pending() tests' => sub { Link Here
272
272
273
    my $patron_1
273
    my $patron_1
274
        = $builder->build(
274
        = $builder->build(
275
        { source => 'Borrower', value => { branchcode => $library_1 } } )
275
        { source => 'Borrower', value => { branchcode => $library_1 } } );
276
        ->{borrowernumber};
277
    my $patron_2
276
    my $patron_2
278
        = $builder->build(
277
        = $builder->build(
279
        { source => 'Borrower', value => { branchcode => $library_2 } } )
278
        { source => 'Borrower', value => { branchcode => $library_2 } } );
280
        ->{borrowernumber};
281
    my $patron_3
279
    my $patron_3
282
        = $builder->build(
280
        = $builder->build(
283
        { source => 'Borrower', value => { branchcode => $library_2 } } )
281
        { source => 'Borrower', value => { branchcode => $library_2 } } );
284
        ->{borrowernumber};
282
    $patron_1 = Koha::Patrons->find( $patron_1->{borrowernumber} );
283
    $patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
284
    $patron_3 = Koha::Patrons->find( $patron_3->{borrowernumber} );
285
    my $verification_token_1 = md5_hex( time().{}.rand().{}.$$ );
285
    my $verification_token_1 = md5_hex( time().{}.rand().{}.$$ );
286
    my $verification_token_2 = md5_hex( time().{}.rand().{}.$$ );
286
    my $verification_token_2 = md5_hex( time().{}.rand().{}.$$ );
287
    my $verification_token_3 = md5_hex( time().{}.rand().{}.$$ );
287
    my $verification_token_3 = md5_hex( time().{}.rand().{}.$$ );
288
288
289
    Koha::Patron::Attribute->new({ borrowernumber => $patron_1, code => 'CODE_1', attribute => 'hello' } )->store();
289
    Koha::Patron::Attribute->new({ borrowernumber => $patron_1->borrowernumber, code => 'CODE_1', attribute => 'hello' } )->store();
290
    Koha::Patron::Attribute->new({ borrowernumber => $patron_2, code => 'CODE_2', attribute => 'bye' } )->store();
290
    Koha::Patron::Attribute->new({ borrowernumber => $patron_2->borrowernumber, code => 'CODE_2', attribute => 'bye' } )->store();
291
291
292
    my $modification_1 = Koha::Patron::Modification->new(
292
    my $modification_1 = Koha::Patron::Modification->new(
293
        {   borrowernumber     => $patron_1,
293
        {   borrowernumber     => $patron_1->borrowernumber,
294
            surname            => 'Hall',
294
            surname            => 'Hall',
295
            firstname          => 'Kyle',
295
            firstname          => 'Kyle',
296
            verification_token => $verification_token_1,
296
            verification_token => $verification_token_1,
Lines 302-308 subtest 'pending_count() and pending() tests' => sub { Link Here
302
        1, 'pending_count() correctly returns 1' );
302
        1, 'pending_count() correctly returns 1' );
303
303
304
    my $modification_2 = Koha::Patron::Modification->new(
304
    my $modification_2 = Koha::Patron::Modification->new(
305
        {   borrowernumber     => $patron_2,
305
        {   borrowernumber     => $patron_2->borrowernumber,
306
            surname            => 'Smith',
306
            surname            => 'Smith',
307
            firstname          => 'Sandy',
307
            firstname          => 'Sandy',
308
            verification_token => $verification_token_2,
308
            verification_token => $verification_token_2,
Lines 311-317 subtest 'pending_count() and pending() tests' => sub { Link Here
311
    )->store();
311
    )->store();
312
312
313
    my $modification_3 = Koha::Patron::Modification->new(
313
    my $modification_3 = Koha::Patron::Modification->new(
314
        {   borrowernumber     => $patron_3,
314
        {   borrowernumber     => $patron_3->borrowernumber,
315
            surname            => 'Smithy',
315
            surname            => 'Smithy',
316
            firstname          => 'Sandy',
316
            firstname          => 'Sandy',
317
            verification_token => $verification_token_3
317
            verification_token => $verification_token_3
Lines 324-330 subtest 'pending_count() and pending() tests' => sub { Link Here
324
    my $pending = Koha::Patron::Modifications->pending();
324
    my $pending = Koha::Patron::Modifications->pending();
325
    is( scalar @{$pending}, 3, 'pending() returns an array with 3 elements' );
325
    is( scalar @{$pending}, 3, 'pending() returns an array with 3 elements' );
326
326
327
    my @filtered_modifications = grep { $_->{borrowernumber} eq $patron_1 } @{$pending};
327
    my @filtered_modifications = grep { $_->{borrowernumber} eq $patron_1->borrowernumber } @{$pending};
328
    my $p1_pm = $filtered_modifications[0];
328
    my $p1_pm = $filtered_modifications[0];
329
    my $p1_pm_attribute_1 = $p1_pm->{extended_attributes}->[0];
329
    my $p1_pm_attribute_1 = $p1_pm->{extended_attributes}->[0];
330
330
Lines 332-338 subtest 'pending_count() and pending() tests' => sub { Link Here
332
    is( ref($p1_pm_attribute_1), 'Koha::Patron::Attribute', 'patron modification has single attribute object' );
332
    is( ref($p1_pm_attribute_1), 'Koha::Patron::Attribute', 'patron modification has single attribute object' );
333
    is( $p1_pm_attribute_1->attribute, '', 'patron 1 has an empty value for the attribute' );
333
    is( $p1_pm_attribute_1->attribute, '', 'patron 1 has an empty value for the attribute' );
334
334
335
    @filtered_modifications = grep { $_->{borrowernumber} eq $patron_2 } @{$pending};
335
    @filtered_modifications = grep { $_->{borrowernumber} eq $patron_2->borrowernumber } @{$pending};
336
    my $p2_pm = $filtered_modifications[0];
336
    my $p2_pm = $filtered_modifications[0];
337
337
338
    is( scalar @{$p2_pm->{extended_attributes}}, 2 , 'patron 2 has 2 attribute modifications' );
338
    is( scalar @{$p2_pm->{extended_attributes}}, 2 , 'patron 2 has 2 attribute modifications' );
Lines 346-351 subtest 'pending_count() and pending() tests' => sub { Link Here
346
    is( $p2_pm_attribute_1->attribute, 'año', 'patron modification has the right attribute change' );
346
    is( $p2_pm_attribute_1->attribute, 'año', 'patron modification has the right attribute change' );
347
    is( $p2_pm_attribute_2->attribute, 'ciao', 'patron modification has the right attribute change' );
347
    is( $p2_pm_attribute_2->attribute, 'ciao', 'patron modification has the right attribute change' );
348
348
349
350
    C4::Context->_new_userenv('xxx');
351
    set_logged_in_user( $patron_1 );
349
    is( Koha::Patron::Modifications->pending_count($library_1),
352
    is( Koha::Patron::Modifications->pending_count($library_1),
350
        1, 'pending_count() correctly returns 1 if filtered by library' );
353
        1, 'pending_count() correctly returns 1 if filtered by library' );
351
354
Lines 370-372 subtest 'pending_count() and pending() tests' => sub { Link Here
370
    $schema->storage->txn_rollback;
373
    $schema->storage->txn_rollback;
371
};
374
};
372
375
373
- 
376
sub set_logged_in_user {
377
    my ($patron) = @_;
378
    C4::Context->set_userenv(
379
        $patron->borrowernumber, $patron->userid,
380
        $patron->cardnumber,     'firstname',
381
        'surname',               $patron->library->branchcode,
382
        'Midway Public Library', $patron->flags,
383
        '',                      ''
384
    );
385
}

Return to bug 18403