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

(-)a/t/db_dependent/Koha/Statistics.t (-25 / +119 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-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
}
(-)a/t/db_dependent/Stats.t (-169 lines)
Lines 1-168 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 => 16;
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
                  interface => "INTERFACE",
47
    };
48
    my $return_error;
49
50
    # returns undef and croaks if type is not allowed
51
    $params -> {type} = "bla";
52
    eval {UpdateStats($params)};
53
    $return_error = $@;
54
    isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
55
56
    delete $params->{type};
57
    # returns undef and croaks if type is missing
58
    eval {UpdateStats($params)};
59
    $return_error = $@;
60
    isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
61
62
    $params -> {type} = undef;
63
    # returns undef and croaks if type is undef
64
    eval {UpdateStats($params)};
65
    $return_error = $@;
66
    isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
67
68
    # returns undef and croaks if mandatory params are missing
69
    my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall);
70
    my @allowed_accounts_types = qw (writeoff payment);
71
    my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
72
    my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
73
74
    my @missing_errors = ();
75
    foreach my $key (@circulation_mandatory_keys) {
76
        my $value = $params->{$key};
77
        delete $params->{$key};
78
        foreach my $type (@allowed_circulation_types) {
79
            $params->{type} = $type;
80
            eval {UpdateStats($params)};
81
            $return_error = $@;
82
            push @missing_errors, "key:$key for type:$type" unless $return_error;
83
        }
84
        $params->{$key} = $value;
85
    }
86
    foreach my $key (@accounts_mandatory_keys) {
87
        my $value = $params->{$key};
88
        delete $params->{$key};
89
        foreach my $type (@allowed_accounts_types) {
90
            $params->{type} = $type;
91
            eval {UpdateStats($params)};
92
            $return_error = $@;
93
            push @missing_errors, "key:$key for type:$type" unless $return_error;
94
        }
95
        $params->{$key} = $value;
96
97
    }
98
    is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
99
100
    # save the params in the right database fields
101
    $dbh->do(q|DELETE FROM statistics|);
102
    $params = {
103
                  branch => "BRA",
104
                  itemnumber => 31,
105
                  borrowernumber => 5,
106
                  amount =>5.1,
107
                  other => "bla",
108
                  itemtype => "BK",
109
                  location => "LOC",
110
                  ccode => "CODE",
111
                  type => "return",
112
                  interface => "INTERFACE",
113
    };
114
    UpdateStats ($params);
115
    my $sth = $dbh->prepare("SELECT * FROM statistics");
116
    $sth->execute();
117
    my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
118
    is ($params->{branch},         $line->{branch},         "UpdateStats save branch param in branch field of statistics table");
119
    is ($params->{type},           $line->{type},           "UpdateStats save type param in type field of statistics table");
120
    is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
121
    is ($params->{value},          $line->{value},          "UpdateStats save amount param in value field of statistics table");
122
    is ($params->{other},          $line->{other},          "UpdateStats save other param in other field of statistics table");
123
    is ($params->{itemtype},       $line->{itemtype},       "UpdateStats save itemtype param in itemtype field of statistics table");
124
    is ($params->{location},       $line->{location},       "UpdateStats save location param in location field of statistics table");
125
    is ($params->{ccode},          $line->{ccode},          "UpdateStats save ccode param in ccode field of statistics table");
126
    is ($params->{interface},      $line->{interface},      "UpdateStats save interface param in interface field of statistics table");
127
128
    $dbh->do(q|DELETE FROM statistics|);
129
    $params = {
130
        branch         => "BRA",
131
        itemnumber     => 31,
132
        borrowernumber => 5,
133
        amount         => 5.1,
134
        other          => "bla",
135
        itemtype       => "BK",
136
        ccode          => "CODE",
137
        type           => "return",
138
        interface      => "INTERFACE",
139
    };
140
    UpdateStats($params);
141
    $sth = $dbh->prepare("SELECT * FROM statistics");
142
    $sth->execute();
143
    $line = ${ $sth->fetchall_arrayref( {} ) }[0];
144
    is( $line->{location}, undef,
145
        "UpdateStats sets location to NULL if no location is passed in." );
146
147
    $dbh->do(q|DELETE FROM statistics|);
148
    $params = {
149
        branch         => "BRA",
150
        itemnumber     => 31,
151
        borrowernumber => 5,
152
        amount         => 5.1,
153
        other          => "bla",
154
        itemtype       => "BK",
155
        location       => undef,
156
        ccode          => "CODE",
157
        type           => "return",
158
        interface      => "interface"
159
    };
160
    UpdateStats($params);
161
    $sth = $dbh->prepare("SELECT * FROM statistics");
162
    $sth->execute();
163
    $line = ${ $sth->fetchall_arrayref( {} ) }[0];
164
    is( $line->{location}, undef,
165
        "UpdateStats sets location to NULL if undef is passed in." );
166
};
167
168
$schema->storage->txn_rollback;
169
- 

Return to bug 33608