From 3cf02d63c1195101fa10f7ddc12fea4452d979cd Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 9 May 2021 09:20:29 +1200 Subject: [PATCH] [Test] Improve cleanup of domain contacts This fixes a situation where the domain contact may not have an email (or exist) if this test is run in combination with certain others. The last test in the suite relies on the domain contact having an email. This test was not following the practice of having a tearDown but the tearDown was also flawed in recreating the domain contacts but not their emails --- .../CRM/Core/BAO/ActionScheduleTest.php | 4 ++-- tests/phpunit/CiviTest/CiviUnitTestCase.php | 24 +++++++++++++++---- tests/phpunit/api/v3/ContactTest.php | 4 ++-- tests/phpunit/api/v3/ContributionTest.php | 2 ++ tests/phpunit/api/v3/DomainTest.php | 4 +++- tests/phpunit/api/v3/EntityBatchTest.php | 8 +++++-- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php index 8e6686d5c0..3c846af07b 100644 --- a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php +++ b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php @@ -1753,7 +1753,7 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { [ // The next day, send an email. 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')), - 'recipients' => [['test-birth_day@example.com']], + 'recipients' => [['test-birth_day@example.com'], ['fixme.domainemail@example.org'], ['domainemail2@example.org']], ], ]); } @@ -1777,7 +1777,7 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { [ // On the eve of 3 years after they were modified, send an email. 'time' => date('Y-m-d H:i:s', strtotime($modifiedDate . ' +3 years -1 day')), - 'recipients' => [['test-birth_day@example.com']], + 'recipients' => [['test-birth_day@example.com'], ['fixme.domainemail@example.org'], ['domainemail2@example.org']], ], ]); } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 4c7bf51e2c..dc55ba8a22 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -453,14 +453,30 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { /** * Create default domain contacts for the two domains added during test class. * database population. + * + * @throws \CiviCRM_API3_Exception */ - public function createDomainContacts() { - $this->organizationCreate(); - $this->organizationCreate(['organization_name' => 'Second Domain']); + public function createDomainContacts(): void { + $this->organizationCreate(['api.Email.create' => ['email' => 'fixme.domainemail@example.org']]); + $this->organizationCreate([ + 'organization_name' => 'Second Domain', + 'api.Email.create' => ['email' => 'domainemail2@example.org'], + 'api.Address.create' => [ + 'street_address' => '15 Main St', + 'location_type_id' => 1, + 'city' => 'Collinsville', + 'country_id' => 1228, + 'state_province_id' => 1003, + 'postal_code' => 6022, + ], + ]); } /** * Common teardown functions for all unit tests. + * + * @throws \CiviCRM_API3_Exception + * @throws \CRM_Core_Exception */ protected function tearDown(): void { $this->_apiversion = 3; @@ -484,7 +500,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { CRM_Core_Transaction::forceRollbackIfEnabled(); \Civi\Core\Transaction\Manager::singleton(TRUE); - $tablesToTruncate = ['civicrm_contact', 'civicrm_uf_match']; + $tablesToTruncate = ['civicrm_contact', 'civicrm_uf_match', 'civicrm_email', 'civicrm_address']; $this->quickCleanup($tablesToTruncate); $this->createDomainContacts(); } diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 87f76dbfed..6d9ae50f07 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3261,7 +3261,7 @@ class api_v3_ContactTest extends CiviUnitTestCase { 'Bob, Bob :: bob@bob.com', 'C Bobby, Bobby', 'H Bobby, Bobby :: bob@h.com', - 'Second Domain', + 'Second Domain :: domainemail2@example.org', $this->callAPISuccessGetValue('Contact', ['id' => $loggedInContactID, 'return' => 'last_name']) . ', Logged In :: anthony_anderson@civicrm.org', ]; $this->assertEquals(6, $result['count']); @@ -3309,7 +3309,7 @@ class api_v3_ContactTest extends CiviUnitTestCase { 'A Bobby, Bobby :: bob@bobby.com', 'Bob, Bob :: bob@bob.com', 'C Bobby, Bobby', - 'Second Domain', + 'Second Domain :: domainemail2@example.org', $this->callAPISuccessGetValue('Contact', ['id' => $loggedInContactID, 'return' => 'last_name']) . ', Logged In :: anthony_anderson@civicrm.org', ]; foreach ($expectedData as $index => $value) { diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 12bc1ef0c5..212fbd500c 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -108,6 +108,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { * Clean up after each test. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public function tearDown(): void { $this->quickCleanUpFinancialEntities(); @@ -125,6 +126,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { } } $this->restoreUFGroupOne(); + parent::tearDown(); } /** diff --git a/tests/phpunit/api/v3/DomainTest.php b/tests/phpunit/api/v3/DomainTest.php index bb9eb817c8..9c111b5a71 100644 --- a/tests/phpunit/api/v3/DomainTest.php +++ b/tests/phpunit/api/v3/DomainTest.php @@ -100,8 +100,10 @@ class api_v3_DomainTest extends CiviUnitTestCase { /** * Test get function with current domain. + * + * @throws \CRM_Core_Exception */ - public function testGetCurrentDomain() { + public function testGetCurrentDomain(): void { $params = ['current_domain' => 1]; $result = $this->callAPISuccess('domain', 'get', $params); diff --git a/tests/phpunit/api/v3/EntityBatchTest.php b/tests/phpunit/api/v3/EntityBatchTest.php index 0ed0b96d63..da52303b6a 100644 --- a/tests/phpunit/api/v3/EntityBatchTest.php +++ b/tests/phpunit/api/v3/EntityBatchTest.php @@ -23,11 +23,15 @@ class api_v3_EntityBatchTest extends CiviUnitTestCase { public $DBResetRequired = FALSE; + /** + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ public function setUp(): void { parent::setUp(); - $this->useTransaction(TRUE); + $this->useTransaction(); - $entityParams = ['contact_id' => 1]; + $entityParams = ['contact_id' => $this->individualCreate()]; $this->_entity = 'EntityBatch'; $this->_entityID = $this->contributionCreate($entityParams); -- 2.25.1