Bug 19489: Koha::Account::Line->issue method and Unit test
[koha.git] / t / db_dependent / Patrons.t
index b22a6e8..f0d116a 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 13;
+use Test::More tests => 17;
 use Test::Warn;
 
 use C4::Context;
 use Koha::Database;
+use Koha::DateUtils;
+
+use t::lib::Dates;
+use t::lib::TestBuilder;
 
 BEGIN {
     use_ok('Koha::Objects');
@@ -29,17 +33,13 @@ BEGIN {
 }
 
 # Start transaction
-my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
-$dbh->do("DELETE FROM issues");
-$dbh->do("DELETE FROM borrowers");
-
-my $categorycode =
-  Koha::Database->new()->schema()->resultset('Category')->first()
-  ->categorycode();
-my $branchcode =
-  Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
+my $database = Koha::Database->new();
+my $schema = $database->schema();
+$schema->storage->txn_begin();
+my $builder = t::lib::TestBuilder->new;
+
+my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
+my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
 
 my $b1 = Koha::Patron->new(
     {
@@ -49,6 +49,7 @@ my $b1 = Koha::Patron->new(
     }
 );
 $b1->store();
+my $now = dt_from_string;
 my $b2 = Koha::Patron->new(
     {
         surname      => 'Test 2',
@@ -57,17 +58,27 @@ my $b2 = Koha::Patron->new(
     }
 );
 $b2->store();
+my $three_days_ago = dt_from_string->add( days => -3 );
 my $b3 = Koha::Patron->new(
     {
         surname      => 'Test 3',
         branchcode   => $branchcode,
-        categorycode => $categorycode
+        categorycode => $categorycode,
+        updated_on   => $three_days_ago,
     }
 );
 $b3->store();
 
 my $b1_new = Koha::Patrons->find( $b1->borrowernumber() );
 is( $b1->surname(), $b1_new->surname(), "Found matching patron" );
+isnt( $b1_new->updated_on, undef, "borrowers.updated_on should be set" );
+is( t::lib::Dates::compare( $b1_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on creating" );
+
+my $b3_new = Koha::Patrons->find( $b3->borrowernumber() );
+is( t::lib::Dates::compare( $b3_new->updated_on, $three_days_ago), 0, "borrowers.updated_on should have been kept to what we set on creating" );
+$b3_new->set({ firstname => 'Some first name for Test 3' })->store();
+$b3_new = Koha::Patrons->find( $b3->borrowernumber() );
+is( t::lib::Dates::compare( $b3_new->updated_on, $now), 0, "borrowers.updated_on should have been set to now on updating" );
 
 my @patrons = Koha::Patrons->search( { branchcode => $branchcode } );
 is( @patrons, 3, "Found 3 patrons with Search" );
@@ -93,4 +104,5 @@ foreach my $b ( $patrons->as_list() ) {
     is( $b->categorycode(), $categorycode, "Iteration returns a patron object" );
 }
 
-1;
+$schema->storage->txn_rollback();
+