Bug 26138: Make Koha::Plugins->call return consistent value
[koha.git] / t / db_dependent / Koha / Pseudonymization.t
blobcfefc82292c168d5f1e0a2e7df16aecacc91ef21
1 #!/usr/bin/perl
3 # Copyright 2019 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
20 use Modern::Perl;
22 use Test::More tests => 3;
23 use Try::Tiny;
25 use C4::Circulation;
26 use C4::Stats;
28 use Koha::Database;
29 use Koha::DateUtils qw( dt_from_string );
30 use Koha::Patrons;
31 use Koha::PseudonymizedTransactions;
33 use t::lib::TestBuilder;
34 use t::lib::Mocks;
36 my $schema = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
39 subtest 'Config does not exist' => sub {
41 plan tests => 2;
43 $schema->storage->txn_begin;
45 t::lib::Mocks::mock_config( 'bcrypt_settings', '' );
46 t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
47 t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields', 'branchcode,categorycode,sort1' );
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' } );
53 try{
54 C4::Stats::UpdateStats(
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,
66 } catch {
67 ok($_->isa('Koha::Exceptions::Config::MissingEntry'), "Koha::Patron->store should raise a Koha::Exceptions::Config::MissingEntry if 'bcrypt_settings' is not defined in the config");
68 is( $_->message, "Missing 'bcrypt_settings' entry in config file");
71 $schema->storage->txn_rollback;
74 subtest 'Koha::Anonymized::Transactions tests' => sub {
76 plan tests => 12;
78 $schema->storage->txn_begin;
80 t::lib::Mocks::mock_config( 'bcrypt_settings', '$2a$08$9lmorEKnwQloheaCLFIfje' );
82 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
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' );
94 t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
95 t::lib::Mocks::mock_preference( 'PseudonymizationTransactionFields', 'datetime,transaction_branchcode,transaction_type,itemnumber,itemtype,holdingbranch,homebranch,location,itemcallnumber,ccode'
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, 'datetime attribute copied correctly' );
106 is( $pseudonymized->transaction_branchcode, $statistic->branch, 'transaction_branchcode copied correctly' );
107 is( $pseudonymized->transaction_type, $statistic->type, 'transacttion_type copied correctly' );
108 is( $pseudonymized->itemnumber, $item->itemnumber, 'itemnumber copied correctly' );
109 is( $pseudonymized->itemtype, $item->effective_itemtype, 'itemtype copied correctly' );
110 is( $pseudonymized->holdingbranch, $item->holdingbranch, 'holdingbranch copied correctly' );
111 is( $pseudonymized->homebranch, $item->homebranch, 'homebranch copied correctly' );
112 is( $pseudonymized->location, $item->location, 'location copied correctly' );
113 is( $pseudonymized->itemcallnumber, $item->itemcallnumber, 'itemcallnumber copied correctly' );
114 is( $pseudonymized->ccode, $item->ccode, 'ccode copied correctly' );
116 $schema->storage->txn_rollback;
119 subtest 'PseudonymizedBorrowerAttributes tests' => sub {
121 plan tests => 3;
123 $schema->storage->txn_begin;
125 t::lib::Mocks::mock_config( 'bcrypt_settings', '$2a$08$9lmorEKnwQloheaCLFIfje' );
126 t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
127 t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields',
128 'branchcode,categorycode,sort1' );
130 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
131 my $patron_info = $patron->unblessed;
132 delete $patron_info->{borrowernumber};
133 $patron->delete;
135 my $attribute_type1 = Koha::Patron::Attribute::Type->new(
137 code => 'my code1',
138 description => 'my description1',
139 repeatable => 1,
140 keep_for_pseudonymization => 1,
142 )->store;
143 my $attribute_type2 = Koha::Patron::Attribute::Type->new(
145 code => 'my code2',
146 description => 'my description2',
147 keep_for_pseudonymization => 0,
149 )->store;
150 my $attribute_type3 = Koha::Patron::Attribute::Type->new(
152 code => 'my code3',
153 description => 'my description3',
154 keep_for_pseudonymization => 1,
156 )->store;
158 $patron = Koha::Patron->new($patron_info)->store->get_from_storage;
159 my $attribute_values = [
161 attribute => 'attribute for code1',
162 code => $attribute_type1->code,
165 attribute => 'attribute for code2',
166 code => $attribute_type2->code
169 attribute => 'attribute for code3',
170 code => $attribute_type3->code
174 $patron->extended_attributes($attribute_values);
177 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
178 my $item = $builder->build_sample_item;
180 C4::Stats::UpdateStats(
182 type => 'issue',
183 branch => $library->branchcode,
184 itemnumber => $item->itemnumber,
185 borrowernumber => $patron->borrowernumber,
186 itemtype => $item->effective_itemtype,
187 location => $item->location,
188 ccode => $item->ccode,
192 my $p = Koha::PseudonymizedTransactions->search({itemnumber => $item->itemnumber})->next;
193 my $attributes = Koha::Database->new->schema->resultset('PseudonymizedBorrowerAttribute')->search({transaction_id => $p->id });
194 is( $attributes->count, 2,
195 'Only the 2 attributes that have a type with keep_for_pseudonymization set should be kept'
197 my $attribute_1 = $attributes->next;
198 is_deeply(
199 { attribute => $attribute_1->attribute, code => $attribute_1->code->code },
200 $attribute_values->[0],
201 'Attribute 1 should be retrieved correctly'
203 my $attribute_2 = $attributes->next;
204 is_deeply(
205 { attribute => $attribute_2->attribute, code => $attribute_2->code->code },
206 $attribute_values->[2],
207 'Attribute 2 should be retrieved correctly'
210 $schema->storage->txn_rollback;