Bug 24151: Sync patron's attributes
[koha.git] / t / db_dependent / Koha / Pseudonymization.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 3;
23 use Try::Tiny;
24
25 use C4::Circulation;
26 use C4::Stats;
27
28 use Koha::Database;
29 use Koha::DateUtils qw( dt_from_string );
30 use Koha::Patrons;
31 use Koha::PseudonymizedTransactions;
32
33 use t::lib::TestBuilder;
34 use t::lib::Mocks;
35
36 my $schema  = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
38
39 subtest 'Config does not exist' => sub {
40
41     plan tests => 2;
42
43     $schema->storage->txn_begin;
44
45     t::lib::Mocks::mock_config( 'key', '' );
46     t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
47     t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields', 'branchcode,categorycode,sort1' );
48
49     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
50     my $item    = $builder->build_sample_item;
51     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
52
53     try{
54         C4::Stats::UpdateStats(
55             {
56                 type           => 'issue',
57                 branch         => $library->branchcode,
58                 itemnumber     => $item->itemnumber,
59                 borrowernumber => $patron->borrowernumber,
60                 itemtype       => $item->effective_itemtype,
61                 location       => $item->location,
62                 ccode          => $item->ccode,
63             }
64         );
65
66     } catch {
67         ok($_->isa('Koha::Exceptions::Config::MissingEntry'), "Koha::Patron->store should raise a Koha::Exceptions::Config::MissingEntry if 'key' is not defined in the config");
68         is( $_->message, "Missing 'key' entry in config file");
69     };
70
71     $schema->storage->txn_rollback;
72 };
73
74 subtest 'Koha::Anonymized::Transactions tests' => sub {
75
76     plan tests => 11;
77
78     $schema->storage->txn_begin;
79
80     t::lib::Mocks::mock_config( 'key', '$2a$08$9lmorEKnwQloheaCLFIfje' );
81
82     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
83
84     t::lib::Mocks::mock_preference( 'Pseudonymization', 0 );
85     my $item = $builder->build_sample_item;
86     t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch });
87     AddIssue( $patron->unblessed, $item->barcode, dt_from_string );
88     AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
89     my $pseudonymized= Koha::PseudonymizedTransactions->search(
90         { itemnumber => $item->itemnumber } )->next;
91     is( $pseudonymized, undef,
92         'No pseudonymized transaction if Pseudonymization is off' );
93
94     t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
95     t::lib::Mocks::mock_preference( 'PseudonymizationTransactionFields', 'datetime,transaction_branchcode,transaction_type,itemnumber,itemtype,holdingbranch,location,itemcallnumber,ccode'
96     );
97     $item = $builder->build_sample_item;
98     t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch });
99     AddIssue( $patron->unblessed, $item->barcode, dt_from_string );
100     AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
101     my $statistic = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next;
102     $pseudonymized = Koha::PseudonymizedTransactions->search( { itemnumber => $item->itemnumber } )->next;
103     like( $pseudonymized->hashed_borrowernumber,
104         qr{^\$2a\$08\$}, "The hashed_borrowernumber must be a bcrypt hash" );
105     is( $pseudonymized->datetime,               $statistic->datetime );
106     is( $pseudonymized->transaction_branchcode, $statistic->branch );
107     is( $pseudonymized->transaction_type,       $statistic->type );
108     is( $pseudonymized->itemnumber,             $item->itemnumber );
109     is( $pseudonymized->itemtype,               $item->effective_itemtype );
110     is( $pseudonymized->holdingbranch,          $item->holdingbranch );
111     is( $pseudonymized->location,               $item->location );
112     is( $pseudonymized->itemcallnumber,         $item->itemcallnumber );
113     is( $pseudonymized->ccode,                  $item->ccode );
114
115     $schema->storage->txn_rollback;
116 };
117
118 subtest 'PseudonymizedBorrowerAttributes tests' => sub {
119
120     plan tests => 3;
121
122     $schema->storage->txn_begin;
123
124     t::lib::Mocks::mock_config( 'key', '$2a$08$9lmorEKnwQloheaCLFIfje' );
125     t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
126     t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields',
127         'branchcode,categorycode,sort1' );
128
129     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
130     my $patron_info = $patron->unblessed;
131     delete $patron_info->{borrowernumber};
132     $patron->delete;
133
134     my $attribute_type1 = Koha::Patron::Attribute::Type->new(
135         {
136             code                => 'my code1',
137             description         => 'my description1',
138             repeatable          => 1,
139             keep_for_pseudonymization => 1,
140         }
141     )->store;
142     my $attribute_type2 = Koha::Patron::Attribute::Type->new(
143         {
144             code                => 'my code2',
145             description         => 'my description2',
146             keep_for_pseudonymization => 0,
147         }
148     )->store;
149     my $attribute_type3 = Koha::Patron::Attribute::Type->new(
150         {
151             code                => 'my code3',
152             description         => 'my description3',
153             keep_for_pseudonymization => 1,
154         }
155     )->store;
156
157     $patron = Koha::Patron->new($patron_info)->store->get_from_storage;
158     my $attribute_values = [
159         {
160             attribute => 'attribute for code1',
161             code      => $attribute_type1->code,
162         },
163         {
164             attribute => 'attribute for code2',
165             code      => $attribute_type2->code
166         },
167         {
168             attribute => 'attribute for code3',
169             code      => $attribute_type3->code
170         },
171     ];
172
173     $patron->extended_attributes($attribute_values);
174
175
176     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
177     my $item    = $builder->build_sample_item;
178
179     C4::Stats::UpdateStats(
180         {
181             type           => 'issue',
182             branch         => $library->branchcode,
183             itemnumber     => $item->itemnumber,
184             borrowernumber => $patron->borrowernumber,
185             itemtype       => $item->effective_itemtype,
186             location       => $item->location,
187             ccode          => $item->ccode,
188         }
189     );
190
191     my $p = Koha::PseudonymizedTransactions->search({itemnumber => $item->itemnumber})->next;
192     my $attributes = Koha::Database->new->schema->resultset('PseudonymizedBorrowerAttribute')->search({transaction_id => $p->id });
193     is( $attributes->count, 2,
194         'Only the 2 attributes that have a type with keep_for_pseudonymization set should be kept'
195     );
196     my $attribute_1 = $attributes->next;
197     is_deeply(
198         { attribute => $attribute_1->attribute, code => $attribute_1->code->code },
199         $attribute_values->[0],
200         'Attribute 1 should be retrieved correctly'
201     );
202     my $attribute_2 = $attributes->next;
203     is_deeply(
204         { attribute => $attribute_2->attribute, code => $attribute_2->code->code },
205         $attribute_values->[2],
206         'Attribute 2 should be retrieved correctly'
207     );
208
209     $schema->storage->txn_rollback;
210 };