Bug 31969: (follow-up) Unit tests for Koha::BackgroundJobs->purge
[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 => 15;
23 use Test::MockModule;
24
25 use List::MoreUtils qw(any);
26
27 use Koha::Database;
28 use Koha::BackgroundJobs;
29 use Koha::DateUtils qw( dt_from_string );
30
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
33 use t::lib::Dates;
34 use t::lib::Koha::BackgroundJob::BatchTest;
35
36 my $builder = t::lib::TestBuilder->new;
37 my $schema  = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39
40 t::lib::Mocks::mock_userenv;
41
42 my $net_stomp = Test::MockModule->new('Net::Stomp');
43 $net_stomp->mock( 'send_with_receipt', sub { return 1 } );
44
45 my $background_job_module = Test::MockModule->new('Koha::BackgroundJob');
46 $background_job_module->mock(
47     'type_to_class_mapping',
48     sub {
49         return { batch_test => 't::lib::Koha::BackgroundJob::BatchTest' };
50     }
51 );
52
53 my $data     = { a => 'aaa', b => 'bbb' };
54 my $job_size = 10;
55 my $job_id   = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue(
56     {
57         size => $job_size,
58         %$data
59     }
60 );
61
62 # Enqueuing a new job
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' );
72
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
75 #        proper exception.
76 # Test cancelled job
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" );
82
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' );
88 is_deeply(
89     $new_job->report,
90     { total_records => 10, total_success => 10 },
91     'Correct number of records processed'
92 );
93
94 is_deeply( $new_job->additional_report(), {} );
95
96 $schema->storage->txn_rollback;
97
98 subtest 'filter_by_current() tests' => sub {
99
100     plan tests => 4;
101
102     $schema->storage->txn_begin;
103
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' } } );
108
109     my $rs = Koha::BackgroundJobs->search(
110         {
111             id => [ $job_new->id, $job_cancelled->id, $job_failed->id, $job_finished->id ]
112         }
113     );
114
115     is( $rs->count, 4, '4 jobs in resultset' );
116     ok( any {$_->status eq 'new'} @{$rs->as_list}, "There is a 'new' job"  );
117
118     $rs = $rs->filter_by_current;
119
120     is( $rs->count, 1, 'Only 1 job in filtered resultset' );
121     is( $rs->next->status, 'new', "The only job in resultset is 'new'"  );
122
123     $schema->storage->txn_rollback;
124 };
125
126 subtest 'search_limited' => sub {
127     plan tests => 3;
128
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 } } );
133
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' );
140
141     $schema->storage->txn_rollback;
142 };
143
144 subtest 'purge' => sub {
145     plan tests => 9;
146     $schema->storage->txn_begin;
147
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' } } );
154
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
158                 };
159     is( Koha::BackgroundJobs->purge($params), 1, 'Only the old finished type1 job would be purged' );
160
161     $params->{'job_types'} = ['all'];
162     is( Koha::BackgroundJobs->purge($params), 2, 'All finished old jobs would be purged with job_types = all' );
163
164     my $rs = Koha::BackgroundJobs->search(
165         {
166             id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
167         }
168     );
169     is( $rs->count, 4, 'All jobs still left in queue');
170
171     $params->{'job_types'} = ['type1'];
172     $params->{'confirm'} = 1;
173     is( Koha::BackgroundJobs->purge($params), 1, 'Only the old finished type1 job is purged' );
174
175     $rs = Koha::BackgroundJobs->search(
176         {
177             id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
178         }
179     );
180     is( $rs->count, 3, '3 jobs still left in queue');
181
182     $params->{'job_types'} = ['all'];
183     is( Koha::BackgroundJobs->purge($params), 1, 'The remaining old finished jobs is purged' );
184         $rs = Koha::BackgroundJobs->search(
185         {
186             id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
187         }
188     );
189     is( $rs->count, 2, '2 jobs still left in queue');
190
191     $rs = Koha::BackgroundJobs->search(
192         {
193             id => [ $job_recent_t1_new->id ]
194         }
195     );
196     is( $rs->count, 1, 'Unfinished job still left in queue');
197
198     $rs = Koha::BackgroundJobs->search(
199         {
200             id => [ $job_recent_t2_fin->id ]
201         }
202     );
203     is( $rs->count, 1, 'Recent finished job still left in queue');
204
205     $schema->storage->txn_rollback;
206
207 };