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