Bug 23012: (QA follow-up) Combine method to get both values
[koha.git] / t / db_dependent / Biblio_holdsqueue.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 => 2;
21 use Test::MockModule;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use Koha::Database;
27
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 subtest 'ModBiblio() + holds_queue update tests' => sub {
32
33     plan tests => 1;
34
35     $schema->storage->txn_begin;
36
37     my $biblio = $builder->build_sample_biblio;
38
39     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
40
41     my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
42     $mock->mock( 'enqueue', sub {
43         my ( $self, $args ) = @_;
44         my ($package, $filename, $line) = caller;
45         is_deeply(
46             $args->{biblio_ids},
47             [ $biblio->id ],
48             'ModBiblio triggers a holds queue update for the related biblio'
49         );
50     } );
51
52     # add a hold
53     $builder->build_object(
54         {
55             class => 'Koha::Holds',
56             value => {
57                 biblionumber   => $biblio->id,
58             }
59         }
60     );
61
62     # this call will trigger the mocked 'enqueue'
63     C4::Biblio::ModBiblio(
64         $biblio->metadata->record, $biblio->id,
65         $biblio->frameworkcode, { skip_holds_queue => 0 }
66     );
67
68     # this call will not trigger the mocked 'enqueue', so the test count is 1
69     C4::Biblio::ModBiblio(
70         $biblio->metadata->record, $biblio->id,
71         $biblio->frameworkcode, { skip_holds_queue => 1 }
72     );
73
74     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
75
76     # this call should not trigger the mocked 'enqueue'
77     C4::Biblio::ModBiblio(
78         $biblio->metadata->record, $biblio->id,
79         $biblio->frameworkcode, { skip_holds_queue => 0 }
80     );
81
82     # this call shoul not trigger the mocked 'enqueue'
83     C4::Biblio::ModBiblio(
84         $biblio->metadata->record, $biblio->id,
85         $biblio->frameworkcode, { skip_holds_queue => 1 }
86     );
87
88     $schema->storage->txn_rollback;
89 };
90
91 subtest 'DelBiblio + holds_queue update tests' => sub {
92
93     plan tests => 1;
94
95     $schema->storage->txn_begin;
96
97     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
98
99     my $biblio = $builder->build_sample_biblio;
100
101     my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
102     $mock->mock( 'enqueue', sub {
103         my ( $self, $args ) = @_;
104         is_deeply(
105             $args->{biblio_ids},
106             [ $biblio->id ],
107             'DelBiblio triggers a holds queue update for the related biblio'
108         );
109     } );
110
111     # add a hold
112     $builder->build_object(
113         {
114             class => 'Koha::Holds',
115             value => {
116                 biblionumber   => $biblio->id,
117             }
118         }
119     );
120
121     C4::Biblio::DelBiblio( $biblio->id );
122
123     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
124
125     $biblio = $builder->build_sample_biblio;
126
127     C4::Biblio::DelBiblio( $biblio->id );
128
129     $schema->storage->txn_rollback;
130 };