3 # Copyright 2020 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 12;
26 use Koha::BackgroundJobs;
27 use Koha::DateUtils qw( dt_from_string );
29 use t::lib::TestBuilder;
32 use t::lib::Koha::BackgroundJob::BatchTest;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 t::lib::Mocks::mock_userenv;
39 my $net_stomp = Test::MockModule->new('Net::Stomp');
40 $net_stomp->mock( 'send_with_receipt', sub { return 1 } );
42 my $background_job_module = Test::MockModule->new('Koha::BackgroundJob');
43 $background_job_module->mock(
44 'type_to_class_mapping',
46 return { batch_test => 't::lib::Koha::BackgroundJob::BatchTest' };
50 my $data = { a => 'aaa', b => 'bbb' };
52 my $job_id = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue(
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' );
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
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" );
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' );
87 { total_records => 10, total_success => 10 },
88 'Correct number of records processed'
91 is_deeply( $new_job->additional_report(), {} );
93 $schema->storage->txn_rollback;