From 89aa2d0055415246ebd138112f5f581435ce1c3e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 28 Oct 2024 09:13:29 -0300 Subject: [PATCH] Bug 38273: Make sure same object is returned This patch adds a test to make sure the same object is returned. A code change is also added to make sure the tests pass. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Katrin Fischer --- Koha/Object.pm | 4 ++-- t/db_dependent/Koha/Object.t | 32 +++++++++++++++----------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index 6b6faad84d..0511d27157 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -226,8 +226,8 @@ Refetch the row from the DB sub discard_changes { my ($self) = @_; - my $object_class = Koha::Object::_get_object_class( $self->_result->result_class ); - return $object_class->_new_from_dbic( $self->_result->discard_changes ); + $self->_result->discard_changes; + return $self; } =head3 $object->update(); diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index 0cb41f1932..606182d241 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -37,7 +37,7 @@ use Koha::Patrons; use Koha::Library::Groups; use JSON; -use Scalar::Util qw( isvstring ); +use Scalar::Util qw( isvstring refaddr ); use Try::Tiny; use t::lib::TestBuilder; @@ -152,31 +152,29 @@ subtest 'get_column' => sub { $schema->storage->txn_rollback; }; -subtest 'discard_changes' => sub { +subtest 'discard_changes() tests' => sub { + plan tests => 3; $schema->storage->txn_begin; - my $patron = $builder->build( { source => 'Borrower' } ); - $patron = Koha::Patrons->find( $patron->{borrowernumber} ); + my $date_expiry = dt_from_string->add( days => 30 ); + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { dateexpiry => $date_expiry } + } + ); $patron->dateexpiry(dt_from_string); - $patron->discard_changes; + my $ret = $patron->discard_changes; is( dt_from_string( $patron->dateexpiry ), - dt_from_string->truncate( to => 'day' ), + $date_expiry->truncate( to => 'day' ), 'discard_changes should refresh the object' ); - my $cardnumber = $patron->cardnumber; - my $categorycode = $patron->categorycode; - my $branchcode = $patron->branchcode; - $patron->delete; - - $patron = - Koha::Patron->new( { cardnumber => $cardnumber, categorycode => $categorycode, branchcode => $branchcode } ) - ->store->discard_changes; - - is( ref($patron), 'Koha::Patron', 'discard_changes should return a Koha::Object object' ); - isnt( $patron->updated_on, undef, 'discard_changes should have fetched the row from the DB' ); + is( ref($ret), 'Koha::Patron', 'discard_changes should return a Koha::Object object' ); + is( refaddr($ret), refaddr($patron), 'Same object referenced' ); $schema->storage->txn_rollback; }; -- 2.39.5