Lines 1-58
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
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. |
9 |
# |
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. |
14 |
# |
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>. |
17 |
|
1 |
use Modern::Perl; |
18 |
use Modern::Perl; |
2 |
use Test::More tests => 2; |
19 |
|
|
|
20 |
use Test::More tests => 3; |
21 |
use Test::MockModule; |
22 |
use t::lib::TestBuilder; |
3 |
|
23 |
|
4 |
use t::lib::Mocks; |
24 |
use t::lib::Mocks; |
5 |
use C4::Biblio; |
25 |
use C4::Biblio; |
6 |
use C4::Circulation; |
26 |
use C4::Circulation; |
7 |
use C4::Items; |
27 |
use C4::Items; |
|
|
28 |
use C4::ItemType; |
8 |
use C4::Members; |
29 |
use C4::Members; |
9 |
use Koha::Database; |
30 |
use Koha::Database; |
10 |
use Koha::DateUtils; |
31 |
use Koha::DateUtils; |
11 |
|
32 |
use Koha::Items; |
12 |
use t::lib::TestBuilder; |
|
|
13 |
|
33 |
|
14 |
use MARC::Record; |
34 |
use MARC::Record; |
|
|
35 |
use MARC::Field; |
15 |
|
36 |
|
16 |
*C4::Context::userenv = \&Mock_userenv; |
37 |
# Mock userenv, used by AddIssue |
|
|
38 |
my $branch; |
39 |
my $context = Test::MockModule->new('C4::Context'); |
40 |
$context->mock( 'userenv', sub { |
41 |
return { branch => $branch } |
42 |
}); |
17 |
|
43 |
|
18 |
my $schema = Koha::Database->schema; |
44 |
my $schema = Koha::Database->schema; |
19 |
$schema->storage->txn_begin; |
45 |
$schema->storage->txn_begin; |
20 |
my $builder = t::lib::TestBuilder->new; |
|
|
21 |
|
46 |
|
22 |
my $library = $builder->build({ |
47 |
my $builder = t::lib::TestBuilder->new(); |
23 |
source => 'Branch', |
48 |
|
24 |
}); |
49 |
subtest "InProcessingToShelvingCart tests" => sub { |
|
|
50 |
|
51 |
plan tests => 2; |
52 |
|
53 |
$branch = $builder->build({ source => 'Branch' })->{ branchcode }; |
54 |
my $permanent_location = 'TEST'; |
55 |
my $location = 'PROC'; |
56 |
|
57 |
# Create a biblio record with biblio-level itemtype |
58 |
my $record = MARC::Record->new(); |
59 |
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' ); |
60 |
my $built_item = $builder->build({ |
61 |
source => 'Item', |
62 |
value => { |
63 |
biblionumber => $biblionumber, |
64 |
homebranch => $branch, |
65 |
holdingbranch => $branch, |
66 |
location => $location, |
67 |
permanent_location => $permanent_location |
68 |
} |
69 |
}); |
70 |
my $barcode = $built_item->{ barcode }; |
71 |
my $itemnumber = $built_item->{ itemnumber }; |
72 |
my $item; |
73 |
|
74 |
C4::Context->set_preference( "InProcessingToShelvingCart", 1 ); |
75 |
AddReturn( $barcode, $branch ); |
76 |
$item = GetItem( $itemnumber ); |
77 |
is( $item->{location}, 'CART', |
78 |
"InProcessingToShelvingCart functions as intended" ); |
79 |
|
80 |
$item->{location} = $location; |
81 |
ModItem( $item, undef, $itemnumber ); |
82 |
|
83 |
C4::Context->set_preference( "InProcessingToShelvingCart", 0 ); |
84 |
AddReturn( $barcode, $branch ); |
85 |
$item = GetItem( $itemnumber ); |
86 |
is( $item->{location}, $permanent_location, |
87 |
"InProcessingToShelvingCart functions as intended" ); |
88 |
}; |
89 |
|
90 |
|
91 |
subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub { |
92 |
|
93 |
plan tests => 2; |
94 |
|
95 |
# Set item-level item types |
96 |
C4::Context->set_preference( "item-level_itypes", 1 ); |
97 |
|
98 |
# Make sure logging is enabled |
99 |
C4::Context->set_preference( "IssueLog", 1 ); |
100 |
C4::Context->set_preference( "ReturnLog", 1 ); |
101 |
|
102 |
# Create an itemtype for biblio-level item type |
103 |
my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype }; |
104 |
# Create an itemtype for item-level item type |
105 |
my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype }; |
106 |
# Create a branch |
107 |
$branch = $builder->build({ source => 'Branch' })->{ branchcode }; |
108 |
# Create a borrower |
109 |
my $borrowernumber = $builder->build({ |
110 |
source => 'Borrower', |
111 |
value => { branchcode => $branch } |
112 |
})->{ borrowernumber }; |
113 |
# Look for the defined MARC field for biblio-level itemtype |
114 |
my $rs = $schema->resultset('MarcSubfieldStructure')->search({ |
115 |
frameworkcode => '', |
116 |
kohafield => 'biblioitems.itemtype' |
117 |
}); |
118 |
my $tagfield = $rs->first->tagfield; |
119 |
my $tagsubfield = $rs->first->tagsubfield; |
120 |
|
121 |
# Create a biblio record with biblio-level itemtype |
122 |
my $record = MARC::Record->new(); |
123 |
$record->append_fields( |
124 |
MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype ) |
125 |
); |
126 |
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' ); |
127 |
my $item_with_itemtype = $builder->build({ |
128 |
source => 'Item', |
129 |
value => { |
130 |
biblionumber => $biblionumber, |
131 |
homebranch => $branch, |
132 |
holdingbranch => $branch, |
133 |
itype => $ilevel_itemtype |
134 |
} |
135 |
}); |
136 |
my $item_without_itemtype = $builder->build({ |
137 |
source => 'Item', |
138 |
value => { |
139 |
biblionumber => $biblionumber, |
140 |
homebranch => $branch, |
141 |
holdingbranch => $branch, |
142 |
itype => undef |
143 |
} |
144 |
}); |
145 |
|
146 |
my $borrower = GetMember( borrowernumber => $borrowernumber ); |
147 |
AddIssue( $borrower, $item_with_itemtype->{ barcode } ); |
148 |
AddReturn( $item_with_itemtype->{ barcode }, $branch ); |
149 |
# Test item-level itemtype was recorded on the 'statistics' table |
150 |
my $stat = $schema->resultset('Statistic')->search({ |
151 |
branch => $branch, |
152 |
type => 'return', |
153 |
itemnumber => $item_with_itemtype->{ itemnumber } |
154 |
}, { order_by => { -asc => 'datetime' } })->next(); |
155 |
|
156 |
is( $stat->itemtype, $ilevel_itemtype, |
157 |
"item-level itype recorded on statistics for return"); |
158 |
|
159 |
AddIssue( $borrower, $item_without_itemtype->{ barcode } ); |
160 |
AddReturn( $item_without_itemtype->{ barcode }, $branch ); |
161 |
# Test biblio-level itemtype was recorded on the 'statistics' table |
162 |
$stat = $schema->resultset('Statistic')->search({ |
163 |
branch => $branch, |
164 |
type => 'return', |
165 |
itemnumber => $item_without_itemtype->{ itemnumber } |
166 |
}, { order_by => { -asc => 'datetime' } })->next(); |
167 |
|
168 |
is( $stat->itemtype, $blevel_itemtype, |
169 |
"biblio-level itype recorded on statistics for return as a fallback for null item-level itype"); |
170 |
|
171 |
}; |
172 |
|
173 |
subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub { |
174 |
|
175 |
plan tests => 2; |
176 |
|
177 |
# Make sure logging is enabled |
178 |
C4::Context->set_preference( "IssueLog", 1 ); |
179 |
C4::Context->set_preference( "ReturnLog", 1 ); |
180 |
|
181 |
# Set item-level item types |
182 |
C4::Context->set_preference( "item-level_itypes", 0 ); |
183 |
|
184 |
# Create an itemtype for biblio-level item type |
185 |
my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype }; |
186 |
# Create an itemtype for item-level item type |
187 |
my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype }; |
188 |
# Create a branch |
189 |
$branch = $builder->build({ source => 'Branch' })->{ branchcode }; |
190 |
# Create a borrower |
191 |
my $borrowernumber = $builder->build({ |
192 |
source => 'Borrower', |
193 |
value => { branchcode => $branch } |
194 |
})->{ borrowernumber }; |
195 |
# Look for the defined MARC field for biblio-level itemtype |
196 |
my $rs = $schema->resultset('MarcSubfieldStructure')->search({ |
197 |
frameworkcode => '', |
198 |
kohafield => 'biblioitems.itemtype' |
199 |
}); |
200 |
my $tagfield = $rs->first->tagfield; |
201 |
my $tagsubfield = $rs->first->tagsubfield; |
202 |
|
203 |
# Create a biblio record with biblio-level itemtype |
204 |
my $record = MARC::Record->new(); |
205 |
$record->append_fields( |
206 |
MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype ) |
207 |
); |
208 |
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' ); |
209 |
my $item_with_itemtype = $builder->build({ |
210 |
source => 'Item', |
211 |
value => { |
212 |
biblionumber => $biblionumber, |
213 |
homebranch => $branch, |
214 |
holdingbranch => $branch, |
215 |
itype => $ilevel_itemtype |
216 |
} |
217 |
}); |
218 |
my $item_without_itemtype = $builder->build({ |
219 |
source => 'Item', |
220 |
value => { |
221 |
biblionumber => $biblionumber, |
222 |
homebranch => $branch, |
223 |
holdingbranch => $branch, |
224 |
itype => undef |
225 |
} |
226 |
}); |
227 |
|
228 |
my $borrower = GetMember( borrowernumber => $borrowernumber ); |
229 |
|
230 |
AddIssue( $borrower, $item_with_itemtype->{ barcode } ); |
231 |
AddReturn( $item_with_itemtype->{ barcode }, $branch ); |
232 |
# Test item-level itemtype was recorded on the 'statistics' table |
233 |
my $stat = $schema->resultset('Statistic')->search({ |
234 |
branch => $branch, |
235 |
type => 'return', |
236 |
itemnumber => $item_with_itemtype->{ itemnumber } |
237 |
}, { order_by => { -asc => 'datetime' } })->next(); |
238 |
|
239 |
is( $stat->itemtype, $blevel_itemtype, |
240 |
"biblio-level itype recorded on statistics for return"); |
241 |
|
242 |
AddIssue( $borrower, $item_without_itemtype->{ barcode } ); |
243 |
AddReturn( $item_without_itemtype->{ barcode }, $branch ); |
244 |
# Test biblio-level itemtype was recorded on the 'statistics' table |
245 |
$stat = $schema->resultset('Statistic')->search({ |
246 |
branch => $branch, |
247 |
type => 'return', |
248 |
itemnumber => $item_without_itemtype->{ itemnumber } |
249 |
}, { order_by => { -asc => 'datetime' } })->next(); |
250 |
|
251 |
is( $stat->itemtype, $blevel_itemtype, |
252 |
"biblio-level itype recorded on statistics for return"); |
253 |
}; |
25 |
|
254 |
|
26 |
my $record = MARC::Record->new(); |
255 |
1; |
27 |
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' ); |
|
|
28 |
|
29 |
my ( undef, undef, $itemnumber ) = AddItem( |
30 |
{ |
31 |
homebranch => $library->{branchcode}, |
32 |
holdingbranch => $library->{branchcode}, |
33 |
barcode => 'i_dont_exist', |
34 |
location => 'PROC', |
35 |
permanent_location => 'TEST' |
36 |
}, |
37 |
$biblionumber |
38 |
); |
39 |
|
40 |
my $item; |
41 |
|
42 |
t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 ); |
43 |
AddReturn( 'i_dont_exist', $library->{branchcode} ); |
44 |
$item = GetItem($itemnumber); |
45 |
is( $item->{location}, 'CART', "InProcessingToShelvingCart functions as intended" ); |
46 |
|
47 |
$item->{location} = 'PROC'; |
48 |
ModItem( $item, undef, $itemnumber ); |
49 |
|
50 |
t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 ); |
51 |
AddReturn( 'i_dont_exist', $library->{branchcode} ); |
52 |
$item = GetItem($itemnumber); |
53 |
is( $item->{location}, 'TEST', "InProcessingToShelvingCart functions as intended" ); |
54 |
|
55 |
# C4::Context->userenv |
56 |
sub Mock_userenv { |
57 |
return { branch => $library->{branchcode} }; |
58 |
} |
59 |
- |
|
|