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