ProviderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\TwoFactorBackupCodes\Test\Unit\Activity;
  28. use InvalidArgumentException;
  29. use OCA\TwoFactorBackupCodes\Activity\Provider;
  30. use OCP\Activity\IEvent;
  31. use OCP\Activity\IManager;
  32. use OCP\IL10N;
  33. use OCP\IURLGenerator;
  34. use OCP\L10N\IFactory;
  35. use Test\TestCase;
  36. class ProviderTest extends TestCase {
  37. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  38. private $l10n;
  39. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  40. private $urlGenerator;
  41. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  42. private $activityManager;
  43. /** @var Provider */
  44. private $provider;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->l10n = $this->createMock(IFactory::class);
  48. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  49. $this->activityManager = $this->createMock(IManager::class);
  50. $this->provider = new Provider($this->l10n, $this->urlGenerator, $this->activityManager);
  51. }
  52. public function testParseUnrelated() {
  53. $lang = 'ru';
  54. $event = $this->createMock(IEvent::class);
  55. $event->expects($this->once())
  56. ->method('getApp')
  57. ->willReturn('comments');
  58. $this->expectException(InvalidArgumentException::class);
  59. $this->provider->parse($lang, $event);
  60. }
  61. public function subjectData() {
  62. return [
  63. ['codes_generated'],
  64. ];
  65. }
  66. /**
  67. * @dataProvider subjectData
  68. */
  69. public function testParse($subject) {
  70. $lang = 'ru';
  71. $event = $this->createMock(IEvent::class);
  72. $l = $this->createMock(IL10N::class);
  73. $event->expects($this->once())
  74. ->method('getApp')
  75. ->willReturn('twofactor_backupcodes');
  76. $this->l10n->expects($this->once())
  77. ->method('get')
  78. ->with('twofactor_backupcodes', $lang)
  79. ->willReturn($l);
  80. $this->urlGenerator->expects($this->once())
  81. ->method('imagePath')
  82. ->with('core', 'actions/password.svg')
  83. ->willReturn('path/to/image');
  84. $this->urlGenerator->expects($this->once())
  85. ->method('getAbsoluteURL')
  86. ->with('path/to/image')
  87. ->willReturn('absolute/path/to/image');
  88. $event->expects($this->once())
  89. ->method('setIcon')
  90. ->with('absolute/path/to/image');
  91. $event->expects($this->once())
  92. ->method('getSubject')
  93. ->willReturn($subject);
  94. $event->expects($this->once())
  95. ->method('setParsedSubject');
  96. $this->provider->parse($lang, $event);
  97. }
  98. public function testParseInvalidSubject() {
  99. $lang = 'ru';
  100. $l = $this->createMock(IL10N::class);
  101. $event = $this->createMock(IEvent::class);
  102. $event->expects($this->once())
  103. ->method('getApp')
  104. ->willReturn('twofactor_backupcodes');
  105. $this->l10n->expects($this->once())
  106. ->method('get')
  107. ->with('twofactor_backupcodes', $lang)
  108. ->willReturn($l);
  109. $event->expects($this->once())
  110. ->method('getSubject')
  111. ->willReturn('unrelated');
  112. $this->expectException(InvalidArgumentException::class);
  113. $this->provider->parse($lang, $event);
  114. }
  115. }