Bug 27993: Add unit tests
[koha.git] / t / db_dependent / RotatingCollections.t
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::Database;
25 use Koha::Library;
26
27 use t::lib::TestBuilder;
28
29 BEGIN {
30 }
31
32 can_ok(
33     'C4::RotatingCollections',
34     qw(
35       AddItemToCollection
36       CreateCollection
37       DeleteCollection
38       GetCollection
39       GetCollectionItemBranches
40       GetCollections
41       GetItemsInCollection
42       RemoveItemFromCollection
43       TransferCollection
44       UpdateCollection
45       isItemInAnyCollection
46       isItemInThisCollection
47       )
48 );
49
50 my $schema = Koha::Database->new->schema;
51 $schema->storage->txn_begin;
52 my $dbh = C4::Context->dbh;
53
54 #Start Tests
55 $dbh->do(q|DELETE FROM issues |);
56 $dbh->do(q|DELETE FROM borrowers |);
57 $dbh->do(q|DELETE FROM items |);
58 $dbh->do(q|DELETE FROM collections_tracking |);
59 $dbh->do(q|DELETE FROM collections |);
60 $dbh->do(q|DELETE FROM branches |);
61 $dbh->do(q|DELETE FROM categories|);
62
63 my $builder = t::lib::TestBuilder->new;
64
65 #Test CreateCollection
66 my $collections     = GetCollections();
67 my $countcollection = scalar(@$collections);
68
69 my ($success,$errorCode,$errorMessage);
70
71 ($success,$errorCode,$errorMessage) = CreateCollection( 'Collection1', 'Description1' );
72 is( $success, 1, "All parameters have been given - Collection 1 added" );
73 ok( !defined $errorCode && !defined $errorMessage,
74     "Collection added, no error code or message");
75 my $collection_id1 = $dbh->last_insert_id( undef, undef, 'collections', undef );
76
77 ($success,$errorCode,$errorMessage) = CreateCollection( 'Collection2', 'Description2' );
78 is( $success, 1, "All parameters have been given - Collection 2 added" );
79 ok( !defined $errorCode && !defined $errorMessage,
80     "Collection added, no error code or message");
81 my $collection_id2 = $dbh->last_insert_id( undef, undef, 'collections', undef );
82
83 $collections = GetCollections();
84 is( scalar(@$collections), $countcollection + 2,
85     "Collection1 and Collection2 have been added" );
86
87 ($success,$errorCode,$errorMessage) = CreateCollection('Collection3');
88 is( $success, 1, "Collections can be created without description" );
89 ok( !defined $errorCode && !defined $errorMessage,
90     "Collection added, no error code or message");
91 my $collection_id3 = $dbh->last_insert_id( undef, undef, 'collections', undef );
92
93 ($success,$errorCode,$errorMessage) = CreateCollection();
94 is( $success, 0, "Title missing, fails to create collection" );
95 is( $errorCode, 1, "Title missing, error code is 1" );
96 is( $errorMessage, 'NO_TITLE', "Title missing, error message is NO_TITLE" );
97
98 $collections = GetCollections();
99 is( scalar(@$collections), $countcollection + 3, "Only one collection added" );
100
101 #FIXME, as the id is auto incremented, two similar Collections (same title /same description) can be created
102 #$collection1 = CreateCollection('Collection1','Description1');
103
104 #Test GetCollections
105 my $collection = GetCollections();
106 is_deeply(
107     $collections,
108     [
109         {
110             colId         => $collection_id1,
111             colTitle      => 'Collection1',
112             colDesc       => 'Description1',
113             colBranchcode => undef
114         },
115         {
116             colId         => $collection_id2,
117             colTitle      => 'Collection2',
118             colDesc       => 'Description2',
119             colBranchcode => undef
120         },
121         {
122             colId         => $collection_id3,
123             colTitle      => 'Collection3',
124             colDesc       => '',
125             colBranchcode => undef
126         }
127
128     ],
129     'All Collections'
130 );
131
132 #Test UpdateCollection
133 ($success,$errorCode,$errorMessage) =
134     UpdateCollection( $collection_id2, 'Collection2bis', undef );
135 is( $success, 1, "UpdateCollection succeeds without description");
136
137 ($success,$errorCode,$errorMessage) =
138     UpdateCollection( $collection_id2, 'Collection2 modified', 'Description2 modified' );
139 is( $success, 1, "Collection2 has been modified" );
140 ok( !defined $errorCode && !defined $errorMessage,
141     "Collection2 modified, no error code or message");
142
143 ($success,$errorCode,$errorMessage) =
144     UpdateCollection( $collection_id2, undef, 'Description' ),
145 ok( !$success, "UpdateCollection fails without title" );
146 is( $errorCode, 2, "Title missing, error code is 2");
147 is( $errorMessage, 'NO_TITLE', "Title missing, error message is NO_TITLE");
148
149 is( UpdateCollection(), 'NO_ID', "UpdateCollection without params" );
150
151 #Test GetCollection
152 my @collection1 = GetCollection($collection_id1);
153 is_deeply(
154     \@collection1,
155     [ $collection_id1, 'Collection1', 'Description1', undef ],
156     "Collection1's informations"
157 );
158 my @collection2 = GetCollection($collection_id2);
159 is_deeply(
160     \@collection2,
161     [ $collection_id2, 'Collection2 modified', 'Description2 modified', undef ],
162     "Collection2's informations"
163 );
164 my @undef_collection = GetCollection();
165 is_deeply(
166     \@undef_collection,
167     [ undef, undef, undef, undef ],
168     "GetCollection without id given"
169 );
170 @undef_collection = GetCollection(-1);
171 is_deeply(
172     \@undef_collection,
173     [ undef, undef, undef, undef ],
174     "GetCollection with a wrong id"
175 );
176
177 #Test TransferCollection
178 my $samplebranch = {
179     branchcode     => 'SAB',
180     branchname     => 'Sample Branch',
181     branchaddress1 => 'sample adr1',
182     branchaddress2 => 'sample adr2',
183     branchaddress3 => 'sample adr3',
184     branchzip      => 'sample zip',
185     branchcity     => 'sample city',
186     branchstate    => 'sample state',
187     branchcountry  => 'sample country',
188     branchphone    => 'sample phone',
189     branchfax      => 'sample fax',
190     branchemail    => 'sample email',
191     branchurl      => 'sample url',
192     branchip       => 'sample ip',
193     branchnotes    => 'sample note',
194     opac_info      => 'sample opac',
195 };
196 Koha::Library->new($samplebranch)->store;
197 my ( $transferred, $messages ) = TransferCollection( $collection_id1, $samplebranch->{branchcode} );
198 is( $transferred,
199     1, "Collection1 has been transfered in the branch SAB" );
200 @collection1 = GetCollection($collection_id1);
201 is_deeply(
202     \@collection1,
203     [
204         $collection_id1, 'Collection1',
205         'Description1',  $samplebranch->{branchcode}
206     ],
207     "Collection1 belongs to the sample branch (SAB)"
208 );
209 ( $transferred, $messages ) = TransferCollection();
210 is( $messages->[0]->{code}, "NO_ID", "TransferCollection without ID" );
211 ( $transferred, $messages ) = TransferCollection($collection_id1);
212 is( $messages->[0]->{code},
213     'NO_BRANCHCODE', "TransferCollection without branchcode" );
214
215 #Test AddItemToCollection
216 my $record = MARC::Record->new();
217 $record->append_fields(
218     MARC::Field->new(
219         '952', '0', '0',
220         a => $samplebranch->{branchcode},
221         b => $samplebranch->{branchcode}
222     )
223 );
224 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
225 my $item_id1 = $builder->build_sample_item(
226     {
227         biblionumber => $biblionumber,
228         library      => $samplebranch->{branchcode},
229         barcode        => 1,              # FIXME This must not be hardcoded!
230         itemcallnumber => 'callnumber1',
231     }
232 )->itemnumber;
233 my $item_id2 = $builder->build_sample_item(
234     {
235         biblionumber => $biblionumber,
236         library      => $samplebranch->{branchcode},
237         barcode        => 2,              # FIXME This must not be hardcoded!
238         itemcallnumber => 'callnumber2',
239     }
240 )->itemnumber;
241
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 );