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