SecurityProviderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  4. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * Two-factor backup codes
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Settings\Activity;
  22. use InvalidArgumentException;
  23. use OC\Settings\Activity\SecurityProvider;
  24. use OCP\Activity\IEvent;
  25. use OCP\Activity\IManager;
  26. use OCP\IL10N;
  27. use OCP\IURLGenerator;
  28. use OCP\L10N\IFactory;
  29. use PHPUnit_Framework_MockObject_MockObject;
  30. use Test\TestCase;
  31. class SecurityProviderTest extends TestCase {
  32. /** @var IFactory|PHPUnit_Framework_MockObject_MockObject */
  33. private $l10n;
  34. /** @var IURLGenerator|PHPUnit_Framework_MockObject_MockObject */
  35. private $urlGenerator;
  36. /** @var IManager|PHPUnit_Framework_MockObject_MockObject */
  37. private $activityManager;
  38. /** @var SecurityProvider */
  39. private $provider;
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->l10n = $this->createMock(IFactory::class);
  43. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  44. $this->activityManager = $this->createMock(IManager::class);
  45. $this->provider = new SecurityProvider($this->l10n, $this->urlGenerator, $this->activityManager);
  46. }
  47. public function testParseUnrelated() {
  48. $lang = 'ru';
  49. $event = $this->createMock(IEvent::class);
  50. $event->expects($this->once())
  51. ->method('getType')
  52. ->willReturn('comments');
  53. $this->expectException(InvalidArgumentException::class);
  54. $this->provider->parse($lang, $event);
  55. }
  56. public function subjectData() {
  57. return [
  58. ['twofactor_success'],
  59. ['twofactor_failed'],
  60. ];
  61. }
  62. /**
  63. * @dataProvider subjectData
  64. */
  65. public function testParse($subject) {
  66. $lang = 'ru';
  67. $event = $this->createMock(IEvent::class);
  68. $l = $this->createMock(IL10N::class);
  69. $event->expects($this->once())
  70. ->method('getType')
  71. ->willReturn('security');
  72. $this->l10n->expects($this->once())
  73. ->method('get')
  74. ->with('settings', $lang)
  75. ->willReturn($l);
  76. $this->urlGenerator->expects($this->once())
  77. ->method('imagePath')
  78. ->with('core', 'actions/password.svg')
  79. ->willReturn('path/to/image');
  80. $this->urlGenerator->expects($this->once())
  81. ->method('getAbsoluteURL')
  82. ->with('path/to/image')
  83. ->willReturn('absolute/path/to/image');
  84. $event->expects($this->once())
  85. ->method('setIcon')
  86. ->with('absolute/path/to/image');
  87. $event->expects($this->once())
  88. ->method('getSubject')
  89. ->willReturn($subject);
  90. $event->method('getSubjectParameters')
  91. ->willReturn([
  92. 'provider' => 'myProvider',
  93. ]);
  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('getType')
  104. ->willReturn('security');
  105. $this->l10n->expects($this->once())
  106. ->method('get')
  107. ->with('settings', $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. }