From 3c8b4cc4c58ad16b7cc15b1b970364274d26475d Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Tue, 11 Nov 2025 09:11:57 -0800 Subject: [PATCH] Change idea author when post author is changed --- event/listener.php | 23 ++++++++++++ factory/idea.php | 23 ++++++++++++ tests/event/listener_test.php | 52 ++++++++++++++++++++++++++++ tests/ideas/idea_attributes_test.php | 15 ++++++++ 4 files changed, 113 insertions(+) diff --git a/event/listener.php b/event/listener.php index bfe94f0..3603895 100644 --- a/event/listener.php +++ b/event/listener.php @@ -91,6 +91,7 @@ public static function getSubscribedEvents() 'core.posting_modify_template_vars' => 'submit_idea_template', 'core.posting_modify_submit_post_before' => 'submit_idea_before', 'core.posting_modify_submit_post_after' => [['submit_idea_after'], ['edit_idea_title']], + 'core.mcp_change_poster_after' => 'change_idea_author', ); } @@ -390,6 +391,28 @@ public function edit_idea_title($event) $this->idea->set_title($idea['idea_id'], $event['post_data']['post_subject']); } + /** + * Change an idea's author when the post author is changed + * + * @param \phpbb\event\data $event The event object + * @return void + */ + public function change_idea_author($event) + { + $forum_id = (int) $event['post_info']['forum_id']; + $topic_id = (int) $event['post_info']['topic_id']; + $old_author_id = (int) $event['post_info']['poster_id']; + $new_author_id = (int) $event['userdata']['user_id']; + + if ($old_author_id === $new_author_id || !$this->is_ideas_forum($forum_id)) + { + return; + } + + $idea = $this->idea->get_idea_by_topic_id($topic_id); + $this->idea->set_author($idea['idea_id'], $new_author_id); + } + /** * Test if we are on the posting page for a new idea * diff --git a/factory/idea.php b/factory/idea.php index 337cf98..40f9047 100644 --- a/factory/idea.php +++ b/factory/idea.php @@ -227,6 +227,29 @@ public function get_title($id) return $idea_title ?: ''; } + /** + * Set the author of an idea + * + * @param int $idea_id + * @param int $user_id + * @return bool True if set, false if invalid. + */ + public function set_author($idea_id, $user_id) + { + if (!$user_id || !is_numeric($user_id)) + { + return false; + } + + $sql_ary = array( + 'idea_author' => (int) $user_id, + ); + + $this->update_idea_data($sql_ary, $idea_id, $this->table_ideas); + + return true; + } + /** * Submit new idea data to the ideas table * diff --git a/tests/event/listener_test.php b/tests/event/listener_test.php index 36480ee..37172b9 100644 --- a/tests/event/listener_test.php +++ b/tests/event/listener_test.php @@ -115,6 +115,7 @@ public function test_getSubscribedEvents() 'core.posting_modify_template_vars', 'core.posting_modify_submit_post_before', 'core.posting_modify_submit_post_after', + 'core.mcp_change_poster_after', ), array_keys(\phpbb\ideas\event\listener::getSubscribedEvents())); } @@ -672,6 +673,57 @@ public function test_submit_idea($mode, $forum_id, $topic_id, $approved, $succes $listener->submit_idea_after($event); } + + /** + * Data set for change_idea_author + * + * @return array Array of test data + */ + public function change_idea_author_data() + { + return [ + [2, 1, 1, 2, true], // Valid: ideas forum, different authors + [1, 1, 1, 2, false], // Invalid: not ideas forum + [2, 1, 1, 1, false], // Invalid: same author + ]; + } + + /** + * Test the change_idea_author event + * + * @dataProvider change_idea_author_data + */ + public function test_change_idea_author($forum_id, $topic_id, $old_author_id, $new_author_id, $should_update) + { + $listener = $this->get_listener(); + + $event = new \phpbb\event\data([ + 'post_info' => [ + 'forum_id' => $forum_id, + 'topic_id' => $topic_id, + 'poster_id' => $old_author_id, + ], + 'userdata' => [ + 'user_id' => $new_author_id, + ], + ]); + + $idea_data = [ + 'idea_id' => 1, + 'idea_author' => $old_author_id, + ]; + + $this->idea->expects($should_update ? self::once() : self::never()) + ->method('get_idea_by_topic_id') + ->with($topic_id) + ->willReturn($idea_data); + + $this->idea->expects($should_update ? self::once() : self::never()) + ->method('set_author') + ->with(1, $new_author_id); + + $listener->change_idea_author($event); + } } /** diff --git a/tests/ideas/idea_attributes_test.php b/tests/ideas/idea_attributes_test.php index c45eaf0..24692f4 100644 --- a/tests/ideas/idea_attributes_test.php +++ b/tests/ideas/idea_attributes_test.php @@ -111,6 +111,7 @@ public function idea_attribute_test_data() array( array( 'idea_id' => 1, + 'idea_author' => 2, 'duplicate_id' => 2, 'rfc_link' => 'https://area51.phpbb.com/phpBB/viewtopic.php?foo&bar', 'implemented_version' => '3.1.0', @@ -121,6 +122,7 @@ public function idea_attribute_test_data() array( array( 'idea_id' => 2, + 'idea_author' => 3, 'duplicate_id' => 1, 'rfc_link' => 'https://area51.phpbb.com/phpBB/viewtopic.php?bar&foo', 'implemented_version' => '3.2.0', @@ -131,6 +133,7 @@ public function idea_attribute_test_data() array( array( 'idea_id' => 3, + 'idea_author' => 4, 'duplicate_id' => '5', 'rfc_link' => '', 'implemented_version' => '', @@ -141,6 +144,7 @@ public function idea_attribute_test_data() array( array( 'idea_id' => 4, + 'idea_author' => '', 'duplicate_id' => 'foo', 'rfc_link' => 'https://www.phpbb.com/phpBB/viewtopic.php?foo', 'implemented_version' => 'foo', @@ -151,6 +155,7 @@ public function idea_attribute_test_data() array( array( 'idea_id' => 5, + 'idea_author' => 'foo', 'duplicate_id' => array(1), 'rfc_link' => 'foobar', 'implemented_version' => '1', @@ -211,6 +216,16 @@ public function test_set_title($data, $expected) $this->set_attribute_test('set_title', 'idea_title', $data, $expected); } + /** + * Test set_author() + * + * @dataProvider idea_attribute_test_data + */ + public function test_set_author($data, $expected) + { + $this->set_attribute_test('set_author', 'idea_author', $data, $expected); + } + /** * Set attribute test runner *