1
0

CustomPropertiesBackendTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  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
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Tests\DAV;
  27. use OCA\DAV\DAV\CustomPropertiesBackend;
  28. use OCP\IDBConnection;
  29. use OCP\IUser;
  30. use Sabre\DAV\PropFind;
  31. use Sabre\DAV\PropPatch;
  32. use Sabre\DAV\Tree;
  33. use Test\TestCase;
  34. /**
  35. * @group DB
  36. */
  37. class CustomPropertiesBackendTest extends TestCase {
  38. /** @var Tree | \PHPUnit_Framework_MockObject_MockObject */
  39. private $tree;
  40. /** @var IDBConnection */
  41. private $dbConnection;
  42. /** @var IUser | \PHPUnit_Framework_MockObject_MockObject */
  43. private $user;
  44. /** @var CustomPropertiesBackend | \PHPUnit_Framework_MockObject_MockObject */
  45. private $backend;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->tree = $this->createMock(Tree::class);
  49. $this->user = $this->createMock(IUser::class);
  50. $this->user->method('getUID')
  51. ->with()
  52. ->willReturn('dummy_user_42');
  53. $this->dbConnection = \OC::$server->getDatabaseConnection();
  54. $this->backend = new CustomPropertiesBackend(
  55. $this->tree,
  56. $this->dbConnection,
  57. $this->user
  58. );
  59. }
  60. protected function tearDown(): void {
  61. $query = $this->dbConnection->getQueryBuilder();
  62. $query->delete('properties');
  63. $query->execute();
  64. parent::tearDown();
  65. }
  66. private function formatPath(string $path): string {
  67. if (strlen($path) > 250) {
  68. return sha1($path);
  69. } else {
  70. return $path;
  71. }
  72. }
  73. protected function insertProps(string $user, string $path, array $props) {
  74. foreach ($props as $name => $value) {
  75. $this->insertProp($user, $path, $name, $value);
  76. }
  77. }
  78. protected function insertProp(string $user, string $path, string $name, string $value) {
  79. $query = $this->dbConnection->getQueryBuilder();
  80. $query->insert('properties')
  81. ->values([
  82. 'userid' => $query->createNamedParameter($user),
  83. 'propertypath' => $query->createNamedParameter($this->formatPath($path)),
  84. 'propertyname' => $query->createNamedParameter($name),
  85. 'propertyvalue' => $query->createNamedParameter($value),
  86. ]);
  87. $query->execute();
  88. }
  89. protected function getProps(string $user, string $path) {
  90. $query = $this->dbConnection->getQueryBuilder();
  91. $query->select('propertyname', 'propertyvalue')
  92. ->from('properties')
  93. ->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
  94. ->where($query->expr()->eq('propertypath', $query->createNamedParameter($this->formatPath($path))));
  95. return $query->execute()->fetchAll(\PDO::FETCH_KEY_PAIR);
  96. }
  97. public function testPropFindNoDbCalls() {
  98. $db = $this->createMock(IDBConnection::class);
  99. $backend = new CustomPropertiesBackend(
  100. $this->tree,
  101. $db,
  102. $this->user
  103. );
  104. $propFind = $this->createMock(PropFind::class);
  105. $propFind->expects($this->at(0))
  106. ->method('get404Properties')
  107. ->with()
  108. ->willReturn([
  109. '{http://owncloud.org/ns}permissions',
  110. '{http://owncloud.org/ns}downloadURL',
  111. '{http://owncloud.org/ns}dDC',
  112. '{http://owncloud.org/ns}size',
  113. ]);
  114. $db->expects($this->never())
  115. ->method($this->anything());
  116. $backend->propFind('foo_bar_path_1337_0', $propFind);
  117. }
  118. public function testPropFindCalendarCall() {
  119. $propFind = $this->createMock(PropFind::class);
  120. $propFind->method('get404Properties')
  121. ->with()
  122. ->willReturn([
  123. '{DAV:}getcontentlength',
  124. '{DAV:}getcontenttype',
  125. '{DAV:}getetag',
  126. '{abc}def',
  127. ]);
  128. $propFind->method('getRequestedProperties')
  129. ->with()
  130. ->willReturn([
  131. '{DAV:}getcontentlength',
  132. '{DAV:}getcontenttype',
  133. '{DAV:}getetag',
  134. '{DAV:}displayname',
  135. '{urn:ietf:params:xml:ns:caldav}calendar-description',
  136. '{urn:ietf:params:xml:ns:caldav}calendar-timezone',
  137. '{abc}def',
  138. ]);
  139. $props = [
  140. '{abc}def' => 'a',
  141. '{DAV:}displayname' => 'b',
  142. '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'c',
  143. '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'd',
  144. ];
  145. $this->insertProps('dummy_user_42', 'calendars/foo/bar_path_1337_0', $props);
  146. $setProps = [];
  147. $propFind->method('set')
  148. ->willReturnCallback(function ($name, $value, $status) use (&$setProps) {
  149. $setProps[$name] = $value;
  150. });
  151. $this->backend->propFind('calendars/foo/bar_path_1337_0', $propFind);
  152. $this->assertEquals($props, $setProps);
  153. }
  154. /**
  155. * @dataProvider propPatchProvider
  156. */
  157. public function testPropPatch(string $path, array $existing, array $props, array $result) {
  158. $this->insertProps($this->user->getUID(), $path, $existing);
  159. $propPatch = new PropPatch($props);
  160. $this->backend->propPatch($path, $propPatch);
  161. $propPatch->commit();
  162. $storedProps = $this->getProps($this->user->getUID(), $path);
  163. $this->assertEquals($result, $storedProps);
  164. }
  165. public function propPatchProvider() {
  166. $longPath = str_repeat('long_path', 100);
  167. return [
  168. ['foo_bar_path_1337', [], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']],
  169. ['foo_bar_path_1337', ['{DAV:}displayname' => 'foo'], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']],
  170. ['foo_bar_path_1337', ['{DAV:}displayname' => 'foo'], ['{DAV:}displayname' => null], []],
  171. [$longPath, [], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']],
  172. ];
  173. }
  174. /**
  175. * @dataProvider deleteProvider
  176. */
  177. public function testDelete(string $path) {
  178. $this->insertProps('dummy_user_42', $path, ['foo' => 'bar']);
  179. $this->backend->delete($path);
  180. $this->assertEquals([], $this->getProps('dummy_user_42', $path));
  181. }
  182. public function deleteProvider() {
  183. return [
  184. ['foo_bar_path_1337'],
  185. [str_repeat('long_path', 100)]
  186. ];
  187. }
  188. /**
  189. * @dataProvider moveProvider
  190. */
  191. public function testMove(string $source, string $target) {
  192. $this->insertProps('dummy_user_42', $source, ['foo' => 'bar']);
  193. $this->backend->move($source, $target);
  194. $this->assertEquals([], $this->getProps('dummy_user_42', $source));
  195. $this->assertEquals(['foo' => 'bar'], $this->getProps('dummy_user_42', $target));
  196. }
  197. public function moveProvider() {
  198. return [
  199. ['foo_bar_path_1337', 'foo_bar_path_7333'],
  200. [str_repeat('long_path1', 100), str_repeat('long_path2', 100)]
  201. ];
  202. }
  203. }