Bug 30889: Unit tests
[koha.git] / t / db_dependent / Koha / BackgroundJob.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 4;
21 use Test::Exception;
22
23 use Koha::Database;
24 use Koha::BackgroundJobs;
25 use Koha::BackgroundJob::BatchUpdateItem;
26
27 use JSON qw( decode_json encode_json );
28
29 use t::lib::Mocks;
30 use t::lib::TestBuilder;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 subtest '_derived_class() tests' => sub {
36
37     plan tests => 3;
38
39     $schema->storage->txn_begin;
40
41     my $job_object = Koha::BackgroundJob->new();
42     my $mapping = $job_object->type_to_class_mapping;
43
44     # pick the first
45     my $type = ( keys %{$mapping} )[0];
46
47     my $job = $builder->build_object(
48         {   class => 'Koha::BackgroundJobs',
49             value => { type => $type, data => 'Foo' }
50         }
51     );
52
53     my $derived = $job->_derived_class;
54
55     is( ref($derived), $mapping->{$type}, 'Job object class is correct' );
56     ok( $derived->in_storage, 'The object is correctly marked as in storage' );
57
58     $derived->data('Bar')->store->discard_changes;
59     $job->discard_changes;
60
61     is_deeply( $job->unblessed, $derived->unblessed, '_derived_class object refers to the same DB object and can be manipulated as expected' );
62
63     $schema->storage->txn_rollback;
64 };
65
66 subtest 'enqueue() tests' => sub {
67
68     plan tests => 7;
69
70     $schema->storage->txn_begin;
71
72     # FIXME: This all feels we need to do it better...
73     my $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2 ] } );
74     my $job    = Koha::BackgroundJobs->find($job_id)->_derived_class;
75
76     is( $job->size,           2,     'Two steps' );
77     is( $job->status,         'new', 'Initial status set correctly' );
78     is( $job->borrowernumber, undef, 'No userenv, borrowernumber undef' );
79
80     my $interface = C4::Context->interface;
81     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
82     t::lib::Mocks::mock_userenv( { patron => $patron } );
83     my $job_context = {
84         number        => $patron->borrowernumber,
85         id            => $patron->userid,
86         cardnumber    => $patron->cardnumber,
87         firstname     => $patron->firstname,
88         surname       => $patron->surname,
89         branch        => $patron->library->branchcode,
90         branchname    => $patron->library->branchname,
91         flags         => $patron->flags,
92         emailaddress  => $patron->email,
93         register_id   => undef,
94         register_name => undef,
95         shibboleth    => undef,
96         desk_id       => undef,
97         desk_name     => undef,
98         interface     => $interface
99     };
100
101     $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2, 3 ] } );
102     $job    = Koha::BackgroundJobs->find($job_id)->_derived_class;
103
104     is( $job->size,           3,           'Three steps' );
105     is( $job->status,         'new',       'Initial status set correctly' );
106     is( $job->borrowernumber, $patron->id, 'Borrowernumber set from userenv' );
107     is_deeply( decode_json( $job->context ), $job_context, 'Context set from userenv + interface' );
108
109     $schema->storage->txn_rollback;
110 };
111
112 subtest 'start(), step() and finish() tests' => sub {
113
114     plan tests => 19;
115
116     $schema->storage->txn_begin;
117
118     # FIXME: This all feels we need to do it better...
119     my $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2 ] } );
120     my $job    = Koha::BackgroundJobs->find($job_id)->_derived_class;
121
122     is( $job->started_on, undef, 'started_on not set yet' );
123     is( $job->size, 2, 'Two steps' );
124
125     $job->start;
126
127     isnt( $job->started_on, undef, 'started_on set' );
128     is( $job->status, 'started' );
129     is( $job->progress, 0, 'No progress yet' );
130
131     $job->step;
132     is( $job->progress, 1, 'First step' );
133     $job->step;
134     is( $job->progress, 2, 'Second step' );
135     throws_ok
136         { $job->step; }
137         'Koha::Exceptions::BackgroundJob::StepOutOfBounds',
138         'Tried to make a forbidden extra step';
139
140     is( $job->progress, 2, 'progress remains unchanged' );
141
142     my $data = { some => 'data' };
143
144     $job->status('cancelled')->store;
145     $job->finish( $data );
146
147     is( $job->status, 'cancelled', "'finish' leaves 'cancelled' untouched" );
148     isnt( $job->ended_on, undef, 'ended_on set' );
149     is_deeply( decode_json( $job->data ), $data );
150
151     $job->status('started')->store;
152     $job->finish( $data );
153
154     is( $job->status, 'finished' );
155     isnt( $job->ended_on, undef, 'ended_on set' );
156     is_deeply( decode_json( $job->data ), $data );
157
158     throws_ok
159         { $job->start; }
160         'Koha::Exceptions::BackgroundJob::InconsistentStatus',
161         'Exception thrown trying to start a finished job';
162
163     is( $@->expected_status, 'new' );
164
165     throws_ok
166         { $job->step; }
167         'Koha::Exceptions::BackgroundJob::InconsistentStatus',
168         'Exception thrown trying to start a finished job';
169
170     is( $@->expected_status, 'started' );
171
172     $schema->storage->txn_rollback;
173 };
174
175 subtest 'decoded_data() and set_encoded_data() tests' => sub {
176
177     plan tests => 3;
178
179     my $job = Koha::BackgroundJob::BatchUpdateItem->new->set_encoded_data( undef );
180     is( $job->decoded_data, undef );
181
182     my $data = { some => 'data' };
183
184     $job->set_encoded_data( $data );
185
186     is_deeply( decode_json($job->data), $data );
187     is_deeply( $job->decoded_data, $data );
188 };