From 7acd34ba74705461e7a0cca5f4f5e062a09ab3b6 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 6 Jun 2022 12:32:07 +0200 Subject: [PATCH] Added unread messages as global var --- includes/includes.php | 1 - includes/pages/user_messages.php | 14 ------- resources/views/layouts/parts/navbar.twig | 7 +++- src/Controllers/MessagesController.php | 11 ------ src/Renderer/Twig/Extensions/Globals.php | 14 +++++-- src/Renderer/Twig/Extensions/Legacy.php | 1 - .../Controllers/MessagesControllerTest.php | 37 ------------------- .../Twig/Extensions/ExtensionTest.php | 2 +- .../Renderer/Twig/Extensions/GlobalsTest.php | 2 + .../Renderer/Twig/Extensions/LegacyTest.php | 1 - 10 files changed, 19 insertions(+), 71 deletions(-) delete mode 100644 includes/pages/user_messages.php diff --git a/includes/includes.php b/includes/includes.php index 871ce3a6..29eedfcc 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -72,7 +72,6 @@ $includeFiles = [ __DIR__ . '/../includes/pages/admin_shifts.php', __DIR__ . '/../includes/pages/admin_user.php', __DIR__ . '/../includes/pages/guest_login.php', - __DIR__ . '/../includes/pages/user_messages.php', __DIR__ . '/../includes/pages/user_myshifts.php', __DIR__ . '/../includes/pages/user_settings.php', __DIR__ . '/../includes/pages/user_shifts.php', diff --git a/includes/pages/user_messages.php b/includes/pages/user_messages.php deleted file mode 100644 index 50e95e51..00000000 --- a/includes/pages/user_messages.php +++ /dev/null @@ -1,14 +0,0 @@ -make(MessagesController::class) - ->numberOfUnreadMessages(); - - return $count > 0 ? ' ' . $count . '' : ''; -} diff --git a/resources/views/layouts/parts/navbar.twig b/resources/views/layouts/parts/navbar.twig index 155df01b..d0cd6067 100644 --- a/resources/views/layouts/parts/navbar.twig +++ b/resources/views/layouts/parts/navbar.twig @@ -44,7 +44,12 @@ {% endif %} {% if is_user() and has_permission_to('user_messages') %} - {{ _self.toolbar_item(menuUserMessages(), url('messages'), 'messages', 'envelope') }} + {{ _self.toolbar_item( + user_messages ? '' ~ user_messages ~ '' : '', + url('messages'), + 'messages', + 'envelope' + ) }} {% endif %} {{ menuUserHints() }} diff --git a/src/Controllers/MessagesController.php b/src/Controllers/MessagesController.php index d88bf82c..9be1ebab 100644 --- a/src/Controllers/MessagesController.php +++ b/src/Controllers/MessagesController.php @@ -202,17 +202,6 @@ class MessagesController extends BaseController return $this->redirect->to('/messages/' . $otherUserId); } - /** - * Returns the overall unread messages of the current user. - */ - public function numberOfUnreadMessages(): int - { - return $this->auth->user() - ->messagesReceived() - ->where('read', false) - ->count(); - } - /** * The number of unread messages per conversation of the current user. * @return Collection of unread message amounts. Each object with key=other user, value=amount of unread messages diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php index 7be96d60..4f19534e 100644 --- a/src/Renderer/Twig/Extensions/Globals.php +++ b/src/Renderer/Twig/Extensions/Globals.php @@ -36,11 +36,16 @@ class Globals extends TwigExtension implements GlobalsInterface { $user = $this->auth->user(); $themes = config('themes'); + $userMessages = null; if ($user === null) { $themeId = config('theme'); } else { $themeId = $user->settings->theme; + $userMessages = $user + ->messagesReceived() + ->where('read', false) + ->count(); } $query = $this->request->query->get('theme'); @@ -55,10 +60,11 @@ class Globals extends TwigExtension implements GlobalsInterface $theme = $themes[$themeId]; return [ - 'user' => $user ?? [], - 'request' => $this->request, - 'themeId' => $themeId, - 'theme' => $theme, + 'user' => $user ?? [], + 'user_messages' => $userMessages, + 'request' => $this->request, + 'themeId' => $themeId, + 'theme' => $theme, ]; } } diff --git a/src/Renderer/Twig/Extensions/Legacy.php b/src/Renderer/Twig/Extensions/Legacy.php index dc0c4775..98a97ac6 100644 --- a/src/Renderer/Twig/Extensions/Legacy.php +++ b/src/Renderer/Twig/Extensions/Legacy.php @@ -28,7 +28,6 @@ class Legacy extends TwigExtension return [ new TwigFunction('menu', 'make_navigation', $isSafeHtml), new TwigFunction('menuUserShiftState', 'User_shift_state_render', $isSafeHtml), - new TwigFunction('menuUserMessages', 'user_unread_messages', $isSafeHtml), new TwigFunction('menuUserHints', 'header_render_hints', $isSafeHtml), new TwigFunction('menuUserSubmenu', 'make_user_submenu', $isSafeHtml), new TwigFunction('page', [$this, 'getPage']), diff --git a/tests/Unit/Controllers/MessagesControllerTest.php b/tests/Unit/Controllers/MessagesControllerTest.php index 79c39153..4c199723 100644 --- a/tests/Unit/Controllers/MessagesControllerTest.php +++ b/tests/Unit/Controllers/MessagesControllerTest.php @@ -519,43 +519,6 @@ class MessagesControllerTest extends ControllerTest $this->assertEquals(0, count(Message::whereId($msg->id)->get())); } - /** - * @testdox NumberOfUnreadMessages: withNoMessages -> returns0 - * @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages - */ - public function testNumberOfUnreadMessagesWithNoMessagesReturns0() - { - $this->assertEquals(0, $this->controller->numberOfUnreadMessages()); - } - - /** - * @testdox NumberOfUnreadMessages: withMessagesNotToMe -> messagesNotToMeAreIgnored - * @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages - */ - public function testNumberOfUnreadMessagesWithMessagesNotToMeMessagesNotToMeAreIgnored() - { - $userC = User::factory(['name' => 'c'])->create(); - - $this->createMessage($this->userA, $this->userB, 'a>b', $this->now); - $this->createMessage($this->userB, $userC, 'b>c', $this->now); - $this->assertEquals(0, $this->controller->numberOfUnreadMessages()); - } - - /** - * @testdox NumberOfUnreadMessages: withMessages -> returnsSumOfUnreadMessagesSentToMe - * @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages - */ - public function testNumberOfUnreadMessagesWithMessagesReturnsSumOfUnreadMessagesSentToMe() - { - $userC = User::factory(['name' => 'c'])->create(); - - $this->createMessage($this->userB, $this->userA, 'b>a1', $this->now); - $this->createMessage($this->userB, $this->userA, 'b>a2', $this->now); - $this->createMessage($userC, $this->userA, 'c>a', $this->now); - - $this->assertEquals(3, $this->controller->numberOfUnreadMessages()); - } - /** * Setup environment */ diff --git a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php index d1bb840e..be55616b 100644 --- a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php @@ -75,7 +75,7 @@ abstract class ExtensionTest extends TestCase */ protected function assertGlobalsExists($name, $value, $globals) { - if (isset($globals[$name])) { + if (array_key_exists($name, $globals)) { $this->assertArraySubset([$name => $value], $globals); return; diff --git a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php index 66aabb0a..8bb59f87 100644 --- a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php @@ -51,6 +51,7 @@ class GlobalsTest extends ExtensionTest // No user $globals = $extension->getGlobals(); $this->assertGlobalsExists('user', [], $globals); + $this->assertGlobalsExists('user_messages', null, $globals); $this->assertGlobalsExists('request', $request, $globals); $this->assertGlobalsExists('themeId', 23, $globals); $this->assertGlobalsExists('theme', $theme2, $globals); @@ -58,6 +59,7 @@ class GlobalsTest extends ExtensionTest // User $globals = $extension->getGlobals(); $this->assertGlobalsExists('user', $user, $globals); + $this->assertGlobalsExists('user_messages', 0, $globals); $this->assertGlobalsExists('themeId', 42, $globals); $this->assertGlobalsExists('theme', $theme, $globals); diff --git a/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php b/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php index 7190c979..df1e7ce0 100644 --- a/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php @@ -22,7 +22,6 @@ class LegacyTest extends ExtensionTest $this->assertExtensionExists('menu', 'make_navigation', $functions, $isSafeHtml); $this->assertExtensionExists('menuUserShiftState', 'User_shift_state_render', $functions, $isSafeHtml); - $this->assertExtensionExists('menuUserMessages', 'user_unread_messages', $functions, $isSafeHtml); $this->assertExtensionExists('menuUserHints', 'header_render_hints', $functions, $isSafeHtml); $this->assertExtensionExists('menuUserSubmenu', 'make_user_submenu', $functions, $isSafeHtml); $this->assertExtensionExists('page', [$extension, 'getPage'], $functions);