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