Bug 28931: Use EXPORT_OK from Koha::DateUtils
[koha.git] / t / db_dependent / Koha / BackgroundJobs.t
1 #!/usr/bin/perl
2
3 # Copyright 2020 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 12;
23 use Test::MockModule;
24 use JSON qw( decode_json );
25
26 use Koha::Database;
27 use Koha::BackgroundJobs;
28 use Koha::DateUtils qw( dt_from_string );
29
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
32 use t::lib::Dates;
33 use t::lib::Koha::BackgroundJob::BatchTest;
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 t::lib::Mocks::mock_userenv;
39
40 my $net_stomp = Test::MockModule->new('Net::Stomp');
41 $net_stomp->mock( 'send_with_receipt', sub { return 1 } );
42
43 my $background_job_module = Test::MockModule->new('Koha::BackgroundJob');
44 $background_job_module->mock( '_derived_class',
45     sub { t::lib::Koha::BackgroundJob::BatchTest->new } );
46
47 my $data     = { a => 'aaa', b => 'bbb' };
48 my $job_size = 10;
49 my $job_id   = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue(
50     {
51         size => $job_size,
52         %$data
53     }
54 );
55
56 # Enqueuing a new job
57 my $new_job = Koha::BackgroundJobs->find($job_id);
58 ok( $new_job, 'New job correctly enqueued' );
59 is_deeply( decode_json( $new_job->data ),
60     $data, 'data retrieved and json encoded correctly' );
61 is( t::lib::Dates::compare( $new_job->enqueued_on, dt_from_string ),
62     0, 'enqueued_on correctly filled with now()' );
63 is( $new_job->size,   $job_size,    'job size retrieved correctly' );
64 is( $new_job->status, "new",        'job has not started yet, status is new' );
65 is( $new_job->type,   "batch_test", 'job type retrieved from ->job_type' );
66
67 # Test cancelled job
68 $new_job->status('cancelled')->store;
69 my $processed_job =
70   t::lib::Koha::BackgroundJob::BatchTest->process( { job_id => $new_job->id } );
71 is( $processed_job, undef );
72 $new_job->discard_changes;
73 is( $new_job->status, "cancelled", "A cancelled job has not been processed" );
74
75 # Test new job to process
76 $new_job->status('new')->store;
77 $new_job =
78   t::lib::Koha::BackgroundJob::BatchTest->process( { job_id => $new_job->id } );
79 is( $new_job->status,             "finished", 'job is new finished!' );
80 is( scalar( @{ $new_job->messages } ), 10,    '10 messages generated' );
81 is_deeply(
82     $new_job->report,
83     { total_records => 10, total_success => 10 },
84     'Correct number of records processed'
85 );
86
87 is_deeply( $new_job->additional_report(), {} );
88
89 $schema->storage->txn_rollback;