Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / SchemaHandlerTest.php
index 0666921f7f1a0f56bf78542cb1c79755b614b84b..ae788ba64f722146f792dcb1dc229e4a16e59b6d 100644 (file)
@@ -1,27 +1,11 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
- |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | 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       |
  +--------------------------------------------------------------------+
  */
 
  *
  * These tests create and drop indexes on the civicrm_uf_join table. The indexes
  * being added and dropped we assume will never exist.
+ *
  * @group headless
  */
 class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
 
+  /**
+   * Ensure any removed indices are put back.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function tearDown() {
+    parent::tearDown();
+    $this->callAPISuccess('System', 'updateindexes', []);
+  }
+
   /**
    * Test creating an index.
    *
@@ -44,13 +39,13 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
     $tables = ['civicrm_uf_join' => ['weight']];
     CRM_Core_BAO_SchemaHandler::createIndexes($tables);
     CRM_Core_BAO_SchemaHandler::createIndexes($tables);
-    $dao = CRM_Core_DAO::executeQuery("SHOW INDEX FROM civicrm_uf_join");
+    $dao = CRM_Core_DAO::executeQuery('SHOW INDEX FROM civicrm_uf_join');
     $count = 0;
 
     while ($dao->fetch()) {
-      if ($dao->Column_name == 'weight') {
+      if ($dao->Column_name === 'weight') {
         $count++;
-        CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_uf_join DROP INDEX " . $dao->Key_name);
+        CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_uf_join DROP INDEX ' . $dao->Key_name);
       }
     }
     $this->assertEquals(1, $count);
@@ -76,24 +71,22 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
 
     $tables = ['civicrm_uf_join' => [['weight', 'module']]];
     CRM_Core_BAO_SchemaHandler::createIndexes($tables);
-    $dao = CRM_Core_DAO::executeQuery("SHOW INDEX FROM civicrm_uf_join");
+    $dao = CRM_Core_DAO::executeQuery('SHOW INDEX FROM civicrm_uf_join');
     $weightCount = 0;
-    $combinedCount = 0;
     $indexes = [];
 
     while ($dao->fetch()) {
-      if ($dao->Column_name == 'weight') {
+      if ($dao->Column_name === 'weight') {
         $weightCount++;
         $indexes[$dao->Key_name] = $dao->Key_name;
       }
-      if ($dao->Column_name == 'module') {
-        $combinedCount++;
+      if ($dao->Column_name === 'module') {
         $this->assertArrayHasKey($dao->Key_name, $indexes);
       }
 
     }
     foreach (array_keys($indexes) as $index) {
-      CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_uf_join DROP INDEX " . $index);
+      CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_uf_join DROP INDEX ' . $index);
     }
     $this->assertEquals(2, $weightCount);
   }
@@ -148,7 +141,7 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
    * @dataProvider columnTests
    */
   public function testCheckIfColumnExists($tableName, $columnName) {
-    if ($columnName == 'xxxx') {
+    if ($columnName === 'xxxx') {
       $this->assertFalse(CRM_Core_BAO_SchemaHandler::checkIfFieldExists($tableName, $columnName));
     }
     else {
@@ -170,9 +163,12 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
    * Test to see if we can drop foreign key
    *
    * @dataProvider foreignKeyTests
+   *
+   * @param string $tableName
+   * @param string $key
    */
   public function testSafeDropForeignKey($tableName, $key) {
-    if ($key == 'FK_civicrm_mailing_recipients_id') {
+    if ($key === 'FK_civicrm_mailing_recipients_id') {
       $this->assertFalse(CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_recipients', $key));
     }
     else {
@@ -185,7 +181,7 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
    */
   public function testGetMissingIndices() {
     $missingIndices = CRM_Core_BAO_SchemaHandler::getMissingIndices();
-    $this->assertTrue(empty($missingIndices));
+    $this->assertEmpty($missingIndices);
   }
 
   /**
@@ -227,10 +223,15 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
 
   /**
    * Check there are no missing indices
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testReconcileMissingIndices() {
     CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contact DROP INDEX index_sort_name');
     $missingIndices = CRM_Core_BAO_SchemaHandler::getMissingIndices();
+    // Check the api also retrieves them.
+    $missingIndicesAPI = $this->callAPISuccess('System', 'getmissingindices', [])['values'];
+    $this->assertEquals($missingIndices, $missingIndicesAPI);
     $this->assertEquals([
       'civicrm_contact' => [
         [
@@ -243,7 +244,42 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
     ], $missingIndices);
     $this->callAPISuccess('System', 'updateindexes', []);
     $missingIndices = CRM_Core_BAO_SchemaHandler::getMissingIndices();
-    $this->assertTrue(empty($missingIndices));
+    $this->assertEmpty($missingIndices);
+  }
+
+  /**
+   * Check there are no missing indices
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testGetMissingIndicesWithTableFilter() {
+    CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contact DROP INDEX index_sort_name');
+    CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contribution DROP INDEX index_total_amount_receive_date');
+    $missingIndices = $this->callAPISuccess('System', 'getmissingindices', [])['values'];
+    $expected = [
+      'civicrm_contact' => [
+        [
+          'name' => 'index_sort_name',
+          'field' => ['sort_name'],
+          'localizable' => FALSE,
+          'sig' => 'civicrm_contact::0::sort_name',
+        ],
+      ],
+      'civicrm_contribution' => [
+        [
+          'name' => 'index_total_amount_receive_date',
+          'field' => ['total_amount', 'receive_date'],
+          'localizable' => FALSE,
+          'sig' => 'civicrm_contribution::0::total_amount::receive_date',
+        ],
+      ],
+    ];
+    $this->assertEquals($expected, $missingIndices);
+    $missingIndices = $this->callAPISuccess('System', 'getmissingindices', ['tables' => ['civicrm_contact']])['values'];
+    $this->assertEquals(['civicrm_contact' => $expected['civicrm_contact']], $missingIndices);
+    $this->callAPISuccess('System', 'updateindexes', ['tables' => 'civicrm_contribution']);
+    $missingIndices = $this->callAPISuccess('System', 'getmissingindices', [])['values'];
+    $this->assertEquals(['civicrm_contact' => $expected['civicrm_contact']], $missingIndices);
   }
 
   /**
@@ -320,7 +356,7 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
     // drop col1
     CRM_Core_DAO::executeQuery(CRM_Core_BAO_SchemaHandler::buildFieldChangeSql($alterParams, FALSE));
 
-    $create_table = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE civicrm_test_drop_column");
+    $create_table = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE civicrm_test_drop_column');
     while ($create_table->fetch()) {
       $this->assertNotContains('col1', $create_table->Create_Table);
       $this->assertContains('col2', $create_table->Create_Table);
@@ -330,7 +366,7 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
     $alterParams['name'] = 'col2';
     CRM_Core_DAO::executeQuery(CRM_Core_BAO_SchemaHandler::buildFieldChangeSql($alterParams, FALSE));
 
-    $create_table = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE civicrm_test_drop_column");
+    $create_table = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE civicrm_test_drop_column');
     while ($create_table->fetch()) {
       $this->assertNotContains('col2', $create_table->Create_Table);
     }
@@ -347,8 +383,8 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
       'type' => 'text',
     ];
     $sql = CRM_Core_BAO_SchemaHandler::buildFieldChangeSql($params, FALSE);
-    $this->assertEquals("ALTER TABLE big_table
-        ADD COLUMN `big_bob` text", trim($sql));
+    $this->assertEquals('ALTER TABLE big_table
+        ADD COLUMN `big_bob` text', trim($sql));
 
     $params['operation'] = 'modify';
     $params['comment'] = 'super big';