Bug 23233: Remove use of AllowItemsOnHoldCheckout from Koha::Item::has_pending_hold...
[koha.git] / t / db_dependent / Koha / Item.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 3;
23
24 use C4::Biblio;
25
26 use Koha::Items;
27 use Koha::Database;
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 subtest 'hidden_in_opac() tests' => sub {
36
37     plan tests => 4;
38
39     $schema->storage->txn_begin;
40
41     my $item  = $builder->build_sample_item({ itemlost => 2 });
42     my $rules = {};
43
44     # disable hidelostitems as it interteres with OpachiddenItems for the calculation
45     t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
46
47     ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
48     ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
49
50     # enable hidelostitems to verify correct behaviour
51     t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
52     ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
53
54     # disable hidelostitems
55     t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
56     my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
57
58     $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
59
60     ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
61
62
63
64     $schema->storage->txn_rollback;
65 };
66
67 subtest 'has_pending_hold() tests' => sub {
68
69     plan tests => 2;
70
71     $schema->storage->txn_begin;
72
73     my $dbh = C4::Context->dbh;
74     my $item  = $builder->build_sample_item({ itemlost => 0 });
75     my $itemnumber = $item->itemnumber;
76
77     # disable AllowItemsOnHoldCheckout as it ignores pending holds
78     $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
79     ok( $item->has_pending_hold, "Yes, we have a pending hold");
80     $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
81     ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
82
83     $schema->storage->txn_rollback;
84 };
85
86 subtest "as_marc_field() tests" => sub {
87
88     my $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } );
89
90     my @schema_columns = $schema->resultset('Item')->result_source->columns;
91     my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
92
93     plan tests => 2 * (scalar @mapped_columns + 1) + 1;
94
95     $schema->storage->txn_begin;
96
97     my $item = $builder->build_sample_item;
98
99     # Tests with the mss parameter
100     my $marc_field = $item->as_marc_field({ mss => $mss });
101
102     is(
103         $marc_field->tag,
104         $mss->{'items.itemnumber'}[0]->{tagfield},
105         'Generated field set the right tag number'
106     );
107
108     foreach my $column ( @mapped_columns ) {
109         my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
110         is( $marc_field->subfield($tagsubfield),
111             $item->$column, "Value is mapped correctly for column $column" );
112     }
113
114     # Tests without the mss parameter
115     $marc_field = $item->as_marc_field();
116
117     is(
118         $marc_field->tag,
119         $mss->{'items.itemnumber'}[0]->{tagfield},
120         'Generated field set the right tag number'
121     );
122
123     foreach my $column (@mapped_columns) {
124         my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
125         is( $marc_field->subfield($tagsubfield),
126             $item->$column, "Value is mapped correctly for column $column" );
127     }
128
129     my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
130         {
131             frameworkcode => '',
132             tagfield      => $mss->{'items.itemnumber'}[0]->{tagfield},
133             tagsubfield   => 'X',
134         }
135     )->store;
136
137     $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 0 } );
138     my @unlinked_subfields;
139     push @unlinked_subfields, X => 'Something weird';
140     $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
141
142     $marc_field = $item->as_marc_field;
143     is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' );
144
145     $schema->storage->txn_rollback;
146 };