3 # This file is part of Koha
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.
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.
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>.
20 use Test::More tests => 2;
26 use t::lib::TestBuilder;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
31 subtest 'anonymize() tests' => sub {
35 $schema->storage->txn_begin;
37 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
39 is( $patron->old_checkouts->count, 0, 'Patron has no old checkouts' );
41 my $checkout_1 = $builder->build_object(
43 class => 'Koha::Old::Checkouts',
44 value => { borrowernumber => $patron->id }
47 my $checkout_2 = $builder->build_object(
49 class => 'Koha::Old::Checkouts',
50 value => { borrowernumber => $patron->id }
54 is( $patron->old_checkouts->count, 2, 'Patron has 2 completed checkouts' );
56 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
59 { $checkout_1->anonymize; }
60 'Koha::Exceptions::SysPref::NotSet',
61 'Exception thrown because AnonymousPatron not set';
63 is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
64 is( $patron->old_checkouts->count, 2, 'No changes, patron has 2 linked completed checkouts' );
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' );
71 my $anonymous_patron =
72 $builder->build_object( { class => 'Koha::Patrons' } );
73 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
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' );
81 $schema->storage->txn_rollback;
84 subtest 'deleteitem() tests' => sub {
88 $schema->storage->txn_begin;
90 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
92 my $checkout_3 = $builder->build_object(
94 class => 'Koha::Old::Checkouts',
95 value => { borrowernumber => $patron->id }
99 # delete first checkout
100 my $item_to_del = $checkout_3->item;
101 $item_to_del->delete;
102 $checkout_3->discard_changes();
103 is( $checkout_3->item, undef, "Item is deleted");
105 $schema->storage->txn_rollback;