From 084c034236352224e8b2ab05006f1e3e0e2a304e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 11 Jan 2023 10:47:18 +1300 Subject: [PATCH] Php 8.2 fix undefined property, move test to appropriate extension --- Civi/Test/ContactTestTrait.php | 7 ++ .../Contact/Form/Search/Custom/FullText.php | 69 ++++++++-------- .../phpunit/Civi/Searches/FullTextTest.php | 80 +++++++++++++++++++ .../Form/Search/Custom/FullTextTest.php | 63 --------------- 4 files changed, 124 insertions(+), 95 deletions(-) create mode 100644 ext/legacycustomsearches/tests/phpunit/Civi/Searches/FullTextTest.php delete mode 100644 tests/phpunit/CRM/Contact/Form/Search/Custom/FullTextTest.php diff --git a/Civi/Test/ContactTestTrait.php b/Civi/Test/ContactTestTrait.php index 80b3a5b48b..0242c83801 100644 --- a/Civi/Test/ContactTestTrait.php +++ b/Civi/Test/ContactTestTrait.php @@ -15,6 +15,13 @@ namespace Civi\Test; */ trait ContactTestTrait { + /** + * API version to use for any api calls. + * + * @var int + */ + public $apiversion = 4; + /** * Emulate a logged in user since certain functions use that. * value to store a record in the DB (like activity) diff --git a/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/FullText.php b/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/FullText.php index f3a9fd78da..2868194137 100644 --- a/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/FullText.php +++ b/ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/FullText.php @@ -29,9 +29,16 @@ class CRM_Contact_Form_Search_Custom_FullText extends CRM_Contact_Form_Search_Cu protected $_text = NULL; - protected $_table = NULL; + protected $_table; - protected $_tableName = NULL; + protected $tableName; + + /** + * @return mixed + */ + public function getTableName() { + return $this->tableName; + } protected $_entityIDTableName = NULL; @@ -53,7 +60,7 @@ class CRM_Contact_Form_Search_Custom_FullText extends CRM_Contact_Form_Search_Cu * * @var array|null */ - protected $_limitRowClause = NULL; + protected $_limitRowClause; /** * Limit detail clause. @@ -119,13 +126,15 @@ class CRM_Contact_Form_Search_Custom_FullText extends CRM_Contact_Form_Search_Cu /** * Get a value from $formValues. If missing, get it from the request. * - * @param $formValues - * @param $field + * @param array $formValues + * @param string $field * @param $type * @param null $default + * * @return mixed|null + * @throws \CRM_Core_Exception */ - public function getFieldValue($formValues, $field, $type, $default = NULL) { + public function getFieldValue(array $formValues, string $field, $type, $default = NULL) { $value = $formValues[$field] ?? NULL; if (!$value) { return CRM_Utils_Request::retrieve($field, $type, CRM_Core_DAO::$_nullObject, FALSE, $default); @@ -148,9 +157,9 @@ class CRM_Contact_Form_Search_Custom_FullText extends CRM_Contact_Form_Search_Cu } } - public function buildTempTable() { + public function buildTempTable(): void { $table = CRM_Utils_SQL_TempTable::build()->setCategory('custom')->setMemory(); - $this->_tableName = $table->getName(); + $this->tableName = $table->getName(); $this->_tableFields = [ 'id' => 'int unsigned NOT NULL AUTO_INCREMENT', @@ -209,33 +218,29 @@ class CRM_Contact_Form_Search_Custom_FullText extends CRM_Contact_Form_Search_Cu $sql .= "$name $desc,\n"; } - $sql .= " + $sql .= ' PRIMARY KEY ( id ) -"; +'; $table->createWithColumns($sql); $entityIdTable = CRM_Utils_SQL_TempTable::build()->setCategory('custom')->setMemory(); $this->_entityIDTableName = $entityIdTable->getName(); - $sql = " + $sql = ' id int unsigned NOT NULL AUTO_INCREMENT, entity_id int unsigned NOT NULL, UNIQUE INDEX unique_entity_id ( entity_id ), PRIMARY KEY ( id ) -"; +'; $entityIdTable->createWithColumns($sql); - - if (!empty($this->_formValues['is_unit_test'])) { - $this->_tableNameForTest = $this->_tableName; - } } - public function fillTable() { + public function fillTable(): void { /** @var CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery $partialQuery */ foreach ($this->_partialQueries as $partialQuery) { if (!$this->_table || $this->_table == $partialQuery->getName()) { if ($partialQuery->isActive()) { - $result = $partialQuery->fillTempTable($this->_text, $this->_entityIDTableName, $this->_tableName, $this->_limitClause, $this->_limitDetailClause); + $result = $partialQuery->fillTempTable($this->_text, $this->_entityIDTableName, $this->tableName, $this->_limitClause, $this->_limitDetailClause); $this->_foundRows[$partialQuery->getName()] = $result['count']; } } @@ -244,9 +249,9 @@ class CRM_Contact_Form_Search_Custom_FullText extends CRM_Contact_Form_Search_Cu $this->filterACLContacts(); } - public function filterACLContacts() { + public function filterACLContacts(): void { if (CRM_Core_Permission::check('view all contacts')) { - CRM_Core_DAO::executeQuery("DELETE FROM {$this->_tableName} WHERE contact_id IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)"); + CRM_Core_DAO::executeQuery("DELETE FROM {$this->tableName} WHERE contact_id IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)"); return; } @@ -262,7 +267,7 @@ class CRM_Contact_Form_Search_Custom_FullText extends CRM_Contact_Form_Search_Cu $sql = " DELETE t.* -FROM {$this->_tableName} t +FROM {$this->tableName} t WHERE NOT EXISTS ( SELECT c.contact_id FROM civicrm_acl_contact_cache c WHERE c.user_id = %1 AND t.contact_id = c.contact_id ) @@ -271,7 +276,7 @@ WHERE NOT EXISTS ( SELECT c.contact_id $sql = " DELETE t.* -FROM {$this->_tableName} t +FROM {$this->tableName} t WHERE t.table_name = 'Activity' AND NOT EXISTS ( SELECT c.contact_id FROM civicrm_acl_contact_cache c @@ -281,7 +286,7 @@ WHERE t.table_name = 'Activity' AND $sql = " DELETE t.* -FROM {$this->_tableName} t +FROM {$this->tableName} t WHERE t.table_name = 'Activity' AND NOT EXISTS ( SELECT c.contact_id FROM civicrm_acl_contact_cache c @@ -366,7 +371,7 @@ WHERE t.table_name = 'Activity' AND } // now iterate through the table and add entries to the relevant section - $sql = "SELECT * FROM {$this->_tableName}"; + $sql = "SELECT * FROM {$this->tableName}"; if ($this->_table) { $sql .= " {$this->toLimit($this->_limitRowClause)} "; } @@ -377,7 +382,7 @@ WHERE t.table_name = 'Activity' AND while ($dao->fetch()) { $row = []; foreach ($this->_tableFields as $name => $dontCare) { - if ($name != 'activity_type_id') { + if ($name !== 'activity_type_id') { $row[$name] = $dao->$name; } else { @@ -430,7 +435,7 @@ WHERE t.table_name = 'Activity' AND return $this->_foundRows[$this->_table]; } else { - return CRM_Core_DAO::singleValueQuery("SELECT count(id) FROM {$this->_tableName}"); + return CRM_Core_DAO::singleValueQuery("SELECT count(id) FROM {$this->tableName}"); } } @@ -449,7 +454,7 @@ WHERE t.table_name = 'Activity' AND return $this->all($offset, $rowcount, $sort, FALSE, TRUE); } else { - return CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM {$this->_tableName}"); + return CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM {$this->tableName}"); } } @@ -477,7 +482,7 @@ WHERE t.table_name = 'Activity' AND $sql = " SELECT $select -FROM {$this->_tableName} contact_a +FROM {$this->tableName} contact_a {$this->toLimit($this->_limitRowClause)} "; return $sql; @@ -509,7 +514,7 @@ FROM {$this->_tableName} contact_a /** * @return array */ - public function setDefaultValues() { + public function setDefaultValues(): array { return []; } @@ -525,16 +530,16 @@ FROM {$this->_tableName} contact_a * SQL * @see CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery::toLimit */ - public function toLimit($limit) { + public function toLimit($limit): string { if (is_array($limit)) { - list ($limit, $offset) = $limit; + [$limit, $offset] = $limit; } if (empty($limit)) { return ''; } $result = "LIMIT {$limit}"; if ($offset) { - $result .= " OFFSET {$offset}"; + $result .= " OFFSET $offset"; } return $result; } diff --git a/ext/legacycustomsearches/tests/phpunit/Civi/Searches/FullTextTest.php b/ext/legacycustomsearches/tests/phpunit/Civi/Searches/FullTextTest.php new file mode 100644 index 0000000000..bf2e9cd789 --- /dev/null +++ b/ext/legacycustomsearches/tests/phpunit/Civi/Searches/FullTextTest.php @@ -0,0 +1,80 @@ +install(['legacycustomsearches']) + ->apply(); + } + + /** + * Test ACL contacts are filtered properly. + */ + public function testFilterACLContacts(): void { + $userId = $this->createLoggedInUser(); + // remove all permissions + CRM_Core_Config::singleton()->userPermissionClass->permissions = []; + + for ($i = 1; $i <= 10; $i++) { + $contactId = $this->individualCreate([], $i); + if ($i <= 5) { + $queryParams = [ + 1 => [$userId, 'Integer'], + 2 => [$contactId, 'Integer'], + ]; + CRM_Core_DAO::executeQuery("INSERT INTO civicrm_acl_contact_cache ( user_id, contact_id, operation ) VALUES(%1, %2, 'View')", $queryParams); + } + $contactIDs[$i] = $contactId; + } + + $formValues = ['component_mode' => 1, 'operator' => 1, 'is_unit_test' => 1]; + $fullText = new CRM_Contact_Form_Search_Custom_FullText($formValues); + $fullText->initialize(); + + //Assert that ACL contacts are filtered. + $queryParams = [1 => [$userId, 'Integer']]; + $whereClause = 'WHERE NOT EXISTS (SELECT c.contact_id + FROM civicrm_acl_contact_cache c + WHERE c.user_id = %1 AND t.contact_id = c.contact_id )'; + + $count = CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) FROM ' . $fullText->getTableName() . " t $whereClause", $queryParams); + $this->assertEmpty($count, 'ACL contacts are not removed.'); + } + +} diff --git a/tests/phpunit/CRM/Contact/Form/Search/Custom/FullTextTest.php b/tests/phpunit/CRM/Contact/Form/Search/Custom/FullTextTest.php deleted file mode 100644 index db7048ea32..0000000000 --- a/tests/phpunit/CRM/Contact/Form/Search/Custom/FullTextTest.php +++ /dev/null @@ -1,63 +0,0 @@ -quickCleanup($this->_tablesToTruncate); - - $userId = $this->createLoggedInUser(); - // remove all permissions - $config = CRM_Core_Config::singleton(); - $config->userPermissionClass->permissions = []; - - for ($i = 1; $i <= 10; $i++) { - $contactId = $this->individualCreate([], $i); - if ($i <= 5) { - $queryParams = [ - 1 => [$userId, 'Integer'], - 2 => [$contactId, 'Integer'], - ]; - CRM_Core_DAO::executeQuery("INSERT INTO civicrm_acl_contact_cache ( user_id, contact_id, operation ) VALUES(%1, %2, 'View')", $queryParams); - } - $contactIDs[$i] = $contactId; - } - - $formValues = ['component_mode' => 1, 'operator' => 1, 'is_unit_test' => 1]; - $fullText = new CRM_Contact_Form_Search_Custom_FullText($formValues); - $fullText->initialize(); - - //Assert that ACL contacts are filtered. - $queryParams = [1 => [$userId, 'Integer']]; - $whereClause = "WHERE NOT EXISTS (SELECT c.contact_id - FROM civicrm_acl_contact_cache c - WHERE c.user_id = %1 AND t.contact_id = c.contact_id )"; - - $count = CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM {$fullText->_tableNameForTest} t {$whereClause}", $queryParams); - $this->assertEmpty($count, 'ACL contacts are not removed.'); - } - -} -- 2.25.1