RootCollectionTest.php 4.8 KB

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