1
0

SecurityProviderTest.php 3.0 KB

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