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 / +27 lines)
Lines 270-294 subtest 'pending_count() and pending() tests' => sub { Link Here
270
270
271
    my $patron_1
271
    my $patron_1
272
        = $builder->build(
272
        = $builder->build(
273
        { source => 'Borrower', value => { branchcode => $library_1 } } )
273
        { source => 'Borrower', value => { branchcode => $library_1 } } );
274
        ->{borrowernumber};
275
    my $patron_2
274
    my $patron_2
276
        = $builder->build(
275
        = $builder->build(
277
        { source => 'Borrower', value => { branchcode => $library_2 } } )
276
        { source => 'Borrower', value => { branchcode => $library_2 } } );
278
        ->{borrowernumber};
279
    my $patron_3
277
    my $patron_3
280
        = $builder->build(
278
        = $builder->build(
281
        { source => 'Borrower', value => { branchcode => $library_2 } } )
279
        { source => 'Borrower', value => { branchcode => $library_2 } } );
282
        ->{borrowernumber};
280
    $patron_1 = Koha::Patrons->find( $patron_1->{borrowernumber} );
281
    $patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
282
    $patron_3 = Koha::Patrons->find( $patron_3->{borrowernumber} );
283
    my $verification_token_1 = md5_hex( time().{}.rand().{}.$$ );
283
    my $verification_token_1 = md5_hex( time().{}.rand().{}.$$ );
284
    my $verification_token_2 = md5_hex( time().{}.rand().{}.$$ );
284
    my $verification_token_2 = md5_hex( time().{}.rand().{}.$$ );
285
    my $verification_token_3 = md5_hex( time().{}.rand().{}.$$ );
285
    my $verification_token_3 = md5_hex( time().{}.rand().{}.$$ );
286
286
287
    Koha::Patron::Attribute->new({ borrowernumber => $patron_1, code => 'CODE_1', attribute => 'hello' } )->store();
287
    Koha::Patron::Attribute->new({ borrowernumber => $patron_1->borrowernumber, code => 'CODE_1', attribute => 'hello' } )->store();
288
    Koha::Patron::Attribute->new({ borrowernumber => $patron_2, code => 'CODE_2', attribute => 'bye' } )->store();
288
    Koha::Patron::Attribute->new({ borrowernumber => $patron_2->borrowernumber, code => 'CODE_2', attribute => 'bye' } )->store();
289
289
290
    my $modification_1 = Koha::Patron::Modification->new(
290
    my $modification_1 = Koha::Patron::Modification->new(
291
        {   borrowernumber     => $patron_1,
291
        {   borrowernumber     => $patron_1->borrowernumber,
292
            surname            => 'Hall',
292
            surname            => 'Hall',
293
            firstname          => 'Kyle',
293
            firstname          => 'Kyle',
294
            verification_token => $verification_token_1,
294
            verification_token => $verification_token_1,
Lines 300-306 subtest 'pending_count() and pending() tests' => sub { Link Here
300
        1, 'pending_count() correctly returns 1' );
300
        1, 'pending_count() correctly returns 1' );
301
301
302
    my $modification_2 = Koha::Patron::Modification->new(
302
    my $modification_2 = Koha::Patron::Modification->new(
303
        {   borrowernumber     => $patron_2,
303
        {   borrowernumber     => $patron_2->borrowernumber,
304
            surname            => 'Smith',
304
            surname            => 'Smith',
305
            firstname          => 'Sandy',
305
            firstname          => 'Sandy',
306
            verification_token => $verification_token_2,
306
            verification_token => $verification_token_2,
Lines 309-315 subtest 'pending_count() and pending() tests' => sub { Link Here
309
    )->store();
309
    )->store();
310
310
311
    my $modification_3 = Koha::Patron::Modification->new(
311
    my $modification_3 = Koha::Patron::Modification->new(
312
        {   borrowernumber     => $patron_3,
312
        {   borrowernumber     => $patron_3->borrowernumber,
313
            surname            => 'Smithy',
313
            surname            => 'Smithy',
314
            firstname          => 'Sandy',
314
            firstname          => 'Sandy',
315
            verification_token => $verification_token_3
315
            verification_token => $verification_token_3
Lines 322-328 subtest 'pending_count() and pending() tests' => sub { Link Here
322
    my $pending = Koha::Patron::Modifications->pending();
322
    my $pending = Koha::Patron::Modifications->pending();
323
    is( scalar @{$pending}, 3, 'pending() returns an array with 3 elements' );
323
    is( scalar @{$pending}, 3, 'pending() returns an array with 3 elements' );
324
324
325
    my @filtered_modifications = grep { $_->{borrowernumber} eq $patron_1 } @{$pending};
325
    my @filtered_modifications = grep { $_->{borrowernumber} eq $patron_1->borrowernumber } @{$pending};
326
    my $p1_pm = $filtered_modifications[0];
326
    my $p1_pm = $filtered_modifications[0];
327
    my $p1_pm_attribute_1 = $p1_pm->{extended_attributes}->[0];
327
    my $p1_pm_attribute_1 = $p1_pm->{extended_attributes}->[0];
328
328
Lines 330-336 subtest 'pending_count() and pending() tests' => sub { Link Here
330
    is( ref($p1_pm_attribute_1), 'Koha::Patron::Attribute', 'patron modification has single attribute object' );
330
    is( ref($p1_pm_attribute_1), 'Koha::Patron::Attribute', 'patron modification has single attribute object' );
331
    is( $p1_pm_attribute_1->attribute, '', 'patron 1 has an empty value for the attribute' );
331
    is( $p1_pm_attribute_1->attribute, '', 'patron 1 has an empty value for the attribute' );
332
332
333
    @filtered_modifications = grep { $_->{borrowernumber} eq $patron_2 } @{$pending};
333
    @filtered_modifications = grep { $_->{borrowernumber} eq $patron_2->borrowernumber } @{$pending};
334
    my $p2_pm = $filtered_modifications[0];
334
    my $p2_pm = $filtered_modifications[0];
335
335
336
    is( scalar @{$p2_pm->{extended_attributes}}, 2 , 'patron 2 has 2 attribute modifications' );
336
    is( scalar @{$p2_pm->{extended_attributes}}, 2 , 'patron 2 has 2 attribute modifications' );
Lines 344-349 subtest 'pending_count() and pending() tests' => sub { Link Here
344
    is( $p2_pm_attribute_1->attribute, 'chau', 'patron modification has the right attribute change' );
344
    is( $p2_pm_attribute_1->attribute, 'chau', 'patron modification has the right attribute change' );
345
    is( $p2_pm_attribute_2->attribute, 'ciao', 'patron modification has the right attribute change' );
345
    is( $p2_pm_attribute_2->attribute, 'ciao', 'patron modification has the right attribute change' );
346
346
347
348
    C4::Context->_new_userenv('xxx');
349
    set_logged_in_user( $patron_1 );
347
    is( Koha::Patron::Modifications->pending_count($library_1),
350
    is( Koha::Patron::Modifications->pending_count($library_1),
348
        1, 'pending_count() correctly returns 1 if filtered by library' );
351
        1, 'pending_count() correctly returns 1 if filtered by library' );
349
352
Lines 368-371 subtest 'pending_count() and pending() tests' => sub { Link Here
368
    $schema->storage->txn_rollback;
371
    $schema->storage->txn_rollback;
369
};
372
};
370
373
374
sub set_logged_in_user {
375
    my ($patron) = @_;
376
    C4::Context->set_userenv(
377
        $patron->borrowernumber, $patron->userid,
378
        $patron->cardnumber,     'firstname',
379
        'surname',               $patron->library->branchcode,
380
        'Midway Public Library', $patron->flags,
381
        '',                      ''
382
    );
383
}
384
371
1;
385
1;
372
- 

Return to bug 18403