Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ services:
- '@template'
- '@user'
- '@notification_manager'
- '@dbal.conn'
- '%tables.phpbb.wpn.notification_push%'
- '%tables.phpbb.wpn.push_subscriptions%'
tags:
- { name: event.listener }

Expand Down
42 changes: 41 additions & 1 deletion event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use phpbb\config\config;
use phpbb\controller\helper as controller_helper;
use phpbb\db\driver\driver_interface;
use phpbb\language\language;
use phpbb\notification\manager;
use phpbb\request\request_interface;
Expand Down Expand Up @@ -50,6 +51,15 @@ class listener implements EventSubscriberInterface
/** @var manager */
protected $phpbb_notifications;

/** @var driver_interface */
protected $db;

/** @var string */
protected $notification_webpush_table;

/** @var string */
protected $push_subscriptions_table;

/**
* Constructor
*
Expand All @@ -61,8 +71,11 @@ class listener implements EventSubscriberInterface
* @param template $template Template object
* @param user $user
* @param manager $phpbb_notifications Notifications manager object
* @param driver_interface $db Database connection
* @param string $notification_webpush_table Web Push notification table
* @param string $push_subscriptions_table Web Push subscriptions table
*/
public function __construct(config $config, controller_helper $controller_helper, form_helper $form_helper, language $language, request_interface $request, template $template, user $user, manager $phpbb_notifications)
public function __construct(config $config, controller_helper $controller_helper, form_helper $form_helper, language $language, request_interface $request, template $template, user $user, manager $phpbb_notifications, driver_interface $db, $notification_webpush_table, $push_subscriptions_table)
{
$this->config = $config;
$this->controller_helper = $controller_helper;
Expand All @@ -72,6 +85,9 @@ public function __construct(config $config, controller_helper $controller_helper
$this->template = $template;
$this->user = $user;
$this->phpbb_notifications = $phpbb_notifications;
$this->db = $db;
$this->notification_webpush_table = $notification_webpush_table;
$this->push_subscriptions_table = $push_subscriptions_table;
}

public static function getSubscribedEvents()
Expand All @@ -83,6 +99,7 @@ public static function getSubscribedEvents()
'core.acp_main_notice' => 'compatibility_notice',
'core.help_manager_add_block_after' => 'wpn_faq',
'core.page_footer' => 'append_agreement',
'core.delete_user_before' => 'delete_user_data',
];
}

Expand Down Expand Up @@ -221,6 +238,29 @@ public function append_agreement()
$this->template->append_var('AGREEMENT_TEXT', $this->language->lang('NOTIFY_WEBPUSH_PRIVACY_POLICY', $this->config['sitename']));
}

/**
* Delete Web Push data belonging to deleted users
*
* @param \phpbb\event\data $event The event object
* @return void
*/
public function delete_user_data($event)
{
$user_ids = array_map('intval', $event['user_ids']);

if (!$user_ids)
{
return;
}

foreach ([$this->notification_webpush_table, $this->push_subscriptions_table] as $table)
{
$sql = 'DELETE FROM ' . $table . '
WHERE ' . $this->db->sql_in_set('user_id', $user_ids);
$this->db->sql_query($sql);
}
}

/**
* Can notifications be used by the user?
*
Expand Down
82 changes: 82 additions & 0 deletions tests/event/fixtures/webpush.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_wpn_notification_push">
<column>notification_type_id</column>
<column>item_id</column>
<column>item_parent_id</column>
<column>user_id</column>
<column>push_data</column>
<column>notification_time</column>
<column>push_token</column>
<row>
<value>1</value>
<value>1</value>
<value>0</value>
<value>2</value>
<value>{}</value>
<value>1</value>
<value>token2</value>
</row>
<row>
<value>1</value>
<value>2</value>
<value>0</value>
<value>3</value>
<value>{}</value>
<value>2</value>
<value>token3</value>
</row>
<row>
<value>1</value>
<value>3</value>
<value>0</value>
<value>4</value>
<value>{}</value>
<value>3</value>
<value>token4</value>
</row>
</table>
<table name="phpbb_wpn_push_subscriptions">
<column>subscription_id</column>
Expand Down Expand Up @@ -34,4 +68,52 @@
<value>0</value>
</row>
</table>
<table name="phpbb_users">
<column>user_id</column>
<column>username</column>
<column>username_clean</column>
<column>user_permissions</column>
<column>user_sig</column>
<column>user_type</column>
<column>user_avatar</column>
<column>user_avatar_type</column>
<column>user_inactive_reason</column>
<column>user_posts</column>
<row>
<value>2</value>
<value>User 2</value>
<value>user 2</value>
<value></value>
<value></value>
<value>0</value>
<value></value>
<value></value>
<value>0</value>
<value>0</value>
</row>
<row>
<value>3</value>
<value>User 3</value>
<value>user 3</value>
<value></value>
<value></value>
<value>0</value>
<value></value>
<value></value>
<value>0</value>
<value>0</value>
</row>
<row>
<value>4</value>
<value>User 4</value>
<value>user 4</value>
<value></value>
<value></value>
<value>0</value>
<value></value>
<value></value>
<value>0</value>
<value>0</value>
</row>
</table>
</dataset>
43 changes: 42 additions & 1 deletion tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use phpbb\webpushnotifications\ext;

require_once __DIR__ . '/../../../../../includes/functions_acp.php';
require_once __DIR__ . '/../../../../../includes/functions_user.php';

class listener_test extends \phpbb_database_test_case
{
Expand Down Expand Up @@ -100,6 +101,8 @@ protected function setUp(): void
'pwa_icon_small' => '',
'pwa_icon_large' => '',
'pwa_short_name' => '',
'num_users' => 3,
'newest_user_id' => 4,
]);

$this->form_helper = new \phpbb\webpushnotifications\form\form_helper(
Expand Down Expand Up @@ -140,7 +143,10 @@ protected function set_listener()
$this->request,
$this->template,
$this->user,
$this->notifications
$this->notifications,
$this->db,
'phpbb_wpn_notification_push',
'phpbb_wpn_push_subscriptions'
);
}

Expand All @@ -159,9 +165,44 @@ public function test_getSubscribedEvents()
'core.acp_main_notice',
'core.help_manager_add_block_after',
'core.page_footer',
'core.delete_user_before',
], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents()));
}

public function test_user_deletion_removes_webpush_data()
{
global $cache, $config, $db, $phpbb_container, $phpbb_dispatcher;

$this->set_listener();

$config = $this->config;
$db = $this->db;
$cache = new \phpbb\cache\driver\dummy();
$phpbb_container = new \phpbb_mock_container_builder();
$phpbb_container->set('auth.provider_collection', new \phpbb\auth\provider_collection($phpbb_container, $this->config));
$phpbb_container->set('notification_manager', new \phpbb_mock_notification_manager());
$phpbb_container->setParameter('tables.auth_provider_oauth_token_storage', 'phpbb_oauth_tokens');
$phpbb_container->setParameter('tables.auth_provider_oauth_states', 'phpbb_oauth_states');
$phpbb_container->setParameter('tables.auth_provider_oauth_account_assoc', 'phpbb_oauth_accounts');
$phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications');

$phpbb_dispatcher = new \phpbb\event\dispatcher();
$phpbb_dispatcher->addSubscriber($this->listener);

user_delete('retain', [2, 3], true);

$this->assertSqlResultEquals([
['user_id' => '4'],
], 'SELECT user_id FROM phpbb_users ORDER BY user_id');

$this->assertSqlResultEquals([
['user_id' => '4'],
], 'SELECT user_id FROM phpbb_wpn_notification_push ORDER BY user_id');
$this->assertSqlResultEquals([
['user_id' => '4'],
], 'SELECT user_id FROM phpbb_wpn_push_subscriptions ORDER BY user_id');
}

public function test_load_language_on_setup($lang_set_ext = [])
{
$this->set_listener();
Expand Down