RootCollectionTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\Comments;
  27. use OC\EventDispatcher\EventDispatcher;
  28. use OCA\DAV\Comments\EntityTypeCollection as EntityTypeCollectionImplementation;
  29. use OCP\Comments\CommentsEntityEvent;
  30. use OCP\Comments\ICommentsManager;
  31. use OCP\EventDispatcher\IEventDispatcher;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use OCP\IUserSession;
  35. use Psr\Log\LoggerInterface;
  36. class RootCollectionTest extends \Test\TestCase {
  37. /** @var \OCP\Comments\ICommentsManager|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $commentsManager;
  39. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $userManager;
  41. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  42. protected $logger;
  43. /** @var \OCA\DAV\Comments\RootCollection */
  44. protected $collection;
  45. /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  46. protected $userSession;
  47. /** @var IEventDispatcher */
  48. protected $dispatcher;
  49. /** @var \OCP\IUser|\PHPUnit\Framework\MockObject\MockObject */
  50. protected $user;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->user = $this->getMockBuilder(IUser::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->userManager = $this->getMockBuilder(IUserManager::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->userSession = $this->getMockBuilder(IUserSession::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->dispatcher = new EventDispatcher(
  69. new \Symfony\Component\EventDispatcher\EventDispatcher(),
  70. \OC::$server,
  71. $this->logger
  72. );
  73. $this->collection = new \OCA\DAV\Comments\RootCollection(
  74. $this->commentsManager,
  75. $this->userManager,
  76. $this->userSession,
  77. $this->dispatcher,
  78. $this->logger
  79. );
  80. }
  81. protected function prepareForInitCollections() {
  82. $this->user->expects($this->any())
  83. ->method('getUID')
  84. ->willReturn('alice');
  85. $this->userSession->expects($this->once())
  86. ->method('getUser')
  87. ->willReturn($this->user);
  88. $this->dispatcher->addListener(CommentsEntityEvent::class, function (CommentsEntityEvent $event): void {
  89. $event->addEntityCollection('files', function () {
  90. return true;
  91. });
  92. });
  93. }
  94. public function testCreateFile(): void {
  95. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  96. $this->collection->createFile('foo');
  97. }
  98. public function testCreateDirectory(): void {
  99. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  100. $this->collection->createDirectory('foo');
  101. }
  102. public function testGetChild(): void {
  103. $this->prepareForInitCollections();
  104. $etc = $this->collection->getChild('files');
  105. $this->assertTrue($etc instanceof EntityTypeCollectionImplementation);
  106. }
  107. public function testGetChildInvalid(): void {
  108. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  109. $this->prepareForInitCollections();
  110. $this->collection->getChild('robots');
  111. }
  112. public function testGetChildNoAuth(): void {
  113. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  114. $this->collection->getChild('files');
  115. }
  116. public function testGetChildren(): void {
  117. $this->prepareForInitCollections();
  118. $children = $this->collection->getChildren();
  119. $this->assertFalse(empty($children));
  120. foreach ($children as $child) {
  121. $this->assertTrue($child instanceof EntityTypeCollectionImplementation);
  122. }
  123. }
  124. public function testGetChildrenNoAuth(): void {
  125. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  126. $this->collection->getChildren();
  127. }
  128. public function testChildExistsYes(): void {
  129. $this->prepareForInitCollections();
  130. $this->assertTrue($this->collection->childExists('files'));
  131. }
  132. public function testChildExistsNo(): void {
  133. $this->prepareForInitCollections();
  134. $this->assertFalse($this->collection->childExists('robots'));
  135. }
  136. public function testChildExistsNoAuth(): void {
  137. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  138. $this->collection->childExists('files');
  139. }
  140. public function testDelete(): void {
  141. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  142. $this->collection->delete();
  143. }
  144. public function testGetName(): void {
  145. $this->assertSame('comments', $this->collection->getName());
  146. }
  147. public function testSetName(): void {
  148. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  149. $this->collection->setName('foobar');
  150. }
  151. public function testGetLastModified(): void {
  152. $this->assertSame(null, $this->collection->getLastModified());
  153. }
  154. }