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

(-)a/Koha/Items.pm (-15 / +50 lines)
Lines 39-47 Koha::Items - Koha Item object set class Link Here
39
39
40
=head3 filter_by_visible_in_opac
40
=head3 filter_by_visible_in_opac
41
41
42
    my $filered_items = $items->filter_by_visible_in_opac;
42
    my $filered_items = $items->filter_by_visible_in_opac(
43
        {
44
            [ patron => $patron ]
45
        }
46
    );
47
48
Returns a new resultset, containing those items that are not expected to be hidden in OPAC
49
for the passed I<Koha::Patron> object that is passed.
43
50
44
Returns a new resultset, containing those items that are not expected to be hidden in OPAC.
45
The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured.
51
The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured.
46
52
47
=cut
53
=cut
Lines 49-54 The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured. Link Here
49
sub filter_by_visible_in_opac {
55
sub filter_by_visible_in_opac {
50
    my ($self, $params) = @_;
56
    my ($self, $params) = @_;
51
57
58
    my $patron = $params->{patron};
59
60
    my $result = $self;
61
62
    unless ( $patron and $patron->category->override_hidden_items ) {
63
        $result = $result->filter_out_opachiddenitems;
64
    }
65
66
    if (C4::Context->preference('hidelostitems')) {
67
        $result = $result->filter_out_lost;
68
    }
69
70
    return $result;
71
}
72
73
=head3 filter_out_opachiddenitems
74
75
    my $filered_items = $items->filter_out_opachiddenitems;
76
77
Returns a new resultset, containing those items that are not expected to be hidden in OPAC.
78
The I<OpacHiddenItems> system preference is honoured.
79
80
=cut
81
82
sub filter_out_opachiddenitems {
83
    my ($self) = @_;
84
52
    my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
85
    my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
53
86
54
    my $rules_params;
87
    my $rules_params;
Lines 56-76 sub filter_by_visible_in_opac { Link Here
56
        $rules_params->{$field}->{'-not_in'} = $rules->{$field};
89
        $rules_params->{$field}->{'-not_in'} = $rules->{$field};
57
    }
90
    }
58
91
59
    my $itemlost_params;
92
    return $self->search( $rules_params );
60
    $itemlost_params = { itemlost => 0 }
93
}
61
        if C4::Context->preference('hidelostitems');
62
94
63
    my $search_params;
95
=head3 filter_out_lost
64
    if ( $rules_params and $itemlost_params ) {
96
65
        $search_params = {
97
    my $filered_items = $items->filter_out_lost;
66
            '-and' => [ $rules_params, $itemlost_params ]
98
67
        };
99
Returns a new resultset, containing those items that are not marked as lost.
68
    }
100
69
    else {
101
=cut
70
        $search_params = $rules_params // $itemlost_params;
102
71
    }
103
sub filter_out_lost {
104
    my ($self) = @_;
105
106
    my $params = { itemlost => 0 };
72
107
73
    return $self->search( $search_params );
108
    return $self->search( $params );
74
}
109
}
75
110
76
=head2 Internal methods
111
=head2 Internal methods
(-)a/t/db_dependent/Koha/Items.t (-3 / +145 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 13;
22
use Test::More tests => 15;
23
24
use Test::MockModule;
23
use Test::Exception;
25
use Test::Exception;
24
26
25
use C4::Circulation;
27
use C4::Circulation;
Lines 244-253 $schema->storage->txn_rollback; Link Here
244
246
245
subtest 'filter_by_visible_in_opac() tests' => sub {
247
subtest 'filter_by_visible_in_opac() tests' => sub {
246
248
247
    plan tests => 8;
249
    plan tests => 11;
248
250
249
    $schema->storage->txn_begin;
251
    $schema->storage->txn_begin;
250
252
253
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
254
    my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
255
    my $exception = 1;
256
    $mocked_category->mock( 'override_hidden_items', sub {
257
        return $exception;
258
    });
259
251
    # have a fresh biblio
260
    # have a fresh biblio
252
    my $biblio = $builder->build_sample_biblio;
261
    my $biblio = $builder->build_sample_biblio;
253
    # have two itemtypes
262
    # have two itemtypes
Lines 314-319 subtest 'filter_by_visible_in_opac() tests' => sub { Link Here
314
    is( $biblio->items->filter_by_visible_in_opac->count,
323
    is( $biblio->items->filter_by_visible_in_opac->count,
315
        6, 'No rules passed, hidelostitems unset' );
324
        6, 'No rules passed, hidelostitems unset' );
316
325
326
    is( $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
327
        6, 'No rules passed, hidelostitems unset, patron exception changes nothing' );
328
317
    $rules = {};
329
    $rules = {};
318
330
319
    t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
331
    t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
Lines 323-328 subtest 'filter_by_visible_in_opac() tests' => sub { Link Here
323
        'No rules passed, hidelostitems set'
335
        'No rules passed, hidelostitems set'
324
    );
336
    );
325
337
338
    is(
339
        $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
340
        3,
341
        'No rules passed, hidelostitems set, patron exception changes nothing'
342
    );
343
326
    $rules = { withdrawn => [ 1, 2 ] };
344
    $rules = { withdrawn => [ 1, 2 ] };
327
    is(
345
    is(
328
        $biblio->items->filter_by_visible_in_opac->count,
346
        $biblio->items->filter_by_visible_in_opac->count,
Lines 330-335 subtest 'filter_by_visible_in_opac() tests' => sub { Link Here
330
        'Rules on withdrawn, hidelostitems set'
348
        'Rules on withdrawn, hidelostitems set'
331
    );
349
    );
332
350
351
    is(
352
        $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
353
        3,
354
        'hidelostitems set, rules on withdrawn but patron override passed'
355
    );
356
333
    $rules = { itype => [ $itype_1->itemtype ] };
357
    $rules = { itype => [ $itype_1->itemtype ] };
334
    is(
358
    is(
335
        $biblio->items->filter_by_visible_in_opac->count,
359
        $biblio->items->filter_by_visible_in_opac->count,
Lines 365-367 subtest 'filter_by_visible_in_opac() tests' => sub { Link Here
365
389
366
    $schema->storage->txn_rollback;
390
    $schema->storage->txn_rollback;
367
};
391
};
368
- 
392
393
subtest 'filter_out_lost() tests' => sub {
394
395
    plan tests => 2;
396
397
    $schema->storage->txn_begin;
398
399
    # have a fresh biblio
400
    my $biblio = $builder->build_sample_biblio;
401
    # have 3 items on that biblio
402
    my $item_1 = $builder->build_sample_item(
403
        {
404
            biblionumber => $biblio->biblionumber,
405
            itemlost     => -1,
406
        }
407
    );
408
    my $item_2 = $builder->build_sample_item(
409
        {
410
            biblionumber => $biblio->biblionumber,
411
            itemlost     => 0,
412
        }
413
    );
414
    my $item_3 = $builder->build_sample_item(
415
        {
416
            biblionumber => $biblio->biblionumber,
417
            itemlost     => 1,
418
        }
419
    );
420
421
    is( $biblio->items->filter_out_lost->next->itemnumber, $item_2->itemnumber, 'Right item returned' );
422
    is( $biblio->items->filter_out_lost->count, 1, 'Only one item is not lost' );
423
424
    $schema->storage->txn_rollback;
425
};
426
427
subtest 'filter_out_opachiddenitems() tests' => sub {
428
429
    plan tests => 6;
430
431
    $schema->storage->txn_begin;
432
433
    # have a fresh biblio
434
    my $biblio = $builder->build_sample_biblio;
435
    # have two itemtypes
436
    my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
437
    my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
438
    # have 5 items on that biblio
439
    my $item_1 = $builder->build_sample_item(
440
        {
441
            biblionumber => $biblio->biblionumber,
442
            itype        => $itype_1->itemtype,
443
            withdrawn    => 1
444
        }
445
    );
446
    my $item_2 = $builder->build_sample_item(
447
        {
448
            biblionumber => $biblio->biblionumber,
449
            itype        => $itype_2->itemtype,
450
            withdrawn    => 2
451
        }
452
    );
453
    my $item_3 = $builder->build_sample_item(
454
        {
455
            biblionumber => $biblio->biblionumber,
456
            itype        => $itype_1->itemtype,
457
            withdrawn    => 3
458
        }
459
    );
460
    my $item_4 = $builder->build_sample_item(
461
        {
462
            biblionumber => $biblio->biblionumber,
463
            itype        => $itype_2->itemtype,
464
            withdrawn    => 4
465
        }
466
    );
467
    my $item_5 = $builder->build_sample_item(
468
        {
469
            biblionumber => $biblio->biblionumber,
470
            itype        => $itype_1->itemtype,
471
            withdrawn    => 5
472
        }
473
    );
474
    my $item_6 = $builder->build_sample_item(
475
        {
476
            biblionumber => $biblio->biblionumber,
477
            itype        => $itype_1->itemtype,
478
            withdrawn    => 5
479
        }
480
    );
481
482
    my $rules = undef;
483
484
    my $mocked_context = Test::MockModule->new('C4::Context');
485
    $mocked_context->mock( 'yaml_preference', sub {
486
        return $rules;
487
    });
488
489
    is( $biblio->items->filter_out_opachiddenitems->count, 6, 'No rules passed' );
490
491
    $rules = {};
492
493
    $rules = { withdrawn => [ 1, 2 ] };
494
    is( $biblio->items->filter_out_opachiddenitems->count, 4, 'Rules on withdrawn' );
495
496
    $rules = { itype => [ $itype_1->itemtype ] };
497
    is( $biblio->items->filter_out_opachiddenitems->count, 2, 'Rules on itype' );
498
499
    $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_1->itemtype ] };
500
    is( $biblio->items->filter_out_opachiddenitems->count, 1, 'Rules on itype and withdrawn' );
501
    is( $biblio->items->filter_out_opachiddenitems->next->itemnumber,
502
        $item_4->itemnumber,
503
        'The right item is returned'
504
    );
505
506
    $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_2->itemtype ] };
507
    is( $biblio->items->filter_out_opachiddenitems->count, 3, 'Rules on itype and withdrawn' );
508
509
    $schema->storage->txn_rollback;
510
};

Return to bug 24254