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.

RootCollectionTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\Comments;
  26. use OC\EventDispatcher\EventDispatcher;
  27. use OC\EventDispatcher\SymfonyAdapter;
  28. use OCA\DAV\Comments\EntityTypeCollection as EntityTypeCollectionImplementation;
  29. use OCP\Comments\CommentsEntityEvent;
  30. use OCP\Comments\ICommentsManager;
  31. use OCP\ILogger;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use OCP\IUserSession;
  35. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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 \OCP\ILogger|\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 EventDispatcherInterface */
  48. protected $dispatcher;
  49. /** @var \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject */
  50. protected $user;
  51. public function setUp() {
  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(ILogger::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->dispatcher = new SymfonyAdapter(
  69. new EventDispatcher(
  70. new \Symfony\Component\EventDispatcher\EventDispatcher(),
  71. \OC::$server,
  72. $this->logger
  73. ),
  74. $this->logger
  75. );
  76. $this->collection = new \OCA\DAV\Comments\RootCollection(
  77. $this->commentsManager,
  78. $this->userManager,
  79. $this->userSession,
  80. $this->dispatcher,
  81. $this->logger
  82. );
  83. }
  84. protected function prepareForInitCollections() {
  85. $this->user->expects($this->any())
  86. ->method('getUID')
  87. ->will($this->returnValue('alice'));
  88. $this->userSession->expects($this->once())
  89. ->method('getUser')
  90. ->will($this->returnValue($this->user));
  91. $this->dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) {
  92. $event->addEntityCollection('files', function() {
  93. return true;
  94. });
  95. });
  96. }
  97. /**
  98. * @expectedException \Sabre\DAV\Exception\Forbidden
  99. */
  100. public function testCreateFile() {
  101. $this->collection->createFile('foo');
  102. }
  103. /**
  104. * @expectedException \Sabre\DAV\Exception\Forbidden
  105. */
  106. public function testCreateDirectory() {
  107. $this->collection->createDirectory('foo');
  108. }
  109. public function testGetChild() {
  110. $this->prepareForInitCollections();
  111. $etc = $this->collection->getChild('files');
  112. $this->assertTrue($etc instanceof EntityTypeCollectionImplementation);
  113. }
  114. /**
  115. * @expectedException \Sabre\DAV\Exception\NotFound
  116. */
  117. public function testGetChildInvalid() {
  118. $this->prepareForInitCollections();
  119. $this->collection->getChild('robots');
  120. }
  121. /**
  122. * @expectedException \Sabre\DAV\Exception\NotAuthenticated
  123. */
  124. public function testGetChildNoAuth() {
  125. $this->collection->getChild('files');
  126. }
  127. public function testGetChildren() {
  128. $this->prepareForInitCollections();
  129. $children = $this->collection->getChildren();
  130. $this->assertFalse(empty($children));
  131. foreach($children as $child) {
  132. $this->assertTrue($child instanceof EntityTypeCollectionImplementation);
  133. }
  134. }
  135. /**
  136. * @expectedException \Sabre\DAV\Exception\NotAuthenticated
  137. */
  138. public function testGetChildrenNoAuth() {
  139. $this->collection->getChildren();
  140. }
  141. public function testChildExistsYes() {
  142. $this->prepareForInitCollections();
  143. $this->assertTrue($this->collection->childExists('files'));
  144. }
  145. public function testChildExistsNo() {
  146. $this->prepareForInitCollections();
  147. $this->assertFalse($this->collection->childExists('robots'));
  148. }
  149. /**
  150. * @expectedException \Sabre\DAV\Exception\NotAuthenticated
  151. */
  152. public function testChildExistsNoAuth() {
  153. $this->collection->childExists('files');
  154. }
  155. /**
  156. * @expectedException \Sabre\DAV\Exception\Forbidden
  157. */
  158. public function testDelete() {
  159. $this->collection->delete();
  160. }
  161. public function testGetName() {
  162. $this->assertSame('comments', $this->collection->getName());
  163. }
  164. /**
  165. * @expectedException \Sabre\DAV\Exception\Forbidden
  166. */
  167. public function testSetName() {
  168. $this->collection->setName('foobar');
  169. }
  170. public function testGetLastModified() {
  171. $this->assertSame(null, $this->collection->getLastModified());
  172. }
  173. }