Bug 35086: Add chunk_size option to elasticsearch configuration
[koha.git] / t / db_dependent / Koha / Statistics.t
1 #!/usr/bin/perl
2
3 # Copyright 2013, 2019, 2023 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 => 3;
23 use Test::Exception;
24
25 use C4::Context;
26 use Koha::Database;
27 use Koha::Statistics;
28
29 use t::lib::TestBuilder;
30
31 our $schema  = Koha::Database->new->schema;
32 our $builder = t::lib::TestBuilder->new;
33
34 our $test_params = {    # No FK checks here
35     branch         => "BRA",
36     itemnumber     => 31,
37     borrowernumber => 5,
38     categorycode   => 'S',
39     amount         => 5.1,
40     other          => "bla",
41     itemtype       => "BK",
42     location       => "LOC",
43     ccode          => "CODE",
44     interface      => 'INTERFACE',
45 };
46
47 subtest 'Basic Koha object tests' => sub {
48     plan tests => 4;
49     $schema->storage->txn_begin;
50
51     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
52     my $item    = $builder->build_sample_item;
53     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
54
55     Koha::Statistic->new(
56         {
57             type           => 'issue',
58             branch         => $library->branchcode,
59             itemnumber     => $item->itemnumber,
60             borrowernumber => $patron->borrowernumber,
61             itemtype       => $item->effective_itemtype,
62             location       => $item->location,
63             ccode          => $item->ccode,
64             interface      => C4::Context->interface,
65         }
66     )->store;
67
68     my $stat = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next;
69     is( $stat->borrowernumber,   $patron->borrowernumber, 'Patron is there' );
70     is( $stat->branch,           $library->branchcode,    'Library is there' );
71     is( ref( $stat->item ),      'Koha::Item',            '->item returns a Koha::Item object' );
72     is( $stat->item->itemnumber, $item->itemnumber,       '->item works great' );
73
74     $schema->storage->txn_rollback;
75 };
76
77 subtest 'Test exceptions in ->new' => sub {
78     plan tests => 6;
79     $schema->storage->txn_begin;
80
81     throws_ok { Koha::Statistic->new } 'Koha::Exceptions::BadParameter', '->new called without params';
82
83     #FIXME Should we remove this for sake of consistency?
84
85     # Type is missing
86     my $params = {%$test_params};
87     throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called without type';
88
89     # Type is not allowed
90     $params = {%$test_params};
91     $params->{type} = "bla";
92     throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called with wrong type';
93
94     # Test mandatory accounts/circulation keys
95     $params = {%$test_params};
96     $params->{type} = 'payment';
97     delete $params->{amount};
98     throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter',
99         '->new called for accounts without amount';
100     $params->{amount} = 0;
101     lives_ok { Koha::Statistic->new($params) } '->new accepts zero amount';
102     $params->{type} = 'issue';
103     delete $params->{itemnumber};
104     throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter',
105         '->new called for circulation without itemnumber';
106
107     $schema->storage->txn_rollback;
108 };
109
110 subtest 'Test new->store (fka UpdateStats)' => sub {
111     plan tests => 15;
112     $schema->storage->txn_begin;
113
114     # save the params in the right database fields
115     my $statistic = insert_and_fetch( { %$test_params, type => 'return' } );
116     is( $statistic->branch,         $test_params->{branch},         "Check branch" );
117     is( $statistic->type,           'return',                       "Check type" );
118     is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" );
119     is( $statistic->value,          $test_params->{amount},         "Check value" );
120     is( $statistic->other,          $test_params->{other},          "Check other" );
121     is( $statistic->itemtype,       $test_params->{itemtype},       "Check itemtype" );
122     is( $statistic->location,       $test_params->{location},       "Check location" );
123     is( $statistic->ccode,          $test_params->{ccode},          "Check ccode" );
124     is( $statistic->interface,      $test_params->{interface},      "Check interface" );
125
126     # Test location with undef and empty string
127     my $params = { %$test_params, type => 'return' };
128     delete $params->{location};
129     $statistic = insert_and_fetch($params);
130     is( $statistic->location, undef, "Location is NULL if not passed" );
131     $params->{location} = q{};
132     $statistic = insert_and_fetch($params);
133     is( $statistic->location, q{}, "Location is empty string if passed" );
134
135     # Test 'other' with undef and empty string (slightly different behavior from location, using _key_or_default)
136     $params = { %$test_params, type => 'return' };
137     delete $params->{other};
138     $statistic = insert_and_fetch($params);
139     is( $statistic->other, q{}, "Other is empty string if not passed" );
140     $params->{other} = undef;
141     $statistic = insert_and_fetch($params);
142     is( $statistic->other, undef, "Other is NULL if passed undef" );
143
144     # Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?)
145     $params    = { %$test_params, type => 'return', value => 0 };
146     $statistic = insert_and_fetch($params);
147     is( $statistic->value, 0, "Value is zero, overriding non-zero amount" );
148     delete $params->{value};
149     $statistic = insert_and_fetch($params);
150     is( $statistic->value, 5.1, "No value passed, amount used" );
151
152     $schema->storage->txn_rollback;
153 };
154
155 sub insert_and_fetch {
156     my $params    = shift;
157     my $statistic = Koha::Statistic->new($params)->store;
158     return Koha::Statistics->search( { borrowernumber => $test_params->{borrowernumber} } )->last;
159
160     # FIXME discard_changes would be nicer, but we dont have a PK (yet)
161 }