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

(-)a/Koha/Old/Checkout.pm (+20 lines)
Lines 18-23 package Koha::Old::Checkout; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Database;
20
use Koha::Database;
21
use Koha::Exceptions::SysPref;
21
use Koha::Libraries;
22
use Koha::Libraries;
22
23
23
use base qw(Koha::Object);
24
use base qw(Koha::Object);
Lines 89-94 sub issuer { Link Here
89
    return Koha::Patron->_new_from_dbic( $issuer_rs );
90
    return Koha::Patron->_new_from_dbic( $issuer_rs );
90
}
91
}
91
92
93
=head3 anonymize
94
95
    $checkout->anonymize();
96
97
Anonymize the given I<Koha::Old::Checkout> object.
98
99
=cut
100
101
sub anonymize {
102
    my ($self) = @_;
103
104
    my $anonymous_id = C4::Context->preference('AnonymousPatron');
105
106
    Koha::Exceptions::SysPref::NotSet->throw( syspref => 'AnonymousPatron' )
107
        unless $anonymous_id;
108
109
    return $self->update( { borrowernumber => $anonymous_id } );
110
}
111
92
=head3 to_api_mapping
112
=head3 to_api_mapping
93
113
94
This method returns the mapping for representing a Koha::Old::Checkout object
114
This method returns the mapping for representing a Koha::Old::Checkout object
(-)a/t/db_dependent/Koha/Old/Checkout.t (-1 / +82 lines)
Line 0 Link Here
0
- 
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::Exception;
22
23
use Koha::Database;
24
25
use t::lib::Mocks;
26
use t::lib::TestBuilder;
27
28
my $schema  = Koha::Database->new->schema;
29
my $builder = t::lib::TestBuilder->new;
30
31
subtest 'anonymize() tests' => sub {
32
33
    plan tests => 8;
34
35
    $schema->storage->txn_begin;
36
37
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
38
39
    is( $patron->old_checkouts->count, 0, 'Patron has no old checkouts' );
40
41
    my $checkout_1 = $builder->build_object(
42
        {
43
            class => 'Koha::Old::Checkouts',
44
            value => { borrowernumber => $patron->id }
45
        }
46
    );
47
    my $checkout_2 = $builder->build_object(
48
        {
49
            class => 'Koha::Old::Checkouts',
50
            value => { borrowernumber => $patron->id }
51
        }
52
    );
53
54
    is( $patron->old_checkouts->count, 2, 'Patron has 2 completed checkouts' );
55
56
    t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
57
58
    throws_ok
59
        { $checkout_1->anonymize; }
60
        'Koha::Exceptions::SysPref::NotSet',
61
        'Exception thrown because AnonymousPatron not set';
62
63
    is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
64
    is( $patron->old_checkouts->count, 2, 'No changes, patron has 2 linked completed checkouts' );
65
66
    is( $checkout_1->borrowernumber, $patron->id,
67
        'Anonymized hold not linked to patron' );
68
    is( $checkout_2->borrowernumber, $patron->id,
69
        'Not anonymized hold still linked to patron' );
70
71
    my $anonymous_patron =
72
      $builder->build_object( { class => 'Koha::Patrons' } );
73
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
74
75
    # anonymize second hold
76
    $checkout_2->anonymize;
77
    $checkout_2->discard_changes;
78
    is( $checkout_2->borrowernumber, $anonymous_patron->id,
79
        'Anonymized hold linked to anonymouspatron' );
80
81
    $schema->storage->txn_rollback;
82
};

Return to bug 30023