NotifierTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Notification;
  29. use OCA\TwoFactorBackupCodes\Notifications\Notifier;
  30. use OCP\IL10N;
  31. use OCP\IURLGenerator;
  32. use OCP\L10N\IFactory;
  33. use OCP\Notification\INotification;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Test\TestCase;
  36. class NotifierTest extends TestCase {
  37. /** @var Notifier */
  38. protected $notifier;
  39. /** @var IFactory|MockObject */
  40. protected $factory;
  41. /** @var IURLGenerator|MockObject */
  42. protected $url;
  43. /** @var IL10N|MockObject */
  44. protected $l;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->l = $this->createMock(IL10N::class);
  48. $this->l->expects($this->any())
  49. ->method('t')
  50. ->willReturnCallback(function ($string, $args) {
  51. return vsprintf($string, $args);
  52. });
  53. $this->factory = $this->createMock(IFactory::class);
  54. $this->url = $this->createMock(IURLGenerator::class);
  55. $this->factory->expects($this->any())
  56. ->method('get')
  57. ->willReturn($this->l);
  58. $this->notifier = new Notifier(
  59. $this->factory,
  60. $this->url
  61. );
  62. }
  63. public function testPrepareWrongApp() {
  64. $this->expectException(\InvalidArgumentException::class);
  65. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  66. $notification = $this->createMock(INotification::class);
  67. $notification->expects($this->once())
  68. ->method('getApp')
  69. ->willReturn('notifications');
  70. $notification->expects($this->never())
  71. ->method('getSubject');
  72. $this->notifier->prepare($notification, 'en');
  73. }
  74. public function testPrepareWrongSubject() {
  75. $this->expectException(\InvalidArgumentException::class);
  76. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  77. $notification = $this->createMock(INotification::class);
  78. $notification->expects($this->once())
  79. ->method('getApp')
  80. ->willReturn('twofactor_backupcodes');
  81. $notification->expects($this->once())
  82. ->method('getSubject')
  83. ->willReturn('wrong subject');
  84. $this->notifier->prepare($notification, 'en');
  85. }
  86. public function testPrepare() {
  87. /** @var \OCP\Notification\INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  88. $notification = $this->createMock(INotification::class);
  89. $notification->expects($this->once())
  90. ->method('getApp')
  91. ->willReturn('twofactor_backupcodes');
  92. $notification->expects($this->once())
  93. ->method('getSubject')
  94. ->willReturn('create_backupcodes');
  95. $this->factory->expects($this->once())
  96. ->method('get')
  97. ->with('twofactor_backupcodes', 'nl')
  98. ->willReturn($this->l);
  99. $notification->expects($this->once())
  100. ->method('setParsedSubject')
  101. ->with('Generate backup codes')
  102. ->willReturnSelf();
  103. $notification->expects($this->once())
  104. ->method('setParsedMessage')
  105. ->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.')
  106. ->willReturnSelf();
  107. $this->url->expects($this->once())
  108. ->method('linkToRouteAbsolute')
  109. ->with('settings.PersonalSettings.index', ['section' => 'security'])
  110. ->willReturn('linkToRouteAbsolute');
  111. $notification->expects($this->once())
  112. ->method('setLink')
  113. ->with('linkToRouteAbsolute')
  114. ->willReturnSelf();
  115. $return = $this->notifier->prepare($notification, 'nl');
  116. $this->assertEquals($notification, $return);
  117. }
  118. }