NotifierTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Notification;
  25. use OCA\TwoFactorBackupCodes\Notifications\Notifier;
  26. use OCP\IL10N;
  27. use OCP\IURLGenerator;
  28. use OCP\L10N\IFactory;
  29. use OCP\Notification\INotification;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Test\TestCase;
  32. class NotifierTest extends TestCase {
  33. /** @var Notifier */
  34. protected $notifier;
  35. /** @var IFactory|MockObject */
  36. protected $factory;
  37. /** @var IURLGenerator|MockObject */
  38. protected $url;
  39. /** @var IL10N|MockObject */
  40. protected $l;
  41. protected function setUp() {
  42. parent::setUp();
  43. $this->l = $this->createMock(IL10N::class);
  44. $this->l->expects($this->any())
  45. ->method('t')
  46. ->willReturnCallback(function($string, $args) {
  47. return vsprintf($string, $args);
  48. });
  49. $this->factory = $this->createMock(IFactory::class);
  50. $this->url = $this->createMock(IURLGenerator::class);
  51. $this->factory->expects($this->any())
  52. ->method('get')
  53. ->willReturn($this->l);
  54. $this->notifier = new Notifier(
  55. $this->factory,
  56. $this->url
  57. );
  58. }
  59. /**
  60. * @expectedException \InvalidArgumentException
  61. */
  62. public function testPrepareWrongApp() {
  63. /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
  64. $notification = $this->createMock(INotification::class);
  65. $notification->expects($this->once())
  66. ->method('getApp')
  67. ->willReturn('notifications');
  68. $notification->expects($this->never())
  69. ->method('getSubject');
  70. $this->notifier->prepare($notification, 'en');
  71. }
  72. /**
  73. * @expectedException \InvalidArgumentException
  74. */
  75. public function testPrepareWrongSubject() {
  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 have enabled two-factor authentication but have not yet generated backup codes. Be sure to do this in case you lose access to 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. }