4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Test::More tests => 3;
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new;
35 # TODO create a subroutine in t::lib::Mocks
36 my $userenv_patron = $builder->build( { source => 'Borrower', }, );
37 C4::Context->_new_userenv('DUMMY SESSION');
38 C4::Context->set_userenv(
39 $userenv_patron->{borrowernumber},
40 $userenv_patron->{userid},
41 'usercnum', 'First name', 'Surname',
42 $userenv_patron->{_fk}{branchcode}{branchcode},
43 $userenv_patron->{_fk}{branchcode}{branchname}, 0
46 my $anonymous = $builder->build( { source => 'Borrower', }, );
48 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
50 subtest 'patron privacy is 1 (default)' => sub {
52 my $patron = $builder->build(
53 { source => 'Borrower',
54 value => { privacy => 1, }
57 my $item = $builder->build(
65 my $issue = $builder->build(
68 borrowernumber => $patron->{borrowernumber},
69 itemnumber => $item->{itemnumber},
74 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
75 is( $returned, 1, 'The item should have been returned' );
76 my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
77 ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
78 is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
80 my $dbh = C4::Context->dbh;
81 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
82 SELECT borrowernumber FROM old_issues where itemnumber = ?
83 |, undef, $item->{itemnumber});
84 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
88 subtest 'patron privacy is 0 (forever)' => sub {
91 my $patron = $builder->build(
92 { source => 'Borrower',
93 value => { privacy => 0, }
96 my $item = $builder->build(
104 my $issue = $builder->build(
107 borrowernumber => $patron->{borrowernumber},
108 itemnumber => $item->{itemnumber},
113 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
114 is( $returned, 1, 'The item should have been returned' );
115 my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
116 is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
118 my $dbh = C4::Context->dbh;
119 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
120 SELECT borrowernumber FROM old_issues where itemnumber = ?
121 |, undef, $item->{itemnumber});
122 is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'With privacy=0, the issue should not be anonymised' );
125 t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
127 subtest 'AnonymousPatron is not defined' => sub {
129 my $patron = $builder->build(
130 { source => 'Borrower',
131 value => { privacy => 1, }
134 my $item = $builder->build(
142 my $issue = $builder->build(
145 borrowernumber => $patron->{borrowernumber},
146 itemnumber => $item->{itemnumber},
151 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
152 is( $returned, 1, 'The item should have been returned' );
153 my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
154 ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
155 is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
157 my $dbh = C4::Context->dbh;
158 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
159 SELECT borrowernumber FROM old_issues where itemnumber = ?
160 |, undef, $item->{itemnumber});
161 is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
164 $schema->storage->txn_rollback;