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

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

Return to bug 33608