Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC/Unit test] - Failing test demonstrating blank timestamp updates #20891

Merged
merged 1 commit into from
Jul 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions tests/phpunit/CRM/Core/DAOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,45 @@ public function testTableHasBeenAdded() {
$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);
}

}