3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 3;
25 use t::lib::TestBuilder;
38 # Mock userenv, used by AddIssue
40 my $context = Test::MockModule->new('C4::Context');
41 $context->mock( 'userenv', sub {
42 return { branch => $branch }
45 my $schema = Koha::Database->schema;
46 $schema->storage->txn_begin;
48 my $builder = t::lib::TestBuilder->new();
50 subtest "InProcessingToShelvingCart tests" => sub {
54 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
55 my $permanent_location = 'TEST';
56 my $location = 'PROC';
58 # Create a biblio record with biblio-level itemtype
59 my $record = MARC::Record->new();
60 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
61 my $built_item = $builder->build({
64 biblionumber => $biblionumber,
65 homebranch => $branch,
66 holdingbranch => $branch,
67 location => $location,
68 permanent_location => $permanent_location
71 my $barcode = $built_item->{ barcode };
72 my $itemnumber = $built_item->{ itemnumber };
75 t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 );
76 AddReturn( $barcode, $branch );
77 $item = GetItem( $itemnumber );
78 is( $item->{location}, 'CART',
79 "InProcessingToShelvingCart functions as intended" );
81 $item->{location} = $location;
82 ModItem( $item, undef, $itemnumber );
84 t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 );
85 AddReturn( $barcode, $branch );
86 $item = GetItem( $itemnumber );
87 is( $item->{location}, $permanent_location,
88 "InProcessingToShelvingCart functions as intended" );
92 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
96 # Set item-level item types
97 t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
99 # Make sure logging is enabled
100 t::lib::Mocks::mock_preference( "IssueLog", 1 );
101 t::lib::Mocks::mock_preference( "ReturnLog", 1 );
103 # Create an itemtype for biblio-level item type
104 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
105 # Create an itemtype for item-level item type
106 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
108 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
110 my $borrowernumber = $builder->build({
111 source => 'Borrower',
112 value => { branchcode => $branch }
113 })->{ borrowernumber };
114 # Look for the defined MARC field for biblio-level itemtype
115 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
117 kohafield => 'biblioitems.itemtype'
119 my $tagfield = $rs->first->tagfield;
120 my $tagsubfield = $rs->first->tagsubfield;
122 # Create a biblio record with biblio-level itemtype
123 my $record = MARC::Record->new();
124 $record->append_fields(
125 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
127 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
128 my $item_with_itemtype = $builder->build(
132 biblionumber => $biblionumber,
133 biblioitemnumber => $biblioitemnumber,
134 homebranch => $branch,
135 holdingbranch => $branch,
136 itype => $ilevel_itemtype
140 my $item_without_itemtype = $builder->build(
144 biblionumber => $biblionumber,
145 biblioitemnumber => $biblioitemnumber,
146 homebranch => $branch,
147 holdingbranch => $branch,
153 my $borrower = GetMember( borrowernumber => $borrowernumber );
154 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
155 AddReturn( $item_with_itemtype->{ barcode }, $branch );
156 # Test item-level itemtype was recorded on the 'statistics' table
157 my $stat = $schema->resultset('Statistic')->search({
160 itemnumber => $item_with_itemtype->{ itemnumber }
161 }, { order_by => { -asc => 'datetime' } })->next();
163 is( $stat->itemtype, $ilevel_itemtype,
164 "item-level itype recorded on statistics for return");
165 warning_like { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
166 qr/^item-level_itypes set but no itemtype set for item/,
167 'Item without itemtype set raises warning on AddIssue';
168 warning_like { AddReturn( $item_without_itemtype->{ barcode }, $branch ) }
169 qr/^item-level_itypes set but no itemtype set for item/,
170 'Item without itemtype set raises warning on AddReturn';
171 # Test biblio-level itemtype was recorded on the 'statistics' table
172 $stat = $schema->resultset('Statistic')->search({
175 itemnumber => $item_without_itemtype->{ itemnumber }
176 }, { order_by => { -asc => 'datetime' } })->next();
178 is( $stat->itemtype, $blevel_itemtype,
179 "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
183 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
187 # Make sure logging is enabled
188 t::lib::Mocks::mock_preference( "IssueLog", 1 );
189 t::lib::Mocks::mock_preference( "ReturnLog", 1 );
191 # Set item-level item types
192 t::lib::Mocks::mock_preference( "item-level_itypes", 0 );
194 # Create an itemtype for biblio-level item type
195 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
196 # Create an itemtype for item-level item type
197 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
199 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
201 my $borrowernumber = $builder->build({
202 source => 'Borrower',
203 value => { branchcode => $branch }
204 })->{ borrowernumber };
205 # Look for the defined MARC field for biblio-level itemtype
206 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
208 kohafield => 'biblioitems.itemtype'
210 my $tagfield = $rs->first->tagfield;
211 my $tagsubfield = $rs->first->tagsubfield;
213 # Create a biblio record with biblio-level itemtype
214 my $record = MARC::Record->new();
215 $record->append_fields(
216 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
218 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
219 my $item_with_itemtype = $builder->build({
222 biblionumber => $biblionumber,
223 homebranch => $branch,
224 holdingbranch => $branch,
225 itype => $ilevel_itemtype
228 my $item_without_itemtype = $builder->build({
231 biblionumber => $biblionumber,
232 homebranch => $branch,
233 holdingbranch => $branch,
238 my $borrower = GetMember( borrowernumber => $borrowernumber );
240 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
241 AddReturn( $item_with_itemtype->{ barcode }, $branch );
242 # Test item-level itemtype was recorded on the 'statistics' table
243 my $stat = $schema->resultset('Statistic')->search({
246 itemnumber => $item_with_itemtype->{ itemnumber }
247 }, { order_by => { -asc => 'datetime' } })->next();
249 is( $stat->itemtype, $blevel_itemtype,
250 "biblio-level itype recorded on statistics for return");
252 AddIssue( $borrower, $item_without_itemtype->{ barcode } );
253 AddReturn( $item_without_itemtype->{ barcode }, $branch );
254 # Test biblio-level itemtype was recorded on the 'statistics' table
255 $stat = $schema->resultset('Statistic')->search({
258 itemnumber => $item_without_itemtype->{ itemnumber }
259 }, { order_by => { -asc => 'datetime' } })->next();
261 is( $stat->itemtype, $blevel_itemtype,
262 "biblio-level itype recorded on statistics for return");