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.

ListenerTest.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  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\Comments\Tests\Unit\Activity;
  26. use OCA\Comments\Activity\Listener;
  27. use OCP\Activity\IEvent;
  28. use OCP\Activity\IManager;
  29. use OCP\App\IAppManager;
  30. use OCP\Comments\CommentsEvent;
  31. use OCP\Comments\IComment;
  32. use OCP\Files\Config\ICachedMountFileInfo;
  33. use OCP\Files\Config\IMountProviderCollection;
  34. use OCP\Files\Config\IUserMountCache;
  35. use OCP\Files\Folder;
  36. use OCP\Files\IRootFolder;
  37. use OCP\Files\Node;
  38. use OCP\IUser;
  39. use OCP\IUserSession;
  40. use OCP\Share\IShareHelper;
  41. use Test\TestCase;
  42. class ListenerTest extends TestCase {
  43. /** @var Listener */
  44. protected $listener;
  45. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  46. protected $activityManager;
  47. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  48. protected $session;
  49. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  50. protected $appManager;
  51. /** @var IMountProviderCollection|\PHPUnit\Framework\MockObject\MockObject */
  52. protected $mountProviderCollection;
  53. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  54. protected $rootFolder;
  55. /** @var IShareHelper|\PHPUnit\Framework\MockObject\MockObject */
  56. protected $shareHelper;
  57. protected function setUp(): void {
  58. parent::setUp();
  59. $this->activityManager = $this->createMock(IManager::class);
  60. $this->session = $this->createMock(IUserSession::class);
  61. $this->appManager = $this->createMock(IAppManager::class);
  62. $this->mountProviderCollection = $this->createMock(IMountProviderCollection::class);
  63. $this->rootFolder = $this->createMock(IRootFolder::class);
  64. $this->shareHelper = $this->createMock(IShareHelper::class);
  65. $this->listener = new Listener(
  66. $this->activityManager,
  67. $this->session,
  68. $this->appManager,
  69. $this->mountProviderCollection,
  70. $this->rootFolder,
  71. $this->shareHelper
  72. );
  73. }
  74. public function testCommentEvent() {
  75. $this->appManager->expects($this->any())
  76. ->method('isInstalled')
  77. ->with('activity')
  78. ->willReturn(true);
  79. $comment = $this->createMock(IComment::class);
  80. $comment->expects($this->any())
  81. ->method('getObjectType')
  82. ->willReturn('files');
  83. /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
  84. $event = $this->createMock(CommentsEvent::class);
  85. $event->expects($this->any())
  86. ->method('getComment')
  87. ->willReturn($comment);
  88. $event->expects($this->any())
  89. ->method('getEvent')
  90. ->willReturn(CommentsEvent::EVENT_ADD);
  91. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $ownerUser */
  92. $ownerUser = $this->createMock(IUser::class);
  93. $ownerUser->expects($this->any())
  94. ->method('getUID')
  95. ->willReturn('937393');
  96. /** @var \PHPUnit\Framework\MockObject\MockObject $mount */
  97. $mount = $this->createMock(ICachedMountFileInfo::class);
  98. $mount->expects($this->any())
  99. ->method('getUser')
  100. ->willReturn($ownerUser); // perhaps not the right user, but does not matter in this scenario
  101. $mounts = [ $mount, $mount ]; // to make sure duplicates are dealt with
  102. $userMountCache = $this->createMock(IUserMountCache::class);
  103. $userMountCache->expects($this->any())
  104. ->method('getMountsForFileId')
  105. ->willReturn($mounts);
  106. $this->mountProviderCollection->expects($this->any())
  107. ->method('getMountCache')
  108. ->willReturn($userMountCache);
  109. $node = $this->createMock(Node::class);
  110. $nodes = [ $node ];
  111. $ownerFolder = $this->createMock(Folder::class);
  112. $ownerFolder->expects($this->any())
  113. ->method('getById')
  114. ->willReturn($nodes);
  115. $this->rootFolder->expects($this->any())
  116. ->method('getUserFolder')
  117. ->willReturn($ownerFolder);
  118. $al = [ 'users' => [
  119. '873304' => 'i/got/it/here',
  120. '254342' => 'there/i/have/it',
  121. 'sandra' => 'and/here/i/placed/it'
  122. ]];
  123. $this->shareHelper->expects($this->any())
  124. ->method('getPathsForAccessList')
  125. ->willReturn($al);
  126. $this->session->expects($this->any())
  127. ->method('getUser')
  128. ->willReturn($ownerUser);
  129. /** @var \PHPUnit\Framework\MockObject\MockObject $activity */
  130. $activity = $this->createMock(IEvent::class);
  131. $activity->expects($this->exactly(count($al['users'])))
  132. ->method('setAffectedUser');
  133. $activity->expects($this->once())
  134. ->method('setApp')
  135. ->with('comments')
  136. ->willReturnSelf();
  137. $activity->expects($this->once())
  138. ->method('setType')
  139. ->with('comments')
  140. ->willReturnSelf();
  141. $activity->expects($this->once())
  142. ->method('setAuthor')
  143. ->with($ownerUser->getUID())
  144. ->willReturnSelf();
  145. $activity->expects($this->once())
  146. ->method('setObject')
  147. ->with('files', $this->anything())
  148. ->willReturnSelf();
  149. $activity->expects($this->once())
  150. ->method('setMessage')
  151. ->with('add_comment_message', $this->anything())
  152. ->willReturnSelf();
  153. $this->activityManager->expects($this->once())
  154. ->method('generateEvent')
  155. ->willReturn($activity);
  156. $this->activityManager->expects($this->exactly(count($al['users'])))
  157. ->method('publish');
  158. $this->listener->commentEvent($event);
  159. }
  160. }