Merge pull request #4940 from mlutfy/crm15824
[civicrm-core.git] / tests / phpunit / CRM / Utils / QueryFormatterTest.php
1 <?php
2
3 require_once 'CiviTest/CiviUnitTestCase.php';
4
5 /**
6 * Class CRM_Utils_QueryFormatterTest
7 */
8 class CRM_Utils_QueryFormatterTest extends CiviUnitTestCase {
9
10 public function dataProvider() {
11 // Array(0=>$inputText, 1=>$language, 2=>$options, 3=>$expectedText).
12 $cases = array();
13
14 $cases[] = array(
15 'first second',
16 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
17 CRM_Utils_QueryFormatter::MODE_NONE,
18 '%first second%',
19 );
20 $cases[] = array(
21 'first second',
22 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
23 CRM_Utils_QueryFormatter::MODE_PHRASE,
24 '%first second%',
25 );
26 $cases[] = array(
27 'first second',
28 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
29 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
30 '%first second%',
31 );
32 $cases[] = array(
33 'first second',
34 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
35 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
36 '%first%second%',
37 );
38 $cases[] = array(
39 'first second',
40 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
41 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
42 '%first%second%',
43 );
44
45 $cases[] = array(
46 'first second',
47 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
48 CRM_Utils_QueryFormatter::MODE_NONE,
49 'first second',
50 );
51 $cases[] = array(
52 'first second',
53 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
54 CRM_Utils_QueryFormatter::MODE_PHRASE,
55 '"first second"',
56 );
57 $cases[] = array(
58 'first second',
59 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
60 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
61 '"*first second*"',
62 );
63 $cases[] = array(
64 'first second',
65 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
66 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
67 '*first* *second*',
68 );
69 $cases[] = array(
70 'first second',
71 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
72 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
73 'first* second*',
74 );
75
76 $cases[] = array(
77 'first second',
78 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
79 CRM_Utils_QueryFormatter::MODE_NONE,
80 '+first +second',
81 );
82 $cases[] = array(
83 'first second',
84 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
85 CRM_Utils_QueryFormatter::MODE_PHRASE,
86 '+"first second"',
87 );
88 $cases[] = array(
89 'first second',
90 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
91 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
92 '+"*first second*"',
93 );
94 $cases[] = array(
95 'first second',
96 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
97 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
98 '+*first* +*second*',
99 );
100 $cases[] = array(
101 'first second',
102 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
103 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
104 '+first* +second*',
105 );
106
107 $cases[] = array(
108 'first second',
109 CRM_Utils_QueryFormatter::LANG_SOLR,
110 CRM_Utils_QueryFormatter::MODE_NONE,
111 'first second',
112 );
113 $cases[] = array(
114 'first second',
115 CRM_Utils_QueryFormatter::LANG_SOLR,
116 CRM_Utils_QueryFormatter::MODE_PHRASE,
117 '"first second"',
118 );
119 $cases[] = array(
120 'first second',
121 CRM_Utils_QueryFormatter::LANG_SOLR,
122 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
123 '"*first second*"',
124 );
125 $cases[] = array(
126 'first second',
127 CRM_Utils_QueryFormatter::LANG_SOLR,
128 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
129 '*first* *second*',
130 );
131 $cases[] = array(
132 'first second',
133 CRM_Utils_QueryFormatter::LANG_SOLR,
134 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
135 'first* second*',
136 );
137
138 // If user supplies wildcards, then ignore mode.
139 foreach (array(
140 CRM_Utils_QueryFormatter::MODE_NONE,
141 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
142 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
143 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
144 ) as $mode) {
145 $cases[] = array(
146 'first% second',
147 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
148 $mode,
149 'first% second',
150 );
151 $cases[] = array(
152 'first% second',
153 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
154 $mode,
155 'first* second',
156 );
157 $cases[] = array(
158 'first% second',
159 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
160 $mode,
161 '+first* +second',
162 );
163 $cases[] = array(
164 'first% second',
165 CRM_Utils_QueryFormatter::LANG_SOLR,
166 $mode,
167 'first* second',
168 );
169 $cases[] = array(
170 'first second%',
171 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
172 $mode,
173 'first second%',
174 );
175 $cases[] = array(
176 'first second%',
177 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
178 $mode,
179 'first second*',
180 );
181 $cases[] = array(
182 'first second%',
183 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
184 $mode,
185 '+first +second*',
186 );
187 $cases[] = array(
188 'first second%',
189 CRM_Utils_QueryFormatter::LANG_SOLR,
190 $mode,
191 'first second*',
192 );
193 }
194
195 return $cases;
196 }
197
198 /**
199 * Test format.
200 *
201 * @param string $text
202 * @param string $language
203 * @param string $mode
204 * @param string $expectedText
205 *
206 * @dataProvider dataProvider
207 */
208 public function testFormat($text, $language, $mode, $expectedText) {
209 $formatter = new CRM_Utils_QueryFormatter($mode);
210 $actualText = $formatter->format($text, $language);
211 $this->assertEquals($expectedText, $actualText);
212 }
213 }