Bug 20287: Replace occurrences of AddMember with Koha::Patron->new->store->borrowernumber
[koha.git] / t / db_dependent / Members / IssueSlip.t
index 8c32c21..cbf3a72 100644 (file)
@@ -1,46 +1,66 @@
 #!/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>
@@ -85,7 +105,7 @@ $record->append_fields(
 );
 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(
@@ -96,11 +116,11 @@ $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 } } );
@@ -141,6 +161,11 @@ EOS
         $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:
 
@@ -206,6 +231,11 @@ EOS
         $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:
 
@@ -223,7 +253,8 @@ Overdues:
 
 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 );
@@ -233,7 +264,7 @@ EOS
         $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
         $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 );
@@ -357,7 +388,7 @@ EOS
         $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
         $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 );
@@ -380,18 +411,10 @@ EOS
 };
 
 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)' );
-};