#!/usr/bin/perl
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+
use Modern::Perl;
+use Data::Dumper; # REMOVEME with diag
use Test::More tests => 3;
use Test::MockModule;
+use Test::MockTime qw( set_fixed_time );
+use t::lib::TestBuilder;
use C4::Biblio;
use C4::Items;
use C4::Members;
-use C4::Branch;
-use C4::Category;
use C4::Circulation;
use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Library;
+use Koha::Patrons;
use DateTime::Duration;
use MARC::Record;
+my $schema = Koha::Database->schema;
+$schema->storage->txn_begin;
my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
$dbh->do(q|DELETE FROM issues|);
$dbh->do(q|DELETE FROM borrowers|);
$dbh->do(q|DELETE FROM items|);
-$dbh->do(q|DELETE FROM branches|);
$dbh->do(q|DELETE FROM biblio|);
$dbh->do(q|DELETE FROM categories|);
$dbh->do(q|DELETE FROM letter|);
-my $branchcode = 'B';
-ModBranch( { add => 1, branchcode => $branchcode, branchname => 'Branch' } );
+my $builder = t::lib::TestBuilder->new;
+set_fixed_time(CORE::time());
-my $categorycode = 'C';
-$dbh->do( "INSERT INTO categories(categorycode) VALUES(?)",
- undef, $categorycode );
+my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
+my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
+my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
-my %item_branch_infos = (
+my %item_infos = (
homebranch => $branchcode,
holdingbranch => $branchcode,
+ itype => $itemtype
);
+
my $slip_content = <<EOS;
Checked out:
<checkedout>
);
my ($biblionumber1) = AddBiblio( $record, '' );
my $itemnumber1 =
- AddItem( { barcode => $barcode1, %item_branch_infos }, $biblionumber1 );
+ AddItem( { barcode => $barcode1, %item_infos }, $biblionumber1 );
$record = MARC::Record->new;
$record->append_fields(
);
my ($biblionumber2) = AddBiblio( $record, '' );
my $itemnumber2 =
- AddItem( { barcode => $barcode2, %item_branch_infos }, $biblionumber2 );
+ AddItem( { barcode => $barcode2, %item_infos }, $biblionumber2 );
my $borrowernumber =
- AddMember( categorycode => $categorycode, branchcode => $branchcode );
-my $borrower = GetMember( borrowernumber => $borrowernumber );
+ Koha::Patron->new({ categorycode => $categorycode, branchcode => $branchcode })->store->borrowernumber;
+my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
my $module = new Test::MockModule('C4::Context');
$module->mock( 'userenv', sub { { branch => $branchcode } } );
subtest 'Daily loans' => sub {
plan tests => 2;
+ skip "It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
# Test 1: No overdue
my $today_daily = $today->clone->set( hour => 23, minute => 59 );
my $today_daily_as_formatted = output_pref( $today_daily );
$issue_date = $yesterday_daily;
AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date );
+ # Set timestamps to the same value to avoid a different order
+ Koha::Checkouts->search(
+ { borrowernumber => $borrower->{borrowernumber} }
+ )->update( { timestamp => dt_from_string } );
+
$expected_slip = <<EOS;
Checked out:
subtest 'Hourly loans' => sub {
plan tests => 2;
+ skip "It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
# Test 1: No overdue
my ( $date_due_in_time, $date_due_in_time_as_formatted, $date_due_in_late, $date_due_in_late_as_formatted, $issue_date, $slip, $expected_slip );
# Assuming today is not hour = 23 and minute = 59
- $date_due_in_time = $today->clone->set(hour => $today->hour + 1);
+ $date_due_in_time = $today->clone->set(hour => ($today->hour < 23 ? $today->hour + 1 : 23), minute => 59);
$date_due_in_time_as_formatted = output_pref( $date_due_in_time );
- $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
+ $issue_date = $date_due_in_time->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
$issue_date = $yesterday->clone;
AddIssue( $borrower, $barcode2, $date_due_in_time, undef, $issue_date );
+ # Set timestamps to the same value to avoid a different order
+ Koha::Checkouts->search(
+ { borrowernumber => $borrower->{borrowernumber} }
+ )->update( { timestamp => dt_from_string } );
+
$expected_slip = <<EOS;
Checked out:
EOS
$slip = IssueSlip( $branchcode, $borrowernumber );
- is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 2 checkouts' );
+ is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 2 checkouts' )
+ or diag(Dumper(Koha::Checkouts->search({borrowernumber => $borrower->{borrowernumber}})->unblessed));
AddReturn( $barcode1, $branchcode );
AddReturn( $barcode2, $branchcode );
# Test 2: 1 Overdue
- $date_due_in_time = $today->clone->set(hour => $today->hour + 1);
+ $date_due_in_time = $today->clone->set(hour => ($today->hour < 23 ? $today->hour + 1 : 23), minute => 59);
$date_due_in_time_as_formatted = output_pref( $date_due_in_time );
- $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
+ $issue_date = $date_due_in_time->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
- $date_due_in_late = $today->clone->set(hour => $today->hour - 1);
+ $date_due_in_late = $today->clone->subtract( hours => 1 );
$date_due_in_late_as_formatted = output_pref( $date_due_in_late );
$issue_date = $yesterday->clone;
AddIssue( $borrower, $barcode2, $date_due_in_late, undef, $issue_date );
subtest 'Daily loans' => sub {
plan tests => 2;
+ skip "It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
# Test 1: No overdue
my $today_daily = $today->clone->set( hour => 23, minute => 59 );
my $today_daily_as_formatted = output_pref( $today_daily );
# Test 1: No overdue
my ( $date_due_in_time, $date_due_in_time_as_formatted, $date_due_in_late, $date_due_in_late_as_formatted, $issue_date, $slip, $expected_slip );
# Assuming today is not hour = 23 and minute = 59
- $date_due_in_time = $today->clone->set(hour => $today->hour + 1);
+ $date_due_in_time = $today->clone->set(hour => ($today->hour < 23 ? $today->hour + 1 : 23), minute => 59);
$date_due_in_time_as_formatted = output_pref( $date_due_in_time );
- $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
+ $issue_date = $date_due_in_time->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
$issue_date = $yesterday->clone;
AddIssue( $borrower, $barcode2, $date_due_in_time, undef, $issue_date );
AddReturn( $barcode2, $branchcode );
# Test 2: 1 Overdue
- $date_due_in_time = $today->clone->set(hour => $today->hour + 1);
+ $date_due_in_time = $today->clone->set(hour => ($today->hour < 23 ? $today->hour + 1 : 23), minute => 59);
$date_due_in_time_as_formatted = output_pref( $date_due_in_time );
- $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
+ $issue_date = $date_due_in_time->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
- $date_due_in_late = $today->clone->set(hour => $today->hour - 1);
+ $date_due_in_late = $today->clone->subtract( hours => 1 );
$date_due_in_late_as_formatted = output_pref( $date_due_in_late );
$issue_date = $yesterday->clone;
AddIssue( $borrower, $barcode2, $date_due_in_late, undef, $issue_date );
};
subtest 'bad calls' => sub {
- plan tests => 2;
- AddIssue( $borrower, $barcode1, $today, undef, $yesterday );
+ plan tests => 1;
my $slip = IssueSlip();
- isnt( $slip, undef, 'IssueSlip should return if no param passed FIXME, should return undef' );
- my $empty_slip = <<EOS;
-Checked out:
-
-
-Overdues:
+ is( $slip, undef, 'IssueSlip should return if no valid borrowernumber is passed' );
+};
-EOS
+$schema->storage->txn_rollback;
- $slip = IssueSlip(undef, $borrowernumber+1);
- is( $slip->{content}, $empty_slip, 'IssueSlip should not return an empty slip if the borrowernumber passed in param does not exist. But it is what it does for now (FIXME)' );
-};