Merge branch '4.5' into 4.6
[civicrm-core.git] / tests / phpunit / CRM / Core / InnoDBIndexerTest.php
CommitLineData
fa5bb5cf
TO
1<?php
2
3require_once 'CiviTest/CiviUnitTestCase.php';
4
5/**
6 * Class CRM_Core_InnoDBIndexerTest
7 */
8class CRM_Core_InnoDBIndexerTest extends CiviUnitTestCase {
00be9182 9 public function tearDown() {
fa5bb5cf
TO
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
00be9182 19 public function testHasDeclaredIndex() {
fa5bb5cf
TO
20 $idx = new CRM_Core_InnoDBIndexer(TRUE, array(
21 'civicrm_contact' => array(
22 array('first_name', 'last_name'),
21dfd5f5 23 array('foo'),
fa5bb5cf
TO
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 */
00be9182 45 public function testDisabled() {
fa5bb5cf
TO
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");
0db6c3e1
TO
56 }
57 catch (Exception $e) {
fa5bb5cf
TO
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 */
00be9182 65 public function testEnabled() {
fa5bb5cf
TO
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
7fe37828
EM
81 /**
82 * @return mixed
83 */
00be9182 84 public function supportsFts() {
fa5bb5cf
TO
85 return version_compare(CRM_Core_DAO::singleValueQuery('SELECT VERSION()'), '5.6.0', '>=');
86 }
96025800 87
fa5bb5cf 88}