RootCollectionTest.php 5.2 KB

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