Skip to content

Commit 6b14985

Browse files
authored
Merge pull request #165 from iMattPro/ticket/164
Change idea author when post author changes
2 parents 024d15e + 3c8b4cc commit 6b14985

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

event/listener.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public static function getSubscribedEvents()
9191
'core.posting_modify_template_vars' => 'submit_idea_template',
9292
'core.posting_modify_submit_post_before' => 'submit_idea_before',
9393
'core.posting_modify_submit_post_after' => [['submit_idea_after'], ['edit_idea_title']],
94+
'core.mcp_change_poster_after' => 'change_idea_author',
9495
);
9596
}
9697

@@ -390,6 +391,28 @@ public function edit_idea_title($event)
390391
$this->idea->set_title($idea['idea_id'], $event['post_data']['post_subject']);
391392
}
392393

394+
/**
395+
* Change an idea's author when the post author is changed
396+
*
397+
* @param \phpbb\event\data $event The event object
398+
* @return void
399+
*/
400+
public function change_idea_author($event)
401+
{
402+
$forum_id = (int) $event['post_info']['forum_id'];
403+
$topic_id = (int) $event['post_info']['topic_id'];
404+
$old_author_id = (int) $event['post_info']['poster_id'];
405+
$new_author_id = (int) $event['userdata']['user_id'];
406+
407+
if ($old_author_id === $new_author_id || !$this->is_ideas_forum($forum_id))
408+
{
409+
return;
410+
}
411+
412+
$idea = $this->idea->get_idea_by_topic_id($topic_id);
413+
$this->idea->set_author($idea['idea_id'], $new_author_id);
414+
}
415+
393416
/**
394417
* Test if we are on the posting page for a new idea
395418
*

factory/idea.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,29 @@ public function get_title($id)
227227
return $idea_title ?: '';
228228
}
229229

230+
/**
231+
* Set the author of an idea
232+
*
233+
* @param int $idea_id
234+
* @param int $user_id
235+
* @return bool True if set, false if invalid.
236+
*/
237+
public function set_author($idea_id, $user_id)
238+
{
239+
if (!$user_id || !is_numeric($user_id))
240+
{
241+
return false;
242+
}
243+
244+
$sql_ary = array(
245+
'idea_author' => (int) $user_id,
246+
);
247+
248+
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
249+
250+
return true;
251+
}
252+
230253
/**
231254
* Submit new idea data to the ideas table
232255
*

tests/event/listener_test.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function test_getSubscribedEvents()
115115
'core.posting_modify_template_vars',
116116
'core.posting_modify_submit_post_before',
117117
'core.posting_modify_submit_post_after',
118+
'core.mcp_change_poster_after',
118119
), array_keys(\phpbb\ideas\event\listener::getSubscribedEvents()));
119120
}
120121

@@ -672,6 +673,57 @@ public function test_submit_idea($mode, $forum_id, $topic_id, $approved, $succes
672673

673674
$listener->submit_idea_after($event);
674675
}
676+
677+
/**
678+
* Data set for change_idea_author
679+
*
680+
* @return array Array of test data
681+
*/
682+
public function change_idea_author_data()
683+
{
684+
return [
685+
[2, 1, 1, 2, true], // Valid: ideas forum, different authors
686+
[1, 1, 1, 2, false], // Invalid: not ideas forum
687+
[2, 1, 1, 1, false], // Invalid: same author
688+
];
689+
}
690+
691+
/**
692+
* Test the change_idea_author event
693+
*
694+
* @dataProvider change_idea_author_data
695+
*/
696+
public function test_change_idea_author($forum_id, $topic_id, $old_author_id, $new_author_id, $should_update)
697+
{
698+
$listener = $this->get_listener();
699+
700+
$event = new \phpbb\event\data([
701+
'post_info' => [
702+
'forum_id' => $forum_id,
703+
'topic_id' => $topic_id,
704+
'poster_id' => $old_author_id,
705+
],
706+
'userdata' => [
707+
'user_id' => $new_author_id,
708+
],
709+
]);
710+
711+
$idea_data = [
712+
'idea_id' => 1,
713+
'idea_author' => $old_author_id,
714+
];
715+
716+
$this->idea->expects($should_update ? self::once() : self::never())
717+
->method('get_idea_by_topic_id')
718+
->with($topic_id)
719+
->willReturn($idea_data);
720+
721+
$this->idea->expects($should_update ? self::once() : self::never())
722+
->method('set_author')
723+
->with(1, $new_author_id);
724+
725+
$listener->change_idea_author($event);
726+
}
675727
}
676728

677729
/**

tests/ideas/idea_attributes_test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public function idea_attribute_test_data()
111111
array(
112112
array(
113113
'idea_id' => 1,
114+
'idea_author' => 2,
114115
'duplicate_id' => 2,
115116
'rfc_link' => 'https://area51.phpbb.com/phpBB/viewtopic.php?foo&bar',
116117
'implemented_version' => '3.1.0',
@@ -121,6 +122,7 @@ public function idea_attribute_test_data()
121122
array(
122123
array(
123124
'idea_id' => 2,
125+
'idea_author' => 3,
124126
'duplicate_id' => 1,
125127
'rfc_link' => 'https://area51.phpbb.com/phpBB/viewtopic.php?bar&foo',
126128
'implemented_version' => '3.2.0',
@@ -131,6 +133,7 @@ public function idea_attribute_test_data()
131133
array(
132134
array(
133135
'idea_id' => 3,
136+
'idea_author' => 4,
134137
'duplicate_id' => '5',
135138
'rfc_link' => '',
136139
'implemented_version' => '',
@@ -141,6 +144,7 @@ public function idea_attribute_test_data()
141144
array(
142145
array(
143146
'idea_id' => 4,
147+
'idea_author' => '',
144148
'duplicate_id' => 'foo',
145149
'rfc_link' => 'https://www.phpbb.com/phpBB/viewtopic.php?foo',
146150
'implemented_version' => 'foo',
@@ -151,6 +155,7 @@ public function idea_attribute_test_data()
151155
array(
152156
array(
153157
'idea_id' => 5,
158+
'idea_author' => 'foo',
154159
'duplicate_id' => array(1),
155160
'rfc_link' => 'foobar',
156161
'implemented_version' => '1',
@@ -211,6 +216,16 @@ public function test_set_title($data, $expected)
211216
$this->set_attribute_test('set_title', 'idea_title', $data, $expected);
212217
}
213218

219+
/**
220+
* Test set_author()
221+
*
222+
* @dataProvider idea_attribute_test_data
223+
*/
224+
public function test_set_author($data, $expected)
225+
{
226+
$this->set_attribute_test('set_author', 'idea_author', $data, $expected);
227+
}
228+
214229
/**
215230
* Set attribute test runner
216231
*

0 commit comments

Comments
 (0)