2 use base qw(Test::Class);
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.
19 use File::Temp qw/ tempdir /;
21 # Since this is an abstract base class, this prevents these tests from
22 # being run directly unless we're testing a subclass. It just makes
24 __PACKAGE__->SKIP_CLASS( 1 );
27 =head2 startup methods
29 these are run once, at the beginning of the whole test suite
33 sub startup_15_truncate_tables : Test( startup => 1 ) {
36 # my @truncate_tables = qw( accountlines
48 # auth_subfield_structure
74 # import_record_matches
84 # language_descriptions
85 # language_rfc4646_to_iso639
86 # language_script_bidi
87 # language_script_mapping
88 # language_subtag_registry
91 # marc_subfield_structure
95 # matchpoint_component_norms
96 # matchpoint_components
108 # repeatable_holidays
124 # subscriptionhistory
125 # subscriptionroutinglist
130 # virtualshelfcontents
136 my @truncate_tables = qw( accountlines
183 subscriptionroutinglist
189 my $failed_to_truncate = 0;
190 foreach my $table ( @truncate_tables ) {
191 my $dbh = C4::Context->dbh();
192 $dbh->do( "truncate $table" )
193 or $failed_to_truncate = 1;
195 is( $failed_to_truncate, 0, 'truncated tables' );
198 =head2 startup_20_add_bookseller
200 we need a bookseller for many of the tests, so let's insert one. Feel
201 free to use this one, or insert your own.
205 sub startup_20_add_bookseller : Test(startup => 1) {
208 my $booksellerinfo = { name => 'bookseller ' . $self->random_string(),
211 my $id = AddBookseller( $booksellerinfo );
212 ok( $id, "created bookseller: $id" );
213 $self->{'booksellerid'} = $id;
218 =head2 startup_22_add_bookfund
220 we need a bookfund for many of the tests. This currently uses one that
221 is in the skeleton database. free to use this one, or insert your
226 sub startup_22_add_bookfund : Test(startup => 2) {
229 my $bookfundid = 'GEN';
230 my $bookfund = GetBookFund( $bookfundid, undef );
231 # diag( Data::Dumper->Dump( [ $bookfund ], qw( bookfund ) ) );
232 is( $bookfund->{'bookfundid'}, $bookfundid, "found bookfund: '$bookfundid'" );
233 is( $bookfund->{'bookfundname'}, 'General Stacks', "found bookfund: '$bookfundid'" );
235 $self->{'bookfundid'} = $bookfundid;
239 =head2 startup_24_add_member
241 Add a patron/member for the tests to use
245 sub startup_24_add_member : Test(startup => 1) {
248 my $memberinfo = { surname => 'surname ' . $self->random_string(),
249 firstname => 'firstname' . $self->random_string(),
250 address => 'address' . $self->random_string(),
251 city => 'city' . $self->random_string(),
252 cardnumber => 'card' . $self->random_string(),
253 branchcode => 'CPL', # CPL => Centerville
254 categorycode => 'PT', # PT => PaTron
255 dateexpiry => '2010-01-01',
256 password => 'testpassword',
259 my $borrowernumber = AddMember( %$memberinfo );
260 ok( $borrowernumber, "created member: $borrowernumber" );
261 $self->{'memberid'} = $borrowernumber;
266 =head2 startup_30_login
270 sub startup_30_login : Test( startup => 2 ) {
273 $self->{'sessionid'} = '12345678'; # does this value matter?
274 my $borrower_details = C4::Members::GetMemberDetails( $self->{'memberid'} );
275 ok( $borrower_details->{'cardnumber'}, 'cardnumber' );
277 # make a cookie and force it into $cgi.
278 # This would be a lot easier with Test::MockObject::Extends.
279 my $cgi = CGI->new( { userid => $borrower_details->{'cardnumber'},
280 password => 'testpassword' } );
281 my $setcookie = $cgi->cookie( -name => 'CGISESSID',
282 -value => $self->{'sessionid'} );
283 $cgi->{'.cookies'} = { CGISESSID => $setcookie };
284 is( $cgi->cookie('CGISESSID'), $self->{'sessionid'}, 'the CGISESSID cookie is set' );
285 # diag( Data::Dumper->Dump( [ $cgi->cookie('CGISESSID') ], [ qw( cookie ) ] ) );
287 # C4::Auth::checkauth sometimes emits a warning about unable to append to sessionlog. That's OK.
288 my ( $userid, $cookie, $sessionID ) = C4::Auth::checkauth( $cgi, 'noauth', {}, 'intranet' );
289 # diag( Data::Dumper->Dump( [ $userid, $cookie, $sessionID ], [ qw( userid cookie sessionID ) ] ) );
291 # my $session = C4::Auth::get_session( $sessionID );
292 # diag( Data::Dumper->Dump( [ $session ], [ qw( session ) ] ) );
299 setup methods are run before every test method
303 =head2 teardown methods
305 teardown methods are many time, once at the end of each test method.
309 =head2 shutdown methods
311 shutdown methods are run once, at the end of the test suite
315 =head2 utility methods
317 These are not test methods, but they're handy
323 Nice for generating names and such. It's not actually random, more
331 my $wordsize = 6; # how many letters in your string?
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 );
337 foreach ( 0..$wordsize ) {
338 $randomstring .= $alphabet[ rand( scalar( @alphabet ) ) ];
340 return $randomstring;
346 $self->add_biblios( count => 10,
350 count: number of biblios to add
351 add_items: should you add items for each one?
357 adds the biblionumbers to the $self->{'biblios'} listref
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.
370 $param{'count'} = 1 unless defined( $param{'count'} );
371 $param{'add_items'} = 0 unless defined( $param{'add_items'} );
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',
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 MARC::Field->new( '952', '0', '0',
383 p => '12345678' . $self->random_string() ), # barcode
384 MARC::Field->new( '952', '0', '0',
389 diag $MARC::Record::ERROR if ( $MARC::Record::ERROR );
390 is( $appendedfieldscount, 4, 'added 4 fields' );
392 my $frameworkcode = ''; # XXX I'd like to put something reasonable here.
393 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marcrecord, $frameworkcode );
394 ok( $biblionumber, "the biblionumber is $biblionumber" );
395 ok( $biblioitemnumber, "the biblioitemnumber is $biblioitemnumber" );
396 if ( $param{'add_items'} ) {
397 # my @iteminfo = AddItem( {}, $biblionumber );
398 my @iteminfo = AddItemFromMarc( $marcrecord, $biblionumber );
399 is( $iteminfo[0], $biblionumber, "biblionumber is $biblionumber" );
400 is( $iteminfo[1], $biblioitemnumber, "biblioitemnumber is $biblioitemnumber" );
401 ok( $iteminfo[2], "itemnumber is $iteminfo[2]" );
403 push @{$self->{'biblios'}}, $biblionumber;
406 my $query = 'Finn Test';
408 # XXX we're going to repeatedly try to fetch the marc records that
409 # we inserted above. It may take a while before they all show
412 DELAY: foreach my $trial ( 1..$tries ) {
413 diag "waiting for zebra indexing. Trial: $trial of $tries";
414 my ( $error, $results ) = SimpleSearch( $query );
415 if ( $param{'count'} <= scalar( @$results ) ) {
416 ok( $tries, "found all $param{'count'} titles after $trial tries" );
421 if ( $trial == $tries ) {
422 fail( "we never found all $param{'count'} titles even after $tries tries." );
431 Do a fast reindexing of all of the bib and authority
432 records and mark all zebraqueue entries done.
434 Useful for test routines that need to do a
435 lot of indexing without having to wait for
438 In NoZebra model, this only marks zebraqueue
439 done - the records should already be indexed.
446 # mark zebraqueue done regardless of the indexing mode
447 my $dbh = C4::Context->dbh();
448 $dbh->do("UPDATE zebraqueue SET done = 1 WHERE done = 0");
450 return if C4::Context->preference('NoZebra');
452 my $directory = tempdir(CLEANUP => 1);
453 foreach my $record_type qw(biblio authority) {
454 mkdir "$directory/$record_type";
455 my $sth = $dbh->prepare($record_type eq "biblio" ? "SELECT marc FROM biblioitems" : "SELECT marc FROM auth_header");
457 open OUT, ">:utf8", "$directory/$record_type/records";
458 while (my ($blob) = $sth->fetchrow_array) {
462 my $zebra_server = "${record_type}server";
463 my $zebra_config = C4::Context->zebraconfig($zebra_server)->{'config'};
464 my $zebra_db_dir = C4::Context->zebraconfig($zebra_server)->{'directory'};
465 my $zebra_db = $record_type eq 'biblio' ? 'biblios' : 'authorities';
466 system "zebraidx -c $zebra_config -d $zebra_db -g iso2709 init > /dev/null 2>\&1";
467 system "zebraidx -c $zebra_config -d $zebra_db -g iso2709 update $directory/${record_type} > /dev/null 2>\&1";
468 system "zebraidx -c $zebra_config -d $zebra_db -g iso2709 commit > /dev/null 2>\&1";