INFRA-132 - tests/ - phpcbf
[civicrm-core.git] / tests / phpunit / CRM / Utils / QueryFormatterTest.php
CommitLineData
ea74069c
TO
1<?php
2
3require_once 'CiviTest/CiviUnitTestCase.php';
4
5/**
6 * Class CRM_Utils_QueryFormatterTest
7 */
8class CRM_Utils_QueryFormatterTest extends CiviUnitTestCase {
9
00be9182 10 public function dataProvider() {
ea74069c
TO
11 $cases = array(); // array(0=>$inputText, 1=>$language, 2=>$options, 3=>$expectedText)
12
6c6e6187
TO
13 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_LIKE, CRM_Utils_QueryFormatter::MODE_NONE, '%first second%');
14 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_LIKE, CRM_Utils_QueryFormatter::MODE_PHRASE, '%first second%');
15 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_LIKE, CRM_Utils_QueryFormatter::MODE_WILDPHRASE, '%first second%');
16 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_LIKE, CRM_Utils_QueryFormatter::MODE_WILDWORDS, '%first%second%');
ea74069c
TO
17 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_LIKE, CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX, '%first%second%');
18
6c6e6187
TO
19 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTS, CRM_Utils_QueryFormatter::MODE_NONE, 'first second');
20 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTS, CRM_Utils_QueryFormatter::MODE_PHRASE, '"first second"');
21 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTS, CRM_Utils_QueryFormatter::MODE_WILDPHRASE, '"*first second*"');
22 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTS, CRM_Utils_QueryFormatter::MODE_WILDWORDS, '*first* *second*');
ea74069c
TO
23 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTS, CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX, 'first* second*');
24
6c6e6187
TO
25 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL, CRM_Utils_QueryFormatter::MODE_NONE, '+first +second');
26 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL, CRM_Utils_QueryFormatter::MODE_PHRASE, '+"first second"');
27 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL, CRM_Utils_QueryFormatter::MODE_WILDPHRASE, '+"*first second*"');
28 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL, CRM_Utils_QueryFormatter::MODE_WILDWORDS, '+*first* +*second*');
3196b7a5
TO
29 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL, CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX, '+first* +second*');
30
6c6e6187
TO
31 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SOLR, CRM_Utils_QueryFormatter::MODE_NONE, 'first second');
32 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SOLR, CRM_Utils_QueryFormatter::MODE_PHRASE, '"first second"');
33 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SOLR, CRM_Utils_QueryFormatter::MODE_WILDPHRASE, '"*first second*"');
34 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SOLR, CRM_Utils_QueryFormatter::MODE_WILDWORDS, '*first* *second*');
ea74069c
TO
35 $cases[] = array('first second', CRM_Utils_QueryFormatter::LANG_SOLR, CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX, 'first* second*');
36
37 // if user supplies wildcards, then ignore mode
38 foreach (array(CRM_Utils_QueryFormatter::MODE_NONE, CRM_Utils_QueryFormatter::MODE_WILDPHRASE, CRM_Utils_QueryFormatter::MODE_WILDWORDS, CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX) as $mode) {
6c6e6187
TO
39 $cases[] = array('first% second', CRM_Utils_QueryFormatter::LANG_SQL_LIKE, $mode, 'first% second');
40 $cases[] = array('first% second', CRM_Utils_QueryFormatter::LANG_SQL_FTS, $mode, 'first* second');
41 $cases[] = array('first% second', CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL, $mode, '+first* +second');
42 $cases[] = array('first% second', CRM_Utils_QueryFormatter::LANG_SOLR, $mode, 'first* second');
43 $cases[] = array('first second%', CRM_Utils_QueryFormatter::LANG_SQL_LIKE, $mode, 'first second%');
44 $cases[] = array('first second%', CRM_Utils_QueryFormatter::LANG_SQL_FTS, $mode, 'first second*');
45 $cases[] = array('first second%', CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL, $mode, '+first +second*');
46 $cases[] = array('first second%', CRM_Utils_QueryFormatter::LANG_SOLR, $mode, 'first second*');
ea74069c
TO
47 }
48
49 return $cases;
50 }
51
52 /**
53 * @param $text
54 * @param $language
55 * @param $mode
56 * @param $expectedText
57 * @dataProvider dataProvider
58 */
00be9182 59 public function testFormat($text, $language, $mode, $expectedText) {
ea74069c
TO
60 $formatter = new CRM_Utils_QueryFormatter($mode);
61 $actualText = $formatter->format($text, $language);
62 $this->assertEquals($expectedText, $actualText);
63 }
ef10e0b5 64}