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