Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[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 => 14;
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     my $cnt = Koha::BackgroundJobs->search({ borrowernumber => undef })->count; # expected to be zero, but theoretically possible
135
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' );
142
143     $schema->storage->txn_rollback;
144 };