3 # Copyright 2013, 2019, 2023 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>.
22 use Test::More tests => 3;
29 use t::lib::TestBuilder;
31 our $schema = Koha::Database->new->schema;
32 our $builder = t::lib::TestBuilder->new;
34 our $test_params = { # No FK checks here
44 interface => 'INTERFACE',
47 subtest 'Basic Koha object tests' => sub {
49 $schema->storage->txn_begin;
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' } );
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,
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' );
74 $schema->storage->txn_rollback;
77 subtest 'Test exceptions in ->new' => sub {
79 $schema->storage->txn_begin;
81 throws_ok { Koha::Statistic->new } 'Koha::Exceptions::BadParameter', '->new called without params';
83 #FIXME Should we remove this for sake of consistency?
86 my $params = {%$test_params};
87 throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called without type';
90 $params = {%$test_params};
91 $params->{type} = "bla";
92 throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called with wrong type';
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';
107 $schema->storage->txn_rollback;
110 subtest 'Test new->store (fka UpdateStats)' => sub {
112 $schema->storage->txn_begin;
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" );
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" );
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" );
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" );
152 $schema->storage->txn_rollback;
155 sub insert_and_fetch {
157 my $statistic = Koha::Statistic->new($params)->store;
158 return Koha::Statistics->search( { borrowernumber => $test_params->{borrowernumber} } )->last;
160 # FIXME discard_changes would be nicer, but we dont have a PK (yet)