Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Items / GetItemsForInventory.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
5 # Copyright (c) 2015   Mark Tompsett
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 => 7;
23 use t::lib::TestBuilder;
24
25 use List::MoreUtils qw( any none );
26
27 use C4::Biblio qw(AddBiblio);
28 use C4::Reserves qw( AddReserve );
29 use C4::ClassSource qw( GetClassSort );
30 use Koha::AuthorisedValues;
31 use Koha::Biblios;
32 use Koha::Database;
33 use MARC::Record;
34
35 BEGIN {
36     use_ok('C4::Context');
37     use_ok('C4::Items', qw( GetItemsForInventory ));
38     use_ok('C4::Biblio');
39     use_ok('C4::Koha');
40 }
41
42 can_ok('C4::Items','GetItemsForInventory');
43
44 my $schema  = Koha::Database->new->schema;
45 my $builder = t::lib::TestBuilder->new;
46
47 subtest 'Skip items with waiting holds' => sub {
48
49     plan tests => 8;
50
51     $schema->storage->txn_begin;
52
53     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
54     my $itemtype
55         = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
56     my $patron_1 = $builder->build_object(
57         { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
58     my $patron_2 = $builder->build_object(
59         { class => 'Koha::Patrons', value => { branchcode => $library->id } } );
60
61
62     my $title_1 = 'Title 1, ';
63     my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist';
64
65     my $biblio_1 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype, title => $title_1 });
66     my $biblio_2 = $builder->build_sample_biblio({ itemtype => $itemtype->itemtype, title => $title_2 });
67
68     my ( $items_1, $first_items_count ) = GetItemsForInventory();
69     is( scalar @{$items_1}, $first_items_count, 'Results and count match' );
70
71     # Add two items, so we don't depend on existing data
72     my $item_1 = $builder->build_object(
73         {   class => 'Koha::Items',
74             value => {
75                 biblionumber     => $biblio_1->biblionumber,
76                 biblioitemnumber => $biblio_1->biblioitem->biblioitemnumber,
77                 homebranch       => $library->id,
78                 holdingbranch    => $library->id,
79                 itype            => $itemtype->itemtype,
80                 reserves         => undef
81             }
82         }
83     );
84
85     my $item_2 = $builder->build_object(
86         {   class => 'Koha::Items',
87             value => {
88                 biblionumber     => $biblio_2->biblionumber,
89                 biblioitemnumber => $biblio_2->biblioitem->biblioitemnumber,
90                 homebranch       => $library->id,
91                 holdingbranch    => $library->id,
92                 itype            => $itemtype->itemtype,
93                 reserves         => undef
94             }
95         }
96     );
97
98     my ( $items_2, $second_items_count ) = GetItemsForInventory();
99     is( scalar @{$items_2},     $second_items_count, 'Results and count match' );
100     is( $first_items_count + 2, $second_items_count, 'Two items added, count makes sense' );
101
102     my $real_itemtype_count = Koha::Items->search({ itype => $itemtype->itemtype })->count;
103     my $itype_str = "'" . $itemtype->itemtype . "'"; # manipulate string for db query
104     my ( $items_3, $itemtype_count ) = GetItemsForInventory({ itemtypes => [ $itype_str ] });
105     is( $itemtype_count, $real_itemtype_count, 'Itemtype filter gets correct number of inventory items' );
106
107     # Add 2 waiting holds
108     C4::Reserves::AddReserve(
109         {
110             branchcode     => $library->branchcode,
111             borrowernumber => $patron_1->borrowernumber,
112             biblionumber   => $item_1->biblionumber,
113             priority       => 1,
114             itemnumber     => $item_1->itemnumber,
115             found          => 'W'
116         }
117     );
118     C4::Reserves::AddReserve(
119         {
120             branchcode     => $library->branchcode,
121             borrowernumber => $patron_1->borrowernumber,
122             biblionumber   => $item_2->biblionumber,
123             priority       => 1,
124             itemnumber     => $item_2->itemnumber,
125         }
126     );
127     C4::Reserves::AddReserve(
128         {
129             branchcode     => $library->branchcode,
130             borrowernumber => $patron_2->borrowernumber,
131             biblionumber   => $item_2->biblionumber,
132             priority       => 2,
133             itemnumber     => $item_2->itemnumber,
134         }
135     );
136
137     my ( $new_items, $new_items_count ) = GetItemsForInventory( { ignore_waiting_holds => 1 } );
138     is( $new_items_count, $first_items_count + 1, 'Item on hold skipped, count makes sense' );
139     ok( (any { $_->{title} eq $title_2 } @{$new_items}),
140         'Item on hold skipped, the other one we added is present' );
141     ok( (none { $_->{title} eq $title_1 } @{$new_items}),
142         'Item on hold skipped, no one matches' );
143     is( scalar(@$new_items), $new_items_count, 'total and number of items is the same');
144
145     $schema->storage->txn_rollback;
146 };
147
148 subtest 'Use cn_sort rather than callnumber to determine correct location' => sub {
149     $schema->storage->txn_begin;
150     plan tests => 1;
151
152     my $builder = t::lib::TestBuilder->new;
153
154     my $class_rule = $builder->build({
155         source => 'ClassSortRule',
156         value => { sort_routine => "LCC" }
157     });
158     my $class_source = $builder->build({
159         source => 'ClassSource',
160         value => {
161             class_sort_rule => $class_rule->{class_sort_rule},
162         }
163     });
164
165     #Find if we have any items in our test range before we start
166     my( undef, $pre_item_count) = GetItemsForInventory({
167         maxlocation => 'GT100',
168         minlocation => 'GT90',
169         class_source => $class_source->{cn_source},
170     });
171
172     my $item_1 = $builder->build({ # Cannot call build_sample_item or cn_sort will be replaced by Koha::Item->store
173             source => 'Item',
174             value  => {
175                 itemcallnumber => 'GT95',
176                 cn_sort => GetClassSort($class_source->{cn_source},undef,'GT95'),
177             }
178     });
179
180     my( undef, $item_count) = GetItemsForInventory({
181         maxlocation => 'GT100',
182         minlocation => 'GT90',
183         class_source => $class_source->{cn_source},
184     });
185     is($item_count,$pre_item_count + 1,"We should return GT95 as between GT90 and GT100");
186     $schema->storage->txn_rollback;
187
188 };