RootCollectionTest.php 5.6 KB

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