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