|
Line 0
Link Here
|
| 0 |
- |
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 <https://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::NoWarnings; |
| 21 |
use Test::More tests => 4; |
| 22 |
use t::lib::TestBuilder; |
| 23 |
use t::lib::Mocks; |
| 24 |
use File::Spec; |
| 25 |
use File::Basename; |
| 26 |
|
| 27 |
use Koha::DateUtils qw( dt_from_string ); |
| 28 |
|
| 29 |
my $scriptDir = dirname( File::Spec->rel2abs(__FILE__) ); |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $dbh = C4::Context->dbh; |
| 33 |
|
| 34 |
my $library1; |
| 35 |
my $library2; |
| 36 |
my $basket1; |
| 37 |
my $basket2; |
| 38 |
my $bookseller1; |
| 39 |
my $bookseller2; |
| 40 |
my $contact1; |
| 41 |
my $contact2; |
| 42 |
my $order1; |
| 43 |
my $order2; |
| 44 |
my $order3; |
| 45 |
|
| 46 |
sub build_test_objects { |
| 47 |
t::lib::Mocks::mock_preference( 'dateformat', 'metric' ); |
| 48 |
|
| 49 |
my $builder = t::lib::TestBuilder->new; |
| 50 |
|
| 51 |
$library1 = $builder->build( |
| 52 |
{ |
| 53 |
source => 'Branch', |
| 54 |
} |
| 55 |
); |
| 56 |
$library2 = $builder->build( |
| 57 |
{ |
| 58 |
source => 'Branch', |
| 59 |
} |
| 60 |
); |
| 61 |
|
| 62 |
$bookseller1 = $builder->build( |
| 63 |
{ |
| 64 |
source => 'Aqbookseller', |
| 65 |
value => { |
| 66 |
name => 'Bookseller 1', |
| 67 |
deliverytime => 10, |
| 68 |
} |
| 69 |
} |
| 70 |
); |
| 71 |
|
| 72 |
$bookseller2 = $builder->build( |
| 73 |
{ |
| 74 |
source => 'Aqbookseller', |
| 75 |
value => { |
| 76 |
name => 'Bookseller 2', |
| 77 |
deliverytime => 15, |
| 78 |
} |
| 79 |
} |
| 80 |
); |
| 81 |
|
| 82 |
$contact1 = $builder->build( |
| 83 |
{ |
| 84 |
source => 'Aqcontact', |
| 85 |
value => { |
| 86 |
booksellerid => $bookseller1->{id}, |
| 87 |
claimacquisition => 1, |
| 88 |
email => 'contact1@example.com', |
| 89 |
} |
| 90 |
} |
| 91 |
); |
| 92 |
|
| 93 |
$contact2 = $builder->build( |
| 94 |
{ |
| 95 |
source => 'Aqcontact', |
| 96 |
value => { |
| 97 |
booksellerid => $bookseller2->{id}, |
| 98 |
claimacquisition => 1, |
| 99 |
email => 'contact2@example.com', |
| 100 |
} |
| 101 |
} |
| 102 |
); |
| 103 |
|
| 104 |
$basket1 = $builder->build( |
| 105 |
{ |
| 106 |
source => 'Aqbasket', |
| 107 |
value => { |
| 108 |
booksellerid => $bookseller1->{id}, |
| 109 |
branch => $library1->{branchcode}, |
| 110 |
basketname => 'Basket 1', |
| 111 |
} |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
$basket2 = $builder->build( |
| 116 |
{ |
| 117 |
source => 'Aqbasket', |
| 118 |
value => { |
| 119 |
booksellerid => $bookseller2->{id}, |
| 120 |
branch => $library2->{branchcode}, |
| 121 |
basketname => 'Basket 2', |
| 122 |
} |
| 123 |
} |
| 124 |
); |
| 125 |
|
| 126 |
# Create a budget for orders |
| 127 |
my $bpid = C4::Budgets::AddBudgetPeriod( |
| 128 |
{ |
| 129 |
budget_period_startdate => '2008-01-01', |
| 130 |
budget_period_enddate => '2008-12-31', |
| 131 |
budget_period_active => 1, |
| 132 |
budget_period_description => "TEST_PERIOD" |
| 133 |
} |
| 134 |
); |
| 135 |
|
| 136 |
my $budgetid = C4::Budgets::AddBudget( |
| 137 |
{ |
| 138 |
budget_code => "TEST_BUDGET", |
| 139 |
budget_name => "Test Budget", |
| 140 |
budget_period_id => $bpid, |
| 141 |
} |
| 142 |
); |
| 143 |
|
| 144 |
my $budget = C4::Budgets::GetBudget($budgetid); |
| 145 |
|
| 146 |
# Create test orders using the same style as Acquisition.t |
| 147 |
my @order_content = ( |
| 148 |
{ |
| 149 |
str => { |
| 150 |
basketno => $basket1->{basketno}, |
| 151 |
biblionumber => $builder->build_sample_biblio( { title => 'Test Title 1' } )->biblionumber, |
| 152 |
budget_id => $budget->{budget_id}, |
| 153 |
orderstatus => 'ordered', |
| 154 |
entrydate => dt_from_string->subtract( days => 20 ), |
| 155 |
quantity => 2, |
| 156 |
listprice => 10.00, |
| 157 |
}, |
| 158 |
num => { |
| 159 |
quantity => 2, |
| 160 |
} |
| 161 |
}, |
| 162 |
{ |
| 163 |
str => { |
| 164 |
basketno => $basket1->{basketno}, |
| 165 |
biblionumber => $builder->build_sample_biblio( { title => 'Test Title 2' } )->biblionumber, |
| 166 |
budget_id => $budget->{budget_id}, |
| 167 |
orderstatus => 'ordered', |
| 168 |
entrydate => dt_from_string->subtract( days => 25 ), |
| 169 |
quantity => 1, |
| 170 |
listprice => 15.00, |
| 171 |
}, |
| 172 |
num => { |
| 173 |
quantity => 1, |
| 174 |
} |
| 175 |
}, |
| 176 |
{ |
| 177 |
str => { |
| 178 |
basketno => $basket2->{basketno}, |
| 179 |
biblionumber => $builder->build_sample_biblio( { title => 'Test Title 3' } )->biblionumber, |
| 180 |
budget_id => $budget->{budget_id}, |
| 181 |
orderstatus => 'ordered', |
| 182 |
entrydate => dt_from_string->subtract( days => 30 ), |
| 183 |
quantity => 3, |
| 184 |
listprice => 20.00, |
| 185 |
}, |
| 186 |
num => { |
| 187 |
quantity => 3, |
| 188 |
} |
| 189 |
} |
| 190 |
); |
| 191 |
|
| 192 |
# Create orders in database |
| 193 |
my @ordernumbers; |
| 194 |
for ( 0 .. 2 ) { |
| 195 |
my %ocontent; |
| 196 |
@ocontent{ keys %{ $order_content[$_]->{num} } } = values %{ $order_content[$_]->{num} }; |
| 197 |
@ocontent{ keys %{ $order_content[$_]->{str} } } = values %{ $order_content[$_]->{str} }; |
| 198 |
$ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->store->ordernumber; |
| 199 |
$order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_]; |
| 200 |
} |
| 201 |
|
| 202 |
# Close the baskets to make orders late |
| 203 |
my $basket1_obj = Koha::Acquisition::Baskets->find( $basket1->{basketno} ); |
| 204 |
my $basket2_obj = Koha::Acquisition::Baskets->find( $basket2->{basketno} ); |
| 205 |
|
| 206 |
# Set closedate to a date that would make the orders late |
| 207 |
my $late_date = dt_from_string->subtract( days => 30 ); |
| 208 |
$basket1_obj->closedate($late_date); |
| 209 |
$basket1_obj->store; |
| 210 |
$basket2_obj->closedate($late_date); |
| 211 |
$basket2_obj->store; |
| 212 |
|
| 213 |
$order1 = Koha::Acquisition::Orders->find( $ordernumbers[0] ); |
| 214 |
$order2 = Koha::Acquisition::Orders->find( $ordernumbers[1] ); |
| 215 |
$order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] ); |
| 216 |
} |
| 217 |
|
| 218 |
sub run_script { |
| 219 |
my $script = shift; |
| 220 |
local @ARGV = @_; |
| 221 |
|
| 222 |
# We simulate script execution by evaluating the script code in the context |
| 223 |
# of this unit test. |
| 224 |
|
| 225 |
eval $script; ## no critic (StringyEval) |
| 226 |
|
| 227 |
die $@ if $@; |
| 228 |
} |
| 229 |
|
| 230 |
# Load the script content once |
| 231 |
my $scriptContent = ''; |
| 232 |
my $scriptFile = "$scriptDir/../../../misc/cronjobs/ordersClaim.pl"; |
| 233 |
open my $scriptfh, "<", $scriptFile or die "Failed to open $scriptFile: $!"; |
| 234 |
|
| 235 |
while (<$scriptfh>) { |
| 236 |
$scriptContent .= $_; |
| 237 |
} |
| 238 |
close $scriptfh; |
| 239 |
|
| 240 |
# Evaluate the script content once to define all subroutines |
| 241 |
{ |
| 242 |
local @ARGV = (); |
| 243 |
eval $scriptContent; ## no critic (StringyEval) |
| 244 |
die $@ if $@; |
| 245 |
} |
| 246 |
|
| 247 |
my $sthmq = $dbh->prepare('SELECT * FROM message_queue WHERE to_address = ?'); |
| 248 |
|
| 249 |
subtest 'Default behaviour tests' => sub { |
| 250 |
|
| 251 |
plan tests => 2; |
| 252 |
|
| 253 |
$schema->storage->txn_begin; |
| 254 |
|
| 255 |
build_test_objects(); |
| 256 |
|
| 257 |
# Set up arguments for the script |
| 258 |
@ARGV = ( 'ordersClaim.pl', '--delay', '15', '--claimed-for', '5', '--max-claims', '2' ); |
| 259 |
|
| 260 |
# Parse arguments as the script would |
| 261 |
my $help; |
| 262 |
my $delay = 15; |
| 263 |
my $claimed_for = 5; |
| 264 |
my $max_claims = 2; |
| 265 |
my $letter_code = 'ACQCLAIM'; |
| 266 |
|
| 267 |
# Process the orders directly using the subroutines |
| 268 |
my $late_orders = get_late_orders($delay); |
| 269 |
my $baskets_orders = get_basket_orders($late_orders); |
| 270 |
|
| 271 |
while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { |
| 272 |
process_basket( $basketno, $orderids, $max_claims, $claimed_for, $letter_code ); |
| 273 |
} |
| 274 |
|
| 275 |
$sthmq->execute('contact1@example.com'); |
| 276 |
|
| 277 |
my $messages = $sthmq->fetchall_hashref('message_id'); |
| 278 |
|
| 279 |
is( scalar( keys %$messages ), 1, 'There is one message in the queue for contact1' ); |
| 280 |
|
| 281 |
$sthmq->execute('contact2@example.com'); |
| 282 |
|
| 283 |
$messages = $sthmq->fetchall_hashref('message_id'); |
| 284 |
|
| 285 |
is( scalar( keys %$messages ), 1, 'There is one message in the queue for contact2' ); |
| 286 |
|
| 287 |
$schema->storage->txn_rollback; |
| 288 |
}; |
| 289 |
|
| 290 |
subtest 'No orders found tests' => sub { |
| 291 |
|
| 292 |
plan tests => 1; |
| 293 |
|
| 294 |
$schema->storage->txn_begin; |
| 295 |
|
| 296 |
build_test_objects(); |
| 297 |
|
| 298 |
$dbh->do('DELETE FROM aqorders'); |
| 299 |
|
| 300 |
# Set up arguments for the script |
| 301 |
@ARGV = ( 'ordersClaim.pl', '--delay', '15', '--claimed-for', '5', '--max-claims', '2' ); |
| 302 |
|
| 303 |
# Parse arguments as the script would |
| 304 |
my $help; |
| 305 |
my $delay = 15; |
| 306 |
my $claimed_for = 5; |
| 307 |
my $max_claims = 2; |
| 308 |
my $letter_code = 'ACQCLAIM'; |
| 309 |
|
| 310 |
# Process the orders directly using the subroutines |
| 311 |
my $late_orders = get_late_orders($delay); |
| 312 |
my $baskets_orders = get_basket_orders($late_orders); |
| 313 |
|
| 314 |
while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { |
| 315 |
process_basket( $basketno, $orderids, $max_claims, $claimed_for, $letter_code ); |
| 316 |
} |
| 317 |
|
| 318 |
$sthmq->execute('contact1@example.com'); |
| 319 |
|
| 320 |
my $messages = $sthmq->fetchall_hashref('message_id'); |
| 321 |
|
| 322 |
is( scalar( keys %$messages ), 0, 'There are no messages in the queue for contact1 when no orders exist' ); |
| 323 |
|
| 324 |
$schema->storage->txn_rollback; |
| 325 |
}; |
| 326 |
|
| 327 |
subtest 'Error handling tests' => sub { |
| 328 |
|
| 329 |
plan tests => 1; |
| 330 |
|
| 331 |
$schema->storage->txn_begin; |
| 332 |
|
| 333 |
build_test_objects(); |
| 334 |
|
| 335 |
# Set up arguments for the script |
| 336 |
@ARGV = ( 'ordersClaim.pl', '--delay', '15', '--claimed-for', '5', '--max-claims', '2' ); |
| 337 |
|
| 338 |
# Parse arguments as the script would |
| 339 |
my $help; |
| 340 |
my $delay = 15; |
| 341 |
my $claimed_for = 5; |
| 342 |
my $max_claims = 2; |
| 343 |
my $letter_code = 'ACQCLAIM'; |
| 344 |
|
| 345 |
# Process the orders directly using the subroutines |
| 346 |
my $late_orders = get_late_orders($delay); |
| 347 |
my $baskets_orders = get_basket_orders($late_orders); |
| 348 |
|
| 349 |
while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { |
| 350 |
process_basket( $basketno, $orderids, $max_claims, $claimed_for, $letter_code ); |
| 351 |
} |
| 352 |
|
| 353 |
ok( 1, 'Script runs without crashing on missing basket' ); |
| 354 |
|
| 355 |
$schema->storage->txn_rollback; |
| 356 |
}; |