Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[koha.git] / t / db_dependent / Koha / BackgroundJobs / BatchDeleteItem.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 => 1;
21 use Test::MockModule;
22
23 use JSON qw( encode_json );
24
25 use Koha::Database;
26 use Koha::BackgroundJob::BatchDeleteItem;
27
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
30
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 subtest "process() tests" => sub {
35
36     plan tests => 2;
37
38     $schema->storage->txn_begin;
39
40     my $biblio = $builder->build_sample_biblio;
41     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->id });
42     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->id });
43
44     my $counter = 0;
45
46     my $mock_holds_queue_job = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
47     $mock_holds_queue_job->mock( 'enqueue', sub {
48         $counter++;
49     });
50
51     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
52
53     my $job = Koha::BackgroundJob::BatchDeleteItem->new(
54         {
55             status         => 'new',
56             size           => 2,
57             borrowernumber => undef,
58             type           => 'batch_item_record_deletion',
59             data           => encode_json {
60                 record_ids     => [ $item_1->id, $item_2->id ],
61                 delete_biblios => 1,
62             }
63         }
64     );
65
66     $job->process(
67         {
68             record_ids     => [ $item_1->id, $item_2->id ],
69             delete_biblios => 1,
70         }
71     );
72
73     is( $counter, 1, 'Holds queue update is enqueued only once' );
74
75     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
76
77     $biblio = $builder->build_sample_biblio;
78     my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
79
80     $job = Koha::BackgroundJob::BatchDeleteItem->new(
81         {
82             status         => 'new',
83             size           => 2,
84             borrowernumber => undef,
85             type           => 'batch_item_record_deletion',
86             data           => encode_json {
87                 record_ids     => [ $item->id ],
88                 delete_biblios => 1,
89             }
90         }
91     );
92
93     $job->process(
94         {
95             record_ids     => [ $item->id ],
96             delete_biblios => 1,
97         }
98     );
99
100     is( $counter, 1, 'Counter untouched with RealTimeHoldsQueue disabled' );
101
102     $schema->storage->txn_rollback;
103 };