Merge pull request #15326 from totten/master-headfoot-2
[civicrm-core.git] / tests / phpunit / CRM / Core / InnoDBIndexerTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_InnoDBIndexerTest
5 * @group headless
6 */
7 class CRM_Core_InnoDBIndexerTest extends CiviUnitTestCase {
8
9 public function tearDown() {
10 // May or may not cleanup well if there's a bug in the indexer.
11 // This is better than nothing -- and better than duplicating the
12 // cleanup logic.
13 $idx = new CRM_Core_InnoDBIndexer(FALSE, []);
14 $idx->fixSchemaDifferences();
15
16 parent::tearDown();
17 }
18
19 public function testHasDeclaredIndex() {
20 $idx = new CRM_Core_InnoDBIndexer(TRUE, [
21 'civicrm_contact' => [
22 ['first_name', 'last_name'],
23 ['foo'],
24 ],
25 'civicrm_email' => [
26 ['whiz'],
27 ],
28 ]);
29
30 $this->assertTrue($idx->hasDeclaredIndex('civicrm_contact', ['first_name', 'last_name']));
31 $this->assertTrue($idx->hasDeclaredIndex('civicrm_contact', ['last_name', 'first_name']));
32 // not sure if this is right behavior
33 $this->assertTrue($idx->hasDeclaredIndex('civicrm_contact', ['first_name']));
34 // not sure if this is right behavior
35 $this->assertTrue($idx->hasDeclaredIndex('civicrm_contact', ['last_name']));
36 $this->assertTrue($idx->hasDeclaredIndex('civicrm_contact', ['foo']));
37 $this->assertFalse($idx->hasDeclaredIndex('civicrm_contact', ['whiz']));
38
39 $this->assertFalse($idx->hasDeclaredIndex('civicrm_email', ['first_name', 'last_name']));
40 $this->assertFalse($idx->hasDeclaredIndex('civicrm_email', ['foo']));
41 $this->assertTrue($idx->hasDeclaredIndex('civicrm_email', ['whiz']));
42 }
43
44 /**
45 * When disabled, there is no FTS index, so queries that rely on FTS index fail.
46 */
47 public function testDisabled() {
48 $idx = new CRM_Core_InnoDBIndexer(FALSE, [
49 'civicrm_contact' => [
50 ['first_name', 'last_name'],
51 ],
52 ]);
53 $idx->fixSchemaDifferences();
54
55 try {
56 CRM_Core_DAO::executeQuery('SELECT id FROM civicrm_contact WHERE MATCH(first_name,last_name) AGAINST ("joe")');
57 $this->fail("Missed expected exception");
58 }
59 catch (Exception $e) {
60 $this->assertTrue(TRUE, 'Received expected exception');
61 }
62 }
63
64 /**
65 * When enabled, the FTS index is created, so queries that rely on FTS work.
66 */
67 public function testEnabled() {
68 if (!$this->supportsFts()) {
69 $this->markTestSkipped("Local installation of InnoDB does not support FTS.");
70 return;
71 }
72
73 $idx = new CRM_Core_InnoDBIndexer(TRUE, [
74 'civicrm_contact' => [
75 ['first_name', 'last_name'],
76 ],
77 ]);
78 $idx->fixSchemaDifferences();
79
80 CRM_Core_DAO::executeQuery('SELECT id FROM civicrm_contact WHERE MATCH(first_name,last_name) AGAINST ("joe")');
81 }
82
83 /**
84 * @return mixed
85 */
86 public function supportsFts() {
87 return version_compare(CRM_Core_DAO::singleValueQuery('SELECT VERSION()'), '5.6.0', '>=');
88 }
89
90 }