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 => 15;
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 C4::Context->set_userenv( undef, q{} );
135 is( Koha::BackgroundJobs->search_limited->count, 0, 'No jobs found without userenv' );
136 C4::Context->set_userenv( $patron1->id, $patron1->userid );
137 is( Koha::BackgroundJobs->search_limited->count, 1, 'My job found' );
138 C4::Context->set_userenv( $patron2->id, $patron2->userid );
139 is( Koha::BackgroundJobs->search_limited->count, 0, 'No jobs for me' );
141 $schema->storage->txn_rollback;
144 subtest 'purge' => sub {
146 $schema->storage->txn_begin;
148 my $recent_date = dt_from_string;
149 my $old_date = dt_from_string->subtract({ days => 3 });
150 my $job_recent_t1_new = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'new', ended_on => $old_date, type => 'type1' } } );
151 my $job_recent_t2_fin = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $recent_date, type => 'type2' } } );
152 my $job_old_t1_fin = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $old_date, type => 'type1' } } );
153 my $job_old_t2_fin = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $old_date, type => 'type2' } } );
155 my $params = { job_types => ['type1'] , # Arrayref of jobtypes to be purged
156 days => 1, # Age in days of jobs to be purged
157 confirm => 0, # Confirm deletion
159 is( Koha::BackgroundJobs->purge($params), 1, 'Only the old finished type1 job would be purged' );
161 $params->{'job_types'} = ['all'];
162 is( Koha::BackgroundJobs->purge($params), 2, 'All finished old jobs would be purged with job_types = all' );
164 my $rs = Koha::BackgroundJobs->search(
166 id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
169 is( $rs->count, 4, 'All jobs still left in queue');
171 $params->{'job_types'} = ['type1'];
172 $params->{'confirm'} = 1;
173 is( Koha::BackgroundJobs->purge($params), 1, 'Only the old finished type1 job is purged' );
175 $rs = Koha::BackgroundJobs->search(
177 id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
180 is( $rs->count, 3, '3 jobs still left in queue');
182 $params->{'job_types'} = ['all'];
183 is( Koha::BackgroundJobs->purge($params), 1, 'The remaining old finished jobs is purged' );
184 $rs = Koha::BackgroundJobs->search(
186 id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
189 is( $rs->count, 2, '2 jobs still left in queue');
191 $rs = Koha::BackgroundJobs->search(
193 id => [ $job_recent_t1_new->id ]
196 is( $rs->count, 1, 'Unfinished job still left in queue');
198 $rs = Koha::BackgroundJobs->search(
200 id => [ $job_recent_t2_fin->id ]
203 is( $rs->count, 1, 'Recent finished job still left in queue');
205 $schema->storage->txn_rollback;