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

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

Return to bug 18606