From 86624e023b9be2477fcecf8e2932a8068d434a2e Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Sat, 17 Jul 2021 18:36:12 -0400 Subject: [PATCH] failing test for timestamp updates --- tests/phpunit/CRM/Core/DAOTest.php | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/phpunit/CRM/Core/DAOTest.php b/tests/phpunit/CRM/Core/DAOTest.php index 29ad92515c..f5b871cc7f 100644 --- a/tests/phpunit/CRM/Core/DAOTest.php +++ b/tests/phpunit/CRM/Core/DAOTest.php @@ -557,4 +557,45 @@ class CRM_Core_DAOTest extends CiviUnitTestCase { $this->assertTrue(CRM_Contact_DAO_RelationshipCache::tableHasBeenAdded()); } + /** + * Test that a timestamp field that allows null doesn't crash when updating + * with a blank date. + * An unpatched PEAR::DB_DataObject will fail when sql_mode has NO_ZERO_DATE. + * + * @throws CRM_Core_Exception + */ + public function testUpdateTimestampWithBlankDate() { + // Arbitrarily using "Cache" since it has a desired type of timestamp field and is simple. + $dao = new CRM_Core_DAO_Cache(); + $fields = $dao->fields(); + $this->assertSame(CRM_Utils_Type::T_TIMESTAMP, $fields['expired_date']['type'], 'Oh somebody changed the type, so this test might not be testing the right type of timestamp anymore. Might need to change the test to have it use a different field.'); + $this->assertFalse($fields['expired_date']['required'], 'Oh somebody changed the REQUIRED setting, so this test might not be testing the right type of timestamp anymore. Might need to change the test to have it use a different field.'); + + $dao->group_name = 'mytest'; + $dao->path = 'mypath'; + $dao->data = 'some data'; + $dao->expired_date = ''; + $dao->save(); + $id = $dao->id; + + // Now retrieve it and update it with a blank timestamp. + $dao = new CRM_Core_DAO_Cache(); + $dao->id = $id; + $dao->find(TRUE); + // sanity check we got the right one since otherwise checking null might falsely be true + $this->assertEquals('mytest', $dao->group_name); + $this->assertNull($dao->expired_date); + + $dao->data = 'some updated data'; + $dao->expired_date = ''; + // would crash here on update + $dao->save(); + + $dao = new CRM_Core_DAO_Cache(); + $dao->id = $id; + $dao->find(TRUE); + $this->assertEquals('some updated data', $dao->data); + $this->assertNull($dao->expired_date); + } + } -- 2.25.1