Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[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 => 3;
40
41     $schema->storage->txn_begin;
42
43     t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
44
45     my $biblio = $builder->build_sample_biblio;
46     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->id });
47     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->id });
48
49     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
50     AddReserve(
51         {
52             borrowernumber => $patron->id,
53             biblionumber   => $biblio->id,
54             itemnumber     => $item_1->id,
55             branchcode     => $patron->branchcode
56         }
57     );
58
59     my $update_biblio_counter = 0;
60
61     my $mock_holds_queue_job = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
62     $mock_holds_queue_job->mock( 'enqueue', sub {
63         $update_biblio_counter++;
64     });
65
66     my $index_biblio_counter = 0;
67
68     my $mock_index = Test::MockModule->new("Koha::SearchEngine::Elasticsearch::Indexer");
69     $mock_index->mock( 'index_records', sub {
70         $index_biblio_counter++;
71     });
72
73     my $job = Koha::BackgroundJob::BatchDeleteBiblio->new(
74         {
75             status         => 'new',
76             size           => 1,
77             borrowernumber => undef,
78             type           => 'batch_biblio_record_deletion',
79             data           => encode_json {
80                 record_ids     => [ $biblio->id ],
81             }
82         }
83     );
84
85     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
86
87     $job->process(
88         {
89             record_ids => [ $biblio->id ],
90         }
91     );
92
93     is( $update_biblio_counter, 1, 'Holds queue update is enqueued only once' );
94     is( $index_biblio_counter,  1, 'Index update is enqueued only once' );
95
96     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
97
98     $biblio = $builder->build_sample_biblio;
99
100     $job = Koha::BackgroundJob::BatchDeleteBiblio->new(
101         {
102             status         => 'new',
103             size           => 1,
104             borrowernumber => undef,
105             type           => 'batch_biblio_record_deletion',
106             data           => encode_json {
107                 record_ids     => [ $biblio->id ],
108             }
109         }
110     );
111
112     $job->process(
113         {
114             record_ids => [ $biblio->id ],
115         }
116     );
117
118     is( $update_biblio_counter, 1, 'Counter untouched with RealTimeHoldsQueue disabled' );
119
120     $schema->storage->txn_rollback;
121 };