[Test] Improve cleanup of domain contacts
authorYour Name <you@example.com>
Sat, 8 May 2021 21:20:29 +0000 (09:20 +1200)
committerYour Name <you@example.com>
Sun, 9 May 2021 21:41:02 +0000 (09:41 +1200)
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

tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ContactTest.php
tests/phpunit/api/v3/ContributionTest.php
tests/phpunit/api/v3/DomainTest.php
tests/phpunit/api/v3/EntityBatchTest.php

index 8e6686d5c0cf08df677c5414d5c203d738796fba..3c846af07b41025ae72becc9fe0d13eb2e8a9e8e 100644 (file)
@@ -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']],
       ],
     ]);
   }
index 4c7bf51e2c46cc16c9d51070260b98284fafd3e1..dc55ba8a225962bd6cda4158467d96661730fc15 100644 (file)
@@ -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();
     }
index 87f76dbfed69825e5a506e30baa16305f5d73386..6d9ae50f07ebf2f03b9d08a4c3b98b5129389704 100644 (file)
@@ -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) {
index 12bc1ef0c5d0b1e9149563a0725e572a8292ef1c..212fbd500c8082aa024ae738f03e686a9f90e99d 100644 (file)
@@ -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();
   }
 
   /**
index bb9eb817c80b1071aea6e5d8296f59a92e7dd12b..9c111b5a71fc854e434d27be029df5c10f5a9711 100644 (file)
@@ -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);
 
index 0ed0b96d631038bd6159f598826364a745e5d202..da52303b6a4c2b1fba0a430b2bbab18ba263d84d 100644 (file)
@@ -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);