Php 8.2 fix undefined property, move test to appropriate extension
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 10 Jan 2023 21:47:18 +0000 (10:47 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 10 Jan 2023 22:03:45 +0000 (11:03 +1300)
Civi/Test/ContactTestTrait.php
ext/legacycustomsearches/CRM/Contact/Form/Search/Custom/FullText.php
ext/legacycustomsearches/tests/phpunit/Civi/Searches/FullTextTest.php [new file with mode: 0644]
tests/phpunit/CRM/Contact/Form/Search/Custom/FullTextTest.php [deleted file]

index 80b3a5b48b0a3a0b1bda62de7d9dc11b6cfee6f6..0242c8380122e13892e1d9bcac9a2fd5dbd0364b 100644 (file)
@@ -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)
index f3a9fd78daa59be48a813db415396c7aa6d20c5f..286819413797d45eb9377c950bc63a14f2127cd5 100644 (file)
@@ -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 (file)
index 0000000..bf2e9cd
--- /dev/null
@@ -0,0 +1,80 @@
+<?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.');
+  }
+
+}
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 (file)
index db7048e..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-<?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.');
-  }
-
-}