You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActivityPublisherTest.php 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Listener;
  23. use OCA\TwoFactorBackupCodes\Event\CodesGenerated;
  24. use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher;
  25. use OCP\Activity\IEvent;
  26. use OCP\Activity\IManager;
  27. use OCP\EventDispatcher\Event;
  28. use OCP\ILogger;
  29. use OCP\IUser;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Test\TestCase;
  32. class ActivityPublisherTest extends TestCase {
  33. /** @var IManager|MockObject */
  34. private $activityManager;
  35. /** @var ILogger */
  36. private $logger;
  37. /** @var ActivityPublisher */
  38. private $listener;
  39. protected function setUp() {
  40. parent::setUp();
  41. $this->activityManager = $this->createMock(IManager::class);
  42. $this->logger = $this->createMock(ILogger::class);
  43. $this->listener = new ActivityPublisher($this->activityManager, $this->logger);
  44. }
  45. public function testHandleGenericEvent() {
  46. $event = $this->createMock(Event::class);
  47. $this->activityManager->expects($this->never())
  48. ->method('publish');
  49. $this->listener->handle($event);
  50. }
  51. public function testHandleCodesGeneratedEvent() {
  52. $user = $this->createMock(IUser::class);
  53. $user->method('getUID')->willReturn('fritz');
  54. $event = new CodesGenerated($user);
  55. $activityEvent = $this->createMock(IEvent::class);
  56. $this->activityManager->expects($this->once())
  57. ->method('generateEvent')
  58. ->will($this->returnValue($activityEvent));
  59. $activityEvent->expects($this->once())
  60. ->method('setApp')
  61. ->with('twofactor_backupcodes')
  62. ->will($this->returnSelf());
  63. $activityEvent->expects($this->once())
  64. ->method('setType')
  65. ->with('security')
  66. ->will($this->returnSelf());
  67. $activityEvent->expects($this->once())
  68. ->method('setAuthor')
  69. ->with('fritz')
  70. ->will($this->returnSelf());
  71. $activityEvent->expects($this->once())
  72. ->method('setAffectedUser')
  73. ->with('fritz')
  74. ->will($this->returnSelf());
  75. $this->activityManager->expects($this->once())
  76. ->method('publish')
  77. ->will($this->returnValue($activityEvent));
  78. $this->listener->handle($event);
  79. }
  80. }