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

(-)a/t/db_dependent/Circulation/StoreLastBorrower.t (-2 / +173 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 2;
21
use Test::More tests => 3;
22
22
23
use C4::Circulation qw( AddReturn );
23
use C4::Circulation qw( AddReturn );
24
use C4::Context;
24
use C4::Context;
Lines 143-147 subtest 'Test StoreLastBorrower' => sub { Link Here
143
    is( $item->last_returned_by, undef, 'Last patron to return item should not be stored if StoreLastBorrower if off' );
143
    is( $item->last_returned_by, undef, 'Last patron to return item should not be stored if StoreLastBorrower if off' );
144
};
144
};
145
145
146
subtest 'Test StoreLastBorrower with multiple borrowers' => sub {
147
    plan tests => 12;
148
149
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
150
    my $item    = $builder->build_sample_item;
151
    t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
152
153
    # Test last_returned_by_all with no borrowers
154
    t::lib::Mocks::mock_preference( 'StoreLastBorrower', '3' );
155
    my @borrowers = $item->last_returned_by_all();
156
    is( scalar(@borrowers), 0, 'last_returned_by_all returns empty array when no borrowers stored' );
157
158
    # Add 3 borrowers for testing, checkout/check in
159
    my @patrons;
160
    for my $i ( 1 .. 3 ) {
161
        my $patron = $builder->build(
162
            {
163
                source => 'Borrower',
164
                value  => { privacy => 1, branchcode => $library->branchcode }
165
            }
166
        );
167
        push @patrons, $patron;
168
169
        my $issue = $builder->build(
170
            {
171
                source => 'Issue',
172
                value  => {
173
                    borrowernumber => $patron->{borrowernumber},
174
                    itemnumber     => $item->itemnumber,
175
                },
176
            }
177
        );
178
179
        my ( $returned, undef, undef ) =
180
            C4::Circulation::AddReturn( $item->barcode, $patron->{branchcode}, undef, dt_from_string("2010-10-1$i") );
181
    }
182
183
    $item = $item->get_from_storage;
184
185
    # Test last_returned_by_all returns all borrowers
186
    @borrowers = $item->last_returned_by_all();
187
    is( scalar(@borrowers), 3, 'Correctly returns 3 borrowers' );
188
189
    # Test ordering
190
    is( $borrowers[0]->borrowernumber, $patrons[2]->{borrowernumber}, 'Most recent borrower first' );
191
    is( $borrowers[1]->borrowernumber, $patrons[1]->{borrowernumber}, 'Second most recent borrower second' );
192
    is( $borrowers[2]->borrowernumber, $patrons[0]->{borrowernumber}, 'Oldest borrower last' );
193
194
    # Add 2 more borrowers/check out/check in
195
    for my $i ( 4 .. 5 ) {
196
        my $patron = $builder->build(
197
            {
198
                source => 'Borrower',
199
                value  => { privacy => 1, branchcode => $library->branchcode }
200
            }
201
        );
202
        push @patrons, $patron;
203
204
        my $issue = $builder->build(
205
            {
206
                source => 'Issue',
207
                value  => {
208
                    borrowernumber => $patron->{borrowernumber},
209
                    itemnumber     => $item->itemnumber,
210
                },
211
            }
212
        );
213
214
        my ( $returned, undef, undef ) =
215
            C4::Circulation::AddReturn( $item->barcode, $patron->{branchcode}, undef, dt_from_string("2010-10-1$i") );
216
    }
217
218
    $item      = $item->get_from_storage;
219
    @borrowers = $item->last_returned_by_all();
220
    is(
221
        scalar(@borrowers), 3,
222
        'We only retain 3 borrowers when the sys pref is set to 3, even though there are 5 checkouts/checkins'
223
    );
224
    is( $borrowers[0]->borrowernumber, $patrons[4]->{borrowernumber}, 'Most recent borrower after cleanup' );
225
226
    # Reduce StoreLastBorrower to 2
227
    t::lib::Mocks::mock_preference( 'StoreLastBorrower', '2' );
228
229
    my $yet_another_patron = $builder->build(
230
        {
231
            source => 'Borrower',
232
            value  => { privacy => 1, branchcode => $library->branchcode }
233
        }
234
    );
235
236
    my $issue = $builder->build(
237
        {
238
            source => 'Issue',
239
            value  => {
240
                borrowernumber => $yet_another_patron->{borrowernumber},
241
                itemnumber     => $item->itemnumber,
242
            },
243
        }
244
    );
245
246
    my ( $returned, undef, undef ) = C4::Circulation::AddReturn(
247
        $item->barcode, $yet_another_patron->{branchcode}, undef,
248
        dt_from_string('2010-10-16')
249
    );
250
251
    $item      = $item->get_from_storage;
252
    @borrowers = $item->last_returned_by_all();
253
    is( scalar(@borrowers), 2, 'StoreLastBorrower was reduced to 2, we should now only keep 2 in the table' );
254
    is(
255
        $borrowers[0]->borrowernumber, $yet_another_patron->{borrowernumber},
256
        'Most recent borrower after limit reduction'
257
    );
258
259
    # Disabled pref
260
    t::lib::Mocks::mock_preference( 'StoreLastBorrower', '0' );
261
262
    # If pref has become disabled, nothing should be stored in the table
263
    my $one_more_patron = $builder->build(
264
        {
265
            source => 'Borrower',
266
            value  => { privacy => 1, branchcode => $library->branchcode }
267
        }
268
    );
269
270
    my $another_issue = $builder->build(
271
        {
272
            source => 'Issue',
273
            value  => {
274
                borrowernumber => $one_more_patron->{borrowernumber},
275
                itemnumber     => $item->itemnumber,
276
            },
277
        }
278
    );
279
280
    ( $returned, undef, undef ) = C4::Circulation::AddReturn(
281
        $item->barcode, $one_more_patron->{branchcode}, undef,
282
        dt_from_string('2010-10-18')
283
    );
284
285
    $item      = $item->get_from_storage;
286
    @borrowers = $item->last_returned_by_all();
287
    is( scalar(@borrowers), 0, 'last_returned_by_all respects preference value 0' );
288
289
    my $cleanup_patron = $builder->build(
290
        {
291
            source => 'Borrower',
292
            value  => { privacy => 1, branchcode => $library->branchcode }
293
        }
294
    );
295
296
    $issue = $builder->build(
297
        {
298
            source => 'Issue',
299
            value  => {
300
                borrowernumber => $cleanup_patron->{borrowernumber},
301
                itemnumber     => $item->itemnumber,
302
            },
303
        }
304
    );
305
306
    ( $returned, undef, undef ) = C4::Circulation::AddReturn(
307
        $item->barcode, $cleanup_patron->{branchcode}, undef,
308
        dt_from_string('2010-10-17')
309
    );
310
311
    $item      = $item->get_from_storage;
312
    @borrowers = $item->last_returned_by_all();
313
    is( scalar(@borrowers),      0,     'All entries cleared when preference is 0' );
314
    is( $item->last_returned_by, undef, 'last_returned_by returns undef when no records' );
315
316
};
317
146
$schema->storage->txn_rollback;
318
$schema->storage->txn_rollback;
147
319
148
- 

Return to bug 26993