Skip to content

Commit 9a3878b

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

File tree

2 files changed

+350
-1
lines changed

2 files changed

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

0 commit comments

Comments
 (0)