Bug 24151: Add tests
[srvgit] / t / db_dependent / Koha / Pseudonymization.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 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 => 1;
23
24 use C4::Circulation;
25
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Patrons;
29 use Koha::PseudonymizedTransactions;
30
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
33
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'Koha::PseudonymizedTransactions tests' => sub {
38
39     plan tests => 11;
40
41     $schema->storage->txn_begin;
42
43     t::lib::Mocks::mock_config( 'key', '$2a$08$9lmorEKnwQloheaCLFIfje' );
44
45     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
46
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' );
56
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'
59     );
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 );
77
78     $schema->storage->txn_rollback;
79 };