Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
22.80% covered (danger)
22.80%
109 / 478
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_mention_controller_test
22.80% covered (danger)
22.80%
109 / 478
75.00% covered (warning)
75.00%
3 / 4
16.50
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%
98 / 98
100.00% covered (success)
100.00%
1 / 1
2
 handle_data
0.00% covered (danger)
0.00%
0 / 369
0.00% covered (danger)
0.00%
0 / 1
2
 test_handle
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
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
14use Symfony\Component\Config\FileLocator;
15use Symfony\Component\DependencyInjection\ContainerBuilder;
16use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
17
18class phpbb_mention_controller_test extends phpbb_database_test_case
19{
20    protected $db, $container, $user, $config, $auth, $cache;
21
22    /**
23     * @var \phpbb\mention\controller\mention
24     */
25    protected $controller;
26
27    /**
28     * @var PHPUnit_Framework_MockObject_MockObject
29     */
30    protected $request;
31
32    public function getDataSet()
33    {
34        return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/mention.xml');
35    }
36
37    public function setUp(): void
38    {
39        parent::setUp();
40
41        global $auth, $cache, $config, $db, $phpbb_container, $phpbb_dispatcher, $lang, $user, $request, $phpEx, $phpbb_root_path, $user_loader;
42
43        // Database
44        $this->db = $this->new_dbal();
45        $db = $this->db;
46
47        // Auth
48        $auth = $this->createMock('\phpbb\auth\auth');
49        $auth->expects($this->any())
50             ->method('acl_gets')
51             ->with('a_group', 'a_groupadd', 'a_groupdel')
52             ->willReturn(false)
53        ;
54
55        // Config
56        $config = new \phpbb\config\config(array(
57            'allow_mentions'      => true,
58            'mention_batch_size'  => 8,
59            'mention_names_limit' => 3,
60        ));
61
62        // Event dispatcher
63        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
64
65        $cache_driver = new \phpbb\cache\driver\dummy();
66        $cache = new \phpbb\cache\service(
67            $cache_driver,
68            $config,
69            $db,
70            $phpbb_dispatcher,
71            $phpbb_root_path,
72            $phpEx
73        );
74
75        // Language
76        $lang = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
77
78        // User
79        $user = $this->createMock('\phpbb\user');
80        $user->ip = '';
81        $user->data = array(
82            'user_id'       => 2,
83            'username'      => 'myself',
84            'is_registered' => true,
85            'user_colour'   => '',
86        );
87
88        // Request
89        $this->request = $request = $this->createMock('\phpbb\request\request');
90
91        $request->expects($this->any())
92                ->method('is_ajax')
93                ->willReturn(true);
94        $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper')
95            ->disableOriginalConstructor()
96            ->getMock();
97
98        $user_loader = new \phpbb\user_loader($avatar_helper, $db, $phpbb_root_path, $phpEx, USERS_TABLE);
99
100        // Controller helper
101        $controller_helper = $this->createMock('\phpbb\controller\helper');
102
103        // Container
104        $phpbb_container = new ContainerBuilder();
105
106        $loader = new YamlFileLoader($phpbb_container, new FileLocator(__DIR__ . '/fixtures'));
107        $loader->load('services_mention.yml');
108        $phpbb_container->set('user_loader', $user_loader);
109        $phpbb_container->set('user', $user);
110        $phpbb_container->set('language', $lang);
111        $phpbb_container->set('config', $config);
112        $phpbb_container->set('dbal.conn', $db);
113        $phpbb_container->set('auth', $auth);
114        $phpbb_container->set('cache.driver', $cache_driver);
115        $phpbb_container->set('cache', $cache);
116        $phpbb_container->set('request', $request);
117        $phpbb_container->set('controller.helper', $controller_helper);
118        $phpbb_container->set('group_helper', new \phpbb\group\helper(
119            $this->getMockBuilder('\phpbb\auth\auth')->disableOriginalConstructor()->getMock(),
120            $avatar_helper,
121            $db,
122            $cache,
123            $config,
124            new \phpbb\language\language(
125                new phpbb\language\language_file_loader($phpbb_root_path, $phpEx)
126            ),
127            new phpbb_mock_event_dispatcher(),
128            new \phpbb\path_helper(
129                new \phpbb\symfony_request(
130                    new phpbb_mock_request()
131                ),
132                $this->getMockBuilder('\phpbb\request\request')->disableOriginalConstructor()->getMock(),
133                $phpbb_root_path,
134                $phpEx
135            ),
136            $this->getMockBuilder('\phpbb\template\template')->disableOriginalConstructor()->getMock(),
137            $user
138        ));
139        $phpbb_container->set('text_formatter.utils', new \phpbb\textformatter\s9e\utils());
140        $phpbb_container->set(
141            'text_formatter.s9e.mention_helper',
142            new \phpbb\textformatter\s9e\mention_helper(
143                $this->db,
144                $auth,
145                $user,
146                $phpbb_root_path,
147                $phpEx
148            )
149        );
150        $phpbb_container->setParameter('core.root_path', $phpbb_root_path);
151        $phpbb_container->setParameter('core.php_ext', $phpEx);
152        $phpbb_container->addCompilerPass(new phpbb\di\pass\markpublic_pass());
153        $phpbb_container->compile();
154
155        // Mention Sources
156        $mention_sources = array('friend', 'group', 'team', 'topic', 'user', 'usergroup');
157        $mention_sources_array = array();
158        foreach ($mention_sources as $source)
159        {
160            $class = $phpbb_container->get('mention.source.' . $source);
161            $mention_sources_array['mention.source.' . $source] = $class;
162        }
163
164        $this->controller = new \phpbb\mention\controller\mention($mention_sources_array, $request, $controller_helper, $phpbb_root_path, $phpEx);
165    }
166
167    public static function handle_data()
168    {
169        /**
170         * NOTE:
171         * 1) in production comparison with 'myself' is being done in JS
172         * 2) team members of hidden groups can also be mentioned (because they are shown on teampage)
173         */
174        return [
175            ['', 0, [
176                'names' => [
177                    [
178                        'name'     => 'friend',
179                        'type'     => 'u',
180                        'id'       => 7,
181                        'avatar'   => [],
182                        'rank'     => '',
183                        'priority' => 1,
184                    ],
185                    [
186                        'name'     => 'Group we are a member of',
187                        'type'     => 'g',
188                        'id'       => 3,
189                        'avatar'   => [],
190                        'rank'     => '',
191                        'priority' => 0,
192                    ],
193                    [
194                        'name'     => 'Normal group',
195                        'type'     => 'g',
196                        'id'       => 1,
197                        'avatar'   => [],
198                        'rank'     => '',
199                        'priority' => 0,
200                    ],
201                    [
202                        'name'     => 'team_member_hidden',
203                        'type'     => 'u',
204                        'id'       => 6,
205                        'avatar'   => [],
206                        'rank'     => '',
207                        'priority' => 1,
208                    ],
209                    [
210                        'name'     => 'team_member_normal',
211                        'type'     => 'u',
212                        'id'       => 5,
213                        'avatar'   => [],
214                        'rank'     => '',
215                        'priority' => 1,
216                    ],
217                    [
218                        'name'     => 'myself',
219                        'type'     => 'u',
220                        'id'       => 2,
221                        'avatar'   => [],
222                        'rank'     => '',
223                        'priority' => 0,
224                    ],
225                    [
226                        'name'     => 'poster',
227                        'type'     => 'u',
228                        'id'       => 3,
229                        'avatar'   => [],
230                        'rank'     => '',
231                        'priority' => 0,
232                    ],
233                    [
234                        'name'     => 'replier',
235                        'type'     => 'u',
236                        'id'       => 4,
237                        'avatar'   => [],
238                        'rank'     => '',
239                        'priority' => 0,
240                    ],
241                    [
242                        'name'     => 'team_member_normal',
243                        'type'     => 'u',
244                        'id'       => 5,
245                        'avatar'   => [],
246                        'rank'     => '',
247                        'priority' => 0,
248                    ],
249                    [
250                        'name'     => 'team_member_hidden',
251                        'type'     => 'u',
252                        'id'       => 6,
253                        'avatar'   => [],
254                        'rank'     => '',
255                        'priority' => 0,
256                    ],
257                    [
258                        'name'     => 'friend',
259                        'type'     => 'u',
260                        'id'       => 7,
261                        'avatar'   => [],
262                        'rank'     => '',
263                        'priority' => 0,
264                    ],
265                    [
266                        'name'     => 'test',
267                        'type'     => 'u',
268                        'id'       => 8,
269                        'avatar'   => [],
270                        'rank'     => '',
271                        'priority' => 0,
272                    ],
273                    [
274                        'name'     => 'test1',
275                        'type'     => 'u',
276                        'id'       => 9,
277                        'avatar'   => [],
278                        'rank'     => '',
279                        'priority' => 0,
280                    ],
281                    [
282                        'name'     => 'Group we are a member of',
283                        'type'     => 'g',
284                        'id'       => 3,
285                        'avatar'   => [],
286                        'rank'     => '',
287                        'priority' => 1,
288                    ],
289                ],
290                'all'   => false,
291            ]],
292            ['', 1, [
293                'names' => [
294                    [
295                        'name'     => 'friend',
296                        'type'     => 'u',
297                        'id'       => 7,
298                        'avatar'   => [],
299                        'rank'     => '',
300                        'priority' => 1,
301                    ],
302                    [
303                        'name'     => 'Group we are a member of',
304                        'type'     => 'g',
305                        'id'       => 3,
306                        'avatar'   => [],
307                        'rank'     => '',
308                        'priority' => 0,
309                    ],
310                    [
311                        'name'     => 'Normal group',
312                        'type'     => 'g',
313                        'id'       => 1,
314                        'avatar'   => [],
315                        'rank'     => '',
316                        'priority' => 0,
317                    ],
318                    [
319                        'name'     => 'team_member_hidden',
320                        'type'     => 'u',
321                        'id'       => 6,
322                        'avatar'   => [],
323                        'rank'     => '',
324                        'priority' => 1,
325                    ],
326                    [
327                        'name'     => 'team_member_normal',
328                        'type'     => 'u',
329                        'id'       => 5,
330                        'avatar'   => [],
331                        'rank'     => '',
332                        'priority' => 1,
333                    ],
334                    [
335                        'name'     => 'replier',
336                        'type'     => 'u',
337                        'id'       => 4,
338                        'avatar'   => [],
339                        'rank'     => '',
340                        'priority' => 1,
341                    ],
342                    [
343                        'name'     => 'poster',
344                        'type'     => 'u',
345                        'id'       => 3,
346                        'avatar'   => [],
347                        'rank'     => '',
348                        'priority' => 5,
349                    ],
350                    [
351                        'name'     => 'myself',
352                        'type'     => 'u',
353                        'id'       => 2,
354                        'avatar'   => [],
355                        'rank'     => '',
356                        'priority' => 0,
357                    ],
358                    [
359                        'name'     => 'poster',
360                        'type'     => 'u',
361                        'id'       => 3,
362                        'avatar'   => [],
363                        'rank'     => '',
364                        'priority' => 0,
365                    ],
366                    [
367                        'name'     => 'replier',
368                        'type'     => 'u',
369                        'id'       => 4,
370                        'avatar'   => [],
371                        'rank'     => '',
372                        'priority' => 0,
373                    ],
374                    [
375                        'name'     => 'team_member_normal',
376                        'type'     => 'u',
377                        'id'       => 5,
378                        'avatar'   => [],
379                        'rank'     => '',
380                        'priority' => 0,
381                    ],
382                    [
383                        'name'     => 'team_member_hidden',
384                        'type'     => 'u',
385                        'id'       => 6,
386                        'avatar'   => [],
387                        'rank'     => '',
388                        'priority' => 0,
389                    ],
390                    [
391                        'name'     => 'friend',
392                        'type'     => 'u',
393                        'id'       => 7,
394                        'avatar'   => [],
395                        'rank'     => '',
396                        'priority' => 0,
397                    ],
398                    [
399                        'name'     => 'test',
400                        'type'     => 'u',
401                        'id'       => 8,
402                        'avatar'   => [],
403                        'rank'     => '',
404                        'priority' => 0,
405                    ],
406                    [
407                        'name'     => 'test1',
408                        'type'     => 'u',
409                        'id'       => 9,
410                        'avatar'   => [],
411                        'rank'     => '',
412                        'priority' => 0,
413                    ],
414                    [
415                        'name'     => 'Group we are a member of',
416                        'type'     => 'g',
417                        'id'       => 3,
418                        'avatar'   => [],
419                        'rank'     => '',
420                        'priority' => 1,
421                    ],
422                ],
423                'all'   => false,
424            ]],
425            ['t', 1, [
426                'names' => [
427                    [
428                        'name'     => 'team_member_hidden',
429                        'type'     => 'u',
430                        'id'       => 6,
431                        'avatar'   => [],
432                        'rank'     => '',
433                        'priority' => 1,
434                    ],
435                    [
436                        'name'     => 'team_member_normal',
437                        'type'     => 'u',
438                        'id'       => 5,
439                        'avatar'   => [],
440                        'rank'     => '',
441                        'priority' => 1,
442                    ],
443                    [
444                        'name'     => 'team_member_normal',
445                        'type'     => 'u',
446                        'id'       => 5,
447                        'avatar'   => [],
448                        'rank'     => '',
449                        'priority' => 0,
450                    ],
451                    [
452                        'name'     => 'team_member_hidden',
453                        'type'     => 'u',
454                        'id'       => 6,
455                        'avatar'   => [],
456                        'rank'     => '',
457                        'priority' => 0,
458                    ],
459                    [
460                        'name'     => 'test',
461                        'type'     => 'u',
462                        'id'       => 8,
463                        'avatar'   => [],
464                        'rank'     => '',
465                        'priority' => 0,
466                    ],
467                    [
468                        'name'     => 'test1',
469                        'type'     => 'u',
470                        'id'       => 9,
471                        'avatar'   => [],
472                        'rank'     => '',
473                        'priority' => 0,
474                    ],
475                    [
476                        'name'     => 'test2',
477                        'type'     => 'u',
478                        'id'       => 10,
479                        'avatar'   => [],
480                        'rank'     => '',
481                        'priority' => 0,
482                    ],
483                    [
484                        'name'     => 'test3',
485                        'type'     => 'u',
486                        'id'       => 11,
487                        'avatar'   => [],
488                        'rank'     => '',
489                        'priority' => 0,
490                    ],
491                ],
492                'all'   => true,
493            ]],
494            ['test', 1, [
495                'names' => [
496                    [
497                        'name'     => 'test',
498                        'type'     => 'u',
499                        'id'       => 8,
500                        'avatar'   => [],
501                        'rank'     => '',
502                        'priority' => 0,
503                    ],
504                    [
505                        'name'     => 'test1',
506                        'type'     => 'u',
507                        'id'       => 9,
508                        'avatar'   => [],
509                        'rank'     => '',
510                        'priority' => 0,
511                    ],
512                    [
513                        'name'     => 'test2',
514                        'type'     => 'u',
515                        'id'       => 10,
516                        'avatar'   => [],
517                        'rank'     => '',
518                        'priority' => 0,
519                    ],
520                    [
521                        'name'     => 'test3',
522                        'type'     => 'u',
523                        'id'       => 11,
524                        'avatar'   => [],
525                        'rank'     => '',
526                        'priority' => 0,
527                    ],
528                ],
529                'all'   => true,
530            ]],
531            ['test1', 1, [
532                'names' => [[
533                    'name'     => 'test1',
534                    'type'     => 'u',
535                    'id'       => 9,
536                    'avatar'   => [],
537                    'rank'     => '',
538                    'priority' => 0,
539                ]],
540                'all'   => true,
541            ]],
542        ];
543    }
544
545    /**
546     * @dataProvider handle_data
547     */
548    public function test_handle($keyword, $topic_id, $expected_result)
549    {
550        $this->request->expects($this->atLeast(2))
551            ->method('variable')
552            ->willReturnCallback(function() use ($keyword, $topic_id) {
553                $args = func_get_args();
554                return match($args) {
555                ['keyword', '', true, \phpbb\request\request_interface::REQUEST] => $keyword,
556                ['topic_id', 0, false, \phpbb\request\request_interface::REQUEST] => $topic_id,
557            };});
558
559        $data = json_decode($this->controller->handle()->getContent(), true);
560        $this->assertEquals($expected_result, $data);
561    }
562}