From 0e7da67b5ef12dc6040379cc30f0736e1ee13884 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 18 Feb 2023 12:10:06 +1300 Subject: [PATCH] php8.2 support in test class ActivitySearchTest --- .../CRM/Contact/BAO/ActivitySearchTest.php | 119 ++++++++++-------- 1 file changed, 64 insertions(+), 55 deletions(-) diff --git a/tests/phpunit/CRM/Contact/BAO/ActivitySearchTest.php b/tests/phpunit/CRM/Contact/BAO/ActivitySearchTest.php index 8f0f521b55..9660aa43a9 100644 --- a/tests/phpunit/CRM/Contact/BAO/ActivitySearchTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ActivitySearchTest.php @@ -31,6 +31,8 @@ * Include class definitions */ +use Civi\Api4\OptionValue; + /** * Test APIv3 civicrm_activity_* functions * @@ -40,33 +42,31 @@ */ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { - protected $_contactID; - - protected $_params; + /** + * @var int + */ + protected $contactID; - protected $test_activity_type_value; + /** + * @var array + */ + protected $params; /** * Test setup for every test. - * - * Connect to the database, truncate the tables that will be used - * and redirect stdin to a temporary file */ public function setUp(): void { - // Connect to the database parent::setUp(); - - $this->_contactID = $this->individualCreate(); - //create activity types - $activityTypes = $this->callAPISuccess('option_value', 'create', [ + $this->contactID = $this->individualCreate(); + $activityTypes = $this->callAPISuccess('OptionValue', 'create', [ 'option_group_id' => 2, 'name' => 'Test activity type', 'label' => 'Test activity type', 'sequential' => 1, ]); - $this->test_activity_type_id = $activityTypes['id']; - $this->_params = [ - 'source_contact_id' => $this->_contactID, + + $this->params = [ + 'source_contact_id' => $this->contactID, 'activity_type_id' => $activityTypes['values'][0]['value'], 'subject' => 'test activity type id', 'activity_date_time' => '2011-06-02 14:36:13', @@ -76,14 +76,14 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'location' => 'Pennsylvania', 'details' => 'a test activity', ]; - // create a logged in USER since the code references it for source_contact_id + // Create a logged in USER since the code references it for source_contact_id. $this->createLoggedInUser(); } /** * Tears down the fixture, for example, closes a network connection. * - * This method is called after a test is executed. + * @throws \CRM_Core_Exception */ public function tearDown(): void { $tablesToTruncate = [ @@ -94,19 +94,18 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'civicrm_entity_tag', ]; $this->quickCleanup($tablesToTruncate, TRUE); - $type = $this->callAPISuccess('optionValue', 'get', ['id' => $this->test_activity_type_id]); - if (!empty($type['count'])) { - $this->callAPISuccess('option_value', 'delete', ['id' => $this->test_activity_type_id]); - } + OptionValue::delete()->addWhere('name', '=', 'Test activity type')->execute(); parent::tearDown(); } /** * Test that activity.get api works when filtering on subject. + * + * @throws \CRM_Core_Exception */ - public function testSearchBySubjectOnly() { + public function testSearchBySubjectOnly(): void { $subject = 'test activity ' . __FUNCTION__; - $params = $this->_params; + $params = $this->params; $params['subject'] = $subject; $this->callAPISuccess('Activity', 'Create', $params); @@ -116,26 +115,28 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'activity_option' => 3, ], 'expected_count' => 1, - 'expected_contact' => [$this->_contactID], + 'expected_contact' => [$this->contactID], ]; $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value'])); - [$select, $from, $where, $having] = $query->query(); + [, $from, $where] = $query->query(); $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll(); foreach ($groupContacts as $key => $value) { $groupContacts[$key] = $value['id']; } - $this->assertEquals($case['expected_count'], count($groupContacts)); + $this->assertCount($case['expected_count'], $groupContacts); $this->checkArrayEquals($case['expected_contact'], $groupContacts); } /** * Test that activity.get api works when filtering on subject. + * + * @throws \CRM_Core_Exception */ - public function testSearchBySubjectBoth() { + public function testSearchBySubjectBoth(): void { $subject = 'test activity ' . __FUNCTION__; - $params = $this->_params; + $params = $this->params; $params['subject'] = $subject; - $activity = $this->callAPISuccess('Activity', 'Create', $params); + $this->callAPISuccess('Activity', 'Create', $params); $case = [ 'form_value' => [ @@ -143,26 +144,28 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'activity_option' => 6, ], 'expected_count' => 1, - 'expected_contact' => [$this->_contactID], + 'expected_contact' => [$this->contactID], ]; $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value'])); - [$select, $from, $where, $having] = $query->query(); + [, $from, $where] = $query->query(); $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll(); foreach ($groupContacts as $key => $value) { $groupContacts[$key] = $value['id']; } - $this->assertEquals($case['expected_count'], count($groupContacts)); + $this->assertCount($case['expected_count'], $groupContacts); $this->checkArrayEquals($case['expected_contact'], $groupContacts); } /** * Test that activity.get api works when filtering on subject. + * + * @throws \CRM_Core_Exception */ - public function testSearchByDetailsOnly() { + public function testSearchByDetailsOnly(): void { $details = 'test activity ' . __FUNCTION__; - $params = $this->_params; + $params = $this->params; $params['details'] = $details; - $activity = $this->callAPISuccess('Activity', 'Create', $params); + $this->callAPISuccess('Activity', 'Create', $params); $case = [ 'form_value' => [ @@ -170,26 +173,28 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'activity_option' => 2, ], 'expected_count' => 1, - 'expected_contact' => [$this->_contactID], + 'expected_contact' => [$this->contactID], ]; $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value'])); - [$select, $from, $where, $having] = $query->query(); + [, $from, $where] = $query->query(); $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll(); foreach ($groupContacts as $key => $value) { $groupContacts[$key] = $value['id']; } - $this->assertEquals($case['expected_count'], count($groupContacts)); + $this->assertCount($case['expected_count'], $groupContacts); $this->checkArrayEquals($case['expected_contact'], $groupContacts); } /** * Test that activity.get api works when filtering on details. + * + * @throws \CRM_Core_Exception */ - public function testSearchByDetailsBoth() { + public function testSearchByDetailsBoth(): void { $details = 'test activity ' . __FUNCTION__; - $params = $this->_params; + $params = $this->params; $params['details'] = $details; - $activity = $this->callAPISuccess('Activity', 'Create', $params); + $this->callAPISuccess('Activity', 'Create', $params); $case = [ 'form_value' => [ @@ -197,29 +202,31 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'activity_option' => 6, ], 'expected_count' => 1, - 'expected_contact' => [$this->_contactID], + 'expected_contact' => [$this->contactID], ]; $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value'])); - [$select, $from, $where, $having] = $query->query(); + [, $from, $where] = $query->query(); $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll(); foreach ($groupContacts as $key => $value) { $groupContacts[$key] = $value['id']; } - $this->assertEquals($case['expected_count'], count($groupContacts)); + $this->assertCount($case['expected_count'], $groupContacts); $this->checkArrayEquals($case['expected_contact'], $groupContacts); } /** * Test that activity.get api works when filtering on bare tags (i.e. tags * not part of a tagset). + * + * @throws \CRM_Core_Exception */ - public function testSearchByBareTags() { + public function testSearchByBareTags(): void { $tag = $this->callAPISuccess('Tag', 'create', [ 'name' => 'a1', 'used_for' => 'Activities', ]); $subject = 'test activity ' . __FUNCTION__; - $params = $this->_params; + $params = $this->params; $params['subject'] = $subject; $activity = $this->callAPISuccess('Activity', 'Create', $params); $this->callAPISuccess('EntityTag', 'create', [ @@ -235,10 +242,10 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'activity_tags' => (string) $tag['id'], ], 'expected_count' => 1, - 'expected_contact' => [$this->_contactID], + 'expected_contact' => [$this->contactID], ]; $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value'])); - [$select, $from, $where, $having] = $query->query(); + [, $from, $where] = $query->query(); $expectedQill = [ 0 => [ @@ -251,7 +258,7 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { foreach ($groupContacts as $key => $value) { $groupContacts[$key] = $value['id']; } - $this->assertEquals($case['expected_count'], count($groupContacts)); + $this->assertCount($case['expected_count'], $groupContacts); $this->checkArrayEquals($case['expected_contact'], $groupContacts); // Clean up. Don't want to use teardown to wipe the table since then @@ -261,8 +268,10 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { /** * Test that activity.get api works when filtering on a tag in a tagset. + * + * @throws \CRM_Core_Exception */ - public function testSearchByTagset() { + public function testSearchByTagset(): void { $tagset = $this->callAPISuccess('Tag', 'create', [ 'name' => 'activity tagset', 'is_tagset' => 1, @@ -274,7 +283,7 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'parent_id' => 'activity tagset', ]); $subject = 'test activity ' . __FUNCTION__; - $params = $this->_params; + $params = $this->params; $params['subject'] = $subject; $activity = $this->callAPISuccess('Activity', 'Create', $params); $this->callAPISuccess('EntityTag', 'create', [ @@ -286,13 +295,13 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { 'form_value' => [ // If multiple tags the array element value is a comma-separated string // and then the qill looks like "IN a OR b". - "activity_taglist" => [$tagset['id'] => (string) $tag['id']], + 'activity_taglist' => [$tagset['id'] => (string) $tag['id']], ], 'expected_count' => 1, - 'expected_contact' => [$this->_contactID], + 'expected_contact' => [$this->contactID], ]; $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value'])); - [$select, $from, $where, $having] = $query->query(); + [, $from, $where] = $query->query(); $expectedQill = [ 0 => [ @@ -305,7 +314,7 @@ class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase { foreach ($groupContacts as $key => $value) { $groupContacts[$key] = $value['id']; } - $this->assertEquals($case['expected_count'], count($groupContacts)); + $this->assertCount($case['expected_count'], $groupContacts); $this->checkArrayEquals($case['expected_contact'], $groupContacts); // Clean up. Don't want to use teardown to wipe the table since then -- 2.25.1