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

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

Return to bug 33608