3 # This file is part of Koha
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.
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.
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>.
20 use Test::More tests => 4;
24 use Koha::BackgroundJobs;
25 use Koha::BackgroundJob::BatchUpdateItem;
27 use JSON qw( decode_json encode_json );
30 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 subtest '_derived_class() tests' => sub {
39 $schema->storage->txn_begin;
41 my $job_object = Koha::BackgroundJob->new();
42 my $mapping = $job_object->type_to_class_mapping;
45 my $type = ( keys %{$mapping} )[0];
47 my $job = $builder->build_object(
48 { class => 'Koha::BackgroundJobs',
49 value => { type => $type, data => 'Foo' }
53 my $derived = $job->_derived_class;
55 is( ref($derived), $mapping->{$type}, 'Job object class is correct' );
56 ok( $derived->in_storage, 'The object is correctly marked as in storage' );
58 $derived->data('Bar')->store->discard_changes;
59 $job->discard_changes;
61 is_deeply( $job->unblessed, $derived->unblessed, '_derived_class object refers to the same DB object and can be manipulated as expected' );
63 $schema->storage->txn_rollback;
66 subtest 'enqueue() tests' => sub {
70 $schema->storage->txn_begin;
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;
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' );
80 my $interface = C4::Context->interface;
81 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
82 t::lib::Mocks::mock_userenv( { patron => $patron } );
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,
94 register_name => undef,
98 interface => $interface
101 $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2, 3 ] } );
102 $job = Koha::BackgroundJobs->find($job_id)->_derived_class;
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' );
109 $schema->storage->txn_rollback;
112 subtest 'start(), step() and finish() tests' => sub {
116 $schema->storage->txn_begin;
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;
122 is( $job->started_on, undef, 'started_on not set yet' );
123 is( $job->size, 2, 'Two steps' );
127 isnt( $job->started_on, undef, 'started_on set' );
128 is( $job->status, 'started' );
129 is( $job->progress, 0, 'No progress yet' );
132 is( $job->progress, 1, 'First step' );
134 is( $job->progress, 2, 'Second step' );
137 'Koha::Exceptions::BackgroundJob::StepOutOfBounds',
138 'Tried to make a forbidden extra step';
140 is( $job->progress, 2, 'progress remains unchanged' );
142 my $data = { some => 'data' };
144 $job->status('cancelled')->store;
145 $job->finish( $data );
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 );
151 $job->status('started')->store;
152 $job->finish( $data );
154 is( $job->status, 'finished' );
155 isnt( $job->ended_on, undef, 'ended_on set' );
156 is_deeply( decode_json( $job->data ), $data );
160 'Koha::Exceptions::BackgroundJob::InconsistentStatus',
161 'Exception thrown trying to start a finished job';
163 is( $@->expected_status, 'new' );
167 'Koha::Exceptions::BackgroundJob::InconsistentStatus',
168 'Exception thrown trying to start a finished job';
170 is( $@->expected_status, 'started' );
172 $schema->storage->txn_rollback;
175 subtest 'decoded_data() and set_encoded_data() tests' => sub {
179 my $job = Koha::BackgroundJob::BatchUpdateItem->new->set_encoded_data( undef );
180 is( $job->decoded_data, undef );
182 my $data = { some => 'data' };
184 $job->set_encoded_data( $data );
186 is_deeply( decode_json($job->data), $data );
187 is_deeply( $job->decoded_data, $data );