Lines 1-6
Link Here
|
1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
2 |
|
2 |
|
3 |
# Copyright 2019 Koha Development team |
3 |
# Copyright 2013, 2019, 2023 Koha Development team |
4 |
# |
4 |
# |
5 |
# This file is part of Koha |
5 |
# This file is part of Koha |
6 |
# |
6 |
# |
Lines 19-59
Link Here
|
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use Test::More tests => 5; |
22 |
use Test::More tests => 3; |
|
|
23 |
use Test::Exception; |
23 |
|
24 |
|
|
|
25 |
use C4::Context; |
24 |
use Koha::Database; |
26 |
use Koha::Database; |
25 |
use Koha::Statistics; |
27 |
use Koha::Statistics; |
26 |
use C4::Context; |
|
|
27 |
use C4::Stats qw( UpdateStats ); |
28 |
|
28 |
|
29 |
use t::lib::TestBuilder; |
29 |
use t::lib::TestBuilder; |
30 |
|
30 |
|
31 |
my $schema = Koha::Database->new->schema; |
31 |
our $schema = Koha::Database->new->schema; |
32 |
$schema->storage->txn_begin; |
32 |
our $builder = t::lib::TestBuilder->new; |
33 |
|
33 |
|
34 |
my $builder = t::lib::TestBuilder->new; |
34 |
our $test_params = { # No FK checks here |
35 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
35 |
branch => "BRA", |
36 |
my $item = $builder->build_sample_item; |
36 |
itemnumber => 31, |
37 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
37 |
borrowernumber => 5, |
38 |
C4::Stats::UpdateStats( |
38 |
categorycode => 'S', |
39 |
{ |
39 |
amount => 5.1, |
40 |
type => 'issue', |
40 |
other => "bla", |
41 |
branch => $library->branchcode, |
41 |
itemtype => "BK", |
42 |
itemnumber => $item->itemnumber, |
42 |
location => "LOC", |
43 |
borrowernumber => $patron->borrowernumber, |
43 |
ccode => "CODE", |
44 |
itemtype => $item->effective_itemtype, |
44 |
interface => 'INTERFACE', |
45 |
location => $item->location, |
45 |
}; |
46 |
ccode => $item->ccode, |
46 |
|
47 |
interface => C4::Context->interface |
47 |
subtest 'Basic Koha object tests' => sub { |
48 |
} |
48 |
plan tests => 4; |
49 |
); |
49 |
$schema->storage->txn_begin; |
50 |
|
50 |
|
51 |
my $stat = |
51 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
52 |
Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next; |
52 |
my $item = $builder->build_sample_item; |
53 |
is( $stat->borrowernumber, $patron->borrowernumber, 'Patron is there' ); |
53 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
54 |
is( $stat->branch, $library->branchcode, 'Library is there' ); |
54 |
|
55 |
is( ref( $stat->item ), 'Koha::Item', '->item returns a Koha::Item object' ); |
55 |
Koha::Statistic->insert( |
56 |
is( $stat->item->itemnumber, $item->itemnumber, '->item works great' ); |
56 |
{ |
57 |
is( $stat->interface, 'opac', 'Interface is recorded successfully' ); |
57 |
type => 'issue', |
58 |
|
58 |
branch => $library->branchcode, |
59 |
$schema->storage->txn_rollback; |
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 |
); |
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 ->insert (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->insert($params); |
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 |
} |