Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
10.66% covered (danger)
10.66%
29 / 272
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_search_native_test
10.66% covered (danger)
10.66%
29 / 272
66.67% covered (warning)
66.67%
4 / 6
53.63
0.00% covered (danger)
0.00%
0 / 1
 getDataSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUp
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
 keywords
0.00% covered (danger)
0.00%
0 / 186
0.00% covered (danger)
0.00%
0 / 1
2
 test_split_keywords
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 data_split_keywords_max
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 1
2
 test_split_max_keywords
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14require_once __DIR__ . '/../test_framework/phpbb_search_test_case.php';
15
16class phpbb_search_native_test extends phpbb_search_test_case
17{
18    protected $db;
19    protected $db_tools;
20    protected $search;
21
22    public function getDataSet()
23    {
24        return $this->createXMLDataSet(__DIR__ . '/fixtures/posts.xml');
25    }
26
27    protected function setUp(): void
28    {
29        global $phpbb_root_path, $phpEx, $config, $cache;
30
31        parent::setUp();
32
33        // dbal uses cache
34        $cache = $this->createMock('\phpbb\cache\service');
35        $language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
36        $user = $this->createMock('\phpbb\user');
37
38        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
39        $lang = new \phpbb\language\language($lang_loader);
40        $user = new \phpbb\user($lang, '\phpbb\datetime');
41
42        $this->db = $this->new_dbal();
43        $tools_factory = new \phpbb\db\tools\factory();
44        $this->db_tools = $tools_factory->get($this->new_doctrine_dbal());
45        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
46        $class = self::get_search_wrapper('\phpbb\search\backend\fulltext_native');
47        $config = new \phpbb\config\config([
48            'fulltext_native_min_chars' => 2,
49            'fulltext_native_max_chars' => 14,
50            'max_num_search_keywords' => 10,
51        ]);
52        $this->search = new $class($config, $this->db, $this->db_tools, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, SEARCH_WORDLIST_TABLE, SEARCH_WORDMATCH_TABLE, $phpbb_root_path, $phpEx);
53    }
54
55    public static function keywords()
56    {
57        return array(
58            // keywords
59            // terms
60            // ok
61            // must contain ids
62            // must not contain ids
63            // common words
64            array(
65                'foo',
66                'all',
67                true,
68                array(1),
69                array(),
70                array(),
71            ),
72            array(
73                'baaz*',
74                'all',
75                true,
76                array('\'baaz%\''),
77                array(),
78                array(),
79            ),
80            array(
81                'ba*az',
82                'all',
83                true,
84                array(4),
85                array(),
86                array(),
87            ),
88            array(
89                'ba*z',
90                'all',
91                true,
92                array(), // <= 3 chars after removing *
93                array(),
94                array(),
95            ),
96            array(
97                'baa* baaz*',
98                'all',
99                true,
100                array('\'baa%\'', 4),
101                array(),
102                array(),
103            ),
104            array(
105                'ba*z baa*',
106                'all',
107                true,
108                array('\'baa%\''), // baz is <= 3 chars, only baa* is left
109                array(),
110                array(),
111            ),
112            array(
113                'baaz* commonword',
114                'all',
115                true,
116                array('\'baaz%\''),
117                array(),
118                array('commonword'),
119            ),
120            array(
121                'foo bar',
122                'all',
123                true,
124                array(1, 2),
125                array(),
126                array(),
127            ),
128            // leading, trailing and multiple spaces
129            array(
130                '      foo    bar   ',
131                'all',
132                true,
133                array(1, 2),
134                array(),
135                array(),
136            ),
137            // words too short
138            array(
139                'f',
140                'all',
141                false,
142                null,
143                null,
144                // short words count as "common" words
145                array('f'),
146            ),
147            array(
148                'f o o',
149                'all',
150                false,
151                null,
152                null,
153                array('f', 'o', 'o'),
154            ),
155            array(
156                'f -o -o',
157                'all',
158                false,
159                null,
160                null,
161                array('f', 'o', 'o'),
162            ),
163            array(
164                'foo -bar',
165                'all',
166                true,
167                array(1),
168                array(2),
169                array(),
170            ),
171            // all negative
172            array(
173                '-foo',
174                'all',
175                true,
176                array(),
177                array(1),
178                array(),
179            ),
180            array(
181                '-foo -bar',
182                'all',
183                true,
184                array(),
185                array(1, 2),
186                array(),
187            ),
188            array(
189                'foo -foo',
190                'all',
191                true,
192                array(1),
193                array(1),
194                array(),
195            ),
196            array(
197                '-foo foo',
198                'all',
199                true,
200                array(1),
201                array(1),
202                array(),
203            ),
204            // some creative edge cases
205            array(
206                'foo foo-',
207                'all',
208                true,
209                array(1, 1),
210                array(),
211                array(),
212            ),
213            array(
214                'foo- foo',
215                'all',
216                true,
217                array(1, 1),
218                array(),
219                array(),
220            ),
221            array(
222                'foo-bar',
223                'all',
224                true,
225                array(1, 2),
226                array(),
227                array(),
228            ),
229            array(
230                'foo-bar-foo',
231                'all',
232                true,
233                array(1, 2, 1),
234                array(),
235                array(),
236            ),
237            // all common
238            array(
239                'commonword',
240                'all',
241                false,
242                null,
243                null,
244                array('commonword'),
245            ),
246            // some common
247            array(
248                'commonword foo',
249                'all',
250                true,
251                array(1),
252                array(),
253                array('commonword'),
254            ),
255        );
256    }
257
258    /**
259    * @dataProvider keywords
260    */
261    public function test_split_keywords($keywords, $terms, $ok, $must_contain, $must_not_contain, $common)
262    {
263        $rv = $this->search->split_keywords($keywords, $terms);
264        $this->assertEquals($ok, $rv);
265        if ($ok)
266        {
267            // only check criteria if the search is going to be performed
268            $this->assert_array_content_equals($must_contain, $this->search->get_must_contain_ids());
269            $this->assert_array_content_equals($must_not_contain, $this->search->get_must_not_contain_ids());
270        }
271        $this->assert_array_content_equals($common, $this->search->get_common_words());
272    }
273
274    public static function data_split_keywords_max(): array
275    {
276        return [
277            'character count within limits separated by more spaces' => [
278                'foo    bar    baz    boo    far   faz    roo     rar    raz    zoo',
279                'all',
280                false,
281            ],
282            'character count within limits separated by spaces' => [
283                'foo bar baz boo far faz roo rar raz zoo',
284                'all',
285                false,
286            ],
287            'character count within limits separated by +, spaces after +' => [
288                'foo+ bar+ baz+ boo+ far+ faz+ roo+ rar+ raz+ zoo',
289                'all',
290                false,
291            ],
292            'character count within limits separated by +, no spaces' => [
293                'foo+bar+baz+boo+far+faz+roo+rar+raz+zoo',
294                'all',
295                false,
296            ],
297            'character count outside limits separated by +, no spaces' => [
298                'foo+bar+baz+boo+far+faz+roo+rar+raz+zoo+zar',
299                'all',
300                true,
301            ],
302            'character count outside limits separated by + and spaces' => [
303                'foo +bar +baz +boo +far +faz +roo +rar +raz +zoo +zar',
304                'all',
305                true,
306            ],
307            'character count outside limits separated by spaces' => [
308                'foo bar baz boo far faz roo rar raz zoo zar',
309                'all',
310                true,
311            ],
312            'character count outside limits separated by -, no spaces' => [
313                'foo-bar-baz-boo-far-faz-roo-rar-raz-zoo-zar',
314                'all',
315                true,
316            ],
317            'character count outside limits separated by - and spaces' => [
318                'foo -bar -baz -boo -far -faz -roo -rar -raz -zoo -zar',
319                'all',
320                true,
321            ],
322            'character count outside limits separated by |, no spaces' => [
323                'foo|bar|baz|boo|far|faz|roo|rar|raz|zoo|zar',
324                'all',
325                true,
326            ],
327            'character count outside limits separated by | and spaces' => [
328                'foo |bar |baz |boo |far |faz |roo |rar |raz |zoo |zar',
329                'all',
330                true,
331            ],
332        ];
333    }
334
335    /**
336     * @dataProvider data_split_keywords_max
337     */
338    public function test_split_max_keywords($keywords, $terms, $expect_error)
339    {
340        if ($expect_error)
341        {
342            $this->setExpectedTriggerError(E_USER_NOTICE, 'MAX_NUM_SEARCH_KEYWORDS_REFINE');
343        }
344
345        $this->assertTrue($this->search->split_keywords($keywords, $terms));
346    }
347}