SecurityProviderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Settings\Tests;
  26. use InvalidArgumentException;
  27. use OCA\Settings\Activity\SecurityProvider;
  28. use OCP\Activity\IEvent;
  29. use OCP\Activity\IManager;
  30. use OCP\IL10N;
  31. use OCP\IURLGenerator;
  32. use OCP\L10N\IFactory;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. use Test\TestCase;
  35. class SecurityProviderTest extends TestCase {
  36. /** @var IFactory|MockObject */
  37. private $l10n;
  38. /** @var IURLGenerator|MockObject */
  39. private $urlGenerator;
  40. /** @var IManager|MockObject */
  41. private $activityManager;
  42. /** @var SecurityProvider */
  43. private $provider;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->l10n = $this->createMock(IFactory::class);
  47. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  48. $this->activityManager = $this->createMock(IManager::class);
  49. $this->provider = new SecurityProvider($this->l10n, $this->urlGenerator, $this->activityManager);
  50. }
  51. public function testParseUnrelated() {
  52. $lang = 'ru';
  53. $event = $this->createMock(IEvent::class);
  54. $event->expects($this->once())
  55. ->method('getType')
  56. ->willReturn('comments');
  57. $this->expectException(InvalidArgumentException::class);
  58. $this->provider->parse($lang, $event);
  59. }
  60. public function subjectData() {
  61. return [
  62. ['twofactor_success'],
  63. ['twofactor_failed'],
  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('getType')
  75. ->willReturn('security');
  76. $this->l10n->expects($this->once())
  77. ->method('get')
  78. ->with('settings', $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->method('getSubjectParameters')
  95. ->willReturn([
  96. 'provider' => 'myProvider',
  97. ]);
  98. $event->expects($this->once())
  99. ->method('setParsedSubject');
  100. $this->provider->parse($lang, $event);
  101. }
  102. public function testParseInvalidSubject() {
  103. $lang = 'ru';
  104. $l = $this->createMock(IL10N::class);
  105. $event = $this->createMock(IEvent::class);
  106. $event->expects($this->once())
  107. ->method('getType')
  108. ->willReturn('security');
  109. $this->l10n->expects($this->once())
  110. ->method('get')
  111. ->with('settings', $lang)
  112. ->willReturn($l);
  113. $event->expects($this->once())
  114. ->method('getSubject')
  115. ->willReturn('unrelated');
  116. $this->expectException(InvalidArgumentException::class);
  117. $this->provider->parse($lang, $event);
  118. }
  119. }