NodeTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OC\Files\FileInfo;
  30. use OC\Files\View;
  31. use OCP\Files\Mount\IMountPoint;
  32. use OCP\Files\Storage;
  33. use OCP\Share\IManager;
  34. use OCP\Share\IShare;
  35. /**
  36. * Class NodeTest
  37. *
  38. * @group DB
  39. * @package OCA\DAV\Tests\unit\Connector\Sabre
  40. */
  41. class NodeTest extends \Test\TestCase {
  42. public function davPermissionsProvider() {
  43. return [
  44. [\OCP\Constants::PERMISSION_ALL, 'file', false, false, 'RGDNVW'],
  45. [\OCP\Constants::PERMISSION_ALL, 'dir', false, false, 'RGDNVCK'],
  46. [\OCP\Constants::PERMISSION_ALL, 'file', true, false, 'SRGDNVW'],
  47. [\OCP\Constants::PERMISSION_ALL, 'file', true, true, 'SRMGDNVW'],
  48. [\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_SHARE, 'file', true, false, 'SGDNVW'],
  49. [\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_UPDATE, 'file', false, false, 'RGD'],
  50. [\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_DELETE, 'file', false, false, 'RGNVW'],
  51. [\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, 'file', false, false, 'RGDNVW'],
  52. [\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_READ, 'file', false, false, 'RDNVW'],
  53. [\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, 'dir', false, false, 'RGDNV'],
  54. [\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_READ, 'dir', false, false, 'RDNVCK'],
  55. ];
  56. }
  57. /**
  58. * @dataProvider davPermissionsProvider
  59. */
  60. public function testDavPermissions($permissions, $type, $shared, $mounted, $expected) {
  61. $info = $this->getMockBuilder(FileInfo::class)
  62. ->disableOriginalConstructor()
  63. ->setMethods(['getPermissions', 'isShared', 'isMounted', 'getType'])
  64. ->getMock();
  65. $info->expects($this->any())
  66. ->method('getPermissions')
  67. ->willReturn($permissions);
  68. $info->expects($this->any())
  69. ->method('isShared')
  70. ->willReturn($shared);
  71. $info->expects($this->any())
  72. ->method('isMounted')
  73. ->willReturn($mounted);
  74. $info->expects($this->any())
  75. ->method('getType')
  76. ->willReturn($type);
  77. $view = $this->getMockBuilder(View::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  81. $this->assertEquals($expected, $node->getDavPermissions());
  82. }
  83. public function sharePermissionsProvider() {
  84. return [
  85. [\OCP\Files\FileInfo::TYPE_FILE, null, 1, 1],
  86. [\OCP\Files\FileInfo::TYPE_FILE, null, 3, 3],
  87. [\OCP\Files\FileInfo::TYPE_FILE, null, 5, 1],
  88. [\OCP\Files\FileInfo::TYPE_FILE, null, 7, 3],
  89. [\OCP\Files\FileInfo::TYPE_FILE, null, 9, 1],
  90. [\OCP\Files\FileInfo::TYPE_FILE, null, 11, 3],
  91. [\OCP\Files\FileInfo::TYPE_FILE, null, 13, 1],
  92. [\OCP\Files\FileInfo::TYPE_FILE, null, 15, 3],
  93. [\OCP\Files\FileInfo::TYPE_FILE, null, 17, 17],
  94. [\OCP\Files\FileInfo::TYPE_FILE, null, 19, 19],
  95. [\OCP\Files\FileInfo::TYPE_FILE, null, 21, 17],
  96. [\OCP\Files\FileInfo::TYPE_FILE, null, 23, 19],
  97. [\OCP\Files\FileInfo::TYPE_FILE, null, 25, 17],
  98. [\OCP\Files\FileInfo::TYPE_FILE, null, 27, 19],
  99. [\OCP\Files\FileInfo::TYPE_FILE, null, 29, 17],
  100. [\OCP\Files\FileInfo::TYPE_FILE, null, 30, 18],
  101. [\OCP\Files\FileInfo::TYPE_FILE, null, 31, 19],
  102. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 1, 1],
  103. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 3, 3],
  104. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 5, 5],
  105. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 7, 7],
  106. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 9, 9],
  107. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 11, 11],
  108. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 13, 13],
  109. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 15, 15],
  110. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 17, 17],
  111. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 19, 19],
  112. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 21, 21],
  113. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 23, 23],
  114. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 25, 25],
  115. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 27, 27],
  116. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 29, 29],
  117. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 30, 30],
  118. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 31, 31],
  119. [\OCP\Files\FileInfo::TYPE_FOLDER, 'shareToken', 7, 7],
  120. ];
  121. }
  122. /**
  123. * @dataProvider sharePermissionsProvider
  124. */
  125. public function testSharePermissions($type, $user, $permissions, $expected) {
  126. $storage = $this->getMockBuilder(Storage::class)
  127. ->disableOriginalConstructor()
  128. ->getMock();
  129. $storage->method('getPermissions')->willReturn($permissions);
  130. $mountpoint = $this->getMockBuilder(IMountPoint::class)
  131. ->disableOriginalConstructor()
  132. ->getMock();
  133. $mountpoint->method('getMountPoint')->willReturn('myPath');
  134. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  135. $share = $this->getMockBuilder(IShare::class)->disableOriginalConstructor()->getMock();
  136. if ($user === null) {
  137. $shareManager->expects($this->never())->method('getShareByToken');
  138. $share->expects($this->never())->method('getPermissions');
  139. } else {
  140. $shareManager->expects($this->once())->method('getShareByToken')->with($user)
  141. ->willReturn($share);
  142. $share->expects($this->once())->method('getPermissions')->willReturn($permissions);
  143. }
  144. $info = $this->getMockBuilder(FileInfo::class)
  145. ->disableOriginalConstructor()
  146. ->setMethods(['getStorage', 'getType', 'getMountPoint', 'getPermissions'])
  147. ->getMock();
  148. $info->method('getStorage')->willReturn($storage);
  149. $info->method('getType')->willReturn($type);
  150. $info->method('getMountPoint')->willReturn($mountpoint);
  151. $info->method('getPermissions')->willReturn($permissions);
  152. $view = $this->getMockBuilder(View::class)
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  156. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  157. $this->assertEquals($expected, $node->getSharePermissions($user));
  158. }
  159. }