From 44efba88019a5a1f69a43c9fda115e42d728caf8 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 26 Nov 2019 17:40:15 +0100 Subject: [PATCH] Bug 24151: Add tests Sponsored-by: Association KohaLa - https://koha-fr.org/ --- t/db_dependent/Koha/Pseudonymization.t | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 t/db_dependent/Koha/Pseudonymization.t diff --git a/t/db_dependent/Koha/Pseudonymization.t b/t/db_dependent/Koha/Pseudonymization.t new file mode 100644 index 0000000000..7c8140cee4 --- /dev/null +++ b/t/db_dependent/Koha/Pseudonymization.t @@ -0,0 +1,71 @@ +#!/usr/bin/perl + +# Copyright 2019 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; + +use Koha::Database; +use Koha::Patrons; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'Koha::Anonymized::Patrons tests' => sub { + + plan tests => 9; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_config( 'key', '$2a$08$9lmorEKnwQloheaCLFIfje' ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $patron_info = $patron->unblessed; + delete $patron_info->{borrowernumber}; + $patron->delete; + + t::lib::Mocks::mock_preference( 'Pseudonymization', 0 ); + $patron = Koha::Patron->new($patron_info)->store; + my $anonymized = Koha::Anonymized::Patrons->find_from_borrowernumber( $patron->borrowernumber ); + is( $anonymized, undef, 'No anonymized patron if Pseudonymization is off' ); + $patron->delete; + + t::lib::Mocks::mock_preference( 'Pseudonymization', 1 ); + t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields', 'branchcode,categorycode,sort1' ); + $patron = Koha::Patron->new($patron_info)->store; + $anonymized = Koha::Anonymized::Patrons->find_from_borrowernumber( $patron->borrowernumber ); + like( $anonymized->hashed_borrowernumber, qr{^\$2a\$08\$}, "The hashed_borrowernumber must be a bcrypt hash" ); + is( $anonymized->branchcode, $patron_info->{branchcode}, "branchcode should be copied" ); + is( $anonymized->categorycode, $patron_info->{categorycode}, "categorycode should be copied" ); + is( $anonymized->sort1, $patron_info->{sort1}, "sort1 should be copied" ); + is( $anonymized->country, undef, "country should not be copied" ); + is( $anonymized->sort2, undef, "sort2 should not be copied" ); + + my $hashed_borrowernumber = $anonymized->hashed_borrowernumber; + $patron->sort1("another sort1")->store; + $anonymized = Koha::Anonymized::Patrons->find_from_borrowernumber( $patron->borrowernumber ); + is( $anonymized->hashed_borrowernumber, $hashed_borrowernumber, "On update original hash should be preserved" ); + is( $patron->sort1, "another sort1", "correctly updated" ); + $patron->delete; + + $schema->storage->txn_rollback; +}; -- 2.11.0