|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2023 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 => 2; |
| 23 |
|
| 24 |
use Test::Exception; |
| 25 |
|
| 26 |
use Koha::Database; |
| 27 |
use Koha::DateUtils qw( dt_from_string ); |
| 28 |
use Koha::Patrons; |
| 29 |
use Koha::PseudonymizedTransactions; |
| 30 |
|
| 31 |
use Crypt::Eksblowfish::Bcrypt qw( bcrypt ); |
| 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 |
my $bcrypt_settings = '$2a$10$f.3Kc8Ofrvad4nI2muehEOACmgw6MgwJLxfhyFYmCwRlAHOYQrhCu'; |
| 40 |
|
| 41 |
subtest 'get_hash() tests' => sub { |
| 42 |
|
| 43 |
plan tests => 2; |
| 44 |
|
| 45 |
t::lib::Mocks::mock_config( 'bcrypt_settings', '' ); |
| 46 |
|
| 47 |
my $string = "foo"; |
| 48 |
|
| 49 |
throws_ok { Koha::PseudonymizedTransaction->get_hash($string) } |
| 50 |
'Koha::Exceptions::Config::MissingEntry'; |
| 51 |
|
| 52 |
t::lib::Mocks::mock_config( 'bcrypt_settings', $bcrypt_settings ); |
| 53 |
is( |
| 54 |
Koha::PseudonymizedTransaction->get_hash($string), bcrypt( $string, $bcrypt_settings ), |
| 55 |
"get_hash() returns the output of the bcrypt method using the bcrypt_settings' config entry" |
| 56 |
); |
| 57 |
}; |
| 58 |
|
| 59 |
subtest 'new_from_statistic() tests' => sub { |
| 60 |
|
| 61 |
plan tests => 15; |
| 62 |
|
| 63 |
$schema->storage->txn_begin; |
| 64 |
|
| 65 |
t::lib::Mocks::mock_config( 'bcrypt_settings', $bcrypt_settings ); |
| 66 |
|
| 67 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 68 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 69 |
my $item = $builder->build_sample_item; |
| 70 |
|
| 71 |
my @fields = qw( |
| 72 |
ccode |
| 73 |
datetime |
| 74 |
holdingbranch |
| 75 |
homebranch |
| 76 |
itemcallnumber |
| 77 |
itemnumber |
| 78 |
itemtype |
| 79 |
location |
| 80 |
transaction_branchcode |
| 81 |
transaction_type |
| 82 |
); |
| 83 |
|
| 84 |
t::lib::Mocks::mock_preference( |
| 85 |
'PseudonymizationTransactionFields', |
| 86 |
join( ',', @fields ) |
| 87 |
); |
| 88 |
|
| 89 |
my $statistic = $builder->build_object( |
| 90 |
{ |
| 91 |
class => 'Koha::Statistics', |
| 92 |
value => { |
| 93 |
borrowernumber => $patron->id, |
| 94 |
branch => $library->id, |
| 95 |
ccode => $item->ccode, |
| 96 |
itemnumber => $item->id, |
| 97 |
itemtype => $item->itype, |
| 98 |
location => $item->location, |
| 99 |
} |
| 100 |
} |
| 101 |
); |
| 102 |
|
| 103 |
my $pseudonymized = Koha::PseudonymizedTransaction->new_from_statistic($statistic); |
| 104 |
|
| 105 |
is( |
| 106 |
$pseudonymized->hashed_borrowernumber, |
| 107 |
Koha::PseudonymizedTransaction->get_hash( $patron->id ), "The hashed_borrowernumber must be a bcrypt hash" |
| 108 |
); |
| 109 |
is( $pseudonymized->datetime, $statistic->datetime, 'datetime attribute copied correctly' ); |
| 110 |
is( $pseudonymized->transaction_branchcode, $statistic->branch, 'transaction_branchcode copied correctly' ); |
| 111 |
is( $pseudonymized->transaction_type, $statistic->type, 'transacttion_type copied correctly' ); |
| 112 |
is( $pseudonymized->itemnumber, $item->itemnumber, 'itemnumber copied correctly' ); |
| 113 |
is( $pseudonymized->itemtype, $item->itype, 'itype copied correctly' ); |
| 114 |
is( $pseudonymized->holdingbranch, $item->holdingbranch, 'holdingbranch copied correctly' ); |
| 115 |
is( $pseudonymized->homebranch, $item->homebranch, 'homebranch copied correctly' ); |
| 116 |
is( $pseudonymized->location, $item->location, 'location copied correctly' ); |
| 117 |
is( $pseudonymized->itemcallnumber, $item->itemcallnumber, 'itemcallnumber copied correctly' ); |
| 118 |
is( $pseudonymized->ccode, $item->ccode, 'ccode copied correctly' ); |
| 119 |
is( $pseudonymized->branchcode, $patron->branchcode, 'branchcode set correctly' ); |
| 120 |
is( $pseudonymized->categorycode, $patron->categorycode, 'categorycode set correctly' ); |
| 121 |
ok( $pseudonymized->has_cardnumber, 'has_cardnumber set correctly' ); |
| 122 |
|
| 123 |
$patron->cardnumber(undef)->store; |
| 124 |
|
| 125 |
ok( |
| 126 |
!Koha::PseudonymizedTransaction->new_from_statistic($statistic)->has_cardnumber, |
| 127 |
'has_cardnumber set to false' |
| 128 |
); |
| 129 |
|
| 130 |
$schema->storage->txn_rollback; |
| 131 |
}; |