Bug 27851: Add tests
[srvgit] / t / db_dependent / Koha / Checkouts.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 9;
23
24 use C4::Circulation;
25 use Koha::Checkouts;
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder         = t::lib::TestBuilder->new;
36 my $library         = $builder->build( { source => 'Branch' } );
37 my $patron          = $builder->build( { source => 'Borrower', value => { branchcode => $library->{branchcode} } } );
38 my $item_1          = $builder->build_sample_item;
39 my $item_2          = $builder->build_sample_item;
40 my $nb_of_checkouts = Koha::Checkouts->search->count;
41 my $new_checkout_1  = Koha::Checkout->new(
42     {   borrowernumber => $patron->{borrowernumber},
43         itemnumber     => $item_1->itemnumber,
44         branchcode     => $library->{branchcode},
45     }
46 )->store;
47 my $new_checkout_2 = Koha::Checkout->new(
48     {   borrowernumber => $patron->{borrowernumber},
49         itemnumber     => $item_2->itemnumber,
50         branchcode     => $library->{branchcode},
51     }
52 )->store;
53
54 like( $new_checkout_1->issue_id, qr|^\d+$|, 'Adding a new checkout should have set the issue_id' );
55 is( Koha::Checkouts->search->count, $nb_of_checkouts + 2, 'The 2 checkouts should have been added' );
56
57 my $retrieved_checkout_1 = Koha::Checkouts->find( $new_checkout_1->issue_id );
58 is( $retrieved_checkout_1->itemnumber, $new_checkout_1->itemnumber, 'Find a checkout by id should return the correct checkout' );
59
60 subtest 'is_overdue' => sub {
61     plan tests => 6;
62     my $ten_days_ago   = dt_from_string->add( days => -10 );
63     my $ten_days_later = dt_from_string->add( days => 10 );
64     my $yesterday      = dt_from_string->add( days => -1 );
65     my $tomorrow       = dt_from_string->add( days => 1 );
66
67     $retrieved_checkout_1->date_due($ten_days_ago)->store;
68     is( $retrieved_checkout_1->is_overdue,
69         1, 'The item should have been returned 10 days ago' );
70
71     $retrieved_checkout_1->date_due($ten_days_later)->store;
72     is( $retrieved_checkout_1->is_overdue, 0, 'The item is due in 10 days' );
73
74     $retrieved_checkout_1->date_due($tomorrow)->store;
75     is( $retrieved_checkout_1->is_overdue($ten_days_later),
76         1, 'The item should have been returned yesterday' );
77
78     $retrieved_checkout_1->date_due($yesterday)->store;
79     is( $retrieved_checkout_1->is_overdue($ten_days_ago),
80         0, 'Ten days ago the item due yesterday was not late' );
81
82     $retrieved_checkout_1->date_due($tomorrow)->store;
83     is( $retrieved_checkout_1->is_overdue($ten_days_later),
84         1, 'In Ten days, the item due tomorrow will be late' );
85
86     $retrieved_checkout_1->date_due($yesterday)->store;
87     is( $retrieved_checkout_1->is_overdue($ten_days_ago),
88         0, 'In Ten days, the item due yesterday will still be late' );
89 };
90
91 subtest 'item' => sub {
92     plan tests => 2;
93     my $item = $retrieved_checkout_1->item;
94     is( ref( $item ), 'Koha::Item', 'Koha::Checkout->item should return a Koha::Item' );
95     is( $item->itemnumber, $item_1->itemnumber, 'Koha::Checkout->item should return the correct item' );
96 };
97
98 subtest 'patron' => sub {
99     plan tests => 3;
100     my $patron = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}});
101
102     my $item = $builder->build_sample_item;
103     my $checkout = Koha::Checkout->new(
104         {   borrowernumber => $patron->borrowernumber,
105             itemnumber     => $item->itemnumber,
106             branchcode     => $library->{branchcode},
107         }
108     )->store;
109
110     my $p = $checkout->patron;
111     is( ref($p), 'Koha::Patron',
112         'Koha::Checkout->patron should return a Koha::Patron' );
113     is( $p->borrowernumber, $patron->borrowernumber,
114         'Koha::Checkout->patron should return the correct patron' );
115
116     # Testing Koha::Old::Checkout->patron now
117     my $issue_id = $checkout->issue_id;
118     C4::Circulation::MarkIssueReturned( $p->borrowernumber, $checkout->itemnumber );
119     $p->delete;
120     my $old_issue = Koha::Old::Checkouts->find($issue_id);
121     is( $old_issue->patron, undef,
122         'Koha::Checkout->patron should return undef if the patron record has been deleted'
123     );
124 };
125
126 $retrieved_checkout_1->delete;
127 is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' );
128
129 subtest 'issuer' => sub {
130     plan tests => 3;
131     my $patron = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}});
132     my $issuer = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}});
133
134     my $item = $builder->build_sample_item;
135     my $checkout = Koha::Checkout->new({
136         borrowernumber => $patron->borrowernumber,
137         issuer_id      => $issuer->borrowernumber,
138         itemnumber     => $item->itemnumber,
139         branchcode     => $library->{branchcode},
140     })->store;
141
142     my $i = $checkout->issuer;
143     is( ref($i), 'Koha::Patron',
144         'Koha::Checkout->issuer should return a Koha::Patron' );
145     is( $i->borrowernumber, $issuer->borrowernumber,
146         'Koha::Checkout->issuer should return the correct patron' );
147
148     # Testing Koha::Old::Checkout->patron now
149     my $issue_id = $checkout->issue_id;
150     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $checkout->itemnumber );
151     $i->delete;
152     my $old_issue = Koha::Old::Checkouts->find($issue_id);
153     is( $old_issue->issuer_id, undef,
154         'Koha::Checkout->issuer_id should return undef if the patron record has been deleted'
155     );
156
157 };
158
159 subtest 'Koha::Old::Checkouts->filter_by_todays_checkins' => sub {
160
161     plan tests => 3;
162
163     # We will create 7 checkins for a given patron
164     # 3 checked in today - 2 days, and 4 checked in today
165     my $librarian = $builder->build_object(
166         {
167             class => 'Koha::Patrons',
168             value => { branchcode => $library->{branchcode} }
169         }
170     );
171     t::lib::Mocks::mock_userenv( { patron => $librarian } );
172     my $patron = $builder->build_object(
173         {
174             class => 'Koha::Patrons',
175             value => { branchcode => $library->{branchcode} }
176         }
177     );
178
179     my @checkouts;
180     # Create 7 checkouts
181     for ( 0 .. 6 ) {
182         my $item = $builder->build_sample_item;
183         push @checkouts,
184           Koha::Checkout->new(
185             {
186                 borrowernumber => $patron->borrowernumber,
187                 itemnumber     => $item->itemnumber,
188                 branchcode     => $library->{branchcode},
189             }
190         )->store;
191     }
192
193     # Checkin 3 today - 2 days
194     my $not_today = dt_from_string->add( days => -2 );
195     for my $i ( 0 .. 2 ) {
196         my $checkout = $checkouts[$i];
197         C4::Circulation::AddReturn(
198             $checkout->item->barcode, $library->{branchcode},
199             undef, $not_today->set_hour( int( rand(24) ) )
200         );
201     }
202     # Checkin 4 today
203     my $today = dt_from_string;
204     for my $i ( 3 .. 6 ) {
205         my $checkout = $checkouts[$i];
206         C4::Circulation::AddReturn(
207             $checkout->item->barcode, $library->{branchcode},
208             undef, $today->set_hour( int( rand(24) ) )
209         );
210     }
211
212     my $old_checkouts = $patron->old_checkouts;
213     is( $old_checkouts->count, 7, 'There should be 7 old checkouts' );
214     my $todays_checkins = $old_checkouts->filter_by_todays_checkins;
215     is( $todays_checkins->count, 4, 'There should be 4 checkins today' );
216     is_deeply(
217         [ $todays_checkins->get_column('itemnumber') ],
218         [ map { $_->itemnumber } @checkouts[ 3 .. 6 ] ],
219         q{Correct list of today's checkins}
220     );
221 };
222
223 $schema->storage->txn_rollback;
224