Skip to content

Commit

Permalink
Php 8.2 fix undefined property, move test to appropriate extension
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jan 10, 2023
1 parent a7c3919 commit ce677b2
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 95 deletions.
2 changes: 2 additions & 0 deletions Civi/Test/ContactTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
trait ContactTestTrait {

public $apiversion = 4;

/**
* Emulate a logged in user since certain functions use that.
* value to store a record in the DB (like activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -119,13 +126,15 @@ public function __construct(&$formValues) {
/**
* 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);
Expand All @@ -148,9 +157,9 @@ public function initialize() {
}
}

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',
Expand Down Expand Up @@ -209,33 +218,29 @@ public function buildTempTable() {
$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'];
}
}
Expand All @@ -244,9 +249,9 @@ public function fillTable() {
$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;
}

Expand All @@ -262,7 +267,7 @@ public function filterACLContacts() {

$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 )
Expand All @@ -271,7 +276,7 @@ public function filterACLContacts() {

$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
Expand All @@ -281,7 +286,7 @@ public function filterACLContacts() {

$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
Expand Down Expand Up @@ -366,7 +371,7 @@ public function summary() {
}

// 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)} ";
}
Expand All @@ -377,7 +382,7 @@ public function summary() {
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 {
Expand Down Expand Up @@ -430,7 +435,7 @@ public function count() {
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}");
}
}

Expand All @@ -449,7 +454,7 @@ public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL
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}");
}
}

Expand Down Expand Up @@ -477,7 +482,7 @@ public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs

$sql = "
SELECT $select
FROM {$this->_tableName} contact_a
FROM {$this->tableName} contact_a
{$this->toLimit($this->_limitRowClause)}
";
return $sql;
Expand Down Expand Up @@ -509,7 +514,7 @@ public function templateFile() {
/**
* @return array
*/
public function setDefaultValues() {
public function setDefaultValues(): array {
return [];
}

Expand All @@ -525,16 +530,16 @@ public function alterRow(&$row) {
* 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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.');
}
}
63 changes: 0 additions & 63 deletions tests/phpunit/CRM/Contact/Form/Search/Custom/FullTextTest.php

This file was deleted.

0 comments on commit ce677b2

Please sign in to comment.