Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / EmailTest.php
index 28cefc25d5356a5e7cbd8c303069fe44a896f65c..7f2e413936aa5456a763fc4c627cdcd184801132 100644 (file)
@@ -1,37 +1,23 @@
 <?php
 /*
  +--------------------------------------------------------------------+
-| CiviCRM version 5                                                  |
-+--------------------------------------------------------------------+
-| Copyright CiviCRM LLC (c) 2004-2020                                |
-+--------------------------------------------------------------------+
-| This file is a part of CiviCRM.                                    |
-|                                                                    |
-| CiviCRM is free software; you can copy, modify, and distribute it  |
-| under the terms of the GNU Affero General Public License           |
-| Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
-|                                                                    |
-| CiviCRM is distributed in the hope that it will be useful, but     |
-| WITHOUT ANY WARRANTY; without even the implied warranty of         |
-| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
-| See the GNU Affero General Public License for more details.        |
-|                                                                    |
-| You should have received a copy of the GNU Affero General Public   |
-| License and the CiviCRM Licensing Exception along                  |
-| with this program; if not, contact CiviCRM LLC                     |
-| at info[AT]civicrm[DOT]org. If you have questions about the        |
-| GNU Affero General Public License or the licensing of CiviCRM,     |
-| see the CiviCRM license FAQ at http://civicrm.org/licensing        |
-+--------------------------------------------------------------------+
+ | 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       |
+ +--------------------------------------------------------------------+
  */
 
 /**
  * Class api_v3_EmailTest
+ *
  * @group headless
  */
 class api_v3_EmailTest extends CiviUnitTestCase {
   protected $_contactID;
   protected $_locationType;
+  protected $locationType2;
   protected $_entity;
   protected $_params;
 
@@ -42,7 +28,7 @@ class api_v3_EmailTest extends CiviUnitTestCase {
 
     $this->_contactID = $this->organizationCreate(NULL);
     $this->_locationType = $this->locationTypeCreate(NULL);
-    $this->_locationType2 = $this->locationTypeCreate([
+    $this->locationType2 = $this->locationTypeCreate([
       'name' => 'New Location Type 2',
       'vcard_name' => 'New Location Type 2',
       'description' => 'Another Location Type',
@@ -59,8 +45,12 @@ class api_v3_EmailTest extends CiviUnitTestCase {
   }
 
   /**
+   * Test create email.
+   *
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testCreateEmail($version) {
     $this->_apiversion = $version;
@@ -69,13 +59,13 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $get = $this->callAPISuccess('email', 'get', [
       'location_type_id' => $this->_locationType->id,
     ]);
-    $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
+    $this->assertEquals(0, $get['count'], 'Contact not successfully deleted.');
 
     $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
     $this->assertEquals(1, $result['count']);
     $this->assertNotNull($result['id']);
     $this->assertNotNull($result['values'][$result['id']]['id']);
-    $delresult = $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
+    $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
   }
 
   /**
@@ -83,7 +73,9 @@ class api_v3_EmailTest extends CiviUnitTestCase {
    * the LocationType default
    *
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testCreateEmailDefaultLocation($version) {
     $this->_apiversion = $version;
@@ -98,8 +90,11 @@ class api_v3_EmailTest extends CiviUnitTestCase {
    * If a new email is set to is_primary the prev should no longer be.
    *
    * If is_primary is not set then it should become is_primary is no others exist
+   *
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testCreateEmailPrimaryHandlingChangeToPrimary($version) {
     $this->_apiversion = $version;
@@ -108,7 +103,7 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $email1 = $this->callAPISuccess('email', 'create', $params);
     //now we check & make sure it has been set to primary
     $expected = 1;
-    $check = $this->callAPISuccess('email', 'getcount', [
+    $this->callAPISuccess('email', 'getcount', [
       'is_primary' => 1,
       'id' => $email1['id'],
     ],
@@ -118,12 +113,14 @@ class api_v3_EmailTest extends CiviUnitTestCase {
 
   /**
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testCreateEmailPrimaryHandlingChangeExisting($version) {
     $this->_apiversion = $version;
-    $email1 = $this->callAPISuccess('email', 'create', $this->_params);
-    $email2 = $this->callAPISuccess('email', 'create', $this->_params);
+    $this->callAPISuccess('email', 'create', $this->_params);
+    $this->callAPISuccess('email', 'create', $this->_params);
     $check = $this->callAPISuccess('email', 'getcount', [
       'is_primary' => 1,
       'contact_id' => $this->_contactID,
@@ -144,7 +141,9 @@ class api_v3_EmailTest extends CiviUnitTestCase {
 
   /**
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testGetEmail($version) {
     $this->_apiversion = $version;
@@ -155,12 +154,14 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $this->assertEquals($get['count'], 1);
     $get = $this->callAPISuccess('email', 'create', $this->_params + ['debug' => 1, 'action' => 'get']);
     $this->assertEquals($get['count'], 1);
-    $delresult = $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
+    $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
   }
 
   /**
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testDeleteEmail($version) {
     $this->_apiversion = $version;
@@ -176,7 +177,7 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $get = $this->callAPISuccess('email', 'get', [
       'location_type_id' => $this->_locationType->id,
     ]);
-    $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
+    $this->assertEquals(0, $get['count'], 'email already exists');
 
     //create one
     $create = $this->callAPISuccess('email', 'create', $params);
@@ -186,12 +187,14 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $get = $this->callAPISuccess('email', 'get', [
       'location_type_id' => $this->_locationType->id,
     ]);
-    $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
+    $this->assertEquals(0, $get['count'], 'Contact not successfully deleted');
   }
 
   /**
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testReplaceEmail($version) {
     $this->_apiversion = $version;
@@ -199,7 +202,7 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $get = $this->callAPISuccess('email', 'get', [
       'contact_id' => $this->_contactID,
     ]);
-    $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
+    $this->assertEquals(0, $get['count'], 'email already exists');
 
     // initialize email list with three emails at loc #1 and two emails at loc #2
     $replace1Params = [
@@ -221,12 +224,12 @@ class api_v3_EmailTest extends CiviUnitTestCase {
           'is_primary' => 0,
         ],
         [
-          'location_type_id' => $this->_locationType2->id,
+          'location_type_id' => $this->locationType2->id,
           'email' => '2-1@example.com',
           'is_primary' => 0,
         ],
         [
-          'location_type_id' => $this->_locationType2->id,
+          'location_type_id' => $this->locationType2->id,
           'email' => '2-2@example.com',
           'is_primary' => 0,
         ],
@@ -239,7 +242,7 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $get = $this->callAPISuccess('email', 'get', [
       'contact_id' => $this->_contactID,
     ]);
-    $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
+    $this->assertEquals(5, $get['count'], 'Incorrect email count');
 
     // replace the subset of emails in location #1, but preserve location #2
     $replace2Params = [
@@ -260,15 +263,15 @@ class api_v3_EmailTest extends CiviUnitTestCase {
       'contact_id' => $this->_contactID,
       'location_type_id' => $this->_locationType->id,
     ]);
-    $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
+    $this->assertEquals(1, $get['count'], 'Incorrect email count');
 
     // check emails at location #2 -- preserve the original two
     $get = $this->callAPISuccess('email', 'get', [
       'contact_id' => $this->_contactID,
-      'location_type_id' => $this->_locationType2->id,
+      'location_type_id' => $this->locationType2->id,
     ]);
 
-    $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
+    $this->assertEquals(2, $get['count'], 'Incorrect email count');
 
     // replace the set of emails with an empty set
     $replace3Params = [
@@ -284,12 +287,14 @@ class api_v3_EmailTest extends CiviUnitTestCase {
       'contact_id' => $this->_contactID,
     ]);
     $this->assertAPISuccess($get);
-    $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__);
+    $this->assertEquals(0, $get['count'], 'Incorrect email count');
   }
 
   /**
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testReplaceEmailsInChain($version) {
     $this->_apiversion = $version;
@@ -299,9 +304,9 @@ class api_v3_EmailTest extends CiviUnitTestCase {
       'contact_id' => $this->_contactID,
     ]);
     $this->assertAPISuccess($get);
-    $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
-    $description = "Demonstrates use of Replace in a nested API call.";
-    $subfile = "NestedReplaceEmail";
+    $this->assertEquals(0, $get['count'], 'email already exists');
+    $description = 'Demonstrates use of Replace in a nested API call.';
+    $subfile = 'NestedReplaceEmail';
     // initialize email list with three emails at loc #1 and two emails at loc #2
     $getReplace1Params = [
 
@@ -324,12 +329,12 @@ class api_v3_EmailTest extends CiviUnitTestCase {
             'is_primary' => 0,
           ],
           [
-            'location_type_id' => $this->_locationType2->id,
+            'location_type_id' => $this->locationType2->id,
             'email' => '2-1@example.com',
             'is_primary' => 0,
           ],
           [
-            'location_type_id' => $this->_locationType2->id,
+            'location_type_id' => $this->locationType2->id,
             'email' => '2-2@example.com',
             'is_primary' => 0,
           ],
@@ -343,7 +348,7 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $get = $this->callAPISuccess('email', 'get', [
       'contact_id' => $this->_contactID,
     ]);
-    $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
+    $this->assertEquals(5, $get['count'], 'Incorrect email count');
 
     // replace the subset of emails in location #1, but preserve location #2
     $getReplace2Params = [
@@ -368,19 +373,21 @@ class api_v3_EmailTest extends CiviUnitTestCase {
       'location_type_id' => $this->_locationType->id,
     ]);
 
-    $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
+    $this->assertEquals(1, $get['count'], 'Incorrect email count');
 
     // check emails at location #2 -- preserve the original two
     $get = $this->callAPISuccess('email', 'get', [
       'contact_id' => $this->_contactID,
-      'location_type_id' => $this->_locationType2->id,
+      'location_type_id' => $this->locationType2->id,
     ]);
-    $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
+    $this->assertEquals(2, $get['count'], 'Incorrect email count');
   }
 
   /**
    * @param int $version
+   *
    * @dataProvider versionThreeAndFour
+   * @throws \CRM_Core_Exception
    */
   public function testReplaceEmailWithId($version) {
     $this->_apiversion = $version;
@@ -433,9 +440,12 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email']);
   }
 
+  /**
+   * Test updates affecting on hold emails.
+   *
+   * @throws \CRM_Core_Exception
+   */
   public function testEmailOnHold() {
-    $params = [];
-    $params_change = [];
     $params = [
       'contact_id' => $this->_contactID,
       'email' => 'api@a-team.com',
@@ -464,7 +474,25 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result_change['values'][$result_change['id']]['reset_date'])));
     $this->assertEmpty($result_change['values'][$result_change['id']]['hold_date']);
 
-    $delresult = $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
+    $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
+  }
+
+  /**
+   * Test setting a bulk email unsets others on the contact.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testSetBulkEmail() {
+    $individualID = $this->individualCreate([]);
+    $email = $this->callAPISuccessGetSingle('Email', ['contact_id' => $individualID]);
+    $this->assertEquals(0, $email['is_bulkmail']);
+    $this->callAPISuccess('Email', 'create', ['id' => $email['id'], 'is_bulkmail' => 1]);
+    $email = $this->callAPISuccessGetSingle('Email', ['contact_id' => $individualID]);
+    $this->assertEquals(1, $email['is_bulkmail']);
+    $email2 = $this->callAPISuccess('Email', 'create', ['contact_id' => $individualID, 'email' => 'mail@Example.com', 'is_bulkmail' => 1]);
+    $emails = $this->callAPISuccess('Email', 'get', ['contact_id' => $individualID])['values'];
+    $this->assertEquals(0, $emails[$email['id']]['is_bulkmail']);
+    $this->assertEquals(1, $emails[$email2['id']]['is_bulkmail']);
   }
 
 }