Skip to content

Commit 9fe40af

Browse files
authored
Merge pull request #89 from iMattPro/Fixins
Refine module auth
2 parents 9a5a147 + b802b5f commit 9fe40af

File tree

4 files changed

+77
-13
lines changed

4 files changed

+77
-13
lines changed

migrations/add_webpush.php

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function update_data(): array
7474
'module_basename' => '\phpbb\webpushnotifications\acp\wpn_acp_module',
7575
'module_langname' => 'ACP_WEBPUSH_EXT_SETTINGS',
7676
'module_mode' => 'webpush',
77+
//'module_auth' => 'ext_phpbb/webpushnotifications && acl_a_server', //Accidentally omitted this, fix is in another migration
7778
'after' => 'ACP_JABBER_SETTINGS',
7879
]]],
7980
];

migrations/fix_acp_module_auth.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414

1515
class fix_acp_module_auth extends migration
1616
{
17+
public function effectively_installed()
18+
{
19+
$sql = 'SELECT module_id
20+
FROM ' . $this->table_prefix . "modules
21+
WHERE module_class = 'acp'
22+
AND module_langname = 'ACP_WEBPUSH_EXT_SETTINGS'
23+
AND module_auth = 'ext_phpbb/webpushnotifications && acl_a_server'";
24+
$result = $this->db->sql_query($sql);
25+
$module_id = $this->db->sql_fetchfield('module_id');
26+
$this->db->sql_freeresult($result);
27+
28+
return $module_id !== false;
29+
}
30+
1731
public static function depends_on()
1832
{
1933
return ['\phpbb\webpushnotifications\migrations\add_webpush'];
@@ -30,8 +44,8 @@ public function set_acp_module_auth()
3044
{
3145
$phpbb_modules_table = $this->table_prefix . 'modules';
3246
$sql = 'UPDATE ' . $phpbb_modules_table . "
33-
SET module_auth = '" . $this->db->sql_escape('ext_phpbb/webpushnotifications && acl_a_server') . "'
34-
WHERE module_langname = '" . $this->db->sql_escape('ACP_WEBPUSH_EXT_SETTINGS') . "'";
47+
SET module_auth = '" . $this->db->sql_escape('ext_phpbb/webpushnotifications && acl_a_server') . "'
48+
WHERE module_langname = '" . $this->db->sql_escape('ACP_WEBPUSH_EXT_SETTINGS') . "'";
3549
$this->db->sql_query($sql);
3650
}
3751
}

notification/method/webpush.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo
371371
{
372372
$subscriptions[] = [
373373
'endpoint' => $subscription['endpoint'],
374-
'expirationTime' => $subscription['expiration_time'],
374+
'expirationTime' => (int) $subscription['expiration_time'],
375375
];
376376
}
377377
}

tests/event/listener_test.php

+59-10
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,63 @@ public function test_load_template_data($user_id, $method_data, $subscriptions,
244244

245245
$this->template->expects($expected ? self::once() : self::never())
246246
->method('assign_vars')
247-
->with([
248-
'NOTIFICATIONS_WEBPUSH_ENABLE' => true,
249-
'U_WEBPUSH_SUBSCRIBE' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_subscribe_controller'),
250-
'U_WEBPUSH_UNSUBSCRIBE' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_unsubscribe_controller'),
251-
'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'],
252-
'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
253-
'SUBSCRIPTIONS' => $subscriptions,
254-
'WEBPUSH_FORM_TOKENS' => $this->form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
247+
->withConsecutive([
248+
$this->callback(function($arg) use ($subscriptions) {
249+
$expectedValues = [
250+
'NOTIFICATIONS_WEBPUSH_ENABLE' => true,
251+
'U_WEBPUSH_SUBSCRIBE' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_subscribe_controller'),
252+
'U_WEBPUSH_UNSUBSCRIBE' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_unsubscribe_controller'),
253+
'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'],
254+
'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
255+
'SUBSCRIPTIONS' => $subscriptions,
256+
'WEBPUSH_FORM_TOKENS' => $this->form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
257+
];
258+
259+
// Check all required keys exist first
260+
$missingKeys = array_diff(array_keys($expectedValues), array_keys($arg));
261+
if (!empty($missingKeys))
262+
{
263+
$this->fail("Expected key(s) '" . implode("', '", $missingKeys) . "' missing from argument array");
264+
}
265+
266+
// Handle WEBPUSH_FORM_TOKENS separately
267+
if (isset($arg['WEBPUSH_FORM_TOKENS']))
268+
{
269+
$tokenArg = $arg['WEBPUSH_FORM_TOKENS'];
270+
$tokenExpected = $expectedValues['WEBPUSH_FORM_TOKENS'];
271+
272+
// Check creation_time separately, allow for 1 second discrepancies during test run
273+
$timeDiff = abs($tokenArg['creation_time'] - $tokenExpected['creation_time']);
274+
if ($timeDiff > 1)
275+
{
276+
$this->fail(sprintf(
277+
"Creation time difference too large. Expected: %d, Actual: %d",
278+
$tokenExpected['creation_time'],
279+
$tokenArg['creation_time']
280+
));
281+
}
282+
// Remove creation_time after checking to allow other fields comparison
283+
unset($tokenArg['creation_time'], $tokenExpected['creation_time']);
284+
$arg['WEBPUSH_FORM_TOKENS'] = $tokenArg;
285+
$expectedValues['WEBPUSH_FORM_TOKENS'] = $tokenExpected;
286+
}
287+
288+
// Compare values individually
289+
foreach ($expectedValues as $key => $value)
290+
{
291+
if ($arg[$key] !== $value)
292+
{
293+
$this->fail(sprintf(
294+
"Mismatch for key '%s'. Expected: %s, Actual: %s",
295+
$key,
296+
var_export($value, true),
297+
var_export($arg[$key], true)
298+
));
299+
}
300+
}
301+
302+
return true;
303+
})
255304
]);
256305

257306
$dispatcher = new \phpbb\event\dispatcher();
@@ -390,8 +439,8 @@ public function test_validate_pwa_options($validate, $cfg_array, $expected_error
390439
$config_name = key($cfg_array);
391440
$config_definition = ['validate' => $validate];
392441

393-
$pwa_icon_small = isset($cfg_array['pwa_icon_small']) ? $cfg_array['pwa_icon_small'] : '';
394-
$pwa_icon_large = isset($cfg_array['pwa_icon_large']) ? $cfg_array['pwa_icon_large'] : '';
442+
$pwa_icon_small = $cfg_array['pwa_icon_small'] ?? '';
443+
$pwa_icon_large = $cfg_array['pwa_icon_large'] ?? '';
395444

396445
[$small_image_name, $small_image_ext] = $pwa_icon_small ? explode('.', $pwa_icon_small, 2) : ['', ''];
397446
[$large_image_name, $large_image_ext] = $pwa_icon_large ? explode('.', $pwa_icon_large, 2) : ['', ''];

0 commit comments

Comments
 (0)