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

(-)a/t/db_dependent/Circulation/Returns.t (-42 / +238 lines)
Lines 1-57 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;
2
use Test::More tests => 2;
19
20
use Test::More tests => 3;
21
use Test::MockModule;
22
use t::lib::TestBuilder;
3
23
4
use C4::Biblio;
24
use C4::Biblio;
5
use C4::Circulation;
25
use C4::Circulation;
6
use C4::Items;
26
use C4::Items;
27
use C4::ItemType;
7
use C4::Members;
28
use C4::Members;
8
use Koha::Database;
29
use Koha::Database;
9
use Koha::DateUtils;
30
use Koha::DateUtils;
10
31
use Koha::Items;
11
use t::lib::TestBuilder;
12
32
13
use MARC::Record;
33
use MARC::Record;
34
use MARC::Field;
14
35
15
*C4::Context::userenv = \&Mock_userenv;
36
# Mock userenv, used by AddIssue
37
my $branch;
38
my $context = Test::MockModule->new('C4::Context');
39
$context->mock( 'userenv', sub {
40
    return { branch => $branch }
41
});
16
42
17
my $schema = Koha::Database->schema;
43
my $schema = Koha::Database->schema;
18
$schema->storage->txn_begin;
44
$schema->storage->txn_begin;
19
my $builder = t::lib::TestBuilder->new;
20
45
21
my $library = $builder->build({
46
my $builder = t::lib::TestBuilder->new();
22
    source => 'Branch',
47
23
});
48
subtest "InProcessingToShelvingCart tests" => sub {
49
50
    plan tests => 2;
51
52
    $branch = $builder->build({ source => 'Branch' })->{ branchcode };
53
    my $permanent_location = 'TEST';
54
    my $location           = 'PROC';
55
56
    # Create a biblio record with biblio-level itemtype
57
    my $record = MARC::Record->new();
58
    my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
59
    my $built_item = $builder->build({
60
        source => 'Item',
61
        value  => {
62
            biblionumber  => $biblionumber,
63
            homebranch    => $branch,
64
            holdingbranch => $branch,
65
            location      => $location,
66
            permanent_location => $permanent_location
67
        }
68
    });
69
    my $barcode = $built_item->{ barcode };
70
    my $itemnumber = $built_item->{ itemnumber };
71
    my $item;
72
73
    C4::Context->set_preference( "InProcessingToShelvingCart", 1 );
74
    AddReturn( $barcode, $branch );
75
    $item = GetItem( $itemnumber );
76
    is( $item->{location}, 'CART',
77
        "InProcessingToShelvingCart functions as intended" );
78
79
    $item->{location} = $location;
80
    ModItem( $item, undef, $itemnumber );
81
82
    C4::Context->set_preference( "InProcessingToShelvingCart", 0 );
83
    AddReturn( $barcode, $branch );
84
    $item = GetItem( $itemnumber );
85
    is( $item->{location}, $permanent_location,
86
        "InProcessingToShelvingCart functions as intended" );
87
};
88
89
90
subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
91
92
    plan tests => 2;
93
94
    # Set item-level item types
95
    C4::Context->set_preference( "item-level_itypes", 1 );
96
97
    # Make sure logging is enabled
98
    C4::Context->set_preference( "IssueLog", 1 );
99
    C4::Context->set_preference( "ReturnLog", 1 );
100
101
    # Create an itemtype for biblio-level item type
102
    my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
103
    # Create an itemtype for item-level item type
104
    my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
105
    # Create a branch
106
    $branch = $builder->build({ source => 'Branch' })->{ branchcode };
107
    # Create a borrower
108
    my $borrowernumber = $builder->build({
109
        source => 'Borrower',
110
        value => { branchcode => $branch }
111
    })->{ borrowernumber };
112
    # Look for the defined MARC field for biblio-level itemtype
113
    my $rs = $schema->resultset('MarcSubfieldStructure')->search({
114
        frameworkcode => '',
115
        kohafield     => 'biblioitems.itemtype'
116
    });
117
    my $tagfield    = $rs->first->tagfield;
118
    my $tagsubfield = $rs->first->tagsubfield;
119
120
    # Create a biblio record with biblio-level itemtype
121
    my $record = MARC::Record->new();
122
    $record->append_fields(
123
        MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
124
    );
125
    my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
126
    my $item_with_itemtype = $builder->build({
127
        source => 'Item',
128
        value  => {
129
            biblionumber  => $biblionumber,
130
            homebranch    => $branch,
131
            holdingbranch => $branch,
132
            itype         => $ilevel_itemtype
133
        }
134
    });
135
    my $item_without_itemtype = $builder->build({
136
        source => 'Item',
137
        value  => {
138
            biblionumber  => $biblionumber,
139
            homebranch    => $branch,
140
            holdingbranch => $branch,
141
            itype         => undef
142
        }
143
    });
144
145
    my $borrower = GetMember( borrowernumber => $borrowernumber );
146
    AddIssue( $borrower, $item_with_itemtype->{ barcode } );
147
    AddReturn( $item_with_itemtype->{ barcode }, $branch );
148
    # Test item-level itemtype was recorded on the 'statistics' table
149
    my $stat = $schema->resultset('Statistic')->search({
150
        branch     => $branch,
151
        type       => 'return',
152
        itemnumber => $item_with_itemtype->{ itemnumber }
153
    }, { order_by => { -asc => 'datetime' } })->next();
154
155
    is( $stat->itemtype, $ilevel_itemtype,
156
        "item-level itype recorded on statistics for return");
157
158
    AddIssue( $borrower, $item_without_itemtype->{ barcode } );
159
    AddReturn( $item_without_itemtype->{ barcode }, $branch );
160
    # Test biblio-level itemtype was recorded on the 'statistics' table
161
    $stat = $schema->resultset('Statistic')->search({
162
        branch     => $branch,
163
        type       => 'return',
164
        itemnumber => $item_without_itemtype->{ itemnumber }
165
    }, { order_by => { -asc => 'datetime' } })->next();
166
167
    is( $stat->itemtype, $blevel_itemtype,
168
        "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
169
170
};
171
172
subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
173
174
    plan tests => 2;
175
176
    # Make sure logging is enabled
177
    C4::Context->set_preference( "IssueLog", 1 );
178
    C4::Context->set_preference( "ReturnLog", 1 );
179
180
    # Set item-level item types
181
    C4::Context->set_preference( "item-level_itypes", 0 );
182
183
    # Create an itemtype for biblio-level item type
184
    my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
185
    # Create an itemtype for item-level item type
186
    my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
187
    # Create a branch
188
    $branch = $builder->build({ source => 'Branch' })->{ branchcode };
189
    # Create a borrower
190
    my $borrowernumber = $builder->build({
191
        source => 'Borrower',
192
        value => { branchcode => $branch }
193
    })->{ borrowernumber };
194
    # Look for the defined MARC field for biblio-level itemtype
195
    my $rs = $schema->resultset('MarcSubfieldStructure')->search({
196
        frameworkcode => '',
197
        kohafield     => 'biblioitems.itemtype'
198
    });
199
    my $tagfield    = $rs->first->tagfield;
200
    my $tagsubfield = $rs->first->tagsubfield;
201
202
    # Create a biblio record with biblio-level itemtype
203
    my $record = MARC::Record->new();
204
    $record->append_fields(
205
        MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
206
    );
207
    my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
208
    my $item_with_itemtype = $builder->build({
209
        source => 'Item',
210
        value  => {
211
            biblionumber  => $biblionumber,
212
            homebranch    => $branch,
213
            holdingbranch => $branch,
214
            itype         => $ilevel_itemtype
215
        }
216
    });
217
    my $item_without_itemtype = $builder->build({
218
        source => 'Item',
219
        value  => {
220
            biblionumber  => $biblionumber,
221
            homebranch    => $branch,
222
            holdingbranch => $branch,
223
            itype         => undef
224
        }
225
    });
226
227
    my $borrower = GetMember( borrowernumber => $borrowernumber );
228
229
    AddIssue( $borrower, $item_with_itemtype->{ barcode } );
230
    AddReturn( $item_with_itemtype->{ barcode }, $branch );
231
    # Test item-level itemtype was recorded on the 'statistics' table
232
    my $stat = $schema->resultset('Statistic')->search({
233
        branch     => $branch,
234
        type       => 'return',
235
        itemnumber => $item_with_itemtype->{ itemnumber }
236
    }, { order_by => { -asc => 'datetime' } })->next();
237
238
    is( $stat->itemtype, $blevel_itemtype,
239
        "biblio-level itype recorded on statistics for return");
240
241
    AddIssue( $borrower, $item_without_itemtype->{ barcode } );
242
    AddReturn( $item_without_itemtype->{ barcode }, $branch );
243
    # Test biblio-level itemtype was recorded on the 'statistics' table
244
    $stat = $schema->resultset('Statistic')->search({
245
        branch     => $branch,
246
        type       => 'return',
247
        itemnumber => $item_without_itemtype->{ itemnumber }
248
    }, { order_by => { -asc => 'datetime' } })->next();
249
250
    is( $stat->itemtype, $blevel_itemtype,
251
        "biblio-level itype recorded on statistics for return");
252
};
24
253
25
my $record = MARC::Record->new();
254
1;
26
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
27
28
my ( undef, undef, $itemnumber ) = AddItem(
29
    {
30
        homebranch         => $library->{branchcode},
31
        holdingbranch      => $library->{branchcode},
32
        barcode            => 'i_dont_exist',
33
        location           => 'PROC',
34
        permanent_location => 'TEST'
35
    },
36
    $biblionumber
37
);
38
39
my $item;
40
41
C4::Context->set_preference( "InProcessingToShelvingCart", 1 );
42
AddReturn( 'i_dont_exist', $library->{branchcode} );
43
$item = GetItem($itemnumber);
44
is( $item->{location}, 'CART', "InProcessingToShelvingCart functions as intended" );
45
46
$item->{location} = 'PROC';
47
ModItem( $item, undef, $itemnumber );
48
49
C4::Context->set_preference( "InProcessingToShelvingCart", 0 );
50
AddReturn( 'i_dont_exist', $library->{branchcode} );
51
$item = GetItem($itemnumber);
52
is( $item->{location}, 'TEST', "InProcessingToShelvingCart functions as intended" );
53
54
# C4::Context->userenv
55
sub Mock_userenv {
56
    return { branch => $library->{branchcode} };
57
}
58
- 

Return to bug 14598