protected $_text = NULL;
- protected $_table = NULL;
+ protected $_table;
- protected $_tableName = NULL;
+ protected $tableName;
+
+ /**
+ * @return mixed
+ */
+ public function getTableName() {
+ return $this->tableName;
+ }
protected $_entityIDTableName = NULL;
*
* @var array|null
*/
- protected $_limitRowClause = NULL;
+ protected $_limitRowClause;
/**
* Limit detail clause.
/**
* 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);
}
}
- 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',
$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'];
}
}
$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;
}
$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 )
$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
$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
}
// 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)} ";
}
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 {
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}");
}
}
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}");
}
}
$sql = "
SELECT $select
-FROM {$this->_tableName} contact_a
+FROM {$this->tableName} contact_a
{$this->toLimit($this->_limitRowClause)}
";
return $sql;
/**
* @return array
*/
- public function setDefaultValues() {
+ public function setDefaultValues(): array {
return [];
}
* 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;
}
--- /dev/null
+<?php
+
+namespace Civi\Searches;
+
+use Civi\Test;
+use Civi\Test\HeadlessInterface;
+use Civi\Test\HookInterface;
+use Civi\Test\TransactionalInterface;
+use CRM_Contact_Form_Search_Custom_FullText;
+use CRM_Core_Config;
+use CRM_Core_DAO;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * FIXME - Add test description.
+ *
+ * Tips:
+ * - With HookInterface, you may implement CiviCRM hooks directly in the test
+ * class. Simply create corresponding functions (e.g. "hook_civicrm_post(...)"
+ * or similar).
+ * - With TransactionalInterface, any data changes made by setUp() or
+ * test****() functions will rollback automatically -- as long as you don't
+ * manipulate schema or truncate tables. If this test needs to manipulate
+ * schema or truncate tables, then either: a. Do all that using setupHeadless()
+ * and Civi\Test. b. Disable TransactionalInterface, and handle all
+ * setup/teardown yourself.
+ *
+ * @group headless
+ */
+class FullTextTest extends TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
+
+ use Test\ContactTestTrait;
+ use Test\Api3TestTrait;
+
+ /**
+ * Civi\Test has many helpers, like install(), uninstall(), sql(), and
+ * sqlFile(). See:
+ * https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
+ */
+ public function setUpHeadless(): Test\CiviEnvBuilder {
+ return Test::headless()
+ ->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.');
+ }
+
+}
+++ /dev/null
-<?php
-/*
- +--------------------------------------------------------------------+
- | 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 |
- +--------------------------------------------------------------------+
- */
-
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- */
-class CRM_Contact_Form_Search_Custom_FullTextTest extends CiviUnitTestCase {
-
- /**
- * @var array
- */
- protected $_tablesToTruncate = [
- 'civicrm_acl_contact_cache',
- ];
-
- /**
- * Test ACL contacts are filtered properly.
- */
- public function testfilterACLContacts() {
- $this->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.');
- }
-
-}