Merge pull request #14878 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / EmailTest.php
index b2abc69c66a5275a3a3c7714762d6e09e9324a63..765150d615e812041b247c953f0e5de1ab17fd28 100644 (file)
@@ -5,10 +5,11 @@
  * @group headless
  */
 class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
+
   public function setUp() {
     parent::setUp();
 
-    $this->quickCleanup(array('civicrm_contact', 'civicrm_email'));
+    $this->quickCleanup(['civicrm_contact', 'civicrm_email']);
   }
 
   /**
@@ -17,13 +18,13 @@ class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
   public function testAdd() {
     $contactId = $this->individualCreate();
 
-    $params = array();
-    $params = array(
+    $params = [];
+    $params = [
       'email' => 'jane.doe@example.com',
       'is_primary' => 1,
       'location_type_id' => 1,
       'contact_id' => $contactId,
-    );
+    ];
 
     CRM_Core_BAO_Email::add($params);
 
@@ -33,13 +34,13 @@ class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
 
     // Now call add() to modify an existing email address
 
-    $params = array();
-    $params = array(
+    $params = [];
+    $params = [
       'id' => $emailId,
       'contact_id' => $contactId,
       'is_bulkmail' => 1,
       'on_hold' => 1,
-    );
+    ];
 
     CRM_Core_BAO_Email::add($params);
 
@@ -57,12 +58,12 @@ class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
   public function testHoldEmail() {
     $contactId = $this->individualCreate();
 
-    $params = array(
+    $params = [
       'email' => 'jane.doe@example.com',
       'is_primary' => 1,
       'location_type_id' => 1,
       'contact_id' => $contactId,
-    );
+    ];
 
     CRM_Core_BAO_Email::add($params);
 
@@ -70,13 +71,13 @@ class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
       'Database check for created email address.'
     );
 
-    // Now call add() to update on_hold=true and check record state
-    $params = array();
-    $params = array(
+    // Now call add() to update on_hold=1 ("On Hold Bounce") and check record state
+    $params = [];
+    $params = [
       'id' => $emailId,
       'contact_id' => $contactId,
       'on_hold' => 1,
-    );
+    ];
 
     CRM_Core_BAO_Email::add($params);
 
@@ -94,14 +95,38 @@ class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
       'Check if on_hold=1 in updated email record.'
     );
 
-    // Now call add() with on_hold=false and verify that reset_date is set.
-    $params = array();
-    $params = array(
+    // Now call add() to update on_hold=2 ("On Hold Opt-out") and check record state
+    $params = [];
+    $params = [
       'id' => $emailId,
       'contact_id' => $contactId,
-      'on_hold' => 'null',
+      'on_hold' => 2,
+    ];
+
+    CRM_Core_BAO_Email::add($params);
+
+    // Use assertDBNotNull to get back value of hold_date and check that it's in the current year.
+    // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
+    $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
+      'Retrieve hold_date from the updated email record.'
     );
 
+    $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
+      'Compare hold_date (' . $holdDate . ') in DB to current year.'
+    );
+
+    $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 2,
+      'Check if on_hold=2 in updated email record.'
+    );
+
+    // Now call add() with on_hold=null (not on hold) and verify that reset_date is set.
+    $params = [];
+    $params = [
+      'id' => $emailId,
+      'contact_id' => $contactId,
+      'on_hold' => 'null',
+    ];
+
     CRM_Core_BAO_Email::add($params);
     $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 0,
       'Check if on_hold=0 in updated email record.'
@@ -127,13 +152,13 @@ class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
    * AllEmails() method - get all emails for our contact, with primary email first
    */
   public function testAllEmails() {
-    $contactParams = array(
+    $contactParams = [
       'first_name' => 'Alan',
       'last_name' => 'Smith',
       'email' => 'alan.smith1@example.com',
-      'api.email.create.0' => array('email' => 'alan.smith2@example.com', 'location_type_id' => 'Home'),
-      'api.email.create.1' => array('email' => 'alan.smith3@example.com', 'location_type_id' => 'Main'),
-    );
+      'api.email.create.0' => ['email' => 'alan.smith2@example.com', 'location_type_id' => 'Home'],
+      'api.email.create.1' => ['email' => 'alan.smith3@example.com', 'location_type_id' => 'Main'],
+    ];
 
     $contactId = $this->individualCreate($contactParams);