CustomPropertiesBackendTest.php 7.0 KB

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