Skip to content

Commit 9edb493

Browse files
committed
Move idea logic out of the notification type and back to idea factory
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent 7b68523 commit 9edb493

5 files changed

Lines changed: 12 additions & 70 deletions

File tree

config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,6 @@ services:
130130
parent: notification.type.base
131131
shared: false
132132
calls:
133-
- [set_additional_services, ['@config', '@controller.helper', '@phpbb.ideas.idea', '@user_loader']]
133+
- [set_additional_services, ['@config', '@controller.helper', '@user_loader']]
134134
tags:
135135
- { name: notification.type }

factory/idea.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ public function set_status($idea_id, $status)
7272
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
7373

7474
// Send a notification
75+
$idea = $this->get_idea($idea_id);
7576
$notifications = $this->notification_manager->get_notified_users(ext::NOTIFICATION_TYPE_STATUS, ['item_id' => (int) $idea_id]);
7677
$this->notification_manager->{empty($notifications) ? 'add_notifications' : 'update_notifications'}(
7778
ext::NOTIFICATION_TYPE_STATUS,
7879
[
7980
'idea_id' => (int) $idea_id,
8081
'status' => (int) $status,
8182
'user_id' => (int) $this->user->data['user_id'],
83+
'idea_author' => (int) $idea['idea_author'],
84+
'idea_title' => $idea['idea_title'],
8285
]
8386
);
8487
}

notification/type/status.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use phpbb\config\config;
1414
use phpbb\controller\helper;
1515
use phpbb\ideas\ext;
16-
use phpbb\ideas\factory\idea;
1716
use phpbb\user_loader;
1817
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1918

@@ -28,31 +27,23 @@ class status extends \phpbb\notification\type\base
2827
/** @var helper */
2928
protected $helper;
3029

31-
/** @var idea */
32-
protected $idea;
33-
3430
/** @var user_loader */
3531
protected $user_loader;
3632

3733
/** @var int */
3834
protected $ideas_forum_id;
3935

40-
/** @var array */
41-
protected $idea_cache = [];
42-
4336
/**
4437
* Set additional services and properties
4538
*
4639
* @param config $config
4740
* @param helper $helper
48-
* @param idea $idea
4941
* @param user_loader $user_loader
5042
* @return void
5143
*/
52-
public function set_additional_services(config $config, helper $helper, idea $idea, user_loader $user_loader)
44+
public function set_additional_services(config $config, helper $helper, user_loader $user_loader)
5345
{
5446
$this->helper = $helper;
55-
$this->idea = $idea;
5647
$this->user_loader = $user_loader;
5748
$this->ideas_forum_id = (int) $config['ideas_forum_id'];
5849
}
@@ -120,9 +111,7 @@ public function find_users_for_notification($type_data, $options = [])
120111
'ignore_users' => [],
121112
], $options);
122113

123-
$idea = $this->load_idea($type_data['idea_id']);
124-
125-
$users = $idea ? [$idea['idea_author']] : [];
114+
$users = [$type_data['idea_author']];
126115

127116
return $this->get_authorised_recipients($users, $this->ideas_forum_id, $options);
128117
}
@@ -206,31 +195,11 @@ public function get_email_template_variables()
206195
*/
207196
public function create_insert_array($type_data, $pre_create_data = [])
208197
{
209-
$idea = $this->load_idea($type_data['idea_id']);
210-
211198
$this->set_data('idea_id', (int) $type_data['idea_id']);
212199
$this->set_data('status', (int) $type_data['status']);
213200
$this->set_data('updater_id', (int) $type_data['user_id']);
214-
$this->set_data('idea_title', $idea ? $idea['idea_title'] : '');
201+
$this->set_data('idea_title', $type_data['idea_title']);
215202

216203
parent::create_insert_array($type_data, $pre_create_data);
217204
}
218-
219-
/**
220-
* Load and cache an idea by id
221-
*
222-
* @param int $idea_id
223-
* @return array|false
224-
*/
225-
protected function load_idea($idea_id)
226-
{
227-
$idea_id = (int) $idea_id;
228-
229-
if (!array_key_exists($idea_id, $this->idea_cache))
230-
{
231-
$this->idea_cache[$idea_id] = $this->idea->get_idea($idea_id) ?: false;
232-
}
233-
234-
return $this->idea_cache[$idea_id];
235-
}
236205
}

tests/ideas/idea_attributes_test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function test_set_status($idea_id, $status)
7272
self::assertEquals($status, $idea['idea_status']);
7373
}
7474

75-
public function test_set_status_notification_data()
75+
public function set_status_notification_data()
7676
{
7777
return [
7878
[1, 1, [], 'add_notifications'],
@@ -83,7 +83,7 @@ public function test_set_status_notification_data()
8383
}
8484

8585
/**
86-
* @dataProvider test_set_status_notification_data
86+
* @dataProvider set_status_notification_data
8787
*/
8888
public function test_set_status_notification($idea_id, $status, $notified_users, $expected)
8989
{

tests/notification/status_test.php

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class status_test extends \phpbb_test_case
2424
/** @var \phpbb\controller\helper|\PHPUnit\Framework\MockObject\MockObject */
2525
protected $helper;
2626

27-
/** @var \phpbb\ideas\factory\idea|\PHPUnit\Framework\MockObject\MockObject */
28-
protected $idea_factory;
29-
3027
/** @var \phpbb\user_loader|\PHPUnit\Framework\MockObject\MockObject */
3128
protected $user_loader;
3229

@@ -47,7 +44,6 @@ protected function setUp(): void
4744

4845
$this->config = $this->createMock(\phpbb\config\config::class);
4946
$this->helper = $this->createMock(\phpbb\controller\helper::class);
50-
$this->idea_factory = $this->createMock(\phpbb\ideas\factory\idea::class);
5147
$this->user_loader = $this->createMock(\phpbb\user_loader::class);
5248
$this->auth = $this->createMock(\phpbb\auth\auth::class);
5349
$this->language = $this->createMock(\phpbb\language\language::class);
@@ -64,7 +60,7 @@ protected function setUp(): void
6460
->willReturn($this->forum_id);
6561

6662
$this->notification_type = new status($db, $this->language, $user, $this->auth, $phpbb_root_path, $phpEx, 'phpbb_user_notifications');
67-
$this->notification_type->set_additional_services($this->config, $this->helper, $this->idea_factory, $this->user_loader);
63+
$this->notification_type->set_additional_services($this->config, $this->helper, $this->user_loader);
6864

6965
// Set protected properties using reflection
7066
$reflection = new \ReflectionClass($this->notification_type);
@@ -130,20 +126,14 @@ public function test_find_users_for_notification()
130126
$idea_id = 1;
131127
$idea_author = 2;
132128

133-
$type_data = ['idea_id' => $idea_id];
134-
$idea_data = ['idea_author' => $idea_author];
129+
$type_data = ['idea_id' => $idea_id, 'idea_author' => $idea_author];
135130
$default_methods = ['board', 'email'];
136131

137132
$this->auth->expects($this->once())
138133
->method('acl_get_list')
139134
->with([$idea_author], 'f_read', $this->forum_id)
140135
->willReturn([$this->forum_id => ['f_read' => [$idea_author]]]);
141136

142-
$this->idea_factory->expects($this->once())
143-
->method('get_idea')
144-
->with($idea_id)
145-
->willReturn($idea_data);
146-
147137
$this->notification_manager->expects($this->once())
148138
->method('get_default_methods')
149139
->willReturn($default_methods);
@@ -152,19 +142,6 @@ public function test_find_users_for_notification()
152142
$this->assertEquals([$idea_author => $default_methods], $result);
153143
}
154144

155-
public function test_find_users_for_notification_idea_not_found()
156-
{
157-
$type_data = ['idea_id' => 999];
158-
159-
$this->idea_factory->expects($this->once())
160-
->method('get_idea')
161-
->with(999)
162-
->willReturn(false);
163-
164-
$result = $this->notification_type->find_users_for_notification($type_data);
165-
$this->assertEquals([], $result);
166-
}
167-
168145
public function test_get_avatar_with_author()
169146
{
170147
$this->setNotificationData(['updater_id' => 5]);
@@ -333,17 +310,10 @@ public function test_create_insert_array()
333310
$type_data = [
334311
'idea_id' => 7,
335312
'status' => 4,
336-
'user_id' => 3
337-
];
338-
$idea_data = [
313+
'user_id' => 3,
339314
'idea_title' => 'Sample Idea'
340315
];
341316

342-
$this->idea_factory->expects($this->once())
343-
->method('get_idea')
344-
->with(7)
345-
->willReturn($idea_data);
346-
347317
$this->notification_type->create_insert_array($type_data);
348318

349319
// Verify data was set by checking get_data

0 commit comments

Comments
 (0)