CommentsPropertiesPluginTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Connector\Sabre;
  27. use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin as CommentPropertiesPluginImplementation;
  28. use OCA\DAV\Connector\Sabre\File;
  29. use OCP\Comments\ICommentsManager;
  30. use OCP\IUser;
  31. use OCP\IUserSession;
  32. use Sabre\DAV\PropFind;
  33. class CommentsPropertiesPluginTest extends \Test\TestCase {
  34. /** @var CommentPropertiesPluginImplementation */
  35. protected $plugin;
  36. protected $commentsManager;
  37. protected $userSession;
  38. protected $server;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->userSession = $this->getMockBuilder(IUserSession::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->plugin = new CommentPropertiesPluginImplementation($this->commentsManager, $this->userSession);
  51. $this->plugin->initialize($this->server);
  52. }
  53. public function nodeProvider() {
  54. $mocks = [];
  55. foreach (['\OCA\DAV\Connector\Sabre\File', '\OCA\DAV\Connector\Sabre\Directory', '\Sabre\DAV\INode'] as $class) {
  56. $mocks[] = $this->getMockBuilder($class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. }
  60. return [
  61. [$mocks[0], true],
  62. [$mocks[1], true],
  63. [$mocks[2], false]
  64. ];
  65. }
  66. /**
  67. * @dataProvider nodeProvider
  68. * @param $node
  69. * @param $expectedSuccessful
  70. */
  71. public function testHandleGetProperties($node, $expectedSuccessful): void {
  72. $propFind = $this->getMockBuilder(PropFind::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. if ($expectedSuccessful) {
  76. $propFind->expects($this->exactly(3))
  77. ->method('handle');
  78. } else {
  79. $propFind->expects($this->never())
  80. ->method('handle');
  81. }
  82. $this->plugin->handleGetProperties($propFind, $node);
  83. }
  84. public function baseUriProvider() {
  85. return [
  86. ['owncloud/remote.php/webdav/', '4567', 'owncloud/remote.php/dav/comments/files/4567'],
  87. ['owncloud/remote.php/files/', '4567', 'owncloud/remote.php/dav/comments/files/4567'],
  88. ['owncloud/wicked.php/files/', '4567', null]
  89. ];
  90. }
  91. /**
  92. * @dataProvider baseUriProvider
  93. * @param $baseUri
  94. * @param $fid
  95. * @param $expectedHref
  96. */
  97. public function testGetCommentsLink($baseUri, $fid, $expectedHref): void {
  98. $node = $this->getMockBuilder(File::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $node->expects($this->any())
  102. ->method('getId')
  103. ->willReturn($fid);
  104. $this->server->expects($this->once())
  105. ->method('getBaseUri')
  106. ->willReturn($baseUri);
  107. $href = $this->plugin->getCommentsLink($node);
  108. $this->assertSame($expectedHref, $href);
  109. }
  110. public function userProvider() {
  111. return [
  112. [
  113. $this->getMockBuilder(IUser::class)
  114. ->disableOriginalConstructor()
  115. ->getMock()
  116. ],
  117. [null]
  118. ];
  119. }
  120. /**
  121. * @dataProvider userProvider
  122. * @param $user
  123. */
  124. public function testGetUnreadCount($user): void {
  125. $node = $this->getMockBuilder(File::class)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $node->expects($this->any())
  129. ->method('getId')
  130. ->willReturn('4567');
  131. $this->userSession->expects($this->once())
  132. ->method('getUser')
  133. ->willReturn($user);
  134. $this->commentsManager->expects($this->any())
  135. ->method('getNumberOfCommentsForObject')
  136. ->willReturn(42);
  137. $unread = $this->plugin->getUnreadCount($node);
  138. if (is_null($user)) {
  139. $this->assertNull($unread);
  140. } else {
  141. $this->assertSame($unread, 42);
  142. }
  143. }
  144. }