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 => 14;
25 use List::MoreUtils qw(any);
28 use Koha::BackgroundJobs;
29 use Koha::DateUtils qw( dt_from_string );
31 use t::lib::TestBuilder;
34 use t::lib::Koha::BackgroundJob::BatchTest;
36 my $builder = t::lib::TestBuilder->new;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
40 t::lib::Mocks::mock_userenv;
42 my $net_stomp = Test::MockModule->new('Net::Stomp');
43 $net_stomp->mock( 'send_with_receipt', sub { return 1 } );
45 my $background_job_module = Test::MockModule->new('Koha::BackgroundJob');
46 $background_job_module->mock(
47 'type_to_class_mapping',
49 return { batch_test => 't::lib::Koha::BackgroundJob::BatchTest' };
53 my $data = { a => 'aaa', b => 'bbb' };
55 my $job_id = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue(
63 my $new_job = Koha::BackgroundJobs->find($job_id);
64 ok( $new_job, 'New job correctly enqueued' );
65 is_deeply( $new_job->json->decode( $new_job->data ),
66 $data, 'data retrieved and json encoded correctly' );
67 is( t::lib::Dates::compare( $new_job->enqueued_on, dt_from_string ),
68 0, 'enqueued_on correctly filled with now()' );
69 is( $new_job->size, $job_size, 'job size retrieved correctly' );
70 is( $new_job->status, "new", 'job has not started yet, status is new' );
71 is( $new_job->type, "batch_test", 'job type retrieved from ->job_type' );
73 # FIXME: This behavior doesn't seem correct. It shouldn't be the background job's
74 # responsibility to return 'undef'. Some higher-level check should raise a
77 $new_job->status('cancelled')->store;
78 my $processed_job = $new_job->process;
79 is( $processed_job, undef );
80 $new_job->discard_changes;
81 is( $new_job->status, "cancelled", "A cancelled job has not been processed" );
83 # Test new job to process
84 $new_job->status('new')->store;
85 $new_job = $new_job->process;
86 is( $new_job->status, "finished", 'job is new finished!' );
87 is( scalar( @{ $new_job->messages } ), 10, '10 messages generated' );
90 { total_records => 10, total_success => 10 },
91 'Correct number of records processed'
94 is_deeply( $new_job->additional_report(), {} );
96 $schema->storage->txn_rollback;
98 subtest 'filter_by_current() tests' => sub {
102 $schema->storage->txn_begin;
104 my $job_new = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'new' } } );
105 my $job_cancelled = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'cancelled' } } );
106 my $job_failed = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'failed' } } );
107 my $job_finished = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished' } } );
109 my $rs = Koha::BackgroundJobs->search(
111 id => [ $job_new->id, $job_cancelled->id, $job_failed->id, $job_finished->id ]
115 is( $rs->count, 4, '4 jobs in resultset' );
116 ok( any {$_->status eq 'new'} @{$rs->as_list}, "There is a 'new' job" );
118 $rs = $rs->filter_by_current;
120 is( $rs->count, 1, 'Only 1 job in filtered resultset' );
121 is( $rs->next->status, 'new', "The only job in resultset is 'new'" );
123 $schema->storage->txn_rollback;
126 subtest 'search_limited' => sub {
129 $schema->storage->txn_begin;
130 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
131 my $patron2 = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
132 my $job1 = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { borrowernumber => $patron1->id } } );
134 my $cnt = Koha::BackgroundJobs->search({ borrowernumber => undef })->count; # expected to be zero, but theoretically possible
136 C4::Context->set_userenv( undef, q{} );
137 is( Koha::BackgroundJobs->search_limited->count, $cnt, 'No jobs found without userenv' );
138 C4::Context->set_userenv( $patron1->id, $patron1->userid );
139 is( Koha::BackgroundJobs->search_limited->count, 1, 'My job found' );
140 C4::Context->set_userenv( $patron2->id, $patron2->userid );
141 is( Koha::BackgroundJobs->search_limited->count, 0, 'No jobs for me' );
143 $schema->storage->txn_rollback;