|
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-42
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 |
|
| 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' } ); |
| 33 |
|
54 |
|
| 34 |
my $builder = t::lib::TestBuilder->new; |
55 |
Koha::Statistic->insert({ |
| 35 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
|
|
| 36 |
my $item = $builder->build_sample_item; |
| 37 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 38 |
C4::Stats::UpdateStats( |
| 39 |
{ |
| 40 |
type => 'issue', |
56 |
type => 'issue', |
| 41 |
branch => $library->branchcode, |
57 |
branch => $library->branchcode, |
| 42 |
itemnumber => $item->itemnumber, |
58 |
itemnumber => $item->itemnumber, |
|
Lines 44-59
C4::Stats::UpdateStats(
Link Here
|
| 44 |
itemtype => $item->effective_itemtype, |
60 |
itemtype => $item->effective_itemtype, |
| 45 |
location => $item->location, |
61 |
location => $item->location, |
| 46 |
ccode => $item->ccode, |
62 |
ccode => $item->ccode, |
| 47 |
interface => C4::Context->interface |
63 |
interface => C4::Context->interface, |
| 48 |
} |
64 |
}); |
| 49 |
); |
65 |
|
| 50 |
|
66 |
my $stat = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next; |
| 51 |
my $stat = |
67 |
is( $stat->borrowernumber, $patron->borrowernumber, 'Patron is there' ); |
| 52 |
Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next; |
68 |
is( $stat->branch, $library->branchcode, 'Library is there' ); |
| 53 |
is( $stat->borrowernumber, $patron->borrowernumber, 'Patron is there' ); |
69 |
is( ref( $stat->item ), 'Koha::Item', '->item returns a Koha::Item object' ); |
| 54 |
is( $stat->branch, $library->branchcode, 'Library is there' ); |
70 |
is( $stat->item->itemnumber, $item->itemnumber, '->item works great' ); |
| 55 |
is( ref( $stat->item ), 'Koha::Item', '->item returns a Koha::Item object' ); |
71 |
|
| 56 |
is( $stat->item->itemnumber, $item->itemnumber, '->item works great' ); |
72 |
$schema->storage->txn_rollback; |
| 57 |
is( $stat->interface, 'opac', 'Interface is recorded successfully' ); |
73 |
}; |
| 58 |
|
74 |
|
| 59 |
$schema->storage->txn_rollback; |
75 |
subtest 'Test exceptions in ->new' => sub { |
|
|
76 |
plan tests => 5; |
| 77 |
$schema->storage->txn_begin; |
| 78 |
|
| 79 |
throws_ok { Koha::Statistic->new } 'Koha::Exceptions::BadParameter', '->new called without params'; |
| 80 |
#FIXME Should we remove this for sake of consistency? |
| 81 |
|
| 82 |
# Type is missing |
| 83 |
my $params = { %$test_params }; |
| 84 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called without type'; |
| 85 |
|
| 86 |
# Type is not allowed |
| 87 |
$params = { %$test_params }; |
| 88 |
$params ->{type} = "bla"; |
| 89 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called with wrong type'; |
| 90 |
|
| 91 |
# Test mandatory accounts/circulation keys |
| 92 |
$params = { %$test_params }; |
| 93 |
$params->{type} = 'payment'; |
| 94 |
delete $params->{amount}; |
| 95 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for accounts without amount'; |
| 96 |
$params->{type} = 'issue'; |
| 97 |
delete $params->{itemnumber}; |
| 98 |
throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for circulation without itemnumber'; |
| 99 |
|
| 100 |
$schema->storage->txn_rollback; |
| 101 |
}; |
| 102 |
|
| 103 |
subtest 'Test ->insert (fka UpdateStats)' => sub { |
| 104 |
plan tests => 15; |
| 105 |
$schema->storage->txn_begin; |
| 106 |
|
| 107 |
# save the params in the right database fields |
| 108 |
my $statistic = insert_and_fetch({ %$test_params, type => 'return' }); |
| 109 |
is( $statistic->branch, $test_params->{branch}, "Check branch" ); |
| 110 |
is( $statistic->type, 'return', "Check type" ); |
| 111 |
is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" ); |
| 112 |
is( $statistic->value, $test_params->{amount}, "Check value" ); |
| 113 |
is( $statistic->other, $test_params->{other}, "Check other" ); |
| 114 |
is( $statistic->itemtype, $test_params->{itemtype}, "Check itemtype" ); |
| 115 |
is( $statistic->location, $test_params->{location}, "Check location" ); |
| 116 |
is( $statistic->ccode, $test_params->{ccode}, "Check ccode" ); |
| 117 |
is( $statistic->interface, $test_params->{interface}, "Check interface" ); |
| 118 |
|
| 119 |
# Test location with undef and empty string |
| 120 |
my $params = { %$test_params, type => 'return' }; |
| 121 |
delete $params->{location}; |
| 122 |
$statistic = insert_and_fetch($params); |
| 123 |
is( $statistic->location, undef, "Location is NULL if not passed" ); |
| 124 |
$params->{location} = q{}; |
| 125 |
$statistic = insert_and_fetch($params); |
| 126 |
is( $statistic->location, q{}, "Location is empty string if passed" ); |
| 127 |
|
| 128 |
# Test 'other' with undef and empty string (slightly different behavior from location, using _key_or_default) |
| 129 |
$params = { %$test_params, type => 'return' }; |
| 130 |
delete $params->{other}; |
| 131 |
$statistic = insert_and_fetch($params); |
| 132 |
is( $statistic->other, q{}, "Other is empty string if not passed" ); |
| 133 |
$params->{other} = undef; |
| 134 |
$statistic = insert_and_fetch($params); |
| 135 |
is( $statistic->other, undef, "Other is NULL if passed undef" ); |
| 136 |
|
| 137 |
# Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?) |
| 138 |
$params = { %$test_params, type => 'return', value => 0 }; |
| 139 |
$statistic = insert_and_fetch($params); |
| 140 |
is( $statistic->value, 0, "Value is zero, overriding non-zero amount" ); |
| 141 |
delete $params->{value}; |
| 142 |
$statistic = insert_and_fetch($params); |
| 143 |
is( $statistic->value, 5.1, "No value passed, amount used" ); |
| 144 |
|
| 145 |
$schema->storage->txn_rollback; |
| 146 |
}; |
| 147 |
|
| 148 |
sub insert_and_fetch { |
| 149 |
my $params = shift; |
| 150 |
my $statistic = Koha::Statistic->insert($params); |
| 151 |
return Koha::Statistics->search({ borrowernumber => $test_params->{borrowernumber} })->last; |
| 152 |
# FIXME discard_changes would be nicer, but we dont have a PK (yet) |
| 153 |
} |