Skip to content

Commit 3f783dc

Browse files
committed
Merge branch 'main' into fixes
Signed-off-by: Matt Friedman <maf675@gmail.com> # Conflicts: # acp/wpn_acp_module.php
2 parents af56139 + 676c29c commit 3f783dc

4 files changed

Lines changed: 260 additions & 3 deletions

File tree

acp/wpn_acp_module.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class wpn_acp_module
4444
/** @var array $errors */
4545
protected $errors = [];
4646

47+
/** @var string Hide/replace private key with asterisks */
48+
public const MASKED_PRIVATE_KEY = '********';
49+
4750
/**
4851
* Main ACP module
4952
*
@@ -97,7 +100,7 @@ public function display_settings()
97100
$this->template->assign_vars([
98101
'S_WEBPUSH_ENABLE' => $this->config['wpn_webpush_enable'],
99102
'WEBPUSH_VAPID_PUBLIC' => $this->config['wpn_webpush_vapid_public'],
100-
'WEBPUSH_VAPID_PRIVATE' => !$this->config['wpn_webpush_vapid_private'] ?: '********', // Replace private key with asterixes
103+
'WEBPUSH_VAPID_PRIVATE' => $this->config['wpn_webpush_vapid_private'] ? self::MASKED_PRIVATE_KEY : '',
101104
'U_ACTION' => $this->u_action,
102105
]);
103106
}
@@ -117,7 +120,7 @@ public function save_settings()
117120
];
118121

119122
// Do not validate and update private key field if the content is ******** and the key was already set
120-
if ($config_array['wpn_webpush_vapid_private'] === '********' && $this->config['wpn_webpush_vapid_private'])
123+
if ($config_array['wpn_webpush_vapid_private'] === self::MASKED_PRIVATE_KEY && $this->config['wpn_webpush_vapid_private'])
121124
{
122125
unset($display_settings['wpn_webpush_vapid_private'], $config_array['wpn_webpush_vapid_private']);
123126
}

tests/event/fixtures/webpush.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<dataset>
3+
<table name="phpbb_wpn_notification_push">
4+
</table>
5+
<table name="phpbb_wpn_push_subscriptions">
6+
<column>subscription_id</column>
7+
<column>user_id</column>
8+
<column>endpoint</column>
9+
<column>p256dh</column>
10+
<column>auth</column>
11+
<column>expiration_time</column>
12+
<row>
13+
<value>1</value>
14+
<value>2</value>
15+
<value>https://web.push.test.localhost/foo2</value>
16+
<value></value>
17+
<value></value>
18+
<value>0</value>
19+
</row>
20+
<row>
21+
<value>2</value>
22+
<value>3</value>
23+
<value>https://web.push.test.localhost/foo3</value>
24+
<value></value>
25+
<value></value>
26+
<value>1</value>
27+
</row>
28+
<row>
29+
<value>3</value>
30+
<value>4</value>
31+
<value>https://web.push.test.localhost/foo4</value>
32+
<value></value>
33+
<value></value>
34+
<value>0</value>
35+
</row>
36+
</table>
37+
</dataset>

tests/event/listener_test.php

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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+
}

tests/functional/functional_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function test_acp_module()
5757

5858
foreach ($form_data as $config_name => $config_value)
5959
{
60-
$config_value = ($config_name === 'config[wpn_webpush_vapid_private]') ? '********' : $config_value;
60+
$config_value = ($config_name === 'config[wpn_webpush_vapid_private]') ? \phpbb\webpushnotifications\acp\wpn_acp_module::MASKED_PRIVATE_KEY : $config_value;
6161
$this->assertEquals($config_value, $crawler->filter('input[name="' . $config_name . '"]')->attr('value'));
6262
}
6363
}

0 commit comments

Comments
 (0)