From e0b829ce2cdd95a56b0c80893866455b2fa80933 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Thu, 20 Apr 2023 12:42:40 -0700 Subject: [PATCH] Fix uid, bitfield, flags Signed-off-by: Matt Friedman --- controller/acp_controller.php | 11 ++++----- event/listener.php | 17 +++++++------- ext.php | 1 - manager/manager.php | 9 +++++--- migrations/v10x/m10_update_data.php | 3 +++ migrations/v10x/m9_schema_update.php | 3 +++ tests/controller/acp_controller_test.php | 14 +++++++++-- tests/fixtures/board_announcements.xml | 18 +++++++++++++++ ...manager_get_expired_announcements_test.php | 3 +++ .../manager_save_announcement_test.php | 23 +++++++++---------- .../manager_update_announcement_test.php | 3 +++ 11 files changed, 72 insertions(+), 33 deletions(-) diff --git a/controller/acp_controller.php b/controller/acp_controller.php index a9fb83d..a6ba334 100644 --- a/controller/acp_controller.php +++ b/controller/acp_controller.php @@ -230,12 +230,11 @@ protected function action_add() } // Prepare announcement text for storage - $uid = $bitfield = $options = ''; generate_text_for_storage( $data['announcement_text'], - $uid, - $bitfield, - $options, + $data['announcement_uid'], + $data['announcement_bitfield'], + $data['announcement_flags'], !$this->request->variable('disable_bbcode', false), !$this->request->variable('disable_magic_url', false), !$this->request->variable('disable_smilies', false) @@ -261,10 +260,10 @@ protected function action_add() } // Prepare a fresh announcement preview - $announcement_text_preview = $id || $preview ? generate_text_for_display($data['announcement_text'], '', '', ext::FLAGS) : ''; + $announcement_text_preview = $id || $preview ? generate_text_for_display($data['announcement_text'], $data['announcement_uid'], $data['announcement_bitfield'], $data['announcement_flags']) : ''; // Prepare the announcement text for editing inside the text box - $announcement_text_edit = generate_text_for_edit($data['announcement_text'], '', ext::FLAGS); + $announcement_text_edit = generate_text_for_edit($data['announcement_text'], $data['announcement_uid'], $data['announcement_flags']); // Output data to the template $this->template->assign_vars([ diff --git a/event/listener.php b/event/listener.php index 0aefc1b..9023ac0 100644 --- a/event/listener.php +++ b/event/listener.php @@ -10,7 +10,6 @@ namespace phpbb\boardannouncements\event; -use phpbb\boardannouncements\ext; use phpbb\boardannouncements\manager\manager; use phpbb\config\config; use phpbb\controller\helper; @@ -107,28 +106,28 @@ public function display_board_announcements() $board_announcements_data = $this->manager->get_visible_announcements($this->user->data['user_id']); - foreach ($board_announcements_data as $board_announcement_data) + foreach ($board_announcements_data as $data) { // Do not continue if announcements are only displayed on the board index, and the user is not currently viewing the board index - if ($board_announcement_data['announcement_indexonly'] && $this->user->page['page_name'] !== "index.$this->php_ext") + if ($data['announcement_indexonly'] && $this->user->page['page_name'] !== "index.$this->php_ext") { continue; } // Do not continue if announcement has been dismissed - if ($this->request->variable($this->config['cookie_name'] . '_ba_' . $board_announcement_data['announcement_id'], '', true, \phpbb\request\request_interface::COOKIE) == $board_announcement_data['announcement_timestamp']) + if ($this->request->variable($this->config['cookie_name'] . '_ba_' . $data['announcement_id'], '', true, \phpbb\request\request_interface::COOKIE) == $data['announcement_timestamp']) { continue; } // Output board announcement to the template $this->template->assign_block_vars('board_announcements', [ - 'BOARD_ANNOUNCEMENT_ID' => $board_announcement_data['announcement_id'], - 'S_BOARD_ANNOUNCEMENT_DISMISS' => (bool) $board_announcement_data['announcement_dismissable'], - 'BOARD_ANNOUNCEMENT' => generate_text_for_display($board_announcement_data['announcement_text'], '', '', ext::FLAGS), - 'BOARD_ANNOUNCEMENT_BGCOLOR' => $board_announcement_data['announcement_bgcolor'], + 'BOARD_ANNOUNCEMENT_ID' => $data['announcement_id'], + 'S_BOARD_ANNOUNCEMENT_DISMISS' => (bool) $data['announcement_dismissable'], + 'BOARD_ANNOUNCEMENT' => generate_text_for_display($data['announcement_text'], $data['announcement_uid'], $data['announcement_bitfield'], $data['announcement_flags']), + 'BOARD_ANNOUNCEMENT_BGCOLOR' => $data['announcement_bgcolor'], 'U_BOARD_ANNOUNCEMENT_CLOSE' => $this->controller_helper->route('phpbb_boardannouncements_controller', [ - 'id' => (int) $board_announcement_data['announcement_id'], + 'id' => (int) $data['announcement_id'], 'hash' => generate_link_hash('close_boardannouncement') ]), ]); diff --git a/ext.php b/ext.php index ccf5887..5089691 100644 --- a/ext.php +++ b/ext.php @@ -19,7 +19,6 @@ class ext extends \phpbb\extension\base public const MEMBERS = 1; public const GUESTS = 2; public const DATE_FORMAT = 'd M Y, H:i'; - public const FLAGS = OPTION_FLAG_BBCODE + OPTION_FLAG_SMILIES + OPTION_FLAG_LINKS; /** * Enable extension if phpBB minimum version requirement is met diff --git a/manager/manager.php b/manager/manager.php index 66274c8..267d8d4 100644 --- a/manager/manager.php +++ b/manager/manager.php @@ -35,7 +35,7 @@ public function get_announcement($id) { $data = $this->nestedset->get_subtree_data($id); - return count($data) ? $data[$id] : []; + return $data[$id] ?? []; } /** @@ -248,8 +248,11 @@ public function announcement_columns() 'announcement_indexonly' => false, 'announcement_dismissable' => true, 'announcement_users' => \phpbb\boardannouncements\ext::ALL, - 'announcement_timestamp' => '', - 'announcement_expiry' => '', + 'announcement_timestamp' => 0, + 'announcement_expiry' => 0, + 'announcement_uid' => '', + 'announcement_bitfield' => '', + 'announcement_flags' => OPTION_FLAG_BBCODE + OPTION_FLAG_SMILIES + OPTION_FLAG_LINKS, ]; } } diff --git a/migrations/v10x/m10_update_data.php b/migrations/v10x/m10_update_data.php index c52ad21..e3adeb0 100644 --- a/migrations/v10x/m10_update_data.php +++ b/migrations/v10x/m10_update_data.php @@ -81,6 +81,9 @@ public function convert_board_announcements() 'announcement_description' => '', 'announcement_text' => $config_text->get('announcement_text'), 'announcement_bgcolor' => $config_text->get('announcement_bgcolor'), + 'announcement_uid' => $config_text->get('announcement_uid'), + 'announcement_bitfield' => $config_text->get('announcement_bitfield'), + 'announcement_flags' => (int) $config_text->get('announcement_options'), 'announcement_timestamp' => (int) $config_text->get('announcement_timestamp'), 'announcement_enabled' => (int) $this->config->offsetGet('board_announcements_enable'), 'announcement_indexonly' => (int) $this->config->offsetGet('board_announcements_index_only'), diff --git a/migrations/v10x/m9_schema_update.php b/migrations/v10x/m9_schema_update.php index 0345714..2f7503a 100644 --- a/migrations/v10x/m9_schema_update.php +++ b/migrations/v10x/m9_schema_update.php @@ -53,6 +53,9 @@ public function update_schema() 'announcement_users' => ['UINT', 0], 'announcement_timestamp' => ['TIMESTAMP', 0], 'announcement_expiry' => ['TIMESTAMP', 0], + 'announcement_uid' => ['VCHAR:8', ''], + 'announcement_bitfield' => ['VCHAR:255', ''], + 'announcement_flags' => ['UINT:11', 7], 'announcement_parent_id' => ['UINT', 0], 'announcement_left_id' => ['UINT', 0], 'announcement_right_id' => ['UINT', 0], diff --git a/tests/controller/acp_controller_test.php b/tests/controller/acp_controller_test.php index f8650e6..4acf60d 100644 --- a/tests/controller/acp_controller_test.php +++ b/tests/controller/acp_controller_test.php @@ -267,6 +267,9 @@ public function action_add_data() 'announcement_users' => \phpbb\boardannouncements\ext::ALL, 'announcement_timestamp' => '', 'announcement_expiry' => '', + 'announcement_uid' => '', + 'announcement_bitfield' => '', + 'announcement_flags' => 7, ]], [0, [ 'announcement_id' => 2, @@ -279,6 +282,9 @@ public function action_add_data() 'announcement_users' => \phpbb\boardannouncements\ext::ALL, 'announcement_timestamp' => '', 'announcement_expiry' => '', + 'announcement_uid' => '', + 'announcement_bitfield' => '', + 'announcement_flags' => 7, ]], ]; } @@ -318,8 +324,12 @@ public function test_action_add($id, $data) 'announcement_indexonly' => false, 'announcement_dismissable' => true, 'announcement_users' => \phpbb\boardannouncements\ext::ALL, - 'announcement_timestamp' => '', - 'announcement_expiry' => '', + 'announcement_timestamp' => 0, + 'announcement_expiry' => 0, + 'announcement_uid' => '', + 'announcement_bitfield' => '', + 'announcement_flags' => 7, + ]); $this->manager->expects($id ? self::once() : self::never()) diff --git a/tests/fixtures/board_announcements.xml b/tests/fixtures/board_announcements.xml index 0736fd3..d6488b2 100644 --- a/tests/fixtures/board_announcements.xml +++ b/tests/fixtures/board_announcements.xml @@ -11,6 +11,9 @@ announcement_users announcement_timestamp announcement_expiry + announcement_uid + announcement_bitfield + announcement_flags announcement_parent_id announcement_left_id announcement_right_id @@ -26,6 +29,9 @@ 0 1586466410 0 + + + 7 0 1 2 @@ -42,6 +48,9 @@ 1 1586466510 0 + + + 7 0 3 4 @@ -58,6 +67,9 @@ 2 1586466610 0 + + + 7 0 5 6 @@ -74,6 +86,9 @@ 0 1586466710 1586466810 + + + 7 0 7 8 @@ -90,6 +105,9 @@ 0 1586466810 0 + + + 7 0 9 10 diff --git a/tests/manager/manager_get_expired_announcements_test.php b/tests/manager/manager_get_expired_announcements_test.php index a330571..d50f03d 100644 --- a/tests/manager/manager_get_expired_announcements_test.php +++ b/tests/manager/manager_get_expired_announcements_test.php @@ -34,6 +34,9 @@ public function data_get_expired_announcements() 'announcement_users' => \phpbb\boardannouncements\ext::ALL, 'announcement_timestamp' => 1586466710, 'announcement_expiry' => 1586466810, + 'announcement_uid' => '', + 'announcement_bitfield' => '', + 'announcement_flags' => 7, 'announcement_parent_id' => 0, 'announcement_left_id' => 7, 'announcement_right_id' => 8, diff --git a/tests/manager/manager_save_announcement_test.php b/tests/manager/manager_save_announcement_test.php index 05d46d5..0e49a75 100644 --- a/tests/manager/manager_save_announcement_test.php +++ b/tests/manager/manager_save_announcement_test.php @@ -30,6 +30,10 @@ public function data_save_announcement() 'announcement_users' => 0, 'announcement_timestamp' => time(), 'announcement_expiry' => 0, + 'announcement_uid' => '', + 'announcement_bitfield' => '', + 'announcement_flags' => 7, + ]], ]; } @@ -38,22 +42,17 @@ public function data_save_announcement() * Test the save_announcement() method * * @dataProvider data_save_announcement - * @param int $expected_id + * @param int $id * @param array $data */ - public function test_save_announcement($expected_id, $data) + public function test_save_announcement($id, $data) { $this->manager->save_announcement($data); - self::assertEquals($expected_id, $this->manager->get_announcement_data($expected_id, 'announcement_id')); - self::assertEquals($data['announcement_text'], $this->manager->get_announcement_data($expected_id, 'announcement_text')); - self::assertEquals($data['announcement_description'], $this->manager->get_announcement_data($expected_id, 'announcement_description')); - self::assertEquals($data['announcement_bgcolor'], $this->manager->get_announcement_data($expected_id, 'announcement_bgcolor')); - self::assertEquals($data['announcement_enabled'], $this->manager->get_announcement_data($expected_id, 'announcement_enabled')); - self::assertEquals($data['announcement_indexonly'], $this->manager->get_announcement_data($expected_id, 'announcement_indexonly')); - self::assertEquals($data['announcement_dismissable'], $this->manager->get_announcement_data($expected_id, 'announcement_dismissable')); - self::assertEquals($data['announcement_users'], $this->manager->get_announcement_data($expected_id, 'announcement_users')); - self::assertEquals($data['announcement_timestamp'], $this->manager->get_announcement_data($expected_id, 'announcement_timestamp')); - self::assertEquals($data['announcement_expiry'], $this->manager->get_announcement_data($expected_id, 'announcement_expiry')); + self::assertEquals($id, $this->manager->get_announcement_data($id, 'announcement_id')); + foreach ($data as $key => $expected) + { + self::assertEquals($expected, $this->manager->get_announcement_data($id, $key)); + } } } diff --git a/tests/manager/manager_update_announcement_test.php b/tests/manager/manager_update_announcement_test.php index 72bed29..5f86de9 100644 --- a/tests/manager/manager_update_announcement_test.php +++ b/tests/manager/manager_update_announcement_test.php @@ -30,6 +30,9 @@ public function data_update_announcement() 'announcement_users' => 1, 'announcement_timestamp' => time(), 'announcement_expiry' => 1, + 'announcement_uid' => '', + 'announcement_bitfield' => '', + 'announcement_flags' => 7, ]], [9, []], ];