CommentsPropertiesPluginTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  8. use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin as CommentPropertiesPluginImplementation;
  9. use OCA\DAV\Connector\Sabre\File;
  10. use OCP\Comments\ICommentsManager;
  11. use OCP\IUser;
  12. use OCP\IUserSession;
  13. use Sabre\DAV\PropFind;
  14. class CommentsPropertiesPluginTest extends \Test\TestCase {
  15. /** @var CommentPropertiesPluginImplementation */
  16. protected $plugin;
  17. protected $commentsManager;
  18. protected $userSession;
  19. protected $server;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->commentsManager = $this->getMockBuilder(ICommentsManager::class)
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $this->userSession = $this->getMockBuilder(IUserSession::class)
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->plugin = new CommentPropertiesPluginImplementation($this->commentsManager, $this->userSession);
  32. $this->plugin->initialize($this->server);
  33. }
  34. public function nodeProvider() {
  35. $mocks = [];
  36. foreach (['\OCA\DAV\Connector\Sabre\File', '\OCA\DAV\Connector\Sabre\Directory', '\Sabre\DAV\INode'] as $class) {
  37. $mocks[] = $this->getMockBuilder($class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. }
  41. return [
  42. [$mocks[0], true],
  43. [$mocks[1], true],
  44. [$mocks[2], false]
  45. ];
  46. }
  47. /**
  48. * @dataProvider nodeProvider
  49. * @param $node
  50. * @param $expectedSuccessful
  51. */
  52. public function testHandleGetProperties($node, $expectedSuccessful): void {
  53. $propFind = $this->getMockBuilder(PropFind::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. if ($expectedSuccessful) {
  57. $propFind->expects($this->exactly(3))
  58. ->method('handle');
  59. } else {
  60. $propFind->expects($this->never())
  61. ->method('handle');
  62. }
  63. $this->plugin->handleGetProperties($propFind, $node);
  64. }
  65. public function baseUriProvider() {
  66. return [
  67. ['owncloud/remote.php/webdav/', '4567', 'owncloud/remote.php/dav/comments/files/4567'],
  68. ['owncloud/remote.php/files/', '4567', 'owncloud/remote.php/dav/comments/files/4567'],
  69. ['owncloud/wicked.php/files/', '4567', null]
  70. ];
  71. }
  72. /**
  73. * @dataProvider baseUriProvider
  74. * @param $baseUri
  75. * @param $fid
  76. * @param $expectedHref
  77. */
  78. public function testGetCommentsLink($baseUri, $fid, $expectedHref): void {
  79. $node = $this->getMockBuilder(File::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $node->expects($this->any())
  83. ->method('getId')
  84. ->willReturn($fid);
  85. $this->server->expects($this->once())
  86. ->method('getBaseUri')
  87. ->willReturn($baseUri);
  88. $href = $this->plugin->getCommentsLink($node);
  89. $this->assertSame($expectedHref, $href);
  90. }
  91. public function userProvider() {
  92. return [
  93. [
  94. $this->getMockBuilder(IUser::class)
  95. ->disableOriginalConstructor()
  96. ->getMock()
  97. ],
  98. [null]
  99. ];
  100. }
  101. /**
  102. * @dataProvider userProvider
  103. * @param $user
  104. */
  105. public function testGetUnreadCount($user): void {
  106. $node = $this->getMockBuilder(File::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $node->expects($this->any())
  110. ->method('getId')
  111. ->willReturn('4567');
  112. $this->userSession->expects($this->once())
  113. ->method('getUser')
  114. ->willReturn($user);
  115. $this->commentsManager->expects($this->any())
  116. ->method('getNumberOfCommentsForObject')
  117. ->willReturn(42);
  118. $unread = $this->plugin->getUnreadCount($node);
  119. if (is_null($user)) {
  120. $this->assertNull($unread);
  121. } else {
  122. $this->assertSame($unread, 42);
  123. }
  124. }
  125. }