Php8-proof SMSCommonTest
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 16 Jun 2023 06:14:44 +0000 (18:14 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 16 Jun 2023 06:14:44 +0000 (18:14 +1200)
tests/phpunit/CRM/Contact/Form/Task/SMSCommonTest.php

index f2739cb97b29f55f1239693d66d01a70ca6fa052..a34ba3946b2cd5529cda22d7ed6cb5ad80e2afbd 100644 (file)
  */
 class CRM_Contact_Form_Task_SMSCommonTest extends CiviUnitTestCase {
 
-  protected $_smsRecipients = [];
-
   /**
-   * Set up for tests.
-   *
-   * @throws \CRM_Core_Exception
+   * Set up SMS recipients.
    */
   protected function setUp(): void {
     parent::setUp();
     $mobile_type_id = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Phone', 'phone_type_id', 'Mobile');
     $phone_type_id = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Phone', 'phone_type_id', 'Phone');
 
-    $contact1 = $this->individualCreate([
+    $this->individualCreate([
       'first_name' => 'First',
       'last_name' => 'Person',
       'do_not_sms' => 0,
@@ -38,8 +34,8 @@ class CRM_Contact_Form_Task_SMSCommonTest extends CiviUnitTestCase {
           'phone' => '1111111111',
         ],
       ],
-    ]);
-    $contact2 = $this->individualCreate([
+    ], 'first');
+    $this->individualCreate([
       'first_name' => 'Second',
       'last_name' => 'Person',
       'do_not_sms' => 0,
@@ -56,8 +52,8 @@ class CRM_Contact_Form_Task_SMSCommonTest extends CiviUnitTestCase {
           'phone' => '2222222222',
         ],
       ],
-    ]);
-    $contact3 = $this->individualCreate([
+    ], 'second');
+    $this->individualCreate([
       'first_name' => 'Third',
       'last_name' => 'Person',
       'do_not_sms' => 0,
@@ -69,8 +65,8 @@ class CRM_Contact_Form_Task_SMSCommonTest extends CiviUnitTestCase {
           'is_primary' => 0,
         ],
       ],
-    ]);
-    $contact4 = $this->individualCreate([
+    ], 'third');
+    $this->individualCreate([
       'first_name' => 'Fourth',
       'last_name' => 'Person',
       'do_not_sms' => 1,
@@ -81,8 +77,8 @@ class CRM_Contact_Form_Task_SMSCommonTest extends CiviUnitTestCase {
           'phone' => '4444444444',
         ],
       ],
-    ]);
-    $contact5 = $this->individualCreate([
+    ], 'fourth');
+    $this->individualCreate([
       'first_name' => 'Fifth',
       'last_name' => 'Person',
       'do_not_sms' => 0,
@@ -94,61 +90,50 @@ class CRM_Contact_Form_Task_SMSCommonTest extends CiviUnitTestCase {
           'phone' => '5555555555',
         ],
       ],
-    ]);
-    // Track the contacts that should get an SMS and which
-    // number they should receive it.
-    $this->_smsRecipients = [
-      $contact1 => "1111111111",
-      $contact2 => "2222222222",
-      $contact3 => "3333333333",
-    ];
-
-    $this->_contactIds = [
-      $contact1,
-      $contact2,
-      $contact3,
-      $contact4,
-      $contact5,
-    ];
+    ], 'fifth');
   }
 
   /**
    * Test to ensure SMS Activity QuickForm displays the right phone numbers.
-   *
-   * @throws \CRM_Core_Exception
-   * @throws \Civi\API\Exception\UnauthorizedException
    */
-  public function testQuickFormMobileNumbersDisplay() {
+  public function testQuickFormMobileNumbersDisplay(): void {
     $form = $this->getFormObject('CRM_Core_Form');
-    $form->_contactIds = $this->_contactIds;
+    $form->_contactIds = array_values($this->ids['Contact']);
     $form->_single = FALSE;
     CRM_Contact_Form_Task_SMSCommon::buildQuickForm($form);
     $contacts = json_decode($form->get_template_vars('toContact'));
     $smsRecipientsActual = [];
+
+    $phoneNumbers = [
+      $this->ids['Contact']['first'] => 1111111111,
+      $this->ids['Contact']['second'] => 2222222222,
+      $this->ids['Contact']['third'] => 3333333333,
+    ];
+
     foreach ($contacts as $contact) {
       $id = $contact->id;
       $ret = preg_match('/^([0-9]+)::([0-9]+)/', $id, $matches);
       // id is in the format: contact_id::phone_number, e.g.: 5::2222222222
-      $this->assertEquals(1, $ret, "Failed to extract the mobile number and contact id.");
+      $this->assertEquals(1, $ret, 'Failed to extract the mobile number and contact id.');
       $contact_id = $matches[1];
       $phone_number = $matches[2];
       // Check if we are supposed to send an SMS to this contact.
-      if (array_key_exists($contact_id, $this->_smsRecipients)) {
+      if (array_key_exists($contact_id, $phoneNumbers)) {
         // We are supposed to send an SMS to this contact, now make sure we have the right phone number.
-        $this->assertEquals($phone_number, $this->_smsRecipients[$contact_id], "Returned incorrect mobile number in SMS send quick form.");
+        $this->assertEquals($phone_number, $phoneNumbers[$contact_id], "Returned incorrect mobile number in SMS send quick form.");
         $smsRecipientsActual[] = $contact_id;
       }
       else {
         // We are not supposed to send this contact an email.
-        $this->assertTrue(FALSE, "We should not be sending an SMS to contact_id: $contact_id.");
+        $this->fail("We should not be sending an SMS to contact_id: $contact_id.");
       }
     }
 
     // Make sure we sent to all the contacts.
     sort($smsRecipientsActual);
-    $smsRecipientsIntended = array_keys($this->_smsRecipients);
+    $smsRecipientsIntended = array_keys($phoneNumbers);
     sort($smsRecipientsIntended);
-    $this->assertEquals($smsRecipientsActual, $smsRecipientsIntended, "We did not send an SMS to all the contacts.");
+    $this->assertEquals($smsRecipientsIntended, $smsRecipientsActual, 'We did not send an SMS to all the contacts.');
   }
 
 }