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