From 378aaefbbf60698c7b8faafc20f6b8cb22357e31 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 15 Jun 2017 09:43:48 +0200 Subject: Notifications Signed-off-by: Thomas Citharel --- .../Command/AdminNotificationCommandTest.php | 101 +++++++++++++++++++++ .../Command/ReleaseNotificationCommandTest.php | 96 ++++++++++++++++++++ .../Controller/NotificationControllerTest.php | 32 +++++++ .../CoreBundle/Twig/WallabagExtensionTest.php | 6 +- 4 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 tests/Wallabag/CoreBundle/Command/AdminNotificationCommandTest.php create mode 100644 tests/Wallabag/CoreBundle/Command/ReleaseNotificationCommandTest.php create mode 100644 tests/Wallabag/CoreBundle/Controller/NotificationControllerTest.php (limited to 'tests/Wallabag/CoreBundle') diff --git a/tests/Wallabag/CoreBundle/Command/AdminNotificationCommandTest.php b/tests/Wallabag/CoreBundle/Command/AdminNotificationCommandTest.php new file mode 100644 index 00000000..42d243dd --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/AdminNotificationCommandTest.php @@ -0,0 +1,101 @@ +getClient()->getKernel()); + $application->add(new AdminNotificationCommand()); + + $command = $application->find('wallabag:notification:send'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + ]); + } + + public function testRunSendNotificationCommandWithBadUsername() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new AdminNotificationCommand()); + + $command = $application->find('wallabag:notification:send'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'username' => 'unknown', + 'title' => 'foo', + 'message' => 'bar' + ]); + + $this->assertContains('User "unknown" not found', $tester->getDisplay()); + } + + public function testSendNotificationCommandForUser() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new AdminNotificationCommand()); + + $command = $application->find('wallabag:notification:send'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'username' => 'admin', + 'title' => 'foo', + 'message' => 'bar' + ]); + + $this->assertContains('Sent notification for user admin', $tester->getDisplay()); + } + + public function testSendNotificationCommand() + { + $client = $this->getClient(); + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + + $this->logInAs('admin'); + + $notifications = $em->getRepository('WallabagCoreBundle:Notification')->findByUser($this->getLoggedInUserId()); + + $this->assertCount(0, $notifications); + + $application = new Application($this->getClient()->getKernel()); + $application->add(new CleanDuplicatesCommand()); + + $command = $application->find('wallabag:notification:send'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'username' => 'admin', + 'title' => 'foo', + 'message' => 'bar' + ]); + + $this->assertContains('Sent notification for user admin', $tester->getDisplay()); + + $notifications = $em->getRepository('WallabagCoreBundle:Notification')->findByUser($this->getLoggedInUserId()); + + $this->assertCount(1, $notifications); + + $crawler = $client->request('GET', '/'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertCount(1, $crawler->filter('.notifications-area .collection')); + } +} diff --git a/tests/Wallabag/CoreBundle/Command/ReleaseNotificationCommandTest.php b/tests/Wallabag/CoreBundle/Command/ReleaseNotificationCommandTest.php new file mode 100644 index 00000000..9b1990a2 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/ReleaseNotificationCommandTest.php @@ -0,0 +1,96 @@ +getClient()->getKernel()); + $application->add(new ReleaseNotificationCommand()); + + $command = $application->find('wallabag:notification:release'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + ]); + + $this->assertContains('Sent notification for user admin', $tester->getDisplay()); + $this->assertContains('Finished sending notifications.', $tester->getDisplay()); + } + + public function testRunSendReleaseNotificationCommandWithBadUsername() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new ReleaseNotificationCommand()); + + $command = $application->find('wallabag:notification:release'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'username' => 'unknown', + 'link' => 'https://wallabag.org', + ]); + + $this->assertContains('User "unknown" not found', $tester->getDisplay()); + } + + public function testSendReleaseNotificationCommandForUser() + { + $application = new Application($this->getClient()->getKernel()); + $application->add(new ReleaseNotificationCommand()); + + $command = $application->find('wallabag:notification:release'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'username' => 'admin', + 'link' => 'https://wallabag.org', + ]); + + $this->assertContains('Sent notification for user admin', $tester->getDisplay()); + } + + public function testSendReleaseNotificationCommand() + { + $client = $this->getClient(); + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + + $this->logInAs('admin'); + + $notifications = $em->getRepository('WallabagCoreBundle:Notification')->findByUser($this->getLoggedInUserId()); + + $this->assertCount(0, $notifications); + + $application = new Application($this->getClient()->getKernel()); + $application->add(new ReleaseNotificationCommand()); + + $command = $application->find('wallabag:notification:release'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + 'username' => 'admin', + 'link' => 'https://wallabag.org', + ]); + + $this->assertContains('Sent notification for user admin', $tester->getDisplay()); + + $notifications = $em->getRepository('WallabagCoreBundle:Notification')->findByUser($this->getLoggedInUserId()); + + $this->assertCount(1, $notifications); + + $crawler = $client->request('GET', '/'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertCount(1, $crawler->filter('.notifications-area .collection')); + } +} diff --git a/tests/Wallabag/CoreBundle/Controller/NotificationControllerTest.php b/tests/Wallabag/CoreBundle/Controller/NotificationControllerTest.php new file mode 100644 index 00000000..e92528ca --- /dev/null +++ b/tests/Wallabag/CoreBundle/Controller/NotificationControllerTest.php @@ -0,0 +1,32 @@ +logInAs('admin'); + + $client = $this->getClient(); + + $em = $client->getContainer() + ->get('doctrine.orm.entity_manager'); + + $notification = new Notification($this->getLoggedInUser()); + $notification->setType(Notification::TYPE_USER) + ->setTitle('fooTitle') + ->setDescription('barDescription'); + + $em->persist($notification); + $em->flush(); + + $crawler = $client->request('GET', '/'); + $this->assertCount(1, $notificationArea = $crawler->filter('.notifications-area .collection')); + $this->assertContains('fooTitle', $notificationArea->text()); + $this->assertContains('barDescription', $notificationArea->text()); + } +} diff --git a/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php b/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php index ceec4b37..1fca2251 100644 --- a/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php +++ b/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php @@ -16,6 +16,10 @@ class WallabagExtensionTest extends \PHPUnit_Framework_TestCase ->disableOriginalConstructor() ->getMock(); + $notificationRepository = $this->getMockBuilder('Wallabag\CoreBundle\Repository\NotificationRepository') + ->disableOriginalConstructor() + ->getMock(); + $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') ->disableOriginalConstructor() ->getMock(); @@ -24,7 +28,7 @@ class WallabagExtensionTest extends \PHPUnit_Framework_TestCase ->disableOriginalConstructor() ->getMock(); - $extension = new WallabagExtension($entryRepository, $tagRepository, $tokenStorage, 0, $translator); + $extension = new WallabagExtension($entryRepository, $tagRepository, $notificationRepository, $tokenStorage, 0, 5, $translator); $this->assertSame('lemonde.fr', $extension->removeWww('www.lemonde.fr')); $this->assertSame('lemonde.fr', $extension->removeWww('lemonde.fr')); -- cgit v1.2.3