RootCollectionTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Comments;
  8. use OC\EventDispatcher\EventDispatcher;
  9. use OCA\DAV\Comments\EntityTypeCollection as EntityTypeCollectionImplementation;
  10. use OCA\DAV\Comments\RootCollection;
  11. use OCP\Comments\CommentsEntityEvent;
  12. use OCP\Comments\ICommentsManager;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use OCP\IUserSession;
  17. use Psr\Log\LoggerInterface;
  18. class RootCollectionTest extends \Test\TestCase {
  19. /** @var ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $commentsManager;
  21. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  22. protected $userManager;
  23. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  24. protected $logger;
  25. /** @var RootCollection */
  26. protected $collection;
  27. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  28. protected $userSession;
  29. /** @var IEventDispatcher */
  30. protected $dispatcher;
  31. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  32. protected $user;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->user = $this->getMockBuilder(IUser::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->userManager = $this->getMockBuilder(IUserManager::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->userSession = $this->getMockBuilder(IUserSession::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->dispatcher = new EventDispatcher(
  51. new \Symfony\Component\EventDispatcher\EventDispatcher(),
  52. \OC::$server,
  53. $this->logger
  54. );
  55. $this->collection = new RootCollection(
  56. $this->commentsManager,
  57. $this->userManager,
  58. $this->userSession,
  59. $this->dispatcher,
  60. $this->logger
  61. );
  62. }
  63. protected function prepareForInitCollections() {
  64. $this->user->expects($this->any())
  65. ->method('getUID')
  66. ->willReturn('alice');
  67. $this->userSession->expects($this->once())
  68. ->method('getUser')
  69. ->willReturn($this->user);
  70. $this->dispatcher->addListener(CommentsEntityEvent::class, function (CommentsEntityEvent $event): void {
  71. $event->addEntityCollection('files', function () {
  72. return true;
  73. });
  74. });
  75. }
  76. public function testCreateFile(): void {
  77. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  78. $this->collection->createFile('foo');
  79. }
  80. public function testCreateDirectory(): void {
  81. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  82. $this->collection->createDirectory('foo');
  83. }
  84. public function testGetChild(): void {
  85. $this->prepareForInitCollections();
  86. $etc = $this->collection->getChild('files');
  87. $this->assertTrue($etc instanceof EntityTypeCollectionImplementation);
  88. }
  89. public function testGetChildInvalid(): void {
  90. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  91. $this->prepareForInitCollections();
  92. $this->collection->getChild('robots');
  93. }
  94. public function testGetChildNoAuth(): void {
  95. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  96. $this->collection->getChild('files');
  97. }
  98. public function testGetChildren(): void {
  99. $this->prepareForInitCollections();
  100. $children = $this->collection->getChildren();
  101. $this->assertFalse(empty($children));
  102. foreach ($children as $child) {
  103. $this->assertTrue($child instanceof EntityTypeCollectionImplementation);
  104. }
  105. }
  106. public function testGetChildrenNoAuth(): void {
  107. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  108. $this->collection->getChildren();
  109. }
  110. public function testChildExistsYes(): void {
  111. $this->prepareForInitCollections();
  112. $this->assertTrue($this->collection->childExists('files'));
  113. }
  114. public function testChildExistsNo(): void {
  115. $this->prepareForInitCollections();
  116. $this->assertFalse($this->collection->childExists('robots'));
  117. }
  118. public function testChildExistsNoAuth(): void {
  119. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  120. $this->collection->childExists('files');
  121. }
  122. public function testDelete(): void {
  123. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  124. $this->collection->delete();
  125. }
  126. public function testGetName(): void {
  127. $this->assertSame('comments', $this->collection->getName());
  128. }
  129. public function testSetName(): void {
  130. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  131. $this->collection->setName('foobar');
  132. }
  133. public function testGetLastModified(): void {
  134. $this->assertSame(null, $this->collection->getLastModified());
  135. }
  136. }