View | Details | Raw Unified | Return to bug 35277
Collapse All | Expand All

(-)a/t/db_dependent/Koha/BackgroundJob/PseudonymizeStatistic.t (-1 / +98 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 2;
21
use Test::Exception;
22
23
use Koha::Database;
24
use Koha::BackgroundJobs;
25
use Koha::BackgroundJob::PseudonymizeStatistic;
26
27
use t::lib::Mocks;
28
use t::lib::TestBuilder;
29
30
use Carp::Always;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
subtest 'enqueue() tests' => sub {
36
37
    plan tests => 4;
38
39
    $schema->storage->txn_begin;
40
41
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
42
    my $item   = $builder->build_sample_item();
43
44
    throws_ok {
45
        Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue()
46
    }
47
    'Koha::Exceptions::MissingParameter',
48
        "Exception thrown if 'statistic' param is missing";
49
50
    my $statistic = $builder->build_object(
51
        {
52
            class => 'Koha::Statistics',
53
            value => { type => 'issue', borrowernumber => $patron->id, itemnumber => $item->id }
54
        }
55
    );
56
57
    my $job_id = Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue( { statistic => $statistic->unblessed } );
58
59
    my $job = Koha::BackgroundJobs->find($job_id)->_derived_class;
60
61
    is( $job->size,   1,         'Size is correct' );
62
    is( $job->status, 'new',     'Initial status set correctly' );
63
    is( $job->queue,  'default', 'PseudonymizeStatistic should use the default queue' );
64
65
    $schema->storage->txn_rollback;
66
};
67
68
subtest 'process() tests' => sub {
69
70
    plan tests => 2;
71
72
    $schema->storage->txn_begin;
73
74
    t::lib::Mocks::mock_config( 'bcrypt_settings', '$2a$08$9lmorEKnwQloheaCLFIfje' );
75
    t::lib::Mocks::mock_preference( 'Pseudonymization',             1 );
76
    t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields', 'branchcode,categorycode,sort1' );
77
78
    my $patron    = $builder->build_object( { class => 'Koha::Patrons' } );
79
    my $item      = $builder->build_sample_item();
80
    my $statistic = $builder->build_object(
81
        {
82
            class => 'Koha::Statistics',
83
            value => { type => 'issue', borrowernumber => $patron->id, itemnumber => $item->id }
84
        }
85
    );
86
87
    my $job_id = Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue( { statistic => $statistic->unblessed } );
88
    my $pseudonymized_transactions_before = Koha::PseudonymizedTransactions->search()->count();
89
    my $job                               = Koha::BackgroundJobs->find($job_id)->_derived_class;
90
    $job->process( { statistic => $statistic->unblessed } );
91
    my $pseudonymized_transactions_after = Koha::PseudonymizedTransactions->search()->count();
92
    is( $pseudonymized_transactions_after, $pseudonymized_transactions_before + 1, "Pseudonymized transaction added" );
93
94
    $job->discard_changes;
95
    is( $job->data, '{"data":""}', "Job data cleared after pseudonymization" );
96
97
    $schema->storage->txn_rollback;
98
};

Return to bug 35277