Ian province abbreviation patch - issue 724
[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
cd5823ae
EM
10 /**
11 * Generate data for tests to iterate through.
12 *
13 * @return array
14 */
00be9182 15 public function dataProvider() {
db7de9c1
EM
16 // Array(0=>$inputText, 1=>$language, 2=>$options, 3=>$expectedText).
17 $cases = array();
ea74069c 18
92915c55
TO
19 $cases[] = array(
20 'first second',
21 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
22 CRM_Utils_QueryFormatter::MODE_NONE,
57507ae6 23 '%first second%',
92915c55
TO
24 );
25 $cases[] = array(
26 'first second',
27 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
28 CRM_Utils_QueryFormatter::MODE_PHRASE,
57507ae6 29 '%first second%',
92915c55
TO
30 );
31 $cases[] = array(
32 'first second',
33 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
34 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
57507ae6 35 '%first second%',
92915c55
TO
36 );
37 $cases[] = array(
38 'first second',
39 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
40 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
57507ae6 41 '%first%second%',
92915c55
TO
42 );
43 $cases[] = array(
44 'first second',
45 CRM_Utils_QueryFormatter::LANG_SQL_LIKE,
46 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
57507ae6 47 '%first%second%',
92915c55
TO
48 );
49
50 $cases[] = array(
51 'first second',
52 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
53 CRM_Utils_QueryFormatter::MODE_NONE,
57507ae6 54 'first second',
92915c55
TO
55 );
56 $cases[] = array(
57 'first second',
58 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
59 CRM_Utils_QueryFormatter::MODE_PHRASE,
57507ae6 60 '"first second"',
92915c55
TO
61 );
62 $cases[] = array(
63 'first second',
64 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
65 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
57507ae6 66 '"*first second*"',
92915c55
TO
67 );
68 $cases[] = array(
69 'first second',
70 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
71 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
57507ae6 72 '*first* *second*',
92915c55
TO
73 );
74 $cases[] = array(
75 'first second',
76 CRM_Utils_QueryFormatter::LANG_SQL_FTS,
77 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
57507ae6 78 'first* second*',
92915c55
TO
79 );
80
81 $cases[] = array(
82 'first second',
83 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
84 CRM_Utils_QueryFormatter::MODE_NONE,
57507ae6 85 '+first +second',
92915c55
TO
86 );
87 $cases[] = array(
88 'first second',
89 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
90 CRM_Utils_QueryFormatter::MODE_PHRASE,
57507ae6 91 '+"first second"',
92915c55
TO
92 );
93 $cases[] = array(
94 'first second',
95 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
96 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
57507ae6 97 '+"*first second*"',
92915c55
TO
98 );
99 $cases[] = array(
100 'first second',
101 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
102 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
63e9c3fd 103 '+*first* +*second*',
92915c55
TO
104 );
105 $cases[] = array(
106 'first second',
107 CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL,
108 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
63e9c3fd 109 '+first* +second*',
92915c55
TO
110 );
111
112 $cases[] = array(
113 'first second',
114 CRM_Utils_QueryFormatter::LANG_SOLR,
115 CRM_Utils_QueryFormatter::MODE_NONE,
63e9c3fd 116 'first second',
92915c55
TO
117 );
118 $cases[] = array(
119 'first second',
120 CRM_Utils_QueryFormatter::LANG_SOLR,
121 CRM_Utils_QueryFormatter::MODE_PHRASE,
63e9c3fd 122 '"first second"',
92915c55
TO
123 );
124 $cases[] = array(
125 'first second',
126 CRM_Utils_QueryFormatter::LANG_SOLR,
127 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
63e9c3fd 128 '"*first second*"',
92915c55
TO
129 );
130 $cases[] = array(
131 'first second',
132 CRM_Utils_QueryFormatter::LANG_SOLR,
133 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
db7de9c1 134 '*first* *second*',
92915c55
TO
135 );
136 $cases[] = array(
137 'first second',
138 CRM_Utils_QueryFormatter::LANG_SOLR,
139 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
db7de9c1 140 'first* second*',
92915c55 141 );
ea74069c 142
63e9c3fd 143 // If user supplies wildcards, then ignore mode.
92915c55
TO
144 foreach (array(
145 CRM_Utils_QueryFormatter::MODE_NONE,
146 CRM_Utils_QueryFormatter::MODE_WILDPHRASE,
147 CRM_Utils_QueryFormatter::MODE_WILDWORDS,
db7de9c1 148 CRM_Utils_QueryFormatter::MODE_WILDWORDS_SUFFIX,
92915c55 149 ) as $mode) {
63e9c3fd
EM
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 );
ea74069c
TO
198 }
199
200 return $cases;
201 }
202
203 /**
567b2076
EM
204 * Test format.
205 *
63e9c3fd
EM
206 * @param string $text
207 * @param string $language
208 * @param string $mode
209 * @param string $expectedText
567b2076 210 *
ea74069c
TO
211 * @dataProvider dataProvider
212 */
00be9182 213 public function testFormat($text, $language, $mode, $expectedText) {
ea74069c
TO
214 $formatter = new CRM_Utils_QueryFormatter($mode);
215 $actualText = $formatter->format($text, $language);
216 $this->assertEquals($expectedText, $actualText);
217 }
96025800 218
ef10e0b5 219}