|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * phpBB Browser Push Notifications. An extension for the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) 2023, phpBB Limited <https://www.phpbb.com> |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace phpbb\webpushnotifications\tests\event; |
| 12 | + |
| 13 | +class listener_test extends \phpbb_database_test_case |
| 14 | +{ |
| 15 | + /** @var \phpbb\webpushnotifications\event\listener */ |
| 16 | + protected $listener; |
| 17 | + |
| 18 | + /* @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\controller\helper */ |
| 19 | + protected $controller_helper; |
| 20 | + |
| 21 | + /* @var \phpbb\webpushnotifications\form\form_helper */ |
| 22 | + protected $form_helper; |
| 23 | + |
| 24 | + /** @var \phpbb\language\language */ |
| 25 | + protected $language; |
| 26 | + |
| 27 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\template\template */ |
| 28 | + protected $template; |
| 29 | + |
| 30 | + /** @var \phpbb\webpushnotifications\notification\method\webpush */ |
| 31 | + protected $notification_method_webpush; |
| 32 | + |
| 33 | + protected static function setup_extensions() |
| 34 | + { |
| 35 | + return ['phpbb/webpushnotifications']; |
| 36 | + } |
| 37 | + |
| 38 | + public function getDataSet() |
| 39 | + { |
| 40 | + return $this->createXMLDataSet(__DIR__ . '/fixtures/webpush.xml'); |
| 41 | + } |
| 42 | + |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + global $phpbb_root_path, $phpEx, $user; |
| 48 | + |
| 49 | + $db = $this->new_dbal(); |
| 50 | + |
| 51 | + $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); |
| 52 | + $lang_loader->set_extension_manager(new \phpbb_mock_extension_manager($phpbb_root_path)); |
| 53 | + $this->language = new \phpbb\language\language($lang_loader); |
| 54 | + $this->template = $this->getMockBuilder('\phpbb\template\template') |
| 55 | + ->getMock(); |
| 56 | + $request = new \phpbb\request\request(); |
| 57 | + $request->enable_super_globals(); |
| 58 | + $user = new \phpbb\user($this->language, '\phpbb\datetime'); |
| 59 | + $this->user = $user; |
| 60 | + $this->user->data['user_form_salt'] = ''; |
| 61 | + $user_loader = new \phpbb\user_loader($db, $phpbb_root_path, $phpEx, 'phpbb_users'); |
| 62 | + |
| 63 | + $this->controller_helper = $this->getMockBuilder('\phpbb\controller\helper') |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->getMock(); |
| 66 | + $this->controller_helper->method('route') |
| 67 | + ->willReturnCallback(function ($route, array $params = []) { |
| 68 | + return $route . '#' . serialize($params); |
| 69 | + }); |
| 70 | + |
| 71 | + $this->config = new \phpbb\config\config([]); |
| 72 | + |
| 73 | + $this->form_helper = new \phpbb\webpushnotifications\form\form_helper( |
| 74 | + $this->config, |
| 75 | + $request, |
| 76 | + $this->user |
| 77 | + ); |
| 78 | + |
| 79 | + $this->notification_method_webpush = new \phpbb\webpushnotifications\notification\method\webpush( |
| 80 | + $this->config, |
| 81 | + $db, |
| 82 | + new \phpbb\log\dummy(), |
| 83 | + $user_loader, |
| 84 | + $this->user, |
| 85 | + $phpbb_root_path, |
| 86 | + $phpEx, |
| 87 | + 'phpbb_wpn_notification_push', |
| 88 | + 'phpbb_wpn_push_subscriptions' |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + protected function set_listener() |
| 93 | + { |
| 94 | + $this->listener = new \phpbb\webpushnotifications\event\listener( |
| 95 | + $this->controller_helper, |
| 96 | + $this->form_helper, |
| 97 | + $this->language, |
| 98 | + $this->template |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public function test_construct() |
| 103 | + { |
| 104 | + $this->set_listener(); |
| 105 | + self::assertInstanceOf('\Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener); |
| 106 | + } |
| 107 | + |
| 108 | + public function test_getSubscribedEvents() |
| 109 | + { |
| 110 | + self::assertEquals([ |
| 111 | + 'core.ucp_notifications_output_notification_types_modify_template_vars', |
| 112 | + 'core.ucp_display_module_before', |
| 113 | + 'core.acp_main_notice', |
| 114 | + ], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents())); |
| 115 | + } |
| 116 | + |
| 117 | + public function test_load_language() |
| 118 | + { |
| 119 | + $this->set_listener(); |
| 120 | + |
| 121 | + self::assertFalse($this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH')); |
| 122 | + |
| 123 | + $dispatcher = new \phpbb\event\dispatcher(); |
| 124 | + $dispatcher->addListener('core.ucp_display_module_before', [$this->listener, 'load_language']); |
| 125 | + $dispatcher->trigger_event('core.ucp_display_module_before'); |
| 126 | + |
| 127 | + self::assertTrue($this->language->is_set('NOTIFICATION_METHOD_PHPBB_WPN_WEBPUSH')); |
| 128 | + } |
| 129 | + |
| 130 | + public function test_compatibility_notice() |
| 131 | + { |
| 132 | + $this->set_listener(); |
| 133 | + |
| 134 | + $this->template->expects(self::once()) |
| 135 | + ->method('assign_var') |
| 136 | + ->with('S_WPN_COMPATIBILITY_NOTICE', false); |
| 137 | + |
| 138 | + $dispatcher = new \phpbb\event\dispatcher(); |
| 139 | + $dispatcher->addListener('core.acp_main_notice', [$this->listener, 'compatibility_notice']); |
| 140 | + $dispatcher->trigger_event('core.acp_main_notice'); |
| 141 | + } |
| 142 | + |
| 143 | + public function get_ucp_template_data_data() |
| 144 | + { |
| 145 | + return [ |
| 146 | + [ // webpush method with a valid webpush subscription |
| 147 | + 2, |
| 148 | + 'method_data' => [ |
| 149 | + 'id' => 'notification.method.phpbb.wpn.webpush', |
| 150 | + ], |
| 151 | + [['endpoint' => 'https://web.push.test.localhost/foo2', 'expirationTime' => 0]], |
| 152 | + true, |
| 153 | + ], |
| 154 | + [ // wrong method, but with a valid webpush subscription, expect no code execution |
| 155 | + 2, |
| 156 | + 'method_data' => [ |
| 157 | + 'id' => 'notification.method.email', |
| 158 | + ], |
| 159 | + [], |
| 160 | + false, |
| 161 | + ], |
| 162 | + [ // webpush method with another valid webpush subscription |
| 163 | + 3, |
| 164 | + 'method_data' => [ |
| 165 | + 'id' => 'notification.method.phpbb.wpn.webpush', |
| 166 | + ], |
| 167 | + [['endpoint' => 'https://web.push.test.localhost/foo3', 'expirationTime' => 1]], |
| 168 | + true, |
| 169 | + ], |
| 170 | + [ // webpush method with an invalid webpush subscription, expect code execution but no subscription data |
| 171 | + 5, |
| 172 | + 'method_data' => [ |
| 173 | + 'id' => 'notification.method.phpbb.wpn.webpush', |
| 174 | + ], |
| 175 | + [], |
| 176 | + true, |
| 177 | + ], |
| 178 | + [ // wrong method with an invalid webpush subscription, expect no code execution |
| 179 | + 5, |
| 180 | + 'method_data' => [ |
| 181 | + 'id' => 'notification.method.email', |
| 182 | + ], |
| 183 | + [], |
| 184 | + false, |
| 185 | + ], |
| 186 | + ]; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @dataProvider get_ucp_template_data_data |
| 191 | + */ |
| 192 | + public function test_get_ucp_template_data($user_id, $method_data, $subscriptions, $expected) |
| 193 | + { |
| 194 | + $this->user->data['user_id'] = $user_id; |
| 195 | + $this->template->expects($expected ? self::once() : self::never()) |
| 196 | + ->method('assign_vars') |
| 197 | + ->with([ |
| 198 | + 'NOTIFICATIONS_WEBPUSH_ENABLE' => true, |
| 199 | + 'U_WEBPUSH_SUBSCRIBE' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_subscribe_controller'), |
| 200 | + 'U_WEBPUSH_UNSUBSCRIBE' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_unsubscribe_controller'), |
| 201 | + 'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'], |
| 202 | + 'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'), |
| 203 | + 'SUBSCRIPTIONS' => $subscriptions, |
| 204 | + 'WEBPUSH_FORM_TOKENS' => $this->form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP), |
| 205 | + ] |
| 206 | + ); |
| 207 | + |
| 208 | + $this->set_listener(); |
| 209 | + |
| 210 | + $method_data['method'] = $this->notification_method_webpush; |
| 211 | + $event_data = ['method_data']; |
| 212 | + |
| 213 | + $dispatcher = new \phpbb\event\dispatcher(); |
| 214 | + $dispatcher->addListener('core.ucp_notifications_output_notification_types_modify_template_vars', [$this->listener, 'load_template_data']); |
| 215 | + $dispatcher->trigger_event('core.ucp_notifications_output_notification_types_modify_template_vars', compact($event_data)); |
| 216 | + } |
| 217 | +} |
0 commit comments