From 26531a6a439b87cdef91e94d37f30f78fc48c008 Mon Sep 17 00:00:00 2001 From: Andrew Moore Date: Wed, 7 May 2008 13:50:37 -0500 Subject: [PATCH] bug 2087: test cases for misc/cronjobs/longoverdue.pl I'm adding some functional tests that demonstrate that long_overdue.pl does what we think it does. These tests don't actually work since override_context_prefs.pm gets in the way of C4::Context::preference. I opened up bug 2088 to address that. There are no functional or documentation changes due to this patch. Signed-off-by: Joshua Ferraro --- t/lib/KohaTest.pm | 75 ++++++++++++++++++-- t/lib/KohaTest/Scripts.pm | 18 +++++ t/lib/KohaTest/Scripts/longoverdue.pm | 99 +++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 5 deletions(-) create mode 100644 t/lib/KohaTest/Scripts.pm create mode 100644 t/lib/KohaTest/Scripts/longoverdue.pm diff --git a/t/lib/KohaTest.pm b/t/lib/KohaTest.pm index d706741b34..7ad2d5c6a2 100644 --- a/t/lib/KohaTest.pm +++ b/t/lib/KohaTest.pm @@ -8,6 +8,7 @@ eval "use Test::Class"; plan skip_all => "Test::Class required for performing database tests" if $@; # Or, maybe I should just die there. +use C4::Auth; use C4::Biblio; use C4::Bookfund; use C4::Bookseller; @@ -195,6 +196,29 @@ sub startup_15_truncate_tables : Test( startup => 1 ) { } +=head startup_18_set_insecure + +=cut + +# sub startup_18_set_insecure : Test( startup => 4 ) { +sub startup_18_set_insecure { + my $self = shift; + + ok( C4::Context->dbh, 'got a database handle' ); + isa_ok( C4::Context->dbh, 'DBI::db' ); + my $query = q( UPDATE systempreferences + SET value = 1 + WHERE variable = 'insecure' ); + my $ok = C4::Context->dbh->do( $query ); + ok( $ok, 'set context to insecure' ); + + my $insecure_from_preference = C4::Context->preference( 'insecure' ); + is( $insecure_from_preference, 1, 'running in insecure mode' ) + or diag( Data::Dumper->Dump( [ $insecure_from_preference ], [ 'insecure_from_preference' ] ) ); + + return $ok; +} + =head2 startup_20_add_bookseller we need a bookseller for many of the tests, so let's insert one. Feel @@ -249,17 +273,51 @@ sub startup_24_add_member : Test(startup => 1) { firstname => 'firstname' . $self->random_string(), address => 'address' . $self->random_string(), city => 'city' . $self->random_string(), + cardnumber => 'card' . $self->random_string(), branchcode => 'CPL', # CPL => Centerville categorycode => 'PT', # PT => PaTron + dateexpiry => '2010-01-01', + password => 'testpassword', }; - my $id = AddMember( %$memberinfo ); - ok( $id, "created member: $id" ); - $self->{'memberid'} = $id; + my $borrowernumber = AddMember( %$memberinfo ); + ok( $borrowernumber, "created member: $borrowernumber" ); + $self->{'memberid'} = $borrowernumber; return; } +=head2 startup_30_login + +=cut + +sub startup_30_login : Test( startup => 2 ) { + my $self = shift; + + $self->{'sessionid'} = '12345678'; # does this value matter? + my $borrower_details = C4::Members::GetMemberDetails( $self->{'memberid'} ); + ok( $borrower_details->{'cardnumber'}, 'cardnumber' ); + + # make a cookie and force it into $cgi. + # This would be a lot easier with Test::MockObject::Extends. + my $cgi = CGI->new( { userid => $borrower_details->{'cardnumber'}, + password => 'testpassword' } ); + my $setcookie = $cgi->cookie( -name => 'CGISESSID', + -value => $self->{'sessionid'} ); + $cgi->{'.cookies'} = { CGISESSID => $setcookie }; + is( $cgi->cookie('CGISESSID'), $self->{'sessionid'}, 'the CGISESSID cookie is set' ); + # diag( Data::Dumper->Dump( [ $cgi->cookie('CGISESSID') ], [ qw( cookie ) ] ) ); + + # C4::Auth::checkauth sometimes emits a warning about unable to append to sessionlog. That's OK. + my ( $userid, $cookie, $sessionID ) = C4::Auth::checkauth( $cgi, 'noauth', {}, 'intranet' ); + # diag( Data::Dumper->Dump( [ $userid, $cookie, $sessionID ], [ qw( userid cookie sessionID ) ] ) ); + + # my $session = C4::Auth::get_session( $sessionID ); + # diag( Data::Dumper->Dump( [ $session ], [ qw( session ) ] ) ); + + +} + =head2 setup methods setup methods are run before every test method @@ -344,9 +402,16 @@ sub add_biblios { d => "1835-1910." ), MARC::Field->new( '245', '1', '4', a => sprintf( 'The Adventures of Huckleberry Finn Test %s', $counter ), - c => "Mark Twain ; illustrated by E.W. Kemble." ) + c => "Mark Twain ; illustrated by E.W. Kemble." ), + MARC::Field->new( '952', '0', '0', + p => '12345678' ), # barcode + MARC::Field->new( '952', '0', '0', + a => 'CPL', + b => 'CPL' ), ); - is( $appendedfieldscount, 2, 'added 2 fields' ); + + diag $MARC::Record::ERROR if ( $MARC::Record::ERROR ); + is( $appendedfieldscount, 4, 'added 4 fields' ); my $frameworkcode = ''; # XXX I'd like to put something reasonable here. my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marcrecord, $frameworkcode ); diff --git a/t/lib/KohaTest/Scripts.pm b/t/lib/KohaTest/Scripts.pm new file mode 100644 index 0000000000..f44274dd15 --- /dev/null +++ b/t/lib/KohaTest/Scripts.pm @@ -0,0 +1,18 @@ +package KohaTest::Scripts; +use base qw( KohaTest ); + +use strict; +use warnings; + +use Test::More; + +use C4::Search; +sub testing_class { return; }; + +# Since this is an abstract base class, this prevents these tests from +# being run directly unless we're testing a subclass. It just makes +# things faster. +__PACKAGE__->SKIP_CLASS( 1 ); + + +1; diff --git a/t/lib/KohaTest/Scripts/longoverdue.pm b/t/lib/KohaTest/Scripts/longoverdue.pm new file mode 100644 index 0000000000..dcfb1a4adc --- /dev/null +++ b/t/lib/KohaTest/Scripts/longoverdue.pm @@ -0,0 +1,99 @@ +package KohaTest::Scripts::longoverdue; +use base qw( KohaTest::Scripts ); + +use strict; +use warnings; + +use Test::More; +use Time::localtime; + + +=head2 STARTUP METHODS + +These get run once, before the main test methods in this module + +=head3 create_overdue_item + +=cut + +sub create_overdue_item : Test( startup => 12 ) { + my $self = shift; + + $self->add_biblios( add_items => 1 ); + + my $biblionumber = $self->{'biblios'}[0]; + ok( $biblionumber, 'biblionumber' ); + my @biblioitems = C4::Biblio::GetBiblioItemByBiblioNumber( $biblionumber ); + ok( scalar @biblioitems > 0, 'there is at least one biblioitem' ); + my $biblioitemnumber = $biblioitems[0]->{'biblioitemnumber'}; + ok( $biblioitemnumber, 'got a biblioitemnumber' ); + + my $items = C4::Items::GetItemsByBiblioitemnumber( $biblioitemnumber); + + my $itemnumber = $items->[0]->{'itemnumber'}; + ok( $items->[0]->{'itemnumber'}, 'item number' ); + + $self->{'overdueitemnumber'} = $itemnumber; + +} + +sub set_overdue_item_lost : Test( 12 ) { + my $self = shift; + + my $item = C4::Items::GetItem( $self->{'overdueitemnumber'} ); + is( $item->{'itemnumber'}, $self->{'overdueitemnumber'}, 'itemnumber' ); + + ok( exists $item->{'itemlost'}, 'itemlost exists' ); + ok( ! $item->{'itemlost'}, 'item is not lost' ); + + # This is a US date, but that's how C4::Dates likes it, apparently. + my $duedatestring = sprintf( '%02d/%02d/%04d', + localtime->mon() + 1, + localtime->mday(), + localtime->year() + 1900 - 1, # it was due a year ago. + ); + my $duedate = C4::Dates->new( $duedatestring ); + # diag( Data::Dumper->Dump( [ $duedate ], [ 'duedate' ] ) ); + + ok( $item->{'barcode'}, 'barcode' ) + or diag( Data::Dumper->Dump( [ $item ], [ 'item' ] ) ); + # my $item_from_barcode = C4::Items::GetItem( undef, $item->{'barcode'} ); + # diag( Data::Dumper->Dump( [ $item_from_barcode ], [ 'item_from_barcode' ] ) ); + + my $borrower = C4::Members::GetMember( $self->{'memberid'} ); + ok( $borrower->{'borrowernumber'}, 'borrowernumber' ); + + my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $borrower, $item->{'barcode'}, $duedate, 0 ); + # diag( Data::Dumper->Dump( [ $issuingimpossible, $needsconfirmation ], [ qw( issuingimpossible needsconfirmation ) ] ) ); + is( keys %$issuingimpossible, 0, 'issuing is not impossible' ); + is( keys %$needsconfirmation, 0, 'issuing needs no confirmation' ); + + my $issue_due_date = C4::Circulation::AddIssue( $borrower, $item->{'barcode'}, $duedate ); + TODO: { + local $TODO = 'C4::Circulation::AddIssue returns undef insead of the due date'; + ok( $issue_due_date, 'due date' ); + } + + # I have to make this in a different format since that's how the database holds it. + my $duedateyyyymmdd = sprintf( '%04d-%02d-%02d', + localtime->year() + 1900 - 1, # it was due a year ago. + localtime->mon() + 1, + localtime->mday(), + ); + + my $issued_item = C4::Items::GetItem( $self->{'overdueitemnumber'} ); + is( $issued_item->{'onloan'}, $duedateyyyymmdd, "the item is checked out and due $duedatestring" ); + is( $issued_item->{'itemlost'}, 0, 'the item is not lost' ); + # diag( Data::Dumper->Dump( [ $issued_item ], [ 'issued_item' ] ) ); + + qx( ../misc/cronjobs/longoverdue.pl ); + + my $lost_item = C4::Items::GetItem( $self->{'overdueitemnumber'} ); + is( $lost_item->{'onloan'}, $duedateyyyymmdd, "the item is checked out and due $duedatestring" ); + is( $lost_item->{'itemlost'}, 2, 'the item is lost' ); + # diag( Data::Dumper->Dump( [ $lost_item ], [ 'lost_item' ] ) ); + +} + + +1; -- 2.39.5