Skip to content
Open
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
23 changes: 23 additions & 0 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}

Expand Down Expand Up @@ -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
*
Expand Down
23 changes: 23 additions & 0 deletions factory/idea.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
52 changes: 52 additions & 0 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/ideas/idea_attributes_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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' => '',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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
*
Expand Down