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

(-)a/t/db_dependent/RotatingCollections.t (-1 / +306 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 41;
6
use C4::Context;
7
use DBI;
8
use C4::Branch;
9
use C4::Biblio;
10
11
BEGIN {
12
    use_ok('C4::RotatingCollections');
13
}
14
15
can_ok(
16
    'C4::RotatingCollections',
17
    qw(
18
      AddItemToCollection
19
      CreateCollection
20
      DeleteCollection
21
      GetCollection
22
      GetCollectionItemBranches
23
      GetCollections
24
      GetItemsInCollection
25
      RemoveItemFromCollection
26
      TransferCollection
27
      UpdateCollection
28
      isItemInAnyCollection
29
      isItemInThisCollection
30
      )
31
);
32
33
#Start transaction
34
my $dbh = C4::Context->dbh;
35
$dbh->{RaiseError} = 1;
36
$dbh->{AutoCommit} = 0;
37
38
#Start Tests
39
$dbh->do(q|DELETE FROM collections_tracking |);
40
$dbh->do(q|DELETE FROM collections |);
41
$dbh->do(q|DELETE FROM borrowers |);
42
$dbh->do(q|DELETE FROM items |);
43
$dbh->do(q|DELETE FROM branches |);
44
45
#Test CreateCollection
46
my $collections     = GetCollections;
47
my $countcollection = scalar(@$collections);
48
49
is( CreateCollection( 'Collection1', 'Description1' ),
50
    1, "All parameters have been given - Collection 1 added" );
51
my $collection_id1 = $dbh->last_insert_id( undef, undef, 'collections', undef );
52
is( CreateCollection( 'Collection2', 'Description2' ),
53
    1, "All parameters have been given - Collection 2 added" );
54
my $collection_id2 = $dbh->last_insert_id( undef, undef, 'collections', undef );
55
$collections = GetCollections;
56
is(
57
    scalar(@$collections),
58
    $countcollection + 2,
59
    "Collection1 and Collection2 have been added"
60
);
61
my $collection = CreateCollection('Collection');
62
is( $collection, 'No Description Given', "The field description is missing" );
63
$collection = CreateCollection();
64
is(
65
    $collection,
66
    'No Title Given',
67
    "The field description and title is missing"
68
);
69
$collections = GetCollections;
70
is( scalar(@$collections), $countcollection + 2, "No collection added" );
71
72
#FIXME, as the id is auto incremented, two similar Collections (same title /same description) can be created
73
#$collection1 = CreateCollection('Collection1','Description1');
74
75
#Test GetCollections
76
$collection = GetCollections;
77
is_deeply(
78
    $collections,
79
    [
80
        {
81
            colId         => $collection_id1,
82
            colTitle      => 'Collection1',
83
            colDesc       => 'Description1',
84
            colBranchcode => undef
85
        },
86
        {
87
            colId         => $collection_id2,
88
            colTitle      => 'Collection2',
89
            colDesc       => 'Description2',
90
            colBranchcode => undef
91
        }
92
    ],
93
    'All Collections'
94
);
95
96
#Test UpdateCollection
97
is(
98
    UpdateCollection(
99
        $collection_id2,
100
        'Collection2 modified',
101
        'Description2 modified'
102
    ),
103
    1,
104
    "Collection2 has been modified"
105
);
106
107
#FIXME : The following test should pass, currently, with a wrong id UpdateCollection returns 1 even if nothing has been modified
108
#is(UpdateCollection(-1,'Collection2 modified','Description2 modified'),
109
#   0,
110
#   "UpdateCollection with a wrong id");
111
is(
112
    UpdateCollection( 'Collection', 'Description' ),
113
    'No Description Given',
114
    "UpdateCollection without description"
115
);
116
is(
117
    UpdateCollection( 'Description' ),
118
    'No Title Given',
119
    "UpdateCollection without title"
120
);
121
is( UpdateCollection(), 'No Id Given', "UpdateCollection without params" );
122
123
#Test GetCollection
124
my @collection1 = GetCollection($collection_id1);
125
is_deeply(
126
    \@collection1,
127
    [ $collection_id1, 'Collection1', 'Description1', undef ],
128
    "Collection1's informations"
129
);
130
my @collection2 = GetCollection($collection_id2);
131
is_deeply(
132
    \@collection2,
133
    [ $collection_id2, 'Collection2 modified', 'Description2 modified', undef ],
134
    "Collection2's informations"
135
);
136
my @undef_collection = GetCollection();
137
is_deeply(
138
    \@undef_collection,
139
    [ undef, undef, undef, undef ],
140
    "GetCollection without id given"
141
);
142
@undef_collection = GetCollection(-1);
143
is_deeply(
144
    \@undef_collection,
145
    [ undef, undef, undef, undef ],
146
    "GetCollection with a wrong id"
147
);
148
149
#Test TransferCollection
150
my $samplebranch = {
151
    add            => 1,
152
    branchcode     => 'SAB',
153
    branchname     => 'Sample Branch',
154
    branchaddress1 => 'sample adr1',
155
    branchaddress2 => 'sample adr2',
156
    branchaddress3 => 'sample adr3',
157
    branchzip      => 'sample zip',
158
    branchcity     => 'sample city',
159
    branchstate    => 'sample state',
160
    branchcountry  => 'sample country',
161
    branchphone    => 'sample phone',
162
    branchfax      => 'sample fax',
163
    branchemail    => 'sample email',
164
    branchurl      => 'sample url',
165
    branchip       => 'sample ip',
166
    branchprinter  => undef,
167
    branchnotes    => 'sample note',
168
    opac_info      => 'sample opac',
169
};
170
ModBranch($samplebranch);
171
is( TransferCollection( $collection_id1, $samplebranch->{branchcode} ),
172
    1, "Collection1 has been transfered in the branch SAB" );
173
@collection1 = GetCollection($collection_id1);
174
is_deeply(
175
    \@collection1,
176
    [
177
        $collection_id1, 'Collection1',
178
        'Description1',  $samplebranch->{branchcode}
179
    ],
180
    "Collection1 belongs to the sample branch (SAB)"
181
);
182
is( TransferCollection, "No Id Given", "TransferCollection without ID" );
183
is(
184
    TransferCollection($collection_id1),
185
    "No Branchcode Given",
186
    "TransferCollection without branchcode"
187
);
188
189
#Test AddItemToCollection
190
my $record = MARC::Record->new();
191
$record->append_fields(
192
    MARC::Field->new(
193
        '952', '0', '0',
194
        a => $samplebranch->{branchcode},
195
        b => $samplebranch->{branchcode}
196
    )
197
);
198
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
199
my @sampleitem1 = C4::Items::AddItem(
200
    {
201
        barcode        => 1,
202
        itemcallnumber => 'callnumber1',
203
        homebranch     => $samplebranch->{branchcode},
204
        holdingbranch  => $samplebranch->{branchcode}
205
    },
206
    $biblionumber
207
);
208
my $item_id1    = $sampleitem1[2];
209
my @sampleitem2 = C4::Items::AddItem(
210
    {
211
        barcode        => 2,
212
        itemcallnumber => 'callnumber2',
213
        homebranch     => $samplebranch->{branchcode},
214
        holdingbranch  => $samplebranch->{branchcode}
215
    },
216
    $biblionumber
217
);
218
my $item_id2 = $sampleitem2[2];
219
is( AddItemToCollection( $collection_id1, $item_id1 ),
220
    1, "Sampleitem1 has been added to Collection1" );
221
is( AddItemToCollection( $collection_id1, $item_id2 ),
222
    1, "Sampleitem2 has been added to Collection1" );
223
224
#Test GetItemsInCollection
225
my $itemsincollection1 = GetItemsInCollection($collection_id1);
226
is( scalar @$itemsincollection1, 2, "Collection1 has 2 items" );
227
is_deeply(
228
    $itemsincollection1,
229
    [
230
        {
231
            title          => undef,
232
            itemcallnumber => 'callnumber1',
233
            barcode        => 1
234
        },
235
        {
236
            title          => undef,
237
            itemcallnumber => 'callnumber2',
238
            barcode        => 2
239
        }
240
    ],
241
    "Collection1 has Item1 and Item2"
242
);
243
244
#Test RemoveItemFromCollection
245
is( RemoveItemFromCollection( $collection_id1, $item_id2 ),
246
    1, "Item2 has been removed from collection 1" );
247
$itemsincollection1 = GetItemsInCollection($collection_id1);
248
is( scalar @$itemsincollection1, 1, "Collection1 has 1 items" );
249
250
#Test isItemInAnyCollection
251
is( C4::RotatingCollections::isItemInAnyCollection($item_id1),
252
    1, "Item1 is in a collection" );
253
is( C4::RotatingCollections::isItemInAnyCollection($item_id2),
254
    0, "Item2 is not in a collection" );
255
is( C4::RotatingCollections::isItemInAnyCollection(),
256
    0, "isItemInAnyCollection returns 0 if no itemid given " );
257
is( C4::RotatingCollections::isItemInAnyCollection(-1),
258
    0, "isItemInAnyCollection returns 0 if a wrong id is given" );
259
260
#Test isItemInThisCollection
261
is(
262
    C4::RotatingCollections::isItemInThisCollection(
263
        $item_id1, $collection_id1
264
    ),
265
    1,
266
    "Item1 is in the Collection1"
267
);
268
is(
269
    C4::RotatingCollections::isItemInThisCollection(
270
        $item_id1, $collection_id2
271
    ),
272
    0,
273
    "Item1 is not in the Collection2"
274
);
275
is(
276
    C4::RotatingCollections::isItemInThisCollection(
277
        $item_id2, $collection_id2
278
    ),
279
    0,
280
    "Item2 is not in the Collection2"
281
);
282
is( C4::RotatingCollections::isItemInThisCollection($collection_id1),
283
    0, "isItemInThisCollection returns 0 is ItemId is missing" );
284
is( C4::RotatingCollections::isItemInThisCollection($item_id1),
285
    0, "isItemInThisCollection returns 0 is Collectionid if missing" );
286
is( C4::RotatingCollections::isItemInThisCollection(),
287
    0, "isItemInThisCollection returns 0 if no params given" );
288
289
#Test DeleteCollection
290
is( DeleteCollection($collection_id2), 1, "Collection2 deleted" );
291
is( DeleteCollection($collection_id1), 1, "Collection1 deleted" );
292
is(
293
    DeleteCollection(),
294
    'No Collection Id Given',
295
    "DeleteCollection without id"
296
);
297
$collections = GetCollections;
298
is(
299
    scalar(@$collections),
300
    $countcollection + 0,
301
    "Two Collections have been deleted"
302
);
303
304
#End transaction
305
$dbh->rollback;
306

Return to bug 10653