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