From 5c58fd2055c2cb137af9f1759712fbff2eff81e9 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Tue, 4 Jun 2024 11:10:44 -0700 Subject: [PATCH] [ticket/17333] Add tests and fixes PHPBB-17333 Signed-off-by: Matt Friedman --- phpBB/phpbb/notification/method/webpush.php | 2 +- .../functional/notification_webpush_test.php | 127 ++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 tests/functional/notification_webpush_test.php diff --git a/phpBB/phpbb/notification/method/webpush.php b/phpBB/phpbb/notification/method/webpush.php index dcf1d1ebbf..a823aed6a0 100644 --- a/phpBB/phpbb/notification/method/webpush.php +++ b/phpBB/phpbb/notification/method/webpush.php @@ -96,7 +96,7 @@ class webpush extends messenger_base implements extended_method_interface */ public function is_enabled_by_default() { - return $this->config['webpush_method_enables']; + return (bool) $this->config['webpush_method_enables']; } /** diff --git a/tests/functional/notification_webpush_test.php b/tests/functional/notification_webpush_test.php new file mode 100644 index 0000000000..a07e0a8948 --- /dev/null +++ b/tests/functional/notification_webpush_test.php @@ -0,0 +1,127 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +/** +* @group functional +*/ +class phpbb_functional_notification_webpush_test extends phpbb_functional_test_case +{ + public function test_acp_module() + { + $this->login(); + $this->admin_login(); + + $this->add_lang(['acp/board', 'acp/common']); + + $crawler = self::request('GET', 'adm/index.php?i=acp_board&mode=webpush&sid=' . $this->sid); + + $this->assertContainsLang('ACP_WEBPUSH_SETTINGS', $crawler->filter('div.main > h1')->text()); + $this->assertContainsLang('ACP_WEBPUSH_SETTINGS_EXPLAIN', $crawler->filter('div.main > p')->text()); + $this->assertContainsLang('WEBPUSH_GENERATE_VAPID_KEYS', $crawler->filter('input[type="button"]')->attr('value')); + + $form_data = [ + 'config[webpush_enable]' => 1, + 'config[webpush_vapid_public]' => 'BDnYSJHVZBxq834LqDGr893IfazEez7q-jYH2QBNlT0ji2C9UwGosiqz8Dp_ZN23lqAngBZyRjXVWF4ZLA8X2zI', + 'config[webpush_vapid_private]' => 'IE5OYlmfWsMbBU1lzvr0bxrxVAXIteSkAnwGlZIhmRk', + 'config[webpush_method_enables]' => 1, + 'config[webpush_dropdown_subscribe]' => 1, + ]; + $form = $crawler->selectButton('submit')->form($form_data); + $crawler = self::submit($form); + $this->assertStringContainsString($this->lang('CONFIG_UPDATED'), $crawler->filter('.successbox')->text()); + + $crawler = self::request('GET', 'adm/index.php?i=acp_board&mode=webpush&sid=' . $this->sid); + + foreach ($form_data as $config_name => $config_value) + { + $config_value = ($config_name === 'config[webpush_vapid_private]') ? '********' : $config_value; + $this->assertEquals($config_value, $crawler->filter('input[name="' . $config_name . '"]')->attr('value')); + } + } + + public function test_ucp_module() + { + $this->login(); + $this->admin_login(); + + $this->add_lang('ucp'); + + $crawler = self::request('GET', 'ucp.php?i=ucp_notifications&mode=notification_options'); + + $this->assertContainsLang('NOTIFY_WEBPUSH_ENABLE', $crawler->filter('label[for="subscribe_webpush"]')->text()); + $this->assertContainsLang('NOTIFICATION_METHOD_WEBPUSH', $crawler->filter('th.mark')->eq(2)->text()); + + // Assert checkbox is checked + $wp_list = $crawler->filter('.table1'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.bookmark_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.mention_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.post_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.quote_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.topic_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.forum_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.group_request_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.pm_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.report_pm_closed_notification.method.webpush'); + $this->assert_checkbox_is_checked($wp_list, 'notification.type.report_post_closed_notification.method.webpush'); + + $this->set_acp_option('webpush_method_enables', 0); + + $crawler = self::request('GET', 'ucp.php?i=ucp_notifications&mode=notification_options'); + + // Assert checkbox is unchecked + $wp_list = $crawler->filter('.table1'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.bookmark_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.mention_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.post_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.quote_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.topic_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.forum_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.group_request_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.pm_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.report_pm_closed_notification.method.webpush'); + $this->assert_checkbox_is_unchecked($wp_list, 'notification.type.report_post_closed_notification.method.webpush'); + } + + public function test_dropdown_subscribe_button() + { + $this->login(); + $this->admin_login(); + + // Assert subscribe dropdown is present + $crawler = self::request('GET', 'index.php'); + $this->assertCount(1, $crawler->filter('.notification-dropdown-footer')); + $this->assertContainsLang('NOTIFY_WEB_PUSH_SUBSCRIBE', $crawler->filter('.notification-dropdown-footer #subscribe_webpush')->text()); + $this->assertContainsLang('NOTIFY_WEB_PUSH_SUBSCRIBED', $crawler->filter('.notification-dropdown-footer #unsubscribe_webpush')->text()); + + // Assert subscribe button is not displayed in UCP when dropdown subscribe is present + $crawler = self::request('GET', 'ucp.php?i=ucp_notifications&mode=notification_options'); + $this->assertCount(0, $crawler->filter('.notification-dropdown-footer')); + + $this->set_acp_option('webpush_dropdown_subscribe', 0); + + // Assert subscribe dropdown is not present by default + $crawler = self::request('GET', 'index.php'); + $this->assertCount(0, $crawler->filter('.notification-dropdown-footer')); + } + + protected function set_acp_option($option, $value) + { + $crawler = self::request('GET', 'adm/index.php?i=acp_board&mode=webpush&sid=' . $this->sid); + $form = $crawler->selectButton('Submit')->form(); + $values = $form->getValues(); + $values["config[{$option}]"] = $value; + $form->setValues($values); + $crawler = self::submit($form); + $this->assertEquals(1, $crawler->filter('.successbox')->count()); + } +}