mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-11 22:08:54 +00:00
[ticket/17010] Move get subscription map to separate function and extend tests
Unit tests will also now ensure there are no special surprises with more than one subscription for users. PHPBB3-17010
This commit is contained in:
parent
b779ce5910
commit
79ff21fdf5
2 changed files with 36 additions and 12 deletions
|
@ -169,15 +169,7 @@ class webpush extends messenger_base
|
||||||
$this->user_loader->load_users($notify_users, array(USER_IGNORE));
|
$this->user_loader->load_users($notify_users, array(USER_IGNORE));
|
||||||
|
|
||||||
// Get subscriptions for users
|
// Get subscriptions for users
|
||||||
$user_subscription_map = [];
|
$user_subscription_map = $this->get_user_subscription_map($notify_users);
|
||||||
$sql = 'SELECT subscription_id, user_id, endpoint, p256dh, auth
|
|
||||||
FROM ' . $this->push_subscriptions_table . '
|
|
||||||
WHERE ' . $this->db->sql_in_set('user_id', $notify_users);
|
|
||||||
$result = $this->db->sql_query($sql);
|
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
|
||||||
{
|
|
||||||
$user_subscription_map[$row['user_id']][] = $row;
|
|
||||||
}
|
|
||||||
|
|
||||||
$auth = [
|
$auth = [
|
||||||
'VAPID' => [
|
'VAPID' => [
|
||||||
|
@ -316,4 +308,29 @@ class webpush extends messenger_base
|
||||||
|
|
||||||
return array_intersect_key($data, $row);
|
return array_intersect_key($data, $row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get subscriptions for notify users
|
||||||
|
*
|
||||||
|
* @param array $notify_users Users to notify
|
||||||
|
*
|
||||||
|
* @return array Subscription map
|
||||||
|
*/
|
||||||
|
protected function get_user_subscription_map(array $notify_users): array
|
||||||
|
{
|
||||||
|
// Get subscriptions for users
|
||||||
|
$user_subscription_map = [];
|
||||||
|
|
||||||
|
$sql = 'SELECT subscription_id, user_id, endpoint, p256dh, auth
|
||||||
|
FROM ' . $this->push_subscriptions_table . '
|
||||||
|
WHERE ' . $this->db->sql_in_set('user_id', $notify_users);
|
||||||
|
$result = $this->db->sql_query($sql);
|
||||||
|
while ($row = $this->db->sql_fetchrow($result))
|
||||||
|
{
|
||||||
|
$user_subscription_map[$row['user_id']][] = $row;
|
||||||
|
}
|
||||||
|
$this->db->sql_freeresult($result);
|
||||||
|
|
||||||
|
return $user_subscription_map;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -338,7 +338,14 @@ class notification_method_webpush_test extends phpbb_tests_notification_base
|
||||||
$subscription_info = [];
|
$subscription_info = [];
|
||||||
foreach ($expected_users as $user_id => $user_data)
|
foreach ($expected_users as $user_id => $user_data)
|
||||||
{
|
{
|
||||||
$subscription_info[$user_id] = $this->create_subscription_for_user($user_id);
|
$subscription_info[$user_id][] = $this->create_subscription_for_user($user_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create second subscription for first user ID passed
|
||||||
|
if (count($expected_users))
|
||||||
|
{
|
||||||
|
$first_user_id = array_key_first($expected_users);
|
||||||
|
$subscription_info[$first_user_id][] = $this->create_subscription_for_user($first_user_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
$post_data = array_merge([
|
$post_data = array_merge([
|
||||||
|
@ -361,7 +368,7 @@ class notification_method_webpush_test extends phpbb_tests_notification_base
|
||||||
|
|
||||||
foreach ($expected_users as $user_id => $data)
|
foreach ($expected_users as $user_id => $data)
|
||||||
{
|
{
|
||||||
$messages = $this->get_messages_for_subscription($subscription_info[$user_id]['clientHash']);
|
$messages = $this->get_messages_for_subscription($subscription_info[$user_id][0]['clientHash']);
|
||||||
$this->assertEmpty($messages);
|
$this->assertEmpty($messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,7 +379,7 @@ class notification_method_webpush_test extends phpbb_tests_notification_base
|
||||||
|
|
||||||
foreach ($expected_users as $user_id => $data)
|
foreach ($expected_users as $user_id => $data)
|
||||||
{
|
{
|
||||||
$messages = $this->get_messages_for_subscription($subscription_info[$user_id]['clientHash']);
|
$messages = $this->get_messages_for_subscription($subscription_info[$user_id][0]['clientHash']);
|
||||||
$this->assertNotEmpty($messages);
|
$this->assertNotEmpty($messages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue