test suite: removed dep on 'acm' user
[koha-ffzg.git] / t / lib / KohaTest.pm
1 package KohaTest;
2 use base qw(Test::Class);
3
4 use Test::More;
5 use Data::Dumper;
6
7 eval "use Test::Class";
8 plan skip_all => "Test::Class required for performing database tests" if $@;
9 # Or, maybe I should just die there.
10
11 use lib qw(..);
12 use C4::Biblio;
13 use C4::Bookfund;
14 use C4::Bookseller;
15 use C4::Context;
16 use C4::Items;
17 use C4::Members;
18 use C4::Search;
19
20 # Since this is an abstract base class, this prevents these tests from
21 # being run directly unless we're testing a subclass. It just makes
22 # things faster.
23 __PACKAGE__->SKIP_CLASS( 1 );
24
25
26 =head2 startup methods
27
28 these are run once, at the beginning of the whole test suite
29
30 =cut
31
32 =head2 startup_10_prepare_database
33
34 prepare a blank database.
35
36 This ends up getting run once for each test module, so that's several
37 times throughout the test suite. That may be too many times to refresh
38 the database. We may have to tune that.
39
40 =cut
41
42 sub startup_10_prepare_database : Test(startup => 1) {
43     my $self = shift;
44     # this is how I'm refreshing my database for now.  I'll think of
45     # something better later.  Eventually, I'd like to drop the
46     # database entirely and use the regular install code to rebuild a
47     # base database.
48     my $class = ref $self;
49
50     # like( C4::Context->config( 'database '), qr/test$/, 'using test database: ' . C4::Context->config( 'database' ) )
51     like( C4::Context->database(), qr/test$/, 'using test database: ' . C4::Context->database() )
52       or BAIL_OUT( 'This appears to not be a test database.' );
53
54     return;
55 }
56
57 sub startup_15_truncate_tables : Test( startup => 1 ) {
58     my $self = shift;
59     
60 #     my @truncate_tables = qw( accountlines 
61 #                               accountoffsets              
62 #                               action_logs                 
63 #                               alert                       
64 #                               aqbasket                    
65 #                               aqbookfund                  
66 #                               aqbooksellers               
67 #                               aqbudget                    
68 #                               aqorderbreakdown            
69 #                               aqorderdelivery             
70 #                               aqorders                    
71 #                               auth_header                 
72 #                               auth_subfield_structure     
73 #                               auth_tag_structure          
74 #                               auth_types                  
75 #                               authorised_values           
76 #                               biblio                      
77 #                               biblio_framework            
78 #                               biblioitems                 
79 #                               borrowers                   
80 #                               branchcategories            
81 #                               branches                    
82 #                               branchrelations             
83 #                               branchtransfers             
84 #                               browser                     
85 #                               categories                  
86 #                               categorytable               
87 #                               cities                      
88 #                               class_sort_rules            
89 #                               class_sources               
90 #                               currency                    
91 #                               deletedbiblio               
92 #                               deletedbiblioitems          
93 #                               deletedborrowers            
94 #                               deleteditems                
95 #                               ethnicity                   
96 #                               import_batches              
97 #                               import_biblios              
98 #                               import_items                
99 #                               import_record_matches       
100 #                               import_records              
101 #                               issues                      
102 #                               issuingrules                
103 #                               items                       
104 #                               itemtypes                   
105 #                               labels                      
106 #                               labels_conf                 
107 #                               labels_profile              
108 #                               labels_templates            
109 #                               language_descriptions       
110 #                               language_rfc4646_to_iso639  
111 #                               language_script_bidi        
112 #                               language_script_mapping     
113 #                               language_subtag_registry    
114 #                               letter                      
115 #                               marc_matchers               
116 #                               marc_subfield_structure     
117 #                               marc_tag_structure          
118 #                               matchchecks                 
119 #                               matcher_matchpoints         
120 #                               matchpoint_component_norms  
121 #                               matchpoint_components       
122 #                               matchpoints                 
123 #                               mediatypetable              
124 #                               notifys                     
125 #                               nozebra                     
126 #                               old_issues                  
127 #                               old_reserves                
128 #                               opac_news                   
129 #                               overduerules                
130 #                               patroncards                 
131 #                               patronimage                 
132 #                               printers                    
133 #                               printers_profile            
134 #                               repeatable_holidays         
135 #                               reports_dictionary          
136 #                               reserveconstraints          
137 #                               reserves                    
138 #                               reviews                     
139 #                               roadtype                    
140 #                               saved_reports               
141 #                               saved_sql                   
142 #                               serial                      
143 #                               serialitems                 
144 #                               services_throttle           
145 #                               sessions                    
146 #                               special_holidays            
147 #                               statistics                  
148 #                               stopwords                   
149 #                               subcategorytable            
150 #                               subscription                
151 #                               subscriptionhistory         
152 #                               subscriptionroutinglist     
153 #                               suggestions                 
154 #                               systempreferences           
155 #                               tags                        
156 #                               userflags                   
157 #                               virtualshelfcontents        
158 #                               virtualshelves              
159 #                               z3950servers                
160 #                               zebraqueue                  
161 #                         );
162
163     my @truncate_tables = qw( accountlines 
164                               accountoffsets              
165                               alert                       
166                               aqbasket                    
167                               aqbooksellers               
168                               aqorderbreakdown            
169                               aqorderdelivery             
170                               aqorders                    
171                               auth_header                 
172                               branchcategories            
173                               branchrelations             
174                               branchtransfers             
175                               browser                     
176                               categorytable               
177                               cities                      
178                               deletedbiblio               
179                               deletedbiblioitems          
180                               deletedborrowers            
181                               deleteditems                
182                               ethnicity                   
183                               import_items                
184                               import_record_matches       
185                               issues                      
186                               issuingrules                
187                               items                       
188                               labels                      
189                               labels_profile              
190                               matchchecks                 
191                               mediatypetable              
192                               notifys                     
193                               nozebra                     
194                               old_issues                  
195                               old_reserves                
196                               overduerules                
197                               patroncards                 
198                               patronimage                 
199                               printers                    
200                               printers_profile            
201                               reports_dictionary          
202                               reserveconstraints          
203                               reserves                    
204                               reviews                     
205                               roadtype                    
206                               saved_reports               
207                               saved_sql                   
208                               serial                      
209                               serialitems                 
210                               services_throttle           
211                               special_holidays            
212                               statistics                  
213                               subcategorytable            
214                               subscription                
215                               subscriptionhistory         
216                               subscriptionroutinglist     
217                               suggestions                 
218                               tags                        
219                               virtualshelfcontents        
220                         );
221     
222     my $failed_to_truncate = 0;
223     foreach my $table ( @truncate_tables ) {
224         my $dbh = C4::Context->dbh();
225         $dbh->do( "truncate $table" )
226           or $failed_to_truncate = 1;
227     }
228     is( $failed_to_truncate, 0, 'truncated tables' );
229     
230 }
231
232 =head2 startup_20_add_bookseller
233
234 we need a bookseller for many of the tests, so let's insert one. Feel
235 free to use this one, or insert your own.
236
237 =cut
238
239 sub startup_20_add_bookseller : Test(startup => 1) {
240     my $self = shift;
241
242     my $booksellerinfo = { name => 'bookseller ' . $self->random_string(),
243                       };
244
245     my $id = AddBookseller( $booksellerinfo );
246     ok( $id, "created bookseller: $id" );
247     $self->{'booksellerid'} = $id;
248     
249     return;
250 }
251
252 =head2 startup_22_add_bookfund
253
254 we need a bookfund for many of the tests. This currently uses one that
255 is in the skeleton database.  free to use this one, or insert your
256 own.
257
258 =cut
259
260 sub startup_22_add_bookfund : Test(startup => 2) {
261     my $self = shift;
262
263     my $bookfundid = 'GEN';
264     my $bookfund = GetBookFund( $bookfundid, undef );
265     # diag( Data::Dumper->Dump( [ $bookfund ], qw( bookfund  ) ) );
266     is( $bookfund->{'bookfundid'},   $bookfundid,      "found bookfund: '$bookfundid'" );
267     is( $bookfund->{'bookfundname'}, 'General Stacks', "found bookfund: '$bookfundid'" );
268     
269     $self->{'bookfundid'} = $bookfundid;
270     return;
271 }
272
273 =head2 startup_24_add_member
274
275 Add a patron/member for the tests to use
276
277 =cut
278
279 sub startup_24_add_member : Test(startup => 1) {
280     my $self = shift;
281
282     my $memberinfo = { surname      => 'surname '  . $self->random_string(),
283                        firstname    => 'firstname' . $self->random_string(),
284                        address      => 'address'   . $self->random_string(),
285                        city         => 'city'      . $self->random_string(),
286                        branchcode   => 'CPL', # CPL => Centerville
287                        categorycode => 'PT',  # PT  => PaTron
288                   };
289
290     my $id = AddMember( %$memberinfo );
291     ok( $id, "created member: $id" );
292     $self->{'memberid'} = $id;
293     
294     return;
295 }
296
297 =head2 setup methods
298
299 setup methods are run before every test method
300
301 =cut
302
303 =head2 teardown methods
304
305 teardown methods are many time, once at the end of each test method.
306
307 =cut
308
309 =head2 shutdown methods
310
311 shutdown methods are run once, at the end of the test suite
312
313 =cut
314
315 =head2 utility methods
316
317 These are not test methods, but they're handy
318
319 =cut
320
321 =head3 random_string
322
323 Nice for generating names and such. It's not actually random, more
324 like arbitrary.
325
326 =cut
327
328 sub random_string {
329     my $self = shift;
330
331     my $wordsize = 6;  # how many letters in your string?
332
333     # leave out these characters: "oOlL10". They're too confusing.
334     my @alphabet = ( 'a'..'k','m','n','p'..'z', 'A'..'K','M','N','P'..'Z', 2..9 );
335
336     my $randomstring;
337     foreach ( 0..$wordsize ) {
338         $randomstring .= $alphabet[ rand( scalar( @alphabet ) ) ];
339     }
340     return $randomstring;
341     
342 }
343
344 =head3 add_biblios
345
346   $self->add_biblios( count     => 10,
347                       add_items => 1, );
348
349   named parameters:
350      count: number of biblios to add
351      add_items: should you add items for each one?
352
353   returns:
354     I don't know yet.
355
356   side effects:
357     adds the biblionumbers to the $self->{'biblios'} listref
358
359   Notes:
360     Should I allow you to pass in biblio information, like title?
361     Since this method is in the KohaTest class, all tests in it will be ignored, unless you call this from your own namespace.
362     This runs 10 tests, plus 4 for each "count", plus 3 more for each item added.
363
364 =cut
365
366 sub add_biblios {
367     my $self = shift;
368     my %param = @_;
369
370     $param{'count'}     = 1 unless defined( $param{'count'} );
371     $param{'add_items'} = 0 unless defined( $param{'add_items'} );
372
373     foreach my $counter ( 1..$param{'count'} ) {
374         my $marcrecord  = MARC::Record->new();
375         isa_ok( $marcrecord, 'MARC::Record' );
376         my $appendedfieldscount = $marcrecord->append_fields( MARC::Field->new( '100', '1', '0',
377                                                                                 a => 'Twain, Mark',
378                                                                                 d => "1835-1910." ),
379                                                               MARC::Field->new( '245', '1', '4',
380                                                                                 a => sprintf( 'The Adventures of Huckleberry Finn Test %s', $counter ),
381                                                                                 c => "Mark Twain ; illustrated by E.W. Kemble." )
382                                                          );
383         is( $appendedfieldscount, 2, 'added 2 fields' );
384         
385         my $frameworkcode = ''; # XXX I'd like to put something reasonable here.
386         my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marcrecord, $frameworkcode );
387         ok( $biblionumber, "the biblionumber is $biblionumber" );
388         ok( $biblioitemnumber, "the biblioitemnumber is $biblioitemnumber" );
389         if ( $param{'add_items'} ) {
390             # my @iteminfo = AddItem( {}, $biblionumber );
391             my @iteminfo = AddItemFromMarc( $marcrecord, $biblionumber );
392             is( $iteminfo[0], $biblionumber,     "biblionumber is $biblionumber" );
393             is( $iteminfo[1], $biblioitemnumber, "biblioitemnumber is $biblioitemnumber" );
394             ok( $iteminfo[2], "itemnumber is $iteminfo[2]" );
395         }
396         push @{$self->{'biblios'}}, $biblionumber;
397     }
398     
399     my $query = 'Finn Test';
400
401     # XXX we're going to repeatedly try to fetch the marc records that
402     # we inserted above. It may take a while before they all show
403     # up. why?
404     my $tries = 30;
405     DELAY: foreach my $trial ( 1..$tries ) {
406         diag "waiting for zebra indexing. Trial: $trial of $tries";
407         my ( $error, $results ) = SimpleSearch( $query );
408         if ( $param{'count'} <= scalar( @$results ) ) {
409             ok( $tries, "found all $param{'count'} titles after $trial tries" );
410             last DELAY;
411         }
412         sleep( 3 );
413     } continue {
414         if ( $trial == $tries ) {
415             fail( "we never found all $param{'count'} titles even after $tries tries." );
416         }
417     }
418
419     
420 }
421
422 1;