dev/core#2046 Fix api to call Address.create now it is standardised
authoreileen <emcnaughton@wikimedia.org>
Wed, 7 Oct 2020 00:45:23 +0000 (13:45 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 7 Oct 2020 01:09:21 +0000 (14:09 +1300)
Switches  api from calling Address.add to Address.create as part of BAO standardisation on create.

This has no material impact - just standardisation

Civi/Api4/Generic/Traits/DAOActionTrait.php
api/v3/Address.php
tests/phpunit/api/v4/Action/IsPrimaryTest.php [new file with mode: 0644]

index 3982e4dba8d3bdc85f899acee03c0e48bdb7cf2e..b7b4c2d3ac09d86ecf15458bec7ae578abc8226d 100644 (file)
@@ -134,7 +134,7 @@ trait DAOActionTrait {
       }
 
       if ($this->getEntityName() === 'Address') {
-        $createResult = $baoName::add($item, $this->fixAddress);
+        $createResult = $baoName::$method($item, $this->fixAddress);
       }
       elseif (method_exists($baoName, $method)) {
         $createResult = $baoName::$method($item);
index 0417091a89a4d632d453d2b330259a894390815e..f7988a249ae096f934698d5a02f2b43fd9f3c89d 100644 (file)
@@ -68,7 +68,7 @@ function civicrm_api3_address_create($params) {
    * Create array for BAO (expects address params in as an
    * element in array 'address'
    */
-  $addressBAO = CRM_Core_BAO_Address::add($params, $params['fix_address']);
+  $addressBAO = CRM_Core_BAO_Address::create($params, $params['fix_address']);
   if (empty($addressBAO)) {
     return civicrm_api3_create_error("Address is not created or updated ");
   }
diff --git a/tests/phpunit/api/v4/Action/IsPrimaryTest.php b/tests/phpunit/api/v4/Action/IsPrimaryTest.php
new file mode 100644 (file)
index 0000000..b99afd0
--- /dev/null
@@ -0,0 +1,184 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+
+namespace api\v4\Action;
+
+use Civi\Api4\Email;
+use Civi\Api4\IM;
+use Civi\Api4\Phone;
+use Civi\Api4\Address;
+use Civi\Api4\OpenID;
+use api\v4\UnitTestCase;
+
+/**
+ * @group headless
+ */
+class IsPrimaryTest extends UnitTestCase {
+
+  /**
+   * Test that creating a location entity or deleting one re-assigns is_primary correctly.
+   */
+  public function testPrimaryHandling() {
+    $contactID = self::createEntity(['type' => 'Individual'])['id'];
+    // Create an entity of each type.
+    Email::create()->setValues(['email' => 'b@example.com', 'contact_id' => $contactID])->execute();
+    Phone::create()->setValues(['phone' => '123', 'contact_id' => $contactID])->execute();
+    IM::create()->setValues(['name' => 'im', 'contact_id' => $contactID])->execute();
+    OpenID::create()->setValues(['openid' => 'openid', 'contact_id' => $contactID])->execute();
+    $firstAddressID = Address::create()->setValues(['street_name' => '1 sesame street', 'contact_id' => $contactID])->execute()->first()['id'];
+    $this->assertValidLocations();
+
+    // Create an second entity of each type - demoting the first
+    Email::create()->setValues(['email' => 'b2@example.com', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
+    Phone::create()->setValues(['phone' => '1232', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
+    IM::create()->setValues(['name' => 'im2', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
+    OpenID::create()->setValues(['openid' => 'openid2', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
+    Address::create()->setValues(['street_name' => '2 sesame street', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
+    $this->assertValidLocations();
+    $this->assertNotEquals($firstAddressID, Address::get()->addWhere('is_primary', '=', TRUE)->addWhere('contact_id', '=', $contactID)->execute()->first()['id']);
+
+    // Update all the non-primaries
+    // to is_primary TRUE.
+    Email::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
+    Phone::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
+    IM::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
+    OpenID::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
+    Address::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
+    $this->assertValidLocations();
+    $this->assertEquals($firstAddressID, Address::get()->addWhere('is_primary', '=', TRUE)->addWhere('contact_id', '=', $contactID)->execute()->first()['id']);
+
+    Email::delete()->addWhere('is_primary', '=', TRUE)->execute();
+    Phone::delete()->addWhere('is_primary', '=', TRUE)->execute();
+    IM::delete()->addWhere('is_primary', '=', TRUE)->execute();
+    OpenID::delete()->addWhere('is_primary', '=', TRUE)->execute();
+    Address::delete()->addWhere('is_primary', '=', TRUE)->execute();
+    $this->assertValidLocations();
+    $this->assertNotEquals($firstAddressID, Address::get()->addWhere('is_primary', '=', TRUE)->addWhere('contact_id', '=', $contactID)->execute()->first()['id']);
+
+  }
+
+  /**
+   * Check that all location entities have exactly one primary.
+   */
+  protected function assertValidLocations() {
+    $this->assertEquals(0, \CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) FROM
+
+(SELECT a1.contact_id
+FROM civicrm_address a1
+  LEFT JOIN civicrm_address a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE
+  a1.is_primary = 1
+  AND a2.id IS NOT NULL
+  AND a1.contact_id IS NOT NULL
+UNION
+SELECT a1.contact_id
+FROM civicrm_address a1
+       LEFT JOIN civicrm_address a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE a1.is_primary = 0
+  AND a2.id IS NULL
+  AND a1.contact_id IS NOT NULL
+
+UNION
+
+SELECT a1.contact_id
+FROM civicrm_email a1
+       LEFT JOIN civicrm_email a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE
+    a1.is_primary = 1
+  AND a2.id IS NOT NULL
+  AND a1.contact_id IS NOT NULL
+UNION
+SELECT a1.contact_id
+FROM civicrm_email a1
+       LEFT JOIN civicrm_email a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE a1.is_primary = 0
+  AND a2.id IS NULL
+  AND a1.contact_id IS NOT NULL
+
+UNION
+
+SELECT a1.contact_id
+FROM civicrm_phone a1
+       LEFT JOIN civicrm_phone a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE
+    a1.is_primary = 1
+  AND a2.id IS NOT NULL
+  AND a1.contact_id IS NOT NULL
+UNION
+SELECT a1.contact_id
+FROM civicrm_phone a1
+       LEFT JOIN civicrm_phone a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE a1.is_primary = 0
+  AND a2.id IS NULL
+  AND a1.contact_id IS NOT NULL
+
+UNION
+
+SELECT a1.contact_id
+FROM civicrm_im a1
+       LEFT JOIN civicrm_im a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE
+    a1.is_primary = 1
+  AND a2.id IS NOT NULL
+  AND a1.contact_id IS NOT NULL
+UNION
+SELECT a1.contact_id
+FROM civicrm_im a1
+       LEFT JOIN civicrm_im a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE a1.is_primary = 0
+  AND a2.id IS NULL
+  AND a1.contact_id IS NOT NULL
+
+UNION
+
+SELECT a1.contact_id
+FROM civicrm_openid a1
+       LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE (a1.is_primary = 1 AND a2.id IS NOT NULL)
+UNION
+
+SELECT a1.contact_id
+FROM civicrm_openid a1
+       LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE
+    a1.is_primary = 1
+  AND a2.id IS NOT NULL
+  AND a1.contact_id IS NOT NULL
+UNION
+SELECT a1.contact_id
+FROM civicrm_openid a1
+       LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
+  AND a1.contact_id = a2.contact_id
+WHERE a1.is_primary = 0
+  AND a2.id IS NULL
+  AND a1.contact_id IS NOT NULL) as primary_descrepancies
+    '));
+  }
+
+}