Merge pull request #20313 from eileenmcnaughton/mem_move2
[civicrm-core.git] / tests / phpunit / CiviTest / CiviUnitTestCase.php
index 8a78474af552e4e45113fc8149df7da70be7a045..dfd7c3db30bce23294cd10df36bdc8b7e3e29cb2 100644 (file)
@@ -26,7 +26,9 @@
  *   <http://www.gnu.org/licenses/>.
  */
 
+use Civi\Api4\OptionGroup;
 use Civi\Payment\System;
+use Civi\Api4\OptionValue;
 use League\Csv\Reader;
 
 /**
@@ -156,11 +158,11 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
    *
    * This can be used to test hooks within tests. For example in the ACL_PermissionTrait:
    *
-   * $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
+   * $this->hookClass->setHook('civicrm_aclWhereClause', [$this, 'aclWhereHookAllResults']);
    *
    * @var \CRM_Utils_Hook_UnitTests
    */
-  public $hookClass = NULL;
+  public $hookClass;
 
   /**
    * @var array
@@ -317,7 +319,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
   /**
    *  Common setup functions for all unit tests.
    */
-  protected function setUp() {
+  protected function setUp(): void {
     $session = CRM_Core_Session::singleton();
     $session->set('userID', NULL);
 
@@ -453,14 +455,38 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
   /**
    * Create default domain contacts for the two domains added during test class.
    * database population.
-   */
-  public function createDomainContacts() {
-    $this->organizationCreate();
-    $this->organizationCreate(['organization_name' => 'Second Domain']);
+   *
+   * @throws \CiviCRM_API3_Exception
+   * @throws \API_Exception
+   */
+  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,
+      ],
+    ]);
+    OptionValue::replace(FALSE)->addWhere(
+      'option_group_id:name', '=', 'from_email_address'
+    )->setDefaults([
+      'is_default' => 1,
+      'name' => '"FIXME" <info@EXAMPLE.ORG>',
+      'label' => '"FIXME" <info@EXAMPLE.ORG>',
+    ])->setRecords([['domain_id' => 1], ['domain_id' => 2]])->execute();
   }
 
   /**
    *  Common teardown functions for all unit tests.
+   *
+   * @throws \CiviCRM_API3_Exception
+   * @throws \CRM_Core_Exception
    */
   protected function tearDown(): void {
     $this->_apiversion = 3;
@@ -484,7 +510,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();
     }
@@ -498,12 +524,18 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
   /**
    * CHeck that all tests that have created payments have created them with the right financial entities.
    *
+   * @throws \API_Exception
    * @throws \CRM_Core_Exception
    */
   protected function assertPostConditions() {
+    // Reset to version 3 as not all (e.g payments) work on v4
+    $this->_apiversion = 3;
     if ($this->isLocationTypesOnPostAssert) {
       $this->assertLocationValidity();
     }
+    $this->assertCount(1, OptionGroup::get(FALSE)
+      ->addWhere('name', '=', 'from_email_address')
+      ->execute());
     if (!$this->isValidateFinancialsOnPostAssert) {
       return;
     }
@@ -571,7 +603,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
   /**
    * @param array $params
    *
-   * @return mixed
+   * @return int
    */
   public function membershipTypeCreate($params = []) {
     CRM_Member_PseudoConstant::flush('membershipType');
@@ -595,7 +627,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
     CRM_Member_PseudoConstant::flush('membershipType');
     CRM_Utils_Cache::singleton()->flush();
 
-    return $result['id'];
+    return (int) $result['id'];
   }
 
   /**