Bug 24151: Add tests
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 26 Nov 2019 16:40:15 +0000 (17:40 +0100)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 20 Jul 2020 13:17:42 +0000 (15:17 +0200)
Sponsored-by: Association KohaLa - https://koha-fr.org/

Signed-off-by: Signed-off-by: Sonia Bouis <sonia.bouis@univ-lyon3.fr>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

t/db_dependent/Koha/Pseudonymization.t [new file with mode: 0644]

diff --git a/t/db_dependent/Koha/Pseudonymization.t b/t/db_dependent/Koha/Pseudonymization.t
new file mode 100644 (file)
index 0000000..99f6e2d
--- /dev/null
@@ -0,0 +1,79 @@
+#!/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 <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+
+use C4::Circulation;
+
+use Koha::Database;
+use Koha::DateUtils qw( dt_from_string );
+use Koha::Patrons;
+use Koha::PseudonymizedTransactions;
+
+use t::lib::TestBuilder;
+use t::lib::Mocks;
+
+my $schema  = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'Koha::PseudonymizedTransactions tests' => sub {
+
+    plan tests => 11;
+
+    $schema->storage->txn_begin;
+
+    t::lib::Mocks::mock_config( 'key', '$2a$08$9lmorEKnwQloheaCLFIfje' );
+
+    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+
+    t::lib::Mocks::mock_preference( 'Pseudonymization', 0 );
+    my $item = $builder->build_sample_item;
+    t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch });
+    AddIssue( $patron->unblessed, $item->barcode, dt_from_string );
+    AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
+    my $pseudonymized= Koha::PseudonymizedTransactions->search(
+        { itemnumber => $item->itemnumber } )->next;
+    is( $pseudonymized, undef,
+        'No pseudonymized transaction if Pseudonymization is off' );
+
+    t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
+    t::lib::Mocks::mock_preference( 'PseudonymizationTransactionFields', 'datetime,transaction_branchcode,transaction_type,itemnumber,itemtype,holdingbranch,location,itemcallnumber,ccode'
+    );
+    $item = $builder->build_sample_item;
+    t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch });
+    AddIssue( $patron->unblessed, $item->barcode, dt_from_string );
+    AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
+    my $statistic = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next;
+    $pseudonymized = Koha::PseudonymizedTransactions->search( { itemnumber => $item->itemnumber } )->next;
+    like( $pseudonymized->hashed_borrowernumber,
+        qr{^\$2a\$08\$}, "The hashed_borrowernumber must be a bcrypt hash" );
+    is( $pseudonymized->datetime,               $statistic->datetime );
+    is( $pseudonymized->transaction_branchcode, $statistic->branch );
+    is( $pseudonymized->transaction_type,       $statistic->type );
+    is( $pseudonymized->itemnumber,             $item->itemnumber );
+    is( $pseudonymized->itemtype,               $item->effective_itemtype );
+    is( $pseudonymized->holdingbranch,          $item->holdingbranch );
+    is( $pseudonymized->location,               $item->location );
+    is( $pseudonymized->itemcallnumber,         $item->itemcallnumber );
+    is( $pseudonymized->ccode,                  $item->ccode );
+
+    $schema->storage->txn_rollback;
+};