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