Bug 24151: Add tests
[koha.git] / t / db_dependent / Koha / Pseudonymization.t
blob99f6e2d40c7419c88d50ef1ed011a8c78767cc0f
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 => 1;
24 use C4::Circulation;
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Patrons;
29 use Koha::PseudonymizedTransactions;
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'Koha::PseudonymizedTransactions tests' => sub {
39 plan tests => 11;
41 $schema->storage->txn_begin;
43 t::lib::Mocks::mock_config( 'key', '$2a$08$9lmorEKnwQloheaCLFIfje' );
45 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
47 t::lib::Mocks::mock_preference( 'Pseudonymization', 0 );
48 my $item = $builder->build_sample_item;
49 t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch });
50 AddIssue( $patron->unblessed, $item->barcode, dt_from_string );
51 AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
52 my $pseudonymized= Koha::PseudonymizedTransactions->search(
53 { itemnumber => $item->itemnumber } )->next;
54 is( $pseudonymized, undef,
55 'No pseudonymized transaction if Pseudonymization is off' );
57 t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
58 t::lib::Mocks::mock_preference( 'PseudonymizationTransactionFields', 'datetime,transaction_branchcode,transaction_type,itemnumber,itemtype,holdingbranch,location,itemcallnumber,ccode'
60 $item = $builder->build_sample_item;
61 t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch });
62 AddIssue( $patron->unblessed, $item->barcode, dt_from_string );
63 AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
64 my $statistic = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next;
65 $pseudonymized = Koha::PseudonymizedTransactions->search( { itemnumber => $item->itemnumber } )->next;
66 like( $pseudonymized->hashed_borrowernumber,
67 qr{^\$2a\$08\$}, "The hashed_borrowernumber must be a bcrypt hash" );
68 is( $pseudonymized->datetime, $statistic->datetime );
69 is( $pseudonymized->transaction_branchcode, $statistic->branch );
70 is( $pseudonymized->transaction_type, $statistic->type );
71 is( $pseudonymized->itemnumber, $item->itemnumber );
72 is( $pseudonymized->itemtype, $item->effective_itemtype );
73 is( $pseudonymized->holdingbranch, $item->holdingbranch );
74 is( $pseudonymized->location, $item->location );
75 is( $pseudonymized->itemcallnumber, $item->itemcallnumber );
76 is( $pseudonymized->ccode, $item->ccode );
78 $schema->storage->txn_rollback;