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