NotifierTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Notification;
  8. use OCA\TwoFactorBackupCodes\Notifications\Notifier;
  9. use OCP\IL10N;
  10. use OCP\IURLGenerator;
  11. use OCP\L10N\IFactory;
  12. use OCP\Notification\INotification;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Test\TestCase;
  15. class NotifierTest extends TestCase {
  16. /** @var Notifier */
  17. protected $notifier;
  18. /** @var IFactory|MockObject */
  19. protected $factory;
  20. /** @var IURLGenerator|MockObject */
  21. protected $url;
  22. /** @var IL10N|MockObject */
  23. protected $l;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->l = $this->createMock(IL10N::class);
  27. $this->l->expects($this->any())
  28. ->method('t')
  29. ->willReturnCallback(function ($string, $args) {
  30. return vsprintf($string, $args);
  31. });
  32. $this->factory = $this->createMock(IFactory::class);
  33. $this->url = $this->createMock(IURLGenerator::class);
  34. $this->factory->expects($this->any())
  35. ->method('get')
  36. ->willReturn($this->l);
  37. $this->notifier = new Notifier(
  38. $this->factory,
  39. $this->url
  40. );
  41. }
  42. public function testPrepareWrongApp() {
  43. $this->expectException(\InvalidArgumentException::class);
  44. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  45. $notification = $this->createMock(INotification::class);
  46. $notification->expects($this->once())
  47. ->method('getApp')
  48. ->willReturn('notifications');
  49. $notification->expects($this->never())
  50. ->method('getSubject');
  51. $this->notifier->prepare($notification, 'en');
  52. }
  53. public function testPrepareWrongSubject() {
  54. $this->expectException(\InvalidArgumentException::class);
  55. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  56. $notification = $this->createMock(INotification::class);
  57. $notification->expects($this->once())
  58. ->method('getApp')
  59. ->willReturn('twofactor_backupcodes');
  60. $notification->expects($this->once())
  61. ->method('getSubject')
  62. ->willReturn('wrong subject');
  63. $this->notifier->prepare($notification, 'en');
  64. }
  65. public function testPrepare() {
  66. /** @var \OCP\Notification\INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  67. $notification = $this->createMock(INotification::class);
  68. $notification->expects($this->once())
  69. ->method('getApp')
  70. ->willReturn('twofactor_backupcodes');
  71. $notification->expects($this->once())
  72. ->method('getSubject')
  73. ->willReturn('create_backupcodes');
  74. $this->factory->expects($this->once())
  75. ->method('get')
  76. ->with('twofactor_backupcodes', 'nl')
  77. ->willReturn($this->l);
  78. $notification->expects($this->once())
  79. ->method('setParsedSubject')
  80. ->with('Generate backup codes')
  81. ->willReturnSelf();
  82. $notification->expects($this->once())
  83. ->method('setParsedMessage')
  84. ->with('You enabled two-factor authentication but did not generate backup codes yet. They are needed to restore access to your account in case you lose your second factor.')
  85. ->willReturnSelf();
  86. $this->url->expects($this->once())
  87. ->method('linkToRouteAbsolute')
  88. ->with('settings.PersonalSettings.index', ['section' => 'security'])
  89. ->willReturn('linkToRouteAbsolute');
  90. $notification->expects($this->once())
  91. ->method('setLink')
  92. ->with('linkToRouteAbsolute')
  93. ->willReturnSelf();
  94. $return = $this->notifier->prepare($notification, 'nl');
  95. $this->assertEquals($notification, $return);
  96. }
  97. }