Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.06% covered (warning)
66.06%
144 / 218
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
notification_method_email_test
66.06% covered (warning)
66.06%
144 / 218
80.00% covered (warning)
80.00%
4 / 5
8.92
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
 get_notification_methods
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setUp
100.00% covered (success)
100.00%
116 / 116
100.00% covered (success)
100.00%
1 / 1
3
 data_notification_email
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 1
2
 test_notification_email
100.00% covered (success)
100.00%
24 / 24
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
18require_once __DIR__ . '/base.php';
19
20class notification_method_email_test extends phpbb_tests_notification_base
21{
22    /** @var \phpbb\notification\method\email */
23    protected $notification_method_email;
24
25    public function getDataSet()
26    {
27        return $this->createXMLDataSet(__DIR__ . '/fixtures/email_notification.type.post.xml');
28    }
29
30    protected function get_notification_methods()
31    {
32        return [
33            'notification.method.email',
34        ];
35    }
36
37    protected function setUp(): void
38    {
39        phpbb_database_test_case::setUp();
40
41        global $phpbb_root_path, $phpEx;
42
43        include_once(__DIR__ . '/ext/test/notification/type/test.' . $phpEx);
44
45        global $db, $config, $user, $auth, $cache, $phpbb_container;
46
47        $avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper')
48                              ->disableOriginalConstructor()
49                              ->getMock();
50        $db = $this->db = $this->new_dbal();
51        $config = $this->config = new \phpbb\config\config([
52            'allow_privmsg'            => true,
53            'allow_bookmarks'        => true,
54            'allow_topic_notify'    => true,
55            'allow_forum_notify'    => true,
56            'allow_board_notifications'    => true,
57        ]);
58        $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
59        $lang = new \phpbb\language\language($lang_loader);
60        $user = new \phpbb\user($lang, '\phpbb\datetime');
61        $this->user = $user;
62        $this->user_loader = new \phpbb\user_loader($avatar_helper, $this->db, $phpbb_root_path, $phpEx, 'phpbb_users');
63        $auth = $this->auth = new phpbb_mock_notifications_auth();
64        $this->phpbb_dispatcher = new phpbb_mock_event_dispatcher();
65        $cache_driver = new \phpbb\cache\driver\dummy();
66        $cache = $this->cache = new \phpbb\cache\service(
67            $cache_driver,
68            $this->config,
69            $this->db,
70            $this->phpbb_dispatcher,
71            $phpbb_root_path,
72            $phpEx
73        );
74
75        $phpbb_container = $this->container = new ContainerBuilder();
76        $loader     = new YamlFileLoader($phpbb_container, new FileLocator(__DIR__ . '/fixtures'));
77        $loader->load('services_notification.yml');
78        $phpbb_container->set('avatar.helper', $avatar_helper);
79        $phpbb_container->set('user_loader', $this->user_loader);
80        $phpbb_container->set('user', $user);
81        $phpbb_container->set('language', $lang);
82        $phpbb_container->set('config', $this->config);
83        $phpbb_container->set('controller.helper', $this->createMock('\phpbb\controller\helper'));
84        $phpbb_container->set('dbal.conn', $this->db);
85        $phpbb_container->set('auth', $auth);
86        $phpbb_container->set('cache.driver', $cache_driver);
87        $phpbb_container->set('cache', $cache);
88        $phpbb_container->set('log', new \phpbb\log\dummy());
89        $phpbb_container->set('text_formatter.utils', new \phpbb\textformatter\s9e\utils());
90        $phpbb_container->set('event_dispatcher', $this->phpbb_dispatcher);
91        $phpbb_container->setParameter('core.root_path', $phpbb_root_path);
92        $phpbb_container->setParameter('core.php_ext', $phpEx);
93        $phpbb_container->setParameter('tables.notifications', 'phpbb_notifications');
94        $phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications');
95        $phpbb_container->setParameter('tables.notification_types', 'phpbb_notification_types');
96        $phpbb_container->setParameter('tables.notification_emails', 'phpbb_notification_emails');
97        $phpbb_container->setParameter('tables.notification_push', 'phpbb_notification_push');
98        $phpbb_container->setParameter('tables.push_subscriptions', 'phpbb_push_subscriptions');
99        $phpbb_container->set(
100            'text_formatter.s9e.mention_helper',
101            new \phpbb\textformatter\s9e\mention_helper(
102                $this->db,
103                $auth,
104                $this->user,
105                $phpbb_root_path,
106                $phpEx
107            )
108        );
109
110        $messenger_method_collection = new \phpbb\di\service_collection($phpbb_container);
111        $messenger_method_collection->add('messenger.method.email');
112        $phpbb_container->set('messenger.method_collection', $messenger_method_collection);
113
114        $this->notification_method_email = $this->getMockBuilder('\phpbb\notification\method\email')
115            ->setConstructorArgs([
116                $phpbb_container->get('user_loader'),
117                $phpbb_container->get('user'),
118                $phpbb_container->get('config'),
119                $phpbb_container->get('dbal.conn'),
120                $phpbb_root_path,
121                $phpEx,
122                $phpbb_container->getParameter('tables.notification_emails'),
123                $phpbb_container->get('messenger.method_collection')
124            ])
125            ->onlyMethods(['notify_using_messenger'])
126            ->getMock();
127        $notification_method_email = $this->notification_method_email;
128
129        $class = new ReflectionClass($notification_method_email);
130        $empty_queue_method = $class->getMethod('empty_queue');
131
132        $this->notification_method_email->method('notify_using_messenger')
133            ->will($this->returnCallback(function () use ($notification_method_email, $empty_queue_method) {
134                $empty_queue_method->invoke($notification_method_email);
135            }));
136
137        $phpbb_container->set('notification.method.email', $this->notification_method_email);
138
139        $this->notifications = new phpbb_notification_manager_helper(
140            array(),
141            array(),
142            $this->container,
143            $this->user_loader,
144            $this->phpbb_dispatcher,
145            $this->db,
146            $this->cache,
147            $lang,
148            $this->user,
149            'phpbb_notification_types',
150            'phpbb_user_notifications'
151        );
152
153        $phpbb_container->set('notification_manager', $this->notifications);
154
155        $phpbb_container->addCompilerPass(new phpbb\di\pass\markpublic_pass());
156
157        $phpbb_container->compile();
158
159        $this->notifications->setDependencies($this->auth, $this->config);
160
161        $types = array();
162        foreach ($this->get_notification_types() as $type)
163        {
164            $class = $this->build_type($type);
165
166            $types[$type] = $class;
167        }
168
169        $this->notifications->set_var('notification_types', $types);
170
171        $methods = array();
172        foreach ($this->get_notification_methods() as $method)
173        {
174            $class = $this->container->get($method);
175
176            $methods[$method] = $class;
177        }
178
179        $this->notifications->set_var('notification_methods', $methods);
180    }
181
182    public static function data_notification_email()
183    {
184        return [
185            /**
186            * Normal post
187            *
188            * User => State description
189            *    2    => Topic id=1 and id=2 subscribed, should receive a new topics post notification
190            *    3    => Topic id=1 subscribed, should receive a new topic post notification
191            *    4    => Topic id=1 subscribed, should receive a new topic post notification
192            *    5    => Topic id=1 subscribed, post id=1 already notified, should receive a new topic post notification
193            *    6    => Topic id=1 and forum id=1 subscribed, should receive a new topic/forum post notification
194            *    7    => Forum id=1 subscribed, should NOT receive a new topic post but a forum post notification
195            *    8    => Forum id=1 subscribed, post id=1 already notified, should NOT receive a new topic post but a forum post notification
196            */
197            [
198                'notification.type.post',
199                [
200                    'forum_id'        => '1',
201                    'post_id'        => '2',
202                    'topic_id'        => '1',
203                ],
204                [
205                    2 => ['user_id' => '2'],
206                    3 => ['user_id' => '3'],
207                    4 => ['user_id' => '4'],
208                    5 => ['user_id' => '5'],
209                    6 => ['user_id' => '6'],
210                ],
211            ],
212            [
213                'notification.type.forum',
214                [
215                    'forum_id'        => '1',
216                    'post_id'        => '3',
217                    'topic_id'        => '1',
218                ],
219                [
220                    6 => ['user_id' => '6'],
221                    7 => ['user_id' => '7'],
222                    8 => ['user_id' => '8']
223                ],
224            ],
225            [
226                'notification.type.post',
227                [
228                    'forum_id'        => '1',
229                    'post_id'        => '4',
230                    'topic_id'        => '2',
231                ],
232                [
233                    2 => ['user_id' => '2'],
234                ],
235            ],
236            [
237                'notification.type.forum',
238                [
239                    'forum_id'        => '1',
240                    'post_id'        => '5',
241                    'topic_id'        => '2',
242                ],
243                [
244                    6 => ['user_id' => '6'],
245                    7 => ['user_id' => '7'],
246                    8 => ['user_id' => '8'],
247                ],
248            ],
249            [
250                'notification.type.post',
251                [
252                    'forum_id'        => '2',
253                    'post_id'        => '6',
254                    'topic_id'        => '3',
255                ],
256                [
257                ],
258            ],
259            [
260                'notification.type.forum',
261                [
262                    'forum_id'        => '2',
263                    'post_id'        => '6',
264                    'topic_id'        => '3',
265                ],
266                [
267                ],
268            ],
269        ];
270    }
271
272    /**
273     * @dataProvider data_notification_email
274     */
275    public function test_notification_email($notification_type, $post_data, $expected_users)
276    {
277        $post_data = array_merge([
278                'post_time' => 1349413322,
279                'poster_id' => 1,
280                'topic_title' => '',
281                'post_subject' => '',
282                'post_username' => '',
283                'forum_name' => '',
284            ],
285
286            $post_data);
287        $notification_options = [
288            'item_id'            => $post_data['post_id'],
289            'item_parent_id'    => $post_data['topic_id'],
290        ];
291
292        $notified_users = $this->notification_method_email->get_notified_users($this->notifications->get_notification_type_id($notification_type), $notification_options);
293        $this->assertEquals(0, count($notified_users), 'Assert no user has been notified yet');
294
295        $this->notifications->add_notifications($notification_type, $post_data);
296
297        $notified_users = $this->notification_method_email->get_notified_users($this->notifications->get_notification_type_id($notification_type), $notification_options);
298        $this->assertEquals($expected_users, $notified_users, 'Assert that expected users have been notified');
299
300        $post_data['post_id']++;
301        $notification_options['item_id'] = $post_data['post_id'];
302        $post_data['post_time'] = 1349413323;
303
304        $this->notifications->add_notifications($notification_type, $post_data);
305
306        $notified_users2 = $this->notification_method_email->get_notified_users($this->notifications->get_notification_type_id($notification_type), $notification_options);
307        $this->assertEquals($expected_users, $notified_users2, 'Assert that expected users stay the same after replying to same topic');
308    }
309}