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

(-)a/Koha/Items.pm (-15 / +50 lines)
Lines 52-60 sub filter_by_for_loan { Link Here
52
52
53
=head3 filter_by_visible_in_opac
53
=head3 filter_by_visible_in_opac
54
54
55
    my $filered_items = $items->filter_by_visible_in_opac;
55
    my $filered_items = $items->filter_by_visible_in_opac(
56
        {
57
            [ patron => $patron ]
58
        }
59
    );
60
61
Returns a new resultset, containing those items that are not expected to be hidden in OPAC
62
for the passed I<Koha::Patron> object that is passed.
56
63
57
Returns a new resultset, containing those items that are not expected to be hidden in OPAC.
58
The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured.
64
The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured.
59
65
60
=cut
66
=cut
Lines 62-67 The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured. Link Here
62
sub filter_by_visible_in_opac {
68
sub filter_by_visible_in_opac {
63
    my ($self, $params) = @_;
69
    my ($self, $params) = @_;
64
70
71
    my $patron = $params->{patron};
72
73
    my $result = $self;
74
75
    unless ( $patron and $patron->category->override_hidden_items ) {
76
        $result = $result->filter_out_opachiddenitems;
77
    }
78
79
    if (C4::Context->preference('hidelostitems')) {
80
        $result = $result->filter_out_lost;
81
    }
82
83
    return $result;
84
}
85
86
=head3 filter_out_opachiddenitems
87
88
    my $filered_items = $items->filter_out_opachiddenitems;
89
90
Returns a new resultset, containing those items that are not expected to be hidden in OPAC.
91
The I<OpacHiddenItems> system preference is honoured.
92
93
=cut
94
95
sub filter_out_opachiddenitems {
96
    my ($self) = @_;
97
65
    my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
98
    my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
66
99
67
    my $rules_params;
100
    my $rules_params;
Lines 69-89 sub filter_by_visible_in_opac { Link Here
69
        $rules_params->{$field}->{'-not_in'} = $rules->{$field};
102
        $rules_params->{$field}->{'-not_in'} = $rules->{$field};
70
    }
103
    }
71
104
72
    my $itemlost_params;
105
    return $self->search( $rules_params );
73
    $itemlost_params = { itemlost => 0 }
106
}
74
        if C4::Context->preference('hidelostitems');
75
107
76
    my $search_params;
108
=head3 filter_out_lost
77
    if ( $rules_params and $itemlost_params ) {
109
78
        $search_params = {
110
    my $filered_items = $items->filter_out_lost;
79
            '-and' => [ $rules_params, $itemlost_params ]
111
80
        };
112
Returns a new resultset, containing those items that are not marked as lost.
81
    }
113
82
    else {
114
=cut
83
        $search_params = $rules_params // $itemlost_params;
115
84
    }
116
sub filter_out_lost {
117
    my ($self) = @_;
118
119
    my $params = { itemlost => 0 };
85
120
86
    return $self->search( $search_params );
121
    return $self->search( $params );
87
}
122
}
88
123
89
=head2 Internal methods
124
=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
use Time::Fake;
26
use Time::Fake;
25
27
Lines 1321-1330 $schema->storage->txn_rollback; Link Here
1321
1323
1322
subtest 'filter_by_visible_in_opac() tests' => sub {
1324
subtest 'filter_by_visible_in_opac() tests' => sub {
1323
1325
1324
    plan tests => 8;
1326
    plan tests => 11;
1325
1327
1326
    $schema->storage->txn_begin;
1328
    $schema->storage->txn_begin;
1327
1329
1330
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1331
    my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
1332
    my $exception = 1;
1333
    $mocked_category->mock( 'override_hidden_items', sub {
1334
        return $exception;
1335
    });
1336
1328
    # have a fresh biblio
1337
    # have a fresh biblio
1329
    my $biblio = $builder->build_sample_biblio;
1338
    my $biblio = $builder->build_sample_biblio;
1330
    # have two itemtypes
1339
    # have two itemtypes
Lines 1391-1396 subtest 'filter_by_visible_in_opac() tests' => sub { Link Here
1391
    is( $biblio->items->filter_by_visible_in_opac->count,
1400
    is( $biblio->items->filter_by_visible_in_opac->count,
1392
        6, 'No rules passed, hidelostitems unset' );
1401
        6, 'No rules passed, hidelostitems unset' );
1393
1402
1403
    is( $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1404
        6, 'No rules passed, hidelostitems unset, patron exception changes nothing' );
1405
1394
    $rules = {};
1406
    $rules = {};
1395
1407
1396
    t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
1408
    t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
Lines 1400-1405 subtest 'filter_by_visible_in_opac() tests' => sub { Link Here
1400
        'No rules passed, hidelostitems set'
1412
        'No rules passed, hidelostitems set'
1401
    );
1413
    );
1402
1414
1415
    is(
1416
        $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1417
        3,
1418
        'No rules passed, hidelostitems set, patron exception changes nothing'
1419
    );
1420
1403
    $rules = { withdrawn => [ 1, 2 ] };
1421
    $rules = { withdrawn => [ 1, 2 ] };
1404
    is(
1422
    is(
1405
        $biblio->items->filter_by_visible_in_opac->count,
1423
        $biblio->items->filter_by_visible_in_opac->count,
Lines 1407-1412 subtest 'filter_by_visible_in_opac() tests' => sub { Link Here
1407
        'Rules on withdrawn, hidelostitems set'
1425
        'Rules on withdrawn, hidelostitems set'
1408
    );
1426
    );
1409
1427
1428
    is(
1429
        $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1430
        3,
1431
        'hidelostitems set, rules on withdrawn but patron override passed'
1432
    );
1433
1410
    $rules = { itype => [ $itype_1->itemtype ] };
1434
    $rules = { itype => [ $itype_1->itemtype ] };
1411
    is(
1435
    is(
1412
        $biblio->items->filter_by_visible_in_opac->count,
1436
        $biblio->items->filter_by_visible_in_opac->count,
Lines 1442-1444 subtest 'filter_by_visible_in_opac() tests' => sub { Link Here
1442
1466
1443
    $schema->storage->txn_rollback;
1467
    $schema->storage->txn_rollback;
1444
};
1468
};
1445
- 
1469
1470
subtest 'filter_out_lost() tests' => sub {
1471
1472
    plan tests => 2;
1473
1474
    $schema->storage->txn_begin;
1475
1476
    # have a fresh biblio
1477
    my $biblio = $builder->build_sample_biblio;
1478
    # have 3 items on that biblio
1479
    my $item_1 = $builder->build_sample_item(
1480
        {
1481
            biblionumber => $biblio->biblionumber,
1482
            itemlost     => -1,
1483
        }
1484
    );
1485
    my $item_2 = $builder->build_sample_item(
1486
        {
1487
            biblionumber => $biblio->biblionumber,
1488
            itemlost     => 0,
1489
        }
1490
    );
1491
    my $item_3 = $builder->build_sample_item(
1492
        {
1493
            biblionumber => $biblio->biblionumber,
1494
            itemlost     => 1,
1495
        }
1496
    );
1497
1498
    is( $biblio->items->filter_out_lost->next->itemnumber, $item_2->itemnumber, 'Right item returned' );
1499
    is( $biblio->items->filter_out_lost->count, 1, 'Only one item is not lost' );
1500
1501
    $schema->storage->txn_rollback;
1502
};
1503
1504
subtest 'filter_out_opachiddenitems() tests' => sub {
1505
1506
    plan tests => 6;
1507
1508
    $schema->storage->txn_begin;
1509
1510
    # have a fresh biblio
1511
    my $biblio = $builder->build_sample_biblio;
1512
    # have two itemtypes
1513
    my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
1514
    my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
1515
    # have 5 items on that biblio
1516
    my $item_1 = $builder->build_sample_item(
1517
        {
1518
            biblionumber => $biblio->biblionumber,
1519
            itype        => $itype_1->itemtype,
1520
            withdrawn    => 1
1521
        }
1522
    );
1523
    my $item_2 = $builder->build_sample_item(
1524
        {
1525
            biblionumber => $biblio->biblionumber,
1526
            itype        => $itype_2->itemtype,
1527
            withdrawn    => 2
1528
        }
1529
    );
1530
    my $item_3 = $builder->build_sample_item(
1531
        {
1532
            biblionumber => $biblio->biblionumber,
1533
            itype        => $itype_1->itemtype,
1534
            withdrawn    => 3
1535
        }
1536
    );
1537
    my $item_4 = $builder->build_sample_item(
1538
        {
1539
            biblionumber => $biblio->biblionumber,
1540
            itype        => $itype_2->itemtype,
1541
            withdrawn    => 4
1542
        }
1543
    );
1544
    my $item_5 = $builder->build_sample_item(
1545
        {
1546
            biblionumber => $biblio->biblionumber,
1547
            itype        => $itype_1->itemtype,
1548
            withdrawn    => 5
1549
        }
1550
    );
1551
    my $item_6 = $builder->build_sample_item(
1552
        {
1553
            biblionumber => $biblio->biblionumber,
1554
            itype        => $itype_1->itemtype,
1555
            withdrawn    => 5
1556
        }
1557
    );
1558
1559
    my $rules = undef;
1560
1561
    my $mocked_context = Test::MockModule->new('C4::Context');
1562
    $mocked_context->mock( 'yaml_preference', sub {
1563
        return $rules;
1564
    });
1565
1566
    is( $biblio->items->filter_out_opachiddenitems->count, 6, 'No rules passed' );
1567
1568
    $rules = {};
1569
1570
    $rules = { withdrawn => [ 1, 2 ] };
1571
    is( $biblio->items->filter_out_opachiddenitems->count, 4, 'Rules on withdrawn' );
1572
1573
    $rules = { itype => [ $itype_1->itemtype ] };
1574
    is( $biblio->items->filter_out_opachiddenitems->count, 2, 'Rules on itype' );
1575
1576
    $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_1->itemtype ] };
1577
    is( $biblio->items->filter_out_opachiddenitems->count, 1, 'Rules on itype and withdrawn' );
1578
    is( $biblio->items->filter_out_opachiddenitems->next->itemnumber,
1579
        $item_4->itemnumber,
1580
        'The right item is returned'
1581
    );
1582
1583
    $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_2->itemtype ] };
1584
    is( $biblio->items->filter_out_opachiddenitems->count, 3, 'Rules on itype and withdrawn' );
1585
1586
    $schema->storage->txn_rollback;
1587
};

Return to bug 24254