dd4d51af |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 4.7 | |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | |
9 | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
11 | | under the terms of the GNU Affero General Public License | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
13 | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
17 | | See the GNU Affero General Public License for more details. | |
18 | | | |
19 | | You should have received a copy of the GNU Affero General Public | |
20 | | License and the CiviCRM Licensing Exception along | |
21 | | with this program; if not, contact CiviCRM LLC | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
25 | +--------------------------------------------------------------------+ |
26 | */ |
27 | |
dd4d51af |
28 | /** |
29 | * Class CRM_Core_BAO_SchemaHandlerTest. |
7181119b |
30 | * |
31 | * These tests create and drop indexes on the civicrm_uf_join table. The indexes |
32 | * being added and dropped we assume will never exist. |
acb109b7 |
33 | * @group headless |
dd4d51af |
34 | */ |
35 | class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase { |
36 | |
37 | /** |
38 | * Test creating an index. |
39 | * |
40 | * We want to be sure it creates an index and exits gracefully if the index |
41 | * already exists. |
42 | */ |
43 | public function testCreateIndex() { |
44 | $tables = array('civicrm_uf_join' => array('weight')); |
45 | CRM_Core_BAO_SchemaHandler::createIndexes($tables); |
46 | CRM_Core_BAO_SchemaHandler::createIndexes($tables); |
47 | $dao = CRM_Core_DAO::executeQuery("SHOW INDEX FROM civicrm_uf_join"); |
48 | $count = 0; |
49 | |
50 | while ($dao->fetch()) { |
51 | if ($dao->Column_name == 'weight') { |
52 | $count++; |
53 | CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_uf_join DROP INDEX " . $dao->Key_name); |
54 | } |
55 | } |
56 | $this->assertEquals(1, $count); |
57 | } |
58 | |
7181119b |
59 | /** |
60 | * Test creating an index. |
61 | * |
62 | * We want to be sure it creates an index and exits gracefully if the index |
63 | * already exists. |
64 | */ |
65 | public function testCombinedIndex() { |
66 | $tables = array('civicrm_uf_join' => array('weight')); |
67 | CRM_Core_BAO_SchemaHandler::createIndexes($tables); |
68 | |
69 | $tables = array('civicrm_uf_join' => array(array('weight', 'module'))); |
70 | CRM_Core_BAO_SchemaHandler::createIndexes($tables); |
71 | $dao = CRM_Core_DAO::executeQuery("SHOW INDEX FROM civicrm_uf_join"); |
72 | $weightCount = 0; |
73 | $combinedCount = 0; |
74 | $indexes = array(); |
75 | |
76 | while ($dao->fetch()) { |
77 | if ($dao->Column_name == 'weight') { |
78 | $weightCount++; |
79 | $indexes[$dao->Key_name] = $dao->Key_name; |
80 | } |
81 | if ($dao->Column_name == 'module') { |
82 | $combinedCount++; |
83 | $this->assertArrayHasKey($dao->Key_name, $indexes); |
84 | } |
85 | |
86 | } |
87 | foreach (array_keys($indexes) as $index) { |
88 | CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_uf_join DROP INDEX " . $index); |
89 | } |
90 | $this->assertEquals(2, $weightCount); |
91 | } |
92 | |
50969d52 |
93 | /** |
94 | * Test the drop index if exists function for a non-existent index. |
95 | */ |
96 | public function testCHeckIndexNotExists() { |
97 | $this->assertFalse(CRM_Core_BAO_SchemaHandler::checkIfIndexExists('civicrm_contact', 'magic_button')); |
98 | } |
99 | |
100 | /** |
101 | * Test the drop index if exists function for a non-existent index. |
102 | */ |
103 | public function testCHeckIndexExists() { |
104 | $this->assertTrue(CRM_Core_BAO_SchemaHandler::checkIfIndexExists('civicrm_contact', 'index_hash')); |
105 | } |
106 | |
107 | /** |
108 | * Test the drop index if exists function for a non-existent index. |
109 | */ |
110 | public function testDropIndexNoneExists() { |
111 | CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_contact', 'magic_button'); |
112 | } |
113 | |
114 | /** |
115 | * Test the drop index if exists function. |
116 | */ |
117 | public function testDropIndexExists() { |
118 | CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_contact', 'index_hash'); |
119 | $this->assertFalse(CRM_Core_BAO_SchemaHandler::checkIfIndexExists('civicrm_contact', 'index_hash')); |
120 | |
121 | // Recreate it to clean up after the test. |
122 | CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contact' => array('hash'))); |
123 | } |
124 | |
dd4d51af |
125 | } |