ProviderTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Test\Unit\Activity;
  8. use InvalidArgumentException;
  9. use OCA\TwoFactorBackupCodes\Activity\Provider;
  10. use OCP\Activity\IEvent;
  11. use OCP\Activity\IManager;
  12. use OCP\IL10N;
  13. use OCP\IURLGenerator;
  14. use OCP\L10N\IFactory;
  15. use Test\TestCase;
  16. class ProviderTest extends TestCase {
  17. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  18. private $l10n;
  19. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  20. private $urlGenerator;
  21. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  22. private $activityManager;
  23. /** @var Provider */
  24. private $provider;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->l10n = $this->createMock(IFactory::class);
  28. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  29. $this->activityManager = $this->createMock(IManager::class);
  30. $this->provider = new Provider($this->l10n, $this->urlGenerator, $this->activityManager);
  31. }
  32. public function testParseUnrelated() {
  33. $lang = 'ru';
  34. $event = $this->createMock(IEvent::class);
  35. $event->expects($this->once())
  36. ->method('getApp')
  37. ->willReturn('comments');
  38. $this->expectException(InvalidArgumentException::class);
  39. $this->provider->parse($lang, $event);
  40. }
  41. public function subjectData() {
  42. return [
  43. ['codes_generated'],
  44. ];
  45. }
  46. /**
  47. * @dataProvider subjectData
  48. */
  49. public function testParse($subject) {
  50. $lang = 'ru';
  51. $event = $this->createMock(IEvent::class);
  52. $l = $this->createMock(IL10N::class);
  53. $event->expects($this->once())
  54. ->method('getApp')
  55. ->willReturn('twofactor_backupcodes');
  56. $this->l10n->expects($this->once())
  57. ->method('get')
  58. ->with('twofactor_backupcodes', $lang)
  59. ->willReturn($l);
  60. $this->urlGenerator->expects($this->once())
  61. ->method('imagePath')
  62. ->with('core', 'actions/password.svg')
  63. ->willReturn('path/to/image');
  64. $this->urlGenerator->expects($this->once())
  65. ->method('getAbsoluteURL')
  66. ->with('path/to/image')
  67. ->willReturn('absolute/path/to/image');
  68. $event->expects($this->once())
  69. ->method('setIcon')
  70. ->with('absolute/path/to/image');
  71. $event->expects($this->once())
  72. ->method('getSubject')
  73. ->willReturn($subject);
  74. $event->expects($this->once())
  75. ->method('setParsedSubject');
  76. $this->provider->parse($lang, $event);
  77. }
  78. public function testParseInvalidSubject() {
  79. $lang = 'ru';
  80. $l = $this->createMock(IL10N::class);
  81. $event = $this->createMock(IEvent::class);
  82. $event->expects($this->once())
  83. ->method('getApp')
  84. ->willReturn('twofactor_backupcodes');
  85. $this->l10n->expects($this->once())
  86. ->method('get')
  87. ->with('twofactor_backupcodes', $lang)
  88. ->willReturn($l);
  89. $event->expects($this->once())
  90. ->method('getSubject')
  91. ->willReturn('unrelated');
  92. $this->expectException(InvalidArgumentException::class);
  93. $this->provider->parse($lang, $event);
  94. }
  95. }