CachedSubscriptionTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV;
  7. use OCA\DAV\CalDAV\CachedSubscription;
  8. use OCA\DAV\CalDAV\CachedSubscriptionObject;
  9. use OCA\DAV\CalDAV\CalDavBackend;
  10. use Sabre\DAV\PropPatch;
  11. class CachedSubscriptionTest extends \Test\TestCase {
  12. public function testGetACL(): void {
  13. $backend = $this->createMock(CalDavBackend::class);
  14. $calendarInfo = [
  15. '{http://owncloud.org/ns}owner-principal' => 'user1',
  16. 'principaluri' => 'user2',
  17. 'id' => 666,
  18. 'uri' => 'cal',
  19. ];
  20. $calendar = new CachedSubscription($backend, $calendarInfo);
  21. $this->assertEquals([
  22. [
  23. 'privilege' => '{DAV:}read',
  24. 'principal' => 'user1',
  25. 'protected' => true,
  26. ],
  27. [
  28. 'privilege' => '{DAV:}read',
  29. 'principal' => 'user1/calendar-proxy-write',
  30. 'protected' => true,
  31. ],
  32. [
  33. 'privilege' => '{DAV:}read',
  34. 'principal' => 'user1/calendar-proxy-read',
  35. 'protected' => true,
  36. ],
  37. [
  38. 'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
  39. 'principal' => '{DAV:}authenticated',
  40. 'protected' => true,
  41. ],
  42. [
  43. 'privilege' => '{DAV:}write-properties',
  44. 'principal' => 'user1',
  45. 'protected' => 'true'
  46. ]
  47. ], $calendar->getACL());
  48. }
  49. public function testGetChildACL(): void {
  50. $backend = $this->createMock(CalDavBackend::class);
  51. $calendarInfo = [
  52. '{http://owncloud.org/ns}owner-principal' => 'user1',
  53. 'principaluri' => 'user2',
  54. 'id' => 666,
  55. 'uri' => 'cal',
  56. ];
  57. $calendar = new CachedSubscription($backend, $calendarInfo);
  58. $this->assertEquals([
  59. [
  60. 'privilege' => '{DAV:}read',
  61. 'principal' => 'user1',
  62. 'protected' => true,
  63. ],
  64. [
  65. 'privilege' => '{DAV:}read',
  66. 'principal' => 'user1/calendar-proxy-write',
  67. 'protected' => true,
  68. ],
  69. [
  70. 'privilege' => '{DAV:}read',
  71. 'principal' => 'user1/calendar-proxy-read',
  72. 'protected' => true,
  73. ]
  74. ], $calendar->getChildACL());
  75. }
  76. public function testGetOwner(): void {
  77. $backend = $this->createMock(CalDavBackend::class);
  78. $calendarInfo = [
  79. '{http://owncloud.org/ns}owner-principal' => 'user1',
  80. 'principaluri' => 'user2',
  81. 'id' => 666,
  82. 'uri' => 'cal',
  83. ];
  84. $calendar = new CachedSubscription($backend, $calendarInfo);
  85. $this->assertEquals('user1', $calendar->getOwner());
  86. }
  87. public function testDelete(): void {
  88. $backend = $this->createMock(CalDavBackend::class);
  89. $calendarInfo = [
  90. '{http://owncloud.org/ns}owner-principal' => 'user1',
  91. 'principaluri' => 'user2',
  92. 'id' => 666,
  93. 'uri' => 'cal',
  94. ];
  95. $backend->expects($this->once())
  96. ->method('deleteSubscription')
  97. ->with(666);
  98. $calendar = new CachedSubscription($backend, $calendarInfo);
  99. $calendar->delete();
  100. }
  101. public function testPropPatch(): void {
  102. $backend = $this->createMock(CalDavBackend::class);
  103. $calendarInfo = [
  104. '{http://owncloud.org/ns}owner-principal' => 'user1',
  105. 'principaluri' => 'user2',
  106. 'id' => 666,
  107. 'uri' => 'cal',
  108. ];
  109. $propPatch = $this->createMock(PropPatch::class);
  110. $backend->expects($this->once())
  111. ->method('updateSubscription')
  112. ->with(666, $propPatch);
  113. $calendar = new CachedSubscription($backend, $calendarInfo);
  114. $calendar->propPatch($propPatch);
  115. }
  116. public function testGetChild(): void {
  117. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  118. $this->expectExceptionMessage('Calendar object not found');
  119. $backend = $this->createMock(CalDavBackend::class);
  120. $calendarInfo = [
  121. '{http://owncloud.org/ns}owner-principal' => 'user1',
  122. 'principaluri' => 'user2',
  123. 'id' => 666,
  124. 'uri' => 'cal',
  125. ];
  126. $backend->expects($this->exactly(2))
  127. ->method('getCalendarObject')
  128. ->withConsecutive(
  129. [666, 'foo1', 1],
  130. [666, 'foo2', 1],
  131. )
  132. ->willReturnOnConsecutiveCalls(
  133. [
  134. 'id' => 99,
  135. 'uri' => 'foo1'
  136. ],
  137. null
  138. );
  139. $calendar = new CachedSubscription($backend, $calendarInfo);
  140. $first = $calendar->getChild('foo1');
  141. $this->assertInstanceOf(CachedSubscriptionObject::class, $first);
  142. $calendar->getChild('foo2');
  143. }
  144. public function testGetChildren(): void {
  145. $backend = $this->createMock(CalDavBackend::class);
  146. $calendarInfo = [
  147. '{http://owncloud.org/ns}owner-principal' => 'user1',
  148. 'principaluri' => 'user2',
  149. 'id' => 666,
  150. 'uri' => 'cal',
  151. ];
  152. $backend->expects($this->once())
  153. ->method('getCalendarObjects')
  154. ->with(666, 1)
  155. ->willReturn([
  156. [
  157. 'id' => 99,
  158. 'uri' => 'foo1'
  159. ],
  160. [
  161. 'id' => 100,
  162. 'uri' => 'foo2'
  163. ],
  164. ]);
  165. $calendar = new CachedSubscription($backend, $calendarInfo);
  166. $res = $calendar->getChildren();
  167. $this->assertCount(2, $res);
  168. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[0]);
  169. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[1]);
  170. }
  171. public function testGetMultipleChildren(): void {
  172. $backend = $this->createMock(CalDavBackend::class);
  173. $calendarInfo = [
  174. '{http://owncloud.org/ns}owner-principal' => 'user1',
  175. 'principaluri' => 'user2',
  176. 'id' => 666,
  177. 'uri' => 'cal',
  178. ];
  179. $backend->expects($this->once())
  180. ->method('getMultipleCalendarObjects')
  181. ->with(666, ['foo1', 'foo2'], 1)
  182. ->willReturn([
  183. [
  184. 'id' => 99,
  185. 'uri' => 'foo1'
  186. ],
  187. [
  188. 'id' => 100,
  189. 'uri' => 'foo2'
  190. ],
  191. ]);
  192. $calendar = new CachedSubscription($backend, $calendarInfo);
  193. $res = $calendar->getMultipleChildren(['foo1', 'foo2']);
  194. $this->assertCount(2, $res);
  195. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[0]);
  196. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[1]);
  197. }
  198. public function testCreateFile(): void {
  199. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  200. $this->expectExceptionMessage('Creating objects in cached subscription is not allowed');
  201. $backend = $this->createMock(CalDavBackend::class);
  202. $calendarInfo = [
  203. '{http://owncloud.org/ns}owner-principal' => 'user1',
  204. 'principaluri' => 'user2',
  205. 'id' => 666,
  206. 'uri' => 'cal',
  207. ];
  208. $calendar = new CachedSubscription($backend, $calendarInfo);
  209. $calendar->createFile('foo', []);
  210. }
  211. public function testChildExists(): void {
  212. $backend = $this->createMock(CalDavBackend::class);
  213. $calendarInfo = [
  214. '{http://owncloud.org/ns}owner-principal' => 'user1',
  215. 'principaluri' => 'user2',
  216. 'id' => 666,
  217. 'uri' => 'cal',
  218. ];
  219. $backend->expects($this->exactly(2))
  220. ->method('getCalendarObject')
  221. ->withConsecutive(
  222. [666, 'foo1', 1],
  223. [666, 'foo2', 1],
  224. )
  225. ->willReturnOnConsecutiveCalls(
  226. [
  227. 'id' => 99,
  228. 'uri' => 'foo1'
  229. ],
  230. null
  231. );
  232. $calendar = new CachedSubscription($backend, $calendarInfo);
  233. $this->assertEquals(true, $calendar->childExists('foo1'));
  234. $this->assertEquals(false, $calendar->childExists('foo2'));
  235. }
  236. public function testCalendarQuery(): void {
  237. $backend = $this->createMock(CalDavBackend::class);
  238. $calendarInfo = [
  239. '{http://owncloud.org/ns}owner-principal' => 'user1',
  240. 'principaluri' => 'user2',
  241. 'id' => 666,
  242. 'uri' => 'cal',
  243. ];
  244. $backend->expects($this->once())
  245. ->method('calendarQuery')
  246. ->with(666, ['foo'], 1)
  247. ->willReturn([99]);
  248. $calendar = new CachedSubscription($backend, $calendarInfo);
  249. $this->assertEquals([99], $calendar->calendarQuery(['foo']));
  250. }
  251. }