ProviderTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  4. * @copyright Copyright (c) 2017 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 OCA\TwoFactorBackupCodes\Test\Unit\Activity;
  22. use InvalidArgumentException;
  23. use OCA\TwoFactorBackupCodes\Activity\Provider;
  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 ProviderTest 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 Provider */
  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 Provider($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('getApp')
  52. ->willReturn('comments');
  53. $this->expectException(InvalidArgumentException::class);
  54. $this->provider->parse($lang, $event);
  55. }
  56. public function subjectData() {
  57. return [
  58. ['codes_generated'],
  59. ];
  60. }
  61. /**
  62. * @dataProvider subjectData
  63. */
  64. public function testParse($subject) {
  65. $lang = 'ru';
  66. $event = $this->createMock(IEvent::class);
  67. $l = $this->createMock(IL10N::class);
  68. $event->expects($this->once())
  69. ->method('getApp')
  70. ->willReturn('twofactor_backupcodes');
  71. $this->l10n->expects($this->once())
  72. ->method('get')
  73. ->with('twofactor_backupcodes', $lang)
  74. ->willReturn($l);
  75. $this->urlGenerator->expects($this->once())
  76. ->method('imagePath')
  77. ->with('core', 'actions/password.svg')
  78. ->willReturn('path/to/image');
  79. $this->urlGenerator->expects($this->once())
  80. ->method('getAbsoluteURL')
  81. ->with('path/to/image')
  82. ->willReturn('absolute/path/to/image');
  83. $event->expects($this->once())
  84. ->method('setIcon')
  85. ->with('absolute/path/to/image');
  86. $event->expects($this->once())
  87. ->method('getSubject')
  88. ->willReturn($subject);
  89. $event->expects($this->once())
  90. ->method('setParsedSubject');
  91. $this->provider->parse($lang, $event);
  92. }
  93. public function testParseInvalidSubject() {
  94. $lang = 'ru';
  95. $l = $this->createMock(IL10N::class);
  96. $event = $this->createMock(IEvent::class);
  97. $event->expects($this->once())
  98. ->method('getApp')
  99. ->willReturn('twofactor_backupcodes');
  100. $this->l10n->expects($this->once())
  101. ->method('get')
  102. ->with('twofactor_backupcodes', $lang)
  103. ->willReturn($l);
  104. $event->expects($this->once())
  105. ->method('getSubject')
  106. ->willReturn('unrelated');
  107. $this->expectException(InvalidArgumentException::class);
  108. $this->provider->parse($lang, $event);
  109. }
  110. }