Modifying Members : Add Mod and GetMember
[koha.git] / t / lib / KohaTest / Scripts / longoverdue.pm
1 package KohaTest::Scripts::longoverdue;
2 use base qw( KohaTest::Scripts );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use Time::localtime;
9
10
11 =head2 STARTUP METHODS
12
13 These get run once, before the main test methods in this module
14
15 =head3 create_overdue_item
16
17 =cut
18
19 sub create_overdue_item : Test( startup => 12 ) {
20     my $self = shift;
21     
22     $self->add_biblios( add_items => 1 );
23     
24     my $biblionumber = $self->{'biblios'}[0];
25     ok( $biblionumber, 'biblionumber' );
26     my @biblioitems = C4::Biblio::GetBiblioItemByBiblioNumber( $biblionumber );
27     ok( scalar @biblioitems > 0, 'there is at least one biblioitem' );
28     my $biblioitemnumber = $biblioitems[0]->{'biblioitemnumber'};
29     ok( $biblioitemnumber, 'got a biblioitemnumber' );
30
31     my $items = C4::Items::GetItemsByBiblioitemnumber( $biblioitemnumber);
32                            
33     my $itemnumber = $items->[0]->{'itemnumber'};
34     ok( $items->[0]->{'itemnumber'}, 'item number' );
35
36     $self->{'overdueitemnumber'} = $itemnumber;
37     
38 }
39
40 sub set_overdue_item_lost : Test( 13 ) {
41     my $self = shift;
42
43     my $item = C4::Items::GetItem( $self->{'overdueitemnumber'} );
44     is( $item->{'itemnumber'}, $self->{'overdueitemnumber'}, 'itemnumber' );
45
46     ok( exists $item->{'itemlost'}, 'itemlost exists' );
47     ok( ! $item->{'itemlost'}, 'item is not lost' );
48
49     # This is a US date, but that's how C4::Dates likes it, apparently.
50     my $duedatestring = sprintf( '%02d/%02d/%04d',
51                                  localtime->mon() + 1,
52                                  localtime->mday(),
53                                  localtime->year() + 1900 - 1, # it was due a year ago.
54                             );
55     my $duedate = C4::Dates->new( $duedatestring );
56     # diag( Data::Dumper->Dump( [ $duedate ], [ 'duedate' ] ) );
57     
58     ok( $item->{'barcode'}, 'barcode' )
59       or diag( Data::Dumper->Dump( [ $item ], [ 'item' ] ) );
60     # my $item_from_barcode = C4::Items::GetItem( undef, $item->{'barcode'} );
61     # diag( Data::Dumper->Dump( [ $item_from_barcode ], [ 'item_from_barcode' ] ) );
62
63     my $borrower = C4::Members::GetMember( borrowernumber => $self->{'memberid'} );
64     ok( $borrower->{'borrowernumber'}, 'borrowernumber' );
65     
66     my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $borrower, $item->{'barcode'}, $duedate, 0 );
67     # diag( Data::Dumper->Dump( [ $issuingimpossible, $needsconfirmation ], [ qw( issuingimpossible needsconfirmation ) ] ) );
68     is( keys %$issuingimpossible, 0, 'issuing is not impossible' );
69     is( keys %$needsconfirmation, 0, 'issuing needs no confirmation' );
70
71     my $issue_due_date = C4::Circulation::AddIssue( $borrower, $item->{'barcode'}, $duedate );
72     ok( $issue_due_date, 'due date' );
73     is( $issue_due_date, $duedate, 'AddIssue returned the same date we passed to it' );
74     
75     # I have to make this in a different format since that's how the database holds it.
76     my $duedateyyyymmdd = sprintf( '%04d-%02d-%02d',
77                                    localtime->year() + 1900 - 1, # it was due a year ago.
78                                    localtime->mon() + 1,
79                                    localtime->mday(),
80                               );
81
82     my $issued_item = C4::Items::GetItem( $self->{'overdueitemnumber'} );
83     is( $issued_item->{'onloan'}, $duedateyyyymmdd, "the item is checked out and due $duedatestring" );
84     is( $issued_item->{'itemlost'}, 0, 'the item is not lost' );
85     # diag( Data::Dumper->Dump( [ $issued_item ], [ 'issued_item' ] ) );
86
87     qx( ../misc/cronjobs/longoverdue.pl --lost 90=2 --confirm );
88
89     my $lost_item = C4::Items::GetItem( $self->{'overdueitemnumber'} );
90     is( $lost_item->{'onloan'}, $duedateyyyymmdd, "the item is checked out and due $duedatestring" );
91     is( $lost_item->{'itemlost'}, 2, 'the item is lost' );
92     # diag( Data::Dumper->Dump( [ $lost_item ], [ 'lost_item' ] ) );
93
94 }
95
96
97 1;