Skip to content

Commit 7f0b413

Browse files
committed
Super optimize code and fix warnings
1 parent fa10bdf commit 7f0b413

1 file changed

Lines changed: 86 additions & 192 deletions

File tree

tests/notification/controller_webpush_test.php

Lines changed: 86 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use phpbb\webpushnotifications\ucp\controller\webpush;
1717
use ReflectionMethod;
1818
use Symfony\Component\HttpFoundation\JsonResponse;
19-
use Symfony\Component\HttpFoundation\Response;
19+
use Twig\Error\LoaderError;
20+
use Twig\Error\RuntimeError;
21+
use Twig\Error\SyntaxError;
2022

2123
class controller_webpush_test extends \phpbb_database_test_case
2224
{
@@ -96,6 +98,59 @@ protected function setUp(): void
9698
);
9799
}
98100

101+
private function setupCommonMocks(): void
102+
{
103+
global $cache;
104+
$cache = $this->createMock(\phpbb\cache\service::class);
105+
$cache->method('obtain_word_list')->willReturn([]);
106+
107+
$this->auth->method('acl_get')->willReturn(true);
108+
109+
$this->notification_manager->method('get_item_type_class')
110+
->willReturnCallback($this->getNotificationTypeCallback());
111+
}
112+
113+
private function getNotificationTypeCallback(): callable
114+
{
115+
return function(string $type_name, array $row_data) {
116+
$notification_type = new quote(
117+
$this->db,
118+
$this->language,
119+
$this->user,
120+
$this->auth,
121+
$this->phpbb_root_path,
122+
$this->php_ext,
123+
'phpbb_notifications'
124+
);
125+
126+
$notification_type->set_user_loader($this->user_loader);
127+
$notification_type->set_initial_data($row_data);
128+
129+
return $notification_type;
130+
};
131+
}
132+
133+
private function setupUserData(int $user_id): void
134+
{
135+
$this->user->data = [
136+
'is_bot' => false,
137+
'user_type' => USER_NORMAL,
138+
'user_id' => $user_id,
139+
'user_options' => 230271,
140+
];
141+
}
142+
143+
private function getExpectedResponse(): array
144+
{
145+
return [
146+
'heading' => 'yourdomain.com',
147+
'title' => 'Quoted by Guest in:',
148+
'text' => '"Welcome to phpBB3"',
149+
'url' => 'phpBB/viewtopic.php?p=1#p1',
150+
'avatar' => ['src' => ''],
151+
];
152+
}
153+
99154
public function data_notification_exceptions(): array
100155
{
101156
return [
@@ -181,29 +236,7 @@ public function test_notification_no_data($is_ajax, $is_bot, $user_type, $user_i
181236

182237
public function test_get_user_notification()
183238
{
184-
global $cache;
185-
$cache = $this->createMock(\phpbb\cache\service::class);
186-
$cache->method('obtain_word_list')->willReturn([]);
187-
188-
$this->auth->method('acl_get')->willReturn(true);
189-
190-
$this->notification_manager->method('get_item_type_class')
191-
->willReturnCallback(function(string $type_name, array $row_data) {
192-
$notification_type = new quote(
193-
$this->db,
194-
$this->language,
195-
$this->user,
196-
$this->auth,
197-
$this->phpbb_root_path,
198-
$this->php_ext,
199-
'phpbb_notifications'
200-
);
201-
202-
$notification_type->set_user_loader($this->user_loader);
203-
$notification_type->set_initial_data($row_data);
204-
205-
return $notification_type;
206-
});
239+
$this->setupCommonMocks();
207240

208241
$this->request->method('is_ajax')->willReturn(true);
209242
$this->request->method('variable')
@@ -212,54 +245,18 @@ public function test_get_user_notification()
212245
['item_id', 0, false, request_interface::REQUEST, 1],
213246
['type_id', 0, false, request_interface::REQUEST, 4],
214247
]);
215-
$this->user->data = [
216-
'is_bot' => false,
217-
'user_type' => USER_NORMAL,
218-
'user_id' => 2,
219-
'user_options' => 230271,
220-
];
221-
$this->user->lang = [
222-
'GUEST' => 'Guest',
223-
];
248+
$this->setupUserData(2);
224249

225250
$json_response = $this->controller->notification();
226251

227252
$response_data = json_decode($json_response->getContent(), true);
228253

229-
$this->assertEquals([
230-
'heading' => 'yourdomain.com',
231-
'title' => 'Quoted by Guest in:',
232-
'text' => '"Welcome to phpBB3"',
233-
'url' => 'phpBB/viewtopic.php?p=1#p1',
234-
'avatar' => ['src' => ''],
235-
], $response_data);
254+
$this->assertEquals($this->getExpectedResponse(), $response_data);
236255
}
237256

238257
public function test_get_user_notification_anonymous()
239258
{
240-
global $cache;
241-
$cache = $this->createMock(\phpbb\cache\service::class);
242-
$cache->method('obtain_word_list')->willReturn([]);
243-
244-
$this->auth->method('acl_get')->willReturn(true);
245-
246-
$this->notification_manager->method('get_item_type_class')
247-
->willReturnCallback(function(string $type_name, array $row_data) {
248-
$notification_type = new quote(
249-
$this->db,
250-
$this->language,
251-
$this->user,
252-
$this->auth,
253-
$this->phpbb_root_path,
254-
$this->php_ext,
255-
'phpbb_notifications'
256-
);
257-
258-
$notification_type->set_user_loader($this->user_loader);
259-
$notification_type->set_initial_data($row_data);
260-
261-
return $notification_type;
262-
});
259+
$this->setupCommonMocks();
263260

264261
$this->request->method('is_ajax')->willReturn(true);
265262
$this->request->method('variable')
@@ -269,54 +266,18 @@ public function test_get_user_notification_anonymous()
269266
['type_id', 0, false, request_interface::REQUEST, 4],
270267
['user_id', 0, false, request_interface::REQUEST, 2],
271268
]);
272-
$this->user->data = [
273-
'is_bot' => false,
274-
'user_type' => USER_NORMAL,
275-
'user_id' => ANONYMOUS,
276-
'user_options' => 230271,
277-
];
278-
$this->user->lang = [
279-
'GUEST' => 'Guest',
280-
];
269+
$this->setupUserData(ANONYMOUS);
281270

282271
$json_response = $this->controller->notification();
283272

284273
$response_data = json_decode($json_response->getContent(), true);
285274

286-
$this->assertEquals([
287-
'heading' => 'yourdomain.com',
288-
'title' => 'Quoted by Guest in:',
289-
'text' => '"Welcome to phpBB3"',
290-
'url' => 'phpBB/viewtopic.php?p=1#p1',
291-
'avatar' => ['src' => ''],
292-
], $response_data);
275+
$this->assertEquals($this->getExpectedResponse(), $response_data);
293276
}
294277

295278
public function test_get_user_notification_anonymous_invalid_token()
296279
{
297-
global $cache;
298-
$cache = $this->createMock(\phpbb\cache\service::class);
299-
$cache->method('obtain_word_list')->willReturn([]);
300-
301-
$this->auth->method('acl_get')->willReturn(true);
302-
303-
$this->notification_manager->method('get_item_type_class')
304-
->willReturnCallback(function(string $type_name, array $row_data) {
305-
$notification_type = new quote(
306-
$this->db,
307-
$this->language,
308-
$this->user,
309-
$this->auth,
310-
$this->phpbb_root_path,
311-
$this->php_ext,
312-
'phpbb_notifications'
313-
);
314-
315-
$notification_type->set_user_loader($this->user_loader);
316-
$notification_type->set_initial_data($row_data);
317-
318-
return $notification_type;
319-
});
280+
$this->setupCommonMocks();
320281

321282
$this->request->method('is_ajax')->willReturn(true);
322283
$this->request->method('variable')
@@ -326,15 +287,7 @@ public function test_get_user_notification_anonymous_invalid_token()
326287
['type_id', 0, false, request_interface::REQUEST, 4],
327288
['user_id', 0, false, request_interface::REQUEST, 2],
328289
]);
329-
$this->user->data = [
330-
'is_bot' => false,
331-
'user_type' => USER_NORMAL,
332-
'user_id' => ANONYMOUS,
333-
'user_options' => 230271,
334-
];
335-
$this->user->lang = [
336-
'GUEST' => 'Guest',
337-
];
290+
$this->setupUserData(ANONYMOUS);
338291

339292
$this->expectException(http_exception::class);
340293
$this->expectExceptionMessage('NO_AUTH_OPERATION');
@@ -344,29 +297,7 @@ public function test_get_user_notification_anonymous_invalid_token()
344297

345298
public function test_get_user_notification_legacy()
346299
{
347-
global $cache;
348-
$cache = $this->createMock(\phpbb\cache\service::class);
349-
$cache->method('obtain_word_list')->willReturn([]);
350-
351-
$this->auth->method('acl_get')->willReturn(true);
352-
353-
$this->notification_manager->method('get_item_type_class')
354-
->willReturnCallback(function(string $type_name, array $row_data) {
355-
$notification_type = new quote(
356-
$this->db,
357-
$this->language,
358-
$this->user,
359-
$this->auth,
360-
$this->phpbb_root_path,
361-
$this->php_ext,
362-
'phpbb_notifications'
363-
);
364-
365-
$notification_type->set_user_loader($this->user_loader);
366-
$notification_type->set_initial_data($row_data);
367-
368-
return $notification_type;
369-
});
300+
$this->setupCommonMocks();
370301

371302
$this->request->method('is_ajax')->willReturn(true);
372303
$this->request->method('variable')
@@ -375,29 +306,20 @@ public function test_get_user_notification_legacy()
375306
['item_id', 0, false, request_interface::REQUEST, 2],
376307
['type_id', 0, false, request_interface::REQUEST, 4],
377308
]);
378-
$this->user->data = [
379-
'is_bot' => false,
380-
'user_type' => USER_NORMAL,
381-
'user_id' => 2,
382-
'user_options' => 230271,
383-
];
384-
$this->user->lang = [
385-
'GUEST' => 'Guest',
386-
];
309+
$this->setupUserData(2);
387310

388311
$json_response = $this->controller->notification();
389312

390313
$response_data = json_decode($json_response->getContent(), true);
391314

392-
$this->assertEquals([
393-
'heading' => 'yourdomain.com',
394-
'title' => 'Quoted by Guest in:',
395-
'text' => '"Welcome to phpBB3"',
396-
'url' => 'phpBB/viewtopic.php?p=1#p1',
397-
'avatar' => ['src' => ''],
398-
], $response_data);
315+
$this->assertEquals($this->getExpectedResponse(), $response_data);
399316
}
400317

318+
/**
319+
* @throws SyntaxError
320+
* @throws RuntimeError
321+
* @throws LoaderError
322+
*/
401323
public function test_worker()
402324
{
403325
$this->template->method('render')->willReturn('rendered_content');
@@ -406,12 +328,16 @@ public function test_worker()
406328

407329
$response = $this->controller->worker();
408330

409-
$this->assertInstanceOf(Response::class, $response);
410331
$this->assertEquals('text/javascript; charset=UTF-8', $response->headers->get('Content-Type'));
411332
$this->assertEquals('rendered_content', $response->getContent());
412333
$this->assertNull($response->headers->get('X-PHPBB-IS-BOT'));
413334
}
414335

336+
/**
337+
* @throws RuntimeError
338+
* @throws SyntaxError
339+
* @throws LoaderError
340+
*/
415341
public function test_worker_bot()
416342
{
417343
$this->template->method('render')->willReturn('rendered_content');
@@ -424,6 +350,9 @@ public function test_worker_bot()
424350
$this->assertEquals('yes', $response->headers->get('X-PHPBB-IS-BOT'));
425351
}
426352

353+
/**
354+
* @throws \ReflectionException
355+
*/
427356
public function test_check_subscribe_requests_invalid_form_token()
428357
{
429358
$this->form_helper->method('check_form_tokens')->willReturn(false);
@@ -436,6 +365,9 @@ public function test_check_subscribe_requests_invalid_form_token()
436365
$check_subscribe_reflection->invoke($this->controller);
437366
}
438367

368+
/**
369+
* @throws \ReflectionException
370+
*/
439371
public function test_check_subscribe_requests_anonymous_user()
440372
{
441373
$this->form_helper->method('check_form_tokens')->willReturn(true);
@@ -450,45 +382,7 @@ public function test_check_subscribe_requests_anonymous_user()
450382
$check_subscribe_reflection->invoke($this->controller);
451383
}
452384

453-
public function test_subscribe_success()
454-
{
455-
$this->form_helper->method('check_form_tokens')->willReturn(true);
456-
$this->request->method('is_ajax')->willReturn(true);
457-
$this->user->data['user_id'] = 2;
458-
$this->user->data['is_bot'] = false;
459-
$this->user->data['user_type'] = USER_NORMAL;
460-
461-
$symfony_request = $this->createMock(\phpbb\symfony_request::class);
462-
$symfony_request->method('get')->willReturn(json_encode([
463-
'endpoint' => 'test_endpoint',
464-
'expiration_time' => 0,
465-
'keys' => ['p256dh' => 'test_p256dh', 'auth' => 'test_auth']
466-
]));
467-
468-
$response = $this->controller->subscribe($symfony_request);
469-
470-
$this->assertInstanceOf(JsonResponse::class, $response);
471-
$this->assertEquals(['success' => true, 'form_tokens' => $this->form_helper->get_form_tokens(webpush::FORM_TOKEN_UCP)], json_decode($response->getContent(), true));
472-
473-
// Get subscription data from database
474-
$sql = 'SELECT *
475-
FROM phpbb_wpn_push_subscriptions
476-
WHERE user_id = 2';
477-
$result = $this->db->sql_query($sql);
478-
$row = $this->db->sql_fetchrow($result);
479-
$this->db->sql_freeresult($result);
480-
481-
$this->assertEquals([
482-
'user_id' => '2',
483-
'endpoint' => 'test_endpoint',
484-
'p256dh' => 'test_p256dh',
485-
'auth' => 'test_auth',
486-
'expiration_time' => 0,
487-
'subscription_id' => '1',
488-
], $row);
489-
}
490-
491-
public function test_unsubscribe_success()
385+
public function test_sub_unsubscribe_success()
492386
{
493387
$this->form_helper->method('check_form_tokens')->willReturn(true);
494388
$this->request->method('is_ajax')->willReturn(true);
@@ -533,8 +427,8 @@ public function test_unsubscribe_success()
533427

534428
// Get subscription data from database
535429
$sql = 'SELECT *
536-
FROM phpbb_wpn_push_subscriptions
537-
WHERE user_id = 2';
430+
FROM phpbb_wpn_push_subscriptions
431+
WHERE user_id = 2';
538432
$result = $this->db->sql_query($sql);
539433
$row = $this->db->sql_fetchrow($result);
540434
$this->db->sql_freeresult($result);

0 commit comments

Comments
 (0)