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

(-)a/t/db_dependent/Koha/RotatingCollections.t (+142 lines)
Line 0 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
18
use Modern::Perl;
19
20
use Test::More tests => 2;
21
use C4::Context;
22
use Koha::Biblios;
23
use Koha::Database;
24
use Koha::Library;
25
use Koha::RotatingCollections;
26
use Koha::Items;
27
use Koha::Item::Transfers;
28
use Koha::Libraries;
29
30
use t::lib::TestBuilder;
31
32
my $schema = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
subtest 'remove_and_add_items' => sub {
36
    plan tests => 8;
37
38
    $schema->storage->txn_begin;
39
40
    my $collection = Koha::RotatingCollection->new( {
41
        colTitle => 'Test title 1',
42
        colDesc  => 'Test description 1',
43
    } )->store;
44
45
    my $biblioitem = $builder->build( { source => 'Biblioitem' } );
46
47
    my $library = $builder->build( { source => 'Branch' } );
48
49
    my $item1 = Koha::Item->new( {
50
        biblionumber => $biblioitem->{biblionumber},
51
        biblioitemnumber => $biblioitem->{biblioitemnumber},
52
        homebranch => $library->{branchcode},
53
        holdingbranch => $library->{branchcode},
54
        barcode => 'barcode1',
55
    } )->store;
56
57
    my $item2 = Koha::Item->new( {
58
        biblionumber => $biblioitem->{biblionumber},
59
        biblioitemnumber => $biblioitem->{biblioitemnumber},
60
        homebranch => $library->{branchcode},
61
        holdingbranch => $library->{branchcode},
62
        barcode => 'barode2',
63
    } )->store;
64
65
    is( $collection->items->count, 0, 'In newly created collection there should not be any item');
66
67
    $collection->add_item( $item1 );
68
    $collection->add_item( $item2 );
69
70
    is( $collection->items->count, 2, 'Added two items, there should be two');
71
72
    eval { $collection->add_item };
73
    is( ref($@), 'Koha::Exceptions::MissingParameter', 'Missing paramater exception');
74
75
    eval { $collection->add_item( $item1 ) };
76
    is( ref($@), 'Koha::Exceptions::DuplicateObject', 'Duplicate Object Exception - you should not add the same item twice');
77
78
    eval { $collection->add_item('bad_itemnumber') };
79
    is( ref($@), 'Koha::Exceptions::ObjectNotFound', 'Object Not Found Exception - you cannot add non existent item');
80
81
    $collection->remove_item( $item1 );
82
83
    is( $collection->items->count, 1, 'We removed first item, there should be one remaining');
84
85
    eval { $collection->remove_item };
86
    is( ref($@), 'Koha::Exceptions::MissingParameter', 'Missing paramater exception');
87
88
    eval { $collection->remove_item( $item1 ) };
89
    is( ref($@), 'Koha::Exceptions::ObjectNotFound', 'Object Not Found Exception - cannot remove the same item twice');
90
91
    $schema->storage->txn_rollback;
92
};
93
94
subtest 'transfer' => sub {
95
    plan tests => 4;
96
97
    $schema->storage->txn_begin;
98
99
    my $collection = Koha::RotatingCollection->new( {
100
        colTitle => 'Test title 1',
101
        colDesc  => 'Test description 1',
102
    } )->store;
103
    my $biblioitem  = $builder->build( { source => 'Biblioitem' } );
104
105
    my $library1 = Koha::Library->new( {
106
        branchcode => 'CODE1',
107
    } )->store;
108
109
    my $library2 = Koha::Library->new( {
110
        branchcode => 'CODE2',
111
    } )->store;
112
113
    my $item = Koha::Item->new( {
114
        biblionumber     => $biblioitem->{biblionumber},
115
        biblioitemnumber => $biblioitem->{biblioitemnumber},
116
        homebranch       => $library1->branchcode,
117
        holdingbranch    => $library1->branchcode,
118
        itype            => 'BK',
119
        barcode          => 'some_barcode',
120
    } )->store;
121
122
    $collection->add_item( $item );
123
124
    eval { $collection->transfer };
125
    is( ref($@), 'Koha::Exceptions::MissingParameter', 'Missing paramater exception');
126
127
    $collection->transfer( $library2 );
128
    my $retrieved_item = Koha::Items->find( $item->itemnumber );
129
130
    is( $collection->colBranchcode, $library2->branchcode, 'Collection should be transferred' );
131
    is( $retrieved_item->holdingbranch, $library2->branchcode, 'Items in collection should be transferred too' );
132
133
    my $transfer = Koha::Item::Transfers->search( {
134
        itemnumber  => $item->itemnumber,
135
        frombranch  => $library1->branchcode,
136
        tobranch    => $library2->branchcode,
137
        datearrived => undef,
138
    } );
139
    is( $transfer->count, 1, 'There should be transfer started for item in collection');
140
141
    $schema->storage->txn_rollback;
142
};
(-)a/t/db_dependent/RotatingCollections.t (-337 lines)
Lines 1-336 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
18
use Modern::Perl;
19
20
use Test::More tests => 51;
21
use C4::Context;
22
use C4::RotatingCollections;
23
use C4::Biblio;
24
use Koha::Library;
25
26
BEGIN {
27
}
28
29
can_ok(
30
    'C4::RotatingCollections',
31
    qw(
32
      AddItemToCollection
33
      CreateCollection
34
      DeleteCollection
35
      GetCollection
36
      GetCollectionItemBranches
37
      GetCollections
38
      GetItemsInCollection
39
      RemoveItemFromCollection
40
      TransferCollection
41
      UpdateCollection
42
      isItemInAnyCollection
43
      isItemInThisCollection
44
      )
45
);
46
47
#Start transaction
48
my $dbh = C4::Context->dbh;
49
$dbh->{RaiseError} = 1;
50
$dbh->{AutoCommit} = 0;
51
52
#Start Tests
53
$dbh->do(q|DELETE FROM issues |);
54
$dbh->do(q|DELETE FROM borrowers |);
55
$dbh->do(q|DELETE FROM items |);
56
$dbh->do(q|DELETE FROM collections_tracking |);
57
$dbh->do(q|DELETE FROM collections |);
58
$dbh->do(q|DELETE FROM branches |);
59
$dbh->do(q|DELETE FROM categories|);
60
61
#Test CreateCollection
62
my $collections     = GetCollections();
63
my $countcollection = scalar(@$collections);
64
65
my ($success,$errorCode,$errorMessage);
66
67
($success,$errorCode,$errorMessage) = CreateCollection( 'Collection1', 'Description1' );
68
is( $success, 1, "All parameters have been given - Collection 1 added" );
69
ok( !defined $errorCode && !defined $errorMessage,
70
    "Collection added, no error code or message");
71
my $collection_id1 = $dbh->last_insert_id( undef, undef, 'collections', undef );
72
73
($success,$errorCode,$errorMessage) = CreateCollection( 'Collection2', 'Description2' );
74
is( $success, 1, "All parameters have been given - Collection 2 added" );
75
ok( !defined $errorCode && !defined $errorMessage,
76
    "Collection added, no error code or message");
77
my $collection_id2 = $dbh->last_insert_id( undef, undef, 'collections', undef );
78
79
$collections = GetCollections();
80
is( scalar(@$collections), $countcollection + 2,
81
    "Collection1 and Collection2 have been added" );
82
83
($success,$errorCode,$errorMessage) = CreateCollection('Collection3');
84
is( $success, 1, "Collections can be created without description" );
85
ok( !defined $errorCode && !defined $errorMessage,
86
    "Collection added, no error code or message");
87
my $collection_id3 = $dbh->last_insert_id( undef, undef, 'collections', undef );
88
89
($success,$errorCode,$errorMessage) = CreateCollection();
90
is( $success, 0, "Title missing, fails to create collection" );
91
is( $errorCode, 1, "Title missing, error code is 1" );
92
is( $errorMessage, 'NO_TITLE', "Title missing, error message is NO_TITLE" );
93
94
$collections = GetCollections();
95
is( scalar(@$collections), $countcollection + 3, "Only one collection added" );
96
97
#FIXME, as the id is auto incremented, two similar Collections (same title /same description) can be created
98
#$collection1 = CreateCollection('Collection1','Description1');
99
100
#Test GetCollections
101
my $collection = GetCollections();
102
is_deeply(
103
    $collections,
104
    [
105
        {
106
            colId         => $collection_id1,
107
            colTitle      => 'Collection1',
108
            colDesc       => 'Description1',
109
            colBranchcode => undef
110
        },
111
        {
112
            colId         => $collection_id2,
113
            colTitle      => 'Collection2',
114
            colDesc       => 'Description2',
115
            colBranchcode => undef
116
        },
117
        {
118
            colId         => $collection_id3,
119
            colTitle      => 'Collection3',
120
            colDesc       => '',
121
            colBranchcode => undef
122
        }
123
124
    ],
125
    'All Collections'
126
);
127
128
#Test UpdateCollection
129
($success,$errorCode,$errorMessage) =
130
    UpdateCollection( $collection_id2, 'Collection2bis', undef );
131
is( $success, 1, "UpdateCollection succeeds without description");
132
133
($success,$errorCode,$errorMessage) =
134
    UpdateCollection( $collection_id2, 'Collection2 modified', 'Description2 modified' );
135
is( $success, 1, "Collection2 has been modified" );
136
ok( !defined $errorCode && !defined $errorMessage,
137
    "Collection2 modified, no error code or message");
138
139
($success,$errorCode,$errorMessage) =
140
    UpdateCollection( $collection_id2, undef, 'Description' ),
141
ok( !$success, "UpdateCollection fails without title" );
142
is( $errorCode, 2, "Title missing, error code is 2");
143
is( $errorMessage, 'NO_TITLE', "Title missing, error message is NO_TITLE");
144
145
is( UpdateCollection(), 'NO_ID', "UpdateCollection without params" );
146
147
#Test GetCollection
148
my @collection1 = GetCollection($collection_id1);
149
is_deeply(
150
    \@collection1,
151
    [ $collection_id1, 'Collection1', 'Description1', undef ],
152
    "Collection1's informations"
153
);
154
my @collection2 = GetCollection($collection_id2);
155
is_deeply(
156
    \@collection2,
157
    [ $collection_id2, 'Collection2 modified', 'Description2 modified', undef ],
158
    "Collection2's informations"
159
);
160
my @undef_collection = GetCollection();
161
is_deeply(
162
    \@undef_collection,
163
    [ undef, undef, undef, undef ],
164
    "GetCollection without id given"
165
);
166
@undef_collection = GetCollection(-1);
167
is_deeply(
168
    \@undef_collection,
169
    [ undef, undef, undef, undef ],
170
    "GetCollection with a wrong id"
171
);
172
173
#Test TransferCollection
174
my $samplebranch = {
175
    branchcode     => 'SAB',
176
    branchname     => 'Sample Branch',
177
    branchaddress1 => 'sample adr1',
178
    branchaddress2 => 'sample adr2',
179
    branchaddress3 => 'sample adr3',
180
    branchzip      => 'sample zip',
181
    branchcity     => 'sample city',
182
    branchstate    => 'sample state',
183
    branchcountry  => 'sample country',
184
    branchphone    => 'sample phone',
185
    branchfax      => 'sample fax',
186
    branchemail    => 'sample email',
187
    branchurl      => 'sample url',
188
    branchip       => 'sample ip',
189
    branchprinter  => undef,
190
    branchnotes    => 'sample note',
191
    opac_info      => 'sample opac',
192
};
193
Koha::Library->new($samplebranch)->store;
194
is( TransferCollection( $collection_id1, $samplebranch->{branchcode} ),
195
    1, "Collection1 has been transfered in the branch SAB" );
196
@collection1 = GetCollection($collection_id1);
197
is_deeply(
198
    \@collection1,
199
    [
200
        $collection_id1, 'Collection1',
201
        'Description1',  $samplebranch->{branchcode}
202
    ],
203
    "Collection1 belongs to the sample branch (SAB)"
204
);
205
is( TransferCollection, "NO_ID", "TransferCollection without ID" );
206
is(
207
    TransferCollection($collection_id1),
208
    'NO_BRANCHCODE',
209
    "TransferCollection without branchcode"
210
);
211
212
#Test AddItemToCollection
213
my $record = MARC::Record->new();
214
$record->append_fields(
215
    MARC::Field->new(
216
        '952', '0', '0',
217
        a => $samplebranch->{branchcode},
218
        b => $samplebranch->{branchcode}
219
    )
220
);
221
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
222
my @sampleitem1 = C4::Items::AddItem(
223
    {
224
        barcode        => 1,
225
        itemcallnumber => 'callnumber1',
226
        homebranch     => $samplebranch->{branchcode},
227
        holdingbranch  => $samplebranch->{branchcode}
228
    },
229
    $biblionumber
230
);
231
my $item_id1    = $sampleitem1[2];
232
my @sampleitem2 = C4::Items::AddItem(
233
    {
234
        barcode        => 2,
235
        itemcallnumber => 'callnumber2',
236
        homebranch     => $samplebranch->{branchcode},
237
        holdingbranch  => $samplebranch->{branchcode}
238
    },
239
    $biblionumber
240
);
241
my $item_id2 = $sampleitem2[2];
242
is( AddItemToCollection( $collection_id1, $item_id1 ),
243
    1, "Sampleitem1 has been added to Collection1" );
244
is( AddItemToCollection( $collection_id1, $item_id2 ),
245
    1, "Sampleitem2 has been added to Collection1" );
246
247
#Test GetItemsInCollection
248
my $itemsincollection1;
249
($itemsincollection1,$success,$errorCode,$errorMessage) = GetItemsInCollection($collection_id1);
250
is( scalar @$itemsincollection1, 2, "Collection1 has 2 items" );
251
is_deeply(
252
    $itemsincollection1,
253
    [
254
        {
255
            title          => undef,
256
            itemcallnumber => 'callnumber1',
257
            biblionumber   => $biblionumber,
258
            barcode        => 1
259
        },
260
        {
261
            title          => undef,
262
            itemcallnumber => 'callnumber2',
263
            biblionumber   => $biblionumber,
264
            barcode        => 2
265
        }
266
    ],
267
    "Collection1 has Item1 and Item2"
268
);
269
($itemsincollection1,$success,$errorCode,$errorMessage) = GetItemsInCollection();
270
ok( !$success, "GetItemsInCollection fails without a collection ID" );
271
is( $errorCode, 1, "Title missing, error code is 2");
272
is( $errorMessage, 'NO_ID', "Collection ID missing, error message is NO_ID");
273
274
#Test RemoveItemFromCollection
275
is( RemoveItemFromCollection( $collection_id1, $item_id2 ),
276
    1, "Item2 has been removed from collection 1" );
277
$itemsincollection1 = GetItemsInCollection($collection_id1);
278
is( scalar @$itemsincollection1, 1, "Collection1 has 1 items" );
279
280
#Test isItemInAnyCollection
281
is( C4::RotatingCollections::isItemInAnyCollection($item_id1),
282
    1, "Item1 is in a collection" );
283
is( C4::RotatingCollections::isItemInAnyCollection($item_id2),
284
    0, "Item2 is not in a collection" );
285
is( C4::RotatingCollections::isItemInAnyCollection(),
286
    0, "isItemInAnyCollection returns 0 if no itemid given " );
287
is( C4::RotatingCollections::isItemInAnyCollection(-1),
288
    0, "isItemInAnyCollection returns 0 if a wrong id is given" );
289
290
#Test isItemInThisCollection
291
is(
292
    C4::RotatingCollections::isItemInThisCollection(
293
        $item_id1, $collection_id1
294
    ),
295
    1,
296
    "Item1 is in the Collection1"
297
);
298
is(
299
    C4::RotatingCollections::isItemInThisCollection(
300
        $item_id1, $collection_id2
301
    ),
302
    0,
303
    "Item1 is not in the Collection2"
304
);
305
is(
306
    C4::RotatingCollections::isItemInThisCollection(
307
        $item_id2, $collection_id2
308
    ),
309
    0,
310
    "Item2 is not in the Collection2"
311
);
312
is( C4::RotatingCollections::isItemInThisCollection($collection_id1),
313
    0, "isItemInThisCollection returns 0 is ItemId is missing" );
314
is( C4::RotatingCollections::isItemInThisCollection($item_id1),
315
    0, "isItemInThisCollection returns 0 is Collectionid if missing" );
316
is( C4::RotatingCollections::isItemInThisCollection(),
317
    0, "isItemInThisCollection returns 0 if no params given" );
318
319
#Test DeleteCollection
320
is( DeleteCollection($collection_id2), 1, "Collection2 deleted" );
321
is( DeleteCollection($collection_id1), 1, "Collection1 deleted" );
322
is(
323
    DeleteCollection(),
324
    'NO_ID',
325
    "DeleteCollection without id"
326
);
327
$collections = GetCollections();
328
is(
329
    scalar(@$collections),
330
    $countcollection + 1,
331
    "Two Collections have been deleted"
332
);
333
334
#End transaction
335
$dbh->rollback;
336
337
- 

Return to bug 18606