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

(-)a/t/db_dependent/Circulation/Returns.t (-35 / +154 lines)
Lines 1-50 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
1
use Modern::Perl;
18
use Modern::Perl;
19
2
use Test::More tests => 2;
20
use Test::More tests => 2;
21
use Test::MockModule;
3
22
4
use C4::Biblio;
23
use C4::Biblio;
5
use C4::Circulation;
24
use C4::Circulation;
6
use C4::Items;
25
use C4::Items;
26
use C4::ItemType;
7
use C4::Members;
27
use C4::Members;
28
use Koha::Database;
8
use Koha::DateUtils;
29
use Koha::DateUtils;
9
30
10
use MARC::Record;
31
use MARC::Record;
32
use MARC::Field;
11
33
12
*C4::Context::userenv = \&Mock_userenv;
34
use t::lib::TestBuilder;
35
my $builder = t::lib::TestBuilder->new();
13
36
37
# Mock userenv, used by AddIssue
38
my $branch;
39
my $context = Test::MockModule->new('C4::Context');
40
$context->mock( 'userenv', sub {
41
    return { branch => $branch }
42
});
43
44
my $schema = Koha::Database->new()->schema();
14
my $dbh = C4::Context->dbh;
45
my $dbh = C4::Context->dbh;
15
$dbh->{AutoCommit} = 0;
46
$dbh->{AutoCommit} = 0;
16
$dbh->{RaiseError} = 1;
47
$dbh->{RaiseError} = 1;
17
48
18
my $record = MARC::Record->new();
49
subtest "InProcessingToShelvingCart tests" => sub {
19
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
50
20
51
    plan tests => 2;
21
my ( undef, undef, $itemnumber ) = AddItem(
52
22
    {
53
    $branch = $builder->build({ source => 'Branch' })->{ branchcode };
23
        homebranch         => 'CPL',
54
    my $permanent_location = 'TEST';
24
        holdingbranch      => 'CPL',
55
    my $location           = 'PROC';
25
        barcode            => 'i_dont_exist',
56
26
        location           => 'PROC',
57
    # Create a biblio record with biblio-level itemtype
27
        permanent_location => 'TEST'
58
    my $record = MARC::Record->new();
28
    },
59
    my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
29
    $biblionumber
60
    my $built_item = $builder->build({
30
);
61
        source => 'Item',
31
62
        value  => {
32
my $item;
63
            biblionumber  => $biblionumber,
33
64
            homebranch    => $branch,
34
C4::Context->set_preference( "InProcessingToShelvingCart", 1 );
65
            holdingbranch => $branch,
35
AddReturn( 'i_dont_exist', 'CPL' );
66
            location      => $location,
36
$item = GetItem($itemnumber);
67
            permanent_location => $permanent_location
37
is( $item->{location}, 'CART', "InProcessingToShelvingCart functions as intended" );
68
        }
38
69
    });
39
$item->{location} = 'PROC';
70
    my $barcode = $built_item->{ barcode };
40
ModItem( $item, undef, $itemnumber );
71
    my $itemnumber = $built_item->{ itemnumber };
41
72
    my $item;
42
C4::Context->set_preference( "InProcessingToShelvingCart", 0 );
73
43
AddReturn( 'i_dont_exist', 'CPL' );
74
    C4::Context->set_preference( "InProcessingToShelvingCart", 1 );
44
$item = GetItem($itemnumber);
75
    AddReturn( $barcode, $branch );
45
is( $item->{location}, 'TEST', "InProcessingToShelvingCart functions as intended" );
76
    $item = GetItem( $itemnumber );
46
77
    is( $item->{location}, 'CART',
47
# C4::Context->userenv
78
        "InProcessingToShelvingCart functions as intended" );
48
sub Mock_userenv {
79
49
    return { branch => 'CPL' };
80
    $item->{location} = $location;
50
}
81
    ModItem( $item, undef, $itemnumber );
82
83
    C4::Context->set_preference( "InProcessingToShelvingCart", 0 );
84
    AddReturn( $barcode, $branch );
85
    $item = GetItem( $itemnumber );
86
    is( $item->{location}, $permanent_location,
87
        "InProcessingToShelvingCart functions as intended" );
88
};
89
90
91
subtest "AddReturn logging on statistics table" => sub {
92
93
    plan tests => 2;
94
95
    # Make sure logging is enabled
96
    C4::Context->set_preference( "IssueLog", 1 );
97
    C4::Context->set_preference( "ReturnLog", 1 );
98
99
    # Create an itemtype for biblio-level item type
100
    my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
101
    # Create an itemtype for item-level item type
102
    my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
103
    # Create a branch
104
    $branch = $builder->build({ source => 'Branch' })->{ branchcode };
105
    # Create a borrower
106
    my $borrowernumber = $builder->build({
107
        source => 'Borrower',
108
        value => { branchcode => $branch }
109
    })->{ borrowernumber };
110
    # Look for the defined MARC field for biblio-level itemtype
111
    my $rs = $schema->resultset('MarcSubfieldStructure')->search({
112
        frameworkcode => '',
113
        kohafield     => 'biblioitems.itemtype'
114
    });
115
    my $tagfield    = $rs->first->tagfield;
116
    my $tagsubfield = $rs->first->tagsubfield;
117
118
    # Create a biblio record with biblio-level itemtype
119
    my $record = MARC::Record->new();
120
    $record->append_fields(
121
        MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
122
    );
123
    my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
124
    my $item_with_itemtype = $builder->build({
125
        source => 'Item',
126
        value  => {
127
            biblionumber  => $biblionumber,
128
            homebranch    => $branch,
129
            holdingbranch => $branch,
130
            itype         => $ilevel_itemtype
131
        }
132
    });
133
    my $item_without_itemtype = $builder->build({
134
        source => 'Item',
135
        value  => {
136
            biblionumber  => $biblionumber,
137
            homebranch    => $branch,
138
            holdingbranch => $branch,
139
            itype         => undef
140
        }
141
    });
142
143
    my $borrower = GetMember( borrowernumber => $borrowernumber );
144
    AddIssue( $borrower, $item_with_itemtype->{ barcode } );
145
    AddReturn( $item_with_itemtype->{ barcode }, $branch );
146
    # Test item-level itemtype was recorded on the 'statistics' table
147
    my $stat = $schema->resultset('Statistic')->search({
148
        branch     => $branch,
149
        type       => 'return',
150
        itemnumber => $item_with_itemtype->{ itemnumber }
151
    }, { order_by => { -asc => 'datetime' } })->next();
152
153
    is( $stat->itemtype, $item_with_itemtype->{ itype },
154
        "item-level itype recorded on statistics for return");
155
156
    AddIssue( $borrower, $item_without_itemtype->{ barcode } );
157
    AddReturn( $item_without_itemtype->{ barcode }, $branch );
158
    # Test biblio-level itemtype was recorded on the 'statistics' table
159
    $stat = $schema->resultset('Statistic')->search({
160
        branch     => $branch,
161
        type       => 'return',
162
        itemnumber => $item_without_itemtype->{ itemnumber }
163
    }, { order_by => { -asc => 'datetime' } })->next();
164
165
    is( $stat->itemtype, $blevel_itemtype,
166
        "biblio-level itype recorded on statistics for return");
167
168
};
169
170
1;
51
- 

Return to bug 14598