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;
22 use t::lib::TestBuilder;
36 # Mock userenv, used by AddIssue
38 my $context = Test::MockModule->new('C4::Context');
39 $context->mock( 'userenv', sub {
40 return { branch => $branch }
43 my $schema = Koha::Database->schema;
44 $schema->storage->txn_begin;
46 my $builder = t::lib::TestBuilder->new();
48 subtest "InProcessingToShelvingCart tests" => sub {
52 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
53 my $permanent_location = 'TEST';
54 my $location = 'PROC';
56 # Create a biblio record with biblio-level itemtype
57 my $record = MARC::Record->new();
58 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
59 my $built_item = $builder->build({
62 biblionumber => $biblionumber,
63 homebranch => $branch,
64 holdingbranch => $branch,
65 location => $location,
66 permanent_location => $permanent_location
69 my $barcode = $built_item->{ barcode };
70 my $itemnumber = $built_item->{ itemnumber };
73 C4::Context->set_preference( "InProcessingToShelvingCart", 1 );
74 AddReturn( $barcode, $branch );
75 $item = GetItem( $itemnumber );
76 is( $item->{location}, 'CART',
77 "InProcessingToShelvingCart functions as intended" );
79 $item->{location} = $location;
80 ModItem( $item, undef, $itemnumber );
82 C4::Context->set_preference( "InProcessingToShelvingCart", 0 );
83 AddReturn( $barcode, $branch );
84 $item = GetItem( $itemnumber );
85 is( $item->{location}, $permanent_location,
86 "InProcessingToShelvingCart functions as intended" );
90 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
94 # Set item-level item types
95 C4::Context->set_preference( "item-level_itypes", 1 );
97 # Make sure logging is enabled
98 C4::Context->set_preference( "IssueLog", 1 );
99 C4::Context->set_preference( "ReturnLog", 1 );
101 # Create an itemtype for biblio-level item type
102 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
103 # Create an itemtype for item-level item type
104 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
106 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
108 my $borrowernumber = $builder->build({
109 source => 'Borrower',
110 value => { branchcode => $branch }
111 })->{ borrowernumber };
112 # Look for the defined MARC field for biblio-level itemtype
113 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
115 kohafield => 'biblioitems.itemtype'
117 my $tagfield = $rs->first->tagfield;
118 my $tagsubfield = $rs->first->tagsubfield;
120 # Create a biblio record with biblio-level itemtype
121 my $record = MARC::Record->new();
122 $record->append_fields(
123 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
125 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
126 my $item_with_itemtype = $builder->build(
130 biblionumber => $biblionumber,
131 biblioitemnumber => $biblioitemnumber,
132 homebranch => $branch,
133 holdingbranch => $branch,
134 itype => $ilevel_itemtype
138 my $item_without_itemtype = $builder->build(
142 biblionumber => $biblionumber,
143 biblioitemnumber => $biblioitemnumber,
144 homebranch => $branch,
145 holdingbranch => $branch,
151 my $borrower = GetMember( borrowernumber => $borrowernumber );
152 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
153 AddReturn( $item_with_itemtype->{ barcode }, $branch );
154 # Test item-level itemtype was recorded on the 'statistics' table
155 my $stat = $schema->resultset('Statistic')->search({
158 itemnumber => $item_with_itemtype->{ itemnumber }
159 }, { order_by => { -asc => 'datetime' } })->next();
161 is( $stat->itemtype, $ilevel_itemtype,
162 "item-level itype recorded on statistics for return");
164 AddIssue( $borrower, $item_without_itemtype->{ barcode } );
165 AddReturn( $item_without_itemtype->{ barcode }, $branch );
166 # Test biblio-level itemtype was recorded on the 'statistics' table
167 $stat = $schema->resultset('Statistic')->search({
170 itemnumber => $item_without_itemtype->{ itemnumber }
171 }, { order_by => { -asc => 'datetime' } })->next();
173 is( $stat->itemtype, $blevel_itemtype,
174 "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
178 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
182 # Make sure logging is enabled
183 C4::Context->set_preference( "IssueLog", 1 );
184 C4::Context->set_preference( "ReturnLog", 1 );
186 # Set item-level item types
187 C4::Context->set_preference( "item-level_itypes", 0 );
189 # Create an itemtype for biblio-level item type
190 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
191 # Create an itemtype for item-level item type
192 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
194 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
196 my $borrowernumber = $builder->build({
197 source => 'Borrower',
198 value => { branchcode => $branch }
199 })->{ borrowernumber };
200 # Look for the defined MARC field for biblio-level itemtype
201 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
203 kohafield => 'biblioitems.itemtype'
205 my $tagfield = $rs->first->tagfield;
206 my $tagsubfield = $rs->first->tagsubfield;
208 # Create a biblio record with biblio-level itemtype
209 my $record = MARC::Record->new();
210 $record->append_fields(
211 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
213 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
214 my $item_with_itemtype = $builder->build({
217 biblionumber => $biblionumber,
218 homebranch => $branch,
219 holdingbranch => $branch,
220 itype => $ilevel_itemtype
223 my $item_without_itemtype = $builder->build({
226 biblionumber => $biblionumber,
227 homebranch => $branch,
228 holdingbranch => $branch,
233 my $borrower = GetMember( borrowernumber => $borrowernumber );
235 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
236 AddReturn( $item_with_itemtype->{ barcode }, $branch );
237 # Test item-level itemtype was recorded on the 'statistics' table
238 my $stat = $schema->resultset('Statistic')->search({
241 itemnumber => $item_with_itemtype->{ itemnumber }
242 }, { order_by => { -asc => 'datetime' } })->next();
244 is( $stat->itemtype, $blevel_itemtype,
245 "biblio-level itype recorded on statistics for return");
247 AddIssue( $borrower, $item_without_itemtype->{ barcode } );
248 AddReturn( $item_without_itemtype->{ barcode }, $branch );
249 # Test biblio-level itemtype was recorded on the 'statistics' table
250 $stat = $schema->resultset('Statistic')->search({
253 itemnumber => $item_without_itemtype->{ itemnumber }
254 }, { order_by => { -asc => 'datetime' } })->next();
256 is( $stat->itemtype, $blevel_itemtype,
257 "biblio-level itype recorded on statistics for return");