SecurityProviderTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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->expects($this->once())
  91. ->method('setParsedSubject');
  92. $this->provider->parse($lang, $event);
  93. }
  94. public function testParseInvalidSubject() {
  95. $lang = 'ru';
  96. $l = $this->createMock(IL10N::class);
  97. $event = $this->createMock(IEvent::class);
  98. $event->expects($this->once())
  99. ->method('getType')
  100. ->willReturn('security');
  101. $this->l10n->expects($this->once())
  102. ->method('get')
  103. ->with('settings', $lang)
  104. ->willReturn($l);
  105. $event->expects($this->once())
  106. ->method('getSubject')
  107. ->willReturn('unrelated');
  108. $this->expectException(InvalidArgumentException::class);
  109. $this->provider->parse($lang, $event);
  110. }
  111. }