View | Details | Raw Unified | Return to bug 31086
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Hold.t (-240 lines)
Lines 61-305 subtest 'store() tests' => sub { Link Here
61
    $schema->storage->txn_rollback;
61
    $schema->storage->txn_rollback;
62
};
62
};
63
63
64
subtest 'fill() tests' => sub {
65
66
    plan tests => 13;
67
68
    $schema->storage->txn_begin;
69
70
    my $fee = 15;
71
72
    my $category = $builder->build_object(
73
        {
74
            class => 'Koha::Patron::Categories',
75
            value => { reservefee => $fee }
76
        }
77
    );
78
    my $patron = $builder->build_object(
79
        {
80
            class => 'Koha::Patrons',
81
            value => { categorycode => $category->id }
82
        }
83
    );
84
    my $manager = $builder->build_object( { class => 'Koha::Patrons' } );
85
86
    my $title  = 'Do what you want';
87
    my $biblio = $builder->build_sample_biblio( { title => $title } );
88
    my $item   = $builder->build_sample_item( { biblionumber => $biblio->id } );
89
    my $hold   = $builder->build_object(
90
        {
91
            class => 'Koha::Holds',
92
            value => {
93
                biblionumber   => $biblio->id,
94
                borrowernumber => $patron->id,
95
                itemnumber     => $item->id,
96
                priority       => 10,
97
            }
98
        }
99
    );
100
101
    t::lib::Mocks::mock_preference( 'HoldFeeMode', 'any_time_is_collected' );
102
    t::lib::Mocks::mock_preference( 'HoldsLog',    1 );
103
    t::lib::Mocks::mock_userenv(
104
        { patron => $manager, branchcode => $manager->branchcode } );
105
106
    my $interface = 'api';
107
    C4::Context->interface($interface);
108
109
    my $ret = $hold->fill;
110
111
    is( ref($ret), 'Koha::Hold', '->fill returns the object type' );
112
    is( $ret->id, $hold->id, '->fill returns the object' );
113
114
    is( Koha::Holds->find($hold->id), undef, 'Hold no longer current' );
115
    my $old_hold = Koha::Old::Holds->find( $hold->id );
116
117
    is( $old_hold->id, $hold->id, 'reserve_id retained' );
118
    is( $old_hold->priority, 0, 'priority set to 0' );
119
    is( $old_hold->found, 'F', 'found set to F' );
120
121
    subtest 'fee applied tests' => sub {
122
123
        plan tests => 9;
124
125
        my $account = $patron->account;
126
        is( $account->balance, $fee, 'Charge applied correctly' );
127
128
        my $debits = $account->outstanding_debits;
129
        is( $debits->count, 1, 'Only one fee charged' );
130
131
        my $fee_debit = $debits->next;
132
        is( $fee_debit->amount * 1, $fee, 'Fee amount stored correctly' );
133
        is( $fee_debit->description, $title,
134
            'Fee description stored correctly' );
135
        is( $fee_debit->manager_id, $manager->id,
136
            'Fee manager_id stored correctly' );
137
        is( $fee_debit->branchcode, $manager->branchcode,
138
            'Fee branchcode stored correctly' );
139
        is( $fee_debit->interface, $interface,
140
            'Fee interface stored correctly' );
141
        is( $fee_debit->debit_type_code,
142
            'RESERVE', 'Fee debit_type_code stored correctly' );
143
        is( $fee_debit->itemnumber, $item->id,
144
            'Fee itemnumber stored correctly' );
145
    };
146
147
    my $logs = Koha::ActionLogs->search(
148
        {
149
            action => 'FILL',
150
            module => 'HOLDS',
151
            object => $hold->id
152
        }
153
    );
154
155
    is( $logs->count, 1, '1 log line added' );
156
157
    # Set HoldFeeMode to something other than any_time_is_collected
158
    t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
159
    # Disable logging
160
    t::lib::Mocks::mock_preference( 'HoldsLog',    0 );
161
162
    $hold = $builder->build_object(
163
        {
164
            class => 'Koha::Holds',
165
            value => {
166
                biblionumber   => $biblio->id,
167
                borrowernumber => $patron->id,
168
                itemnumber     => $item->id,
169
                priority       => 10,
170
            }
171
        }
172
    );
173
174
    $hold->fill;
175
176
    my $account = $patron->account;
177
    is( $account->balance, $fee, 'No new charge applied' );
178
179
    my $debits = $account->outstanding_debits;
180
    is( $debits->count, 1, 'Only one fee charged, because of HoldFeeMode' );
181
182
    $logs = Koha::ActionLogs->search(
183
        {
184
            action => 'FILL',
185
            module => 'HOLDS',
186
            object => $hold->id
187
        }
188
    );
189
190
    is( $logs->count, 0, 'HoldsLog disabled, no logs added' );
191
192
    subtest 'anonymization behavior tests' => sub {
193
194
        plan tests => 5;
195
196
        # reduce the tests noise
197
        t::lib::Mocks::mock_preference( 'HoldsLog',    0 );
198
        t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
199
        # unset AnonymousPatron
200
        t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
201
202
        # 0 == keep forever
203
        $patron->privacy(0)->store;
204
        my $hold = $builder->build_object(
205
            {
206
                class => 'Koha::Holds',
207
                value => { borrowernumber => $patron->id, found => undef }
208
            }
209
        );
210
        $hold->fill();
211
        is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
212
            $patron->borrowernumber, 'Patron link is kept' );
213
214
        # 1 == "default", meaning it is not protected from removal
215
        $patron->privacy(1)->store;
216
        $hold = $builder->build_object(
217
            {
218
                class => 'Koha::Holds',
219
                value => { borrowernumber => $patron->id, found => undef }
220
            }
221
        );
222
        $hold->fill();
223
        is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
224
            $patron->borrowernumber, 'Patron link is kept' );
225
226
        # 2 == delete immediately
227
        $patron->privacy(2)->store;
228
        $hold = $builder->build_object(
229
            {
230
                class => 'Koha::Holds',
231
                value => { borrowernumber => $patron->id, found => undef }
232
            }
233
        );
234
235
        throws_ok
236
            { $hold->fill(); }
237
            'Koha::Exception',
238
            'AnonymousPatron not set, exception thrown';
239
240
        $hold->discard_changes; # refresh from DB
241
242
        ok( !$hold->is_found, 'Hold is not filled' );
243
244
        my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
245
        t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
246
247
        $hold = $builder->build_object(
248
            {
249
                class => 'Koha::Holds',
250
                value => { borrowernumber => $patron->id, found => undef }
251
            }
252
        );
253
        $hold->fill();
254
        is(
255
            Koha::Old::Holds->find( $hold->id )->borrowernumber,
256
            $anonymous_patron->id,
257
            'Patron link is set to the configured anonymous patron immediately'
258
        );
259
    };
260
261
    subtest 'holds_queue update tests' => sub {
262
263
        plan tests => 1;
264
265
        my $biblio = $builder->build_sample_biblio;
266
267
        my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
268
        $mock->mock( 'enqueue', sub {
269
            my ( $self, $args ) = @_;
270
            is_deeply(
271
                $args->{biblio_ids},
272
                [ $biblio->id ],
273
                '->fill triggers a holds queue update for the related biblio'
274
            );
275
        } );
276
277
        t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
278
279
        $builder->build_object(
280
            {
281
                class => 'Koha::Holds',
282
                value => {
283
                    biblionumber   => $biblio->id,
284
                }
285
            }
286
        )->fill;
287
288
        t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
289
        # this call shouldn't add a new test
290
        $builder->build_object(
291
            {
292
                class => 'Koha::Holds',
293
                value => {
294
                    biblionumber   => $biblio->id,
295
                }
296
            }
297
        )->fill;
298
    };
299
300
    $schema->storage->txn_rollback;
301
};
302
303
subtest 'patron() tests' => sub {
64
subtest 'patron() tests' => sub {
304
65
305
    plan tests => 2;
66
    plan tests => 2;
306
- 

Return to bug 31086