Skip to content

Commit 02e7faf

Browse files
committed
Add unit tests
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent 3c8c3b6 commit 02e7faf

File tree

2 files changed

+352
-1
lines changed

2 files changed

+352
-1
lines changed

factory/idea.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function set_status($idea_id, $status)
7575
// Increment our notifications sent counter
7676
$this->config->increment('ideas_status_notifications_id', 1);
7777
$this->notification_manager->add_notifications('phpbb.ideas.notification.type.status', [
78-
'item_id' => $this->config['ideas_status_notifications_id'],
78+
'item_id' => (int) $this->config['ideas_status_notifications_id'],
7979
'idea_id' => (int) $idea_id,
8080
'status' => (int) $status,
8181
]);

tests/notification/status_test.php

Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
<?php
2+
/**
3+
*
4+
* Ideas extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\ideas\tests\notification\type;
12+
13+
use phpbb\ideas\notification\type\status;
14+
15+
class status_test extends \phpbb_test_case
16+
{
17+
/** @var status */
18+
protected $notification_type;
19+
20+
/** @var \phpbb\config\config|\PHPUnit\Framework\MockObject\MockObject */
21+
protected $config;
22+
23+
/** @var \phpbb\controller\helper|\PHPUnit\Framework\MockObject\MockObject */
24+
protected $helper;
25+
26+
/** @var \phpbb\ideas\factory\idea|\PHPUnit\Framework\MockObject\MockObject */
27+
protected $idea_factory;
28+
29+
/** @var \phpbb\user_loader|\PHPUnit\Framework\MockObject\MockObject */
30+
protected $user_loader;
31+
32+
/** @var \phpbb\auth\auth|\PHPUnit\Framework\MockObject\MockObject */
33+
protected $auth;
34+
35+
/** @var \phpbb\language\language|\PHPUnit\Framework\MockObject\MockObject */
36+
protected $language;
37+
38+
/** @var \phpbb\notification\manager|\PHPUnit\Framework\MockObject\MockObject */
39+
protected $notification_manager;
40+
41+
protected function setUp(): void
42+
{
43+
parent::setUp();
44+
45+
global $cache, $user, $phpbb_root_path, $phpEx;
46+
47+
$this->config = $this->createMock(\phpbb\config\config::class);
48+
$this->helper = $this->createMock(\phpbb\controller\helper::class);
49+
$this->idea_factory = $this->createMock(\phpbb\ideas\factory\idea::class);
50+
$this->user_loader = $this->createMock(\phpbb\user_loader::class);
51+
$this->auth = $this->createMock(\phpbb\auth\auth::class);
52+
$this->language = $this->createMock(\phpbb\language\language::class);
53+
$this->notification_manager = $this->createMock(\phpbb\notification\manager::class);
54+
$db = $this->createMock('\phpbb\db\driver\driver_interface');
55+
$user = new \phpbb\user($this->language, '\phpbb\datetime');
56+
$user->data['user_options'] = 230271;
57+
$cache = new \phpbb_mock_cache();
58+
59+
$this->notification_type = new status($db, $this->language, $user, $this->auth, $phpbb_root_path, $phpEx, 'phpbb_user_notifications');
60+
$this->notification_type->set_config($this->config);
61+
$this->notification_type->set_controller_helper($this->helper);
62+
$this->notification_type->set_idea_factory($this->idea_factory);
63+
$this->notification_type->set_user_loader($this->user_loader);
64+
65+
// Set protected properties using reflection
66+
$reflection = new \ReflectionClass($this->notification_type);
67+
$notification_manager_property = $reflection->getProperty('notification_manager');
68+
$notification_manager_property->setAccessible(true);
69+
$notification_manager_property->setValue($this->notification_type, $this->notification_manager);
70+
}
71+
72+
/**
73+
* Helper method to set notification data using reflection
74+
*/
75+
protected function setNotificationData(array $data)
76+
{
77+
$reflection = new \ReflectionClass($this->notification_type);
78+
$method = $reflection->getMethod('set_data');
79+
$method->setAccessible(true);
80+
81+
foreach ($data as $key => $value)
82+
{
83+
$method->invoke($this->notification_type, $key, $value);
84+
}
85+
}
86+
87+
public function test_get_type()
88+
{
89+
$this->assertEquals('phpbb.ideas.notification.type.status', $this->notification_type->get_type());
90+
}
91+
92+
public function test_is_available_with_permission()
93+
{
94+
$this->config->expects($this->once())
95+
->method('offsetGet')
96+
->with('ideas_forum_id')
97+
->willReturn(5);
98+
99+
$this->auth->expects($this->once())
100+
->method('acl_get')
101+
->with('f_', 5)
102+
->willReturn(true);
103+
104+
$this->assertTrue($this->notification_type->is_available());
105+
}
106+
107+
public function test_is_available_without_permission()
108+
{
109+
$this->config->expects($this->once())
110+
->method('offsetGet')
111+
->with('ideas_forum_id')
112+
->willReturn(5);
113+
114+
$this->auth->expects($this->once())
115+
->method('acl_get')
116+
->with('f_', 5)
117+
->willReturn(false);
118+
119+
$this->assertFalse($this->notification_type->is_available());
120+
}
121+
122+
public function test_get_item_id()
123+
{
124+
$type_data = ['item_id' => 123];
125+
$this->assertEquals(123, status::get_item_id($type_data));
126+
}
127+
128+
public function test_get_item_parent_id()
129+
{
130+
$type_data = ['parent_id' => 456];
131+
$this->assertEquals(0, status::get_item_parent_id($type_data));
132+
}
133+
134+
public function test_find_users_for_notification()
135+
{
136+
$type_data = ['idea_id' => 1];
137+
$idea_data = ['idea_author' => 2];
138+
$default_methods = ['board', 'email'];
139+
140+
$this->idea_factory->expects($this->once())
141+
->method('get_idea')
142+
->with(1)
143+
->willReturn($idea_data);
144+
145+
$this->notification_manager->expects($this->once())
146+
->method('get_default_methods')
147+
->willReturn($default_methods);
148+
149+
$result = $this->notification_type->find_users_for_notification($type_data);
150+
$this->assertEquals([2 => $default_methods], $result);
151+
}
152+
153+
public function test_find_users_for_notification_idea_not_found()
154+
{
155+
$type_data = ['idea_id' => 999];
156+
157+
$this->idea_factory->expects($this->once())
158+
->method('get_idea')
159+
->with(999)
160+
->willReturn(false);
161+
162+
$result = $this->notification_type->find_users_for_notification($type_data);
163+
$this->assertEquals([], $result);
164+
}
165+
166+
public function test_get_avatar_with_author()
167+
{
168+
$this->setNotificationData(['idea_author' => 5]);
169+
170+
$this->user_loader->expects($this->once())
171+
->method('get_avatar')
172+
->with(5, true)
173+
->willReturn('<img src="avatar.png">');
174+
175+
$this->assertEquals('<img src="avatar.png">', $this->notification_type->get_avatar());
176+
}
177+
178+
public function test_get_avatar_without_author()
179+
{
180+
$this->setNotificationData(['idea_author' => 0]);
181+
$this->assertEquals('', $this->notification_type->get_avatar());
182+
}
183+
184+
public function test_users_to_query()
185+
{
186+
$this->assertEquals([], $this->notification_type->users_to_query());
187+
}
188+
189+
public function test_get_title()
190+
{
191+
$this->setNotificationData(['idea_title' => 'Test Idea']);
192+
193+
$this->language->expects($this->once())
194+
->method('is_set')
195+
->with('IDEA_STATUS_CHANGE')
196+
->willReturn(true);
197+
198+
$this->language->expects($this->once())
199+
->method('lang')
200+
->with('IDEA_STATUS_CHANGE', 'Test Idea')
201+
->willReturn('Status changed for: Test Idea');
202+
203+
$this->assertEquals('Status changed for: Test Idea', $this->notification_type->get_title());
204+
}
205+
206+
public function test_get_title_loads_language()
207+
{
208+
$this->setNotificationData(['idea_title' => 'Test Idea']);
209+
210+
$this->language->expects($this->once())
211+
->method('is_set')
212+
->with('IDEA_STATUS_CHANGE')
213+
->willReturn(false);
214+
215+
$this->language->expects($this->once())
216+
->method('add_lang')
217+
->with('common', 'phpbb/ideas');
218+
219+
$this->language->expects($this->once())
220+
->method('lang')
221+
->with('IDEA_STATUS_CHANGE', 'Test Idea')
222+
->willReturn('Status changed for: Test Idea');
223+
224+
$this->assertEquals('Status changed for: Test Idea', $this->notification_type->get_title());
225+
}
226+
227+
public function test_get_reference()
228+
{
229+
$this->setNotificationData(['status' => 2]);
230+
231+
$this->language->expects($this->once())
232+
->method('lang')
233+
->with('IN_PROGRESS')
234+
->willReturn('In Progress');
235+
236+
$this->assertEquals('In Progress', $this->notification_type->get_reference());
237+
}
238+
239+
public function test_get_url()
240+
{
241+
$this->setNotificationData(['idea_id' => 42]);
242+
243+
$this->helper->expects($this->once())
244+
->method('route')
245+
->with('phpbb_ideas_idea_controller', ['idea_id' => 42])
246+
->willReturn('/ideas/42');
247+
248+
$this->assertEquals('/ideas/42', $this->notification_type->get_url());
249+
}
250+
251+
public function test_get_email_template()
252+
{
253+
$this->assertEquals('@phpbb_ideas/status_notification', $this->notification_type->get_email_template());
254+
}
255+
256+
public function test_get_email_template_variables()
257+
{
258+
$this->setNotificationData([
259+
'idea_title' => 'Test & Idea',
260+
'status' => 3,
261+
'idea_id' => 10
262+
]);
263+
264+
$this->helper->expects($this->once())
265+
->method('route')
266+
->with('phpbb_ideas_idea_controller', ['idea_id' => 10])
267+
->willReturn('/ideas/10');
268+
269+
$this->language->expects($this->once())
270+
->method('lang')
271+
->with('IMPLEMENTED')
272+
->willReturn('Implemented');
273+
274+
$result = $this->notification_type->get_email_template_variables();
275+
276+
$expected = [
277+
'IDEA_TITLE' => 'Test & Idea',
278+
'STATUS' => 'Implemented',
279+
'U_VIEW_IDEA' => '/ideas/10',
280+
];
281+
282+
$this->assertEquals($expected, $result);
283+
}
284+
285+
public function test_pre_create_insert_array()
286+
{
287+
$type_data = ['idea_id' => 5];
288+
$notify_users = [];
289+
$idea_data = [
290+
'idea_title' => 'Sample Idea',
291+
'idea_author' => 3
292+
];
293+
294+
$this->idea_factory->expects($this->once())
295+
->method('get_idea')
296+
->with(5)
297+
->willReturn($idea_data);
298+
299+
$result = $this->notification_type->pre_create_insert_array($type_data, $notify_users);
300+
301+
$expected = [
302+
'idea_title' => 'Sample Idea',
303+
'idea_author' => 3
304+
];
305+
306+
$this->assertEquals($expected, $result);
307+
}
308+
309+
public function test_pre_create_insert_array_idea_not_found()
310+
{
311+
$type_data = ['idea_id' => 999];
312+
$notify_users = [];
313+
314+
$this->idea_factory->expects($this->once())
315+
->method('get_idea')
316+
->with(999)
317+
->willReturn(false);
318+
319+
$result = $this->notification_type->pre_create_insert_array($type_data, $notify_users);
320+
$this->assertEquals([], $result);
321+
}
322+
323+
public function test_create_insert_array()
324+
{
325+
$type_data = [
326+
'item_id' => 1,
327+
'idea_id' => 7,
328+
'status' => 4
329+
];
330+
$pre_create_data = [
331+
'idea_title' => 'Another Idea',
332+
'idea_author' => 8
333+
];
334+
335+
// Use reflection to access set_data calls
336+
$reflection = new \ReflectionClass($this->notification_type);
337+
$set_data_method = $reflection->getMethod('set_data');
338+
$set_data_method->setAccessible(true);
339+
340+
$this->notification_type->create_insert_array($type_data, $pre_create_data);
341+
342+
// Verify data was set by checking get_data
343+
$get_data_method = $reflection->getMethod('get_data');
344+
$get_data_method->setAccessible(true);
345+
346+
$this->assertEquals(7, $get_data_method->invoke($this->notification_type, 'idea_id'));
347+
$this->assertEquals(4, $get_data_method->invoke($this->notification_type, 'status'));
348+
$this->assertEquals('Another Idea', $get_data_method->invoke($this->notification_type, 'idea_title'));
349+
$this->assertEquals(8, $get_data_method->invoke($this->notification_type, 'idea_author'));
350+
}
351+
}

0 commit comments

Comments
 (0)