Bug 31351: (QA follow-up) Adjust tests accordingly
[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
25 use Koha::Database;
26 use Koha::BackgroundJobs;
27 use Koha::DateUtils qw( dt_from_string );
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31 use t::lib::Dates;
32 use t::lib::Koha::BackgroundJob::BatchTest;
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 t::lib::Mocks::mock_userenv;
38
39 my $net_stomp = Test::MockModule->new('Net::Stomp');
40 $net_stomp->mock( 'send_with_receipt', sub { return 1 } );
41
42 my $background_job_module = Test::MockModule->new('Koha::BackgroundJob');
43 $background_job_module->mock(
44     'type_to_class_mapping',
45     sub {
46         return { batch_test => 't::lib::Koha::BackgroundJob::BatchTest' };
47     }
48 );
49
50 my $data     = { a => 'aaa', b => 'bbb' };
51 my $job_size = 10;
52 my $job_id   = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue(
53     {
54         size => $job_size,
55         %$data
56     }
57 );
58
59 # Enqueuing a new job
60 my $new_job = Koha::BackgroundJobs->find($job_id);
61 ok( $new_job, 'New job correctly enqueued' );
62 is_deeply( $new_job->json->decode( $new_job->data ),
63     $data, 'data retrieved and json encoded correctly' );
64 is( t::lib::Dates::compare( $new_job->enqueued_on, dt_from_string ),
65     0, 'enqueued_on correctly filled with now()' );
66 is( $new_job->size,   $job_size,    'job size retrieved correctly' );
67 is( $new_job->status, "new",        'job has not started yet, status is new' );
68 is( $new_job->type,   "batch_test", 'job type retrieved from ->job_type' );
69
70 # FIXME: This behavior doesn't seem correct. It shouldn't be the background job's
71 #        responsibility to return 'undef'. Some higher-level check should raise a
72 #        proper exception.
73 # Test cancelled job
74 $new_job->status('cancelled')->store;
75 my $processed_job = $new_job->process;
76 is( $processed_job, undef );
77 $new_job->discard_changes;
78 is( $new_job->status, "cancelled", "A cancelled job has not been processed" );
79
80 # Test new job to process
81 $new_job->status('new')->store;
82 $new_job = $new_job->process;
83 is( $new_job->status,             "finished", 'job is new finished!' );
84 is( scalar( @{ $new_job->messages } ), 10,    '10 messages generated' );
85 is_deeply(
86     $new_job->report,
87     { total_records => 10, total_success => 10 },
88     'Correct number of records processed'
89 );
90
91 is_deeply( $new_job->additional_report(), {} );
92
93 $schema->storage->txn_rollback;