1
0

CachedSubscriptionTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Georg Ehrke
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\CalDAV;
  26. use OCA\DAV\CalDAV\CachedSubscription;
  27. use OCA\DAV\CalDAV\CachedSubscriptionObject;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use Sabre\DAV\PropPatch;
  30. class CachedSubscriptionTest extends \Test\TestCase {
  31. public function testGetACL(): void {
  32. $backend = $this->createMock(CalDavBackend::class);
  33. $calendarInfo = [
  34. '{http://owncloud.org/ns}owner-principal' => 'user1',
  35. 'principaluri' => 'user2',
  36. 'id' => 666,
  37. 'uri' => 'cal',
  38. ];
  39. $calendar = new CachedSubscription($backend, $calendarInfo);
  40. $this->assertEquals([
  41. [
  42. 'privilege' => '{DAV:}read',
  43. 'principal' => 'user1',
  44. 'protected' => true,
  45. ],
  46. [
  47. 'privilege' => '{DAV:}read',
  48. 'principal' => 'user1/calendar-proxy-write',
  49. 'protected' => true,
  50. ],
  51. [
  52. 'privilege' => '{DAV:}read',
  53. 'principal' => 'user1/calendar-proxy-read',
  54. 'protected' => true,
  55. ],
  56. [
  57. 'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
  58. 'principal' => '{DAV:}authenticated',
  59. 'protected' => true,
  60. ],
  61. ], $calendar->getACL());
  62. }
  63. public function testGetChildACL(): void {
  64. $backend = $this->createMock(CalDavBackend::class);
  65. $calendarInfo = [
  66. '{http://owncloud.org/ns}owner-principal' => 'user1',
  67. 'principaluri' => 'user2',
  68. 'id' => 666,
  69. 'uri' => 'cal',
  70. ];
  71. $calendar = new CachedSubscription($backend, $calendarInfo);
  72. $this->assertEquals([
  73. [
  74. 'privilege' => '{DAV:}read',
  75. 'principal' => 'user1',
  76. 'protected' => true,
  77. ],
  78. [
  79. 'privilege' => '{DAV:}read',
  80. 'principal' => 'user1/calendar-proxy-write',
  81. 'protected' => true,
  82. ],
  83. [
  84. 'privilege' => '{DAV:}read',
  85. 'principal' => 'user1/calendar-proxy-read',
  86. 'protected' => true,
  87. ]
  88. ], $calendar->getChildACL());
  89. }
  90. public function testGetOwner(): void {
  91. $backend = $this->createMock(CalDavBackend::class);
  92. $calendarInfo = [
  93. '{http://owncloud.org/ns}owner-principal' => 'user1',
  94. 'principaluri' => 'user2',
  95. 'id' => 666,
  96. 'uri' => 'cal',
  97. ];
  98. $calendar = new CachedSubscription($backend, $calendarInfo);
  99. $this->assertEquals('user1', $calendar->getOwner());
  100. }
  101. public function testDelete(): 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. $backend->expects($this->once())
  110. ->method('deleteSubscription')
  111. ->with(666);
  112. $calendar = new CachedSubscription($backend, $calendarInfo);
  113. $calendar->delete();
  114. }
  115. public function testPropPatch(): void {
  116. $backend = $this->createMock(CalDavBackend::class);
  117. $calendarInfo = [
  118. '{http://owncloud.org/ns}owner-principal' => 'user1',
  119. 'principaluri' => 'user2',
  120. 'id' => 666,
  121. 'uri' => 'cal',
  122. ];
  123. $propPatch = $this->createMock(PropPatch::class);
  124. $backend->expects($this->once())
  125. ->method('updateSubscription')
  126. ->with(666, $propPatch);
  127. $calendar = new CachedSubscription($backend, $calendarInfo);
  128. $calendar->propPatch($propPatch);
  129. }
  130. public function testGetChild(): void {
  131. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  132. $this->expectExceptionMessage('Calendar object not found');
  133. $backend = $this->createMock(CalDavBackend::class);
  134. $calendarInfo = [
  135. '{http://owncloud.org/ns}owner-principal' => 'user1',
  136. 'principaluri' => 'user2',
  137. 'id' => 666,
  138. 'uri' => 'cal',
  139. ];
  140. $backend->expects($this->exactly(2))
  141. ->method('getCalendarObject')
  142. ->withConsecutive(
  143. [666, 'foo1', 1],
  144. [666, 'foo2', 1],
  145. )
  146. ->willReturnOnConsecutiveCalls(
  147. [
  148. 'id' => 99,
  149. 'uri' => 'foo1'
  150. ],
  151. null
  152. );
  153. $calendar = new CachedSubscription($backend, $calendarInfo);
  154. $first = $calendar->getChild('foo1');
  155. $this->assertInstanceOf(CachedSubscriptionObject::class, $first);
  156. $calendar->getChild('foo2');
  157. }
  158. public function testGetChildren(): void {
  159. $backend = $this->createMock(CalDavBackend::class);
  160. $calendarInfo = [
  161. '{http://owncloud.org/ns}owner-principal' => 'user1',
  162. 'principaluri' => 'user2',
  163. 'id' => 666,
  164. 'uri' => 'cal',
  165. ];
  166. $backend->expects($this->once())
  167. ->method('getCalendarObjects')
  168. ->with(666, 1)
  169. ->willReturn([
  170. [
  171. 'id' => 99,
  172. 'uri' => 'foo1'
  173. ],
  174. [
  175. 'id' => 100,
  176. 'uri' => 'foo2'
  177. ],
  178. ]);
  179. $calendar = new CachedSubscription($backend, $calendarInfo);
  180. $res = $calendar->getChildren();
  181. $this->assertCount(2, $res);
  182. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[0]);
  183. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[1]);
  184. }
  185. public function testGetMultipleChildren(): void {
  186. $backend = $this->createMock(CalDavBackend::class);
  187. $calendarInfo = [
  188. '{http://owncloud.org/ns}owner-principal' => 'user1',
  189. 'principaluri' => 'user2',
  190. 'id' => 666,
  191. 'uri' => 'cal',
  192. ];
  193. $backend->expects($this->once())
  194. ->method('getMultipleCalendarObjects')
  195. ->with(666, ['foo1', 'foo2'], 1)
  196. ->willReturn([
  197. [
  198. 'id' => 99,
  199. 'uri' => 'foo1'
  200. ],
  201. [
  202. 'id' => 100,
  203. 'uri' => 'foo2'
  204. ],
  205. ]);
  206. $calendar = new CachedSubscription($backend, $calendarInfo);
  207. $res = $calendar->getMultipleChildren(['foo1', 'foo2']);
  208. $this->assertCount(2, $res);
  209. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[0]);
  210. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[1]);
  211. }
  212. public function testCreateFile(): void {
  213. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  214. $this->expectExceptionMessage('Creating objects in cached subscription is not allowed');
  215. $backend = $this->createMock(CalDavBackend::class);
  216. $calendarInfo = [
  217. '{http://owncloud.org/ns}owner-principal' => 'user1',
  218. 'principaluri' => 'user2',
  219. 'id' => 666,
  220. 'uri' => 'cal',
  221. ];
  222. $calendar = new CachedSubscription($backend, $calendarInfo);
  223. $calendar->createFile('foo', []);
  224. }
  225. public function testChildExists(): void {
  226. $backend = $this->createMock(CalDavBackend::class);
  227. $calendarInfo = [
  228. '{http://owncloud.org/ns}owner-principal' => 'user1',
  229. 'principaluri' => 'user2',
  230. 'id' => 666,
  231. 'uri' => 'cal',
  232. ];
  233. $backend->expects($this->exactly(2))
  234. ->method('getCalendarObject')
  235. ->withConsecutive(
  236. [666, 'foo1', 1],
  237. [666, 'foo2', 1],
  238. )
  239. ->willReturnOnConsecutiveCalls(
  240. [
  241. 'id' => 99,
  242. 'uri' => 'foo1'
  243. ],
  244. null
  245. );
  246. $calendar = new CachedSubscription($backend, $calendarInfo);
  247. $this->assertEquals(true, $calendar->childExists('foo1'));
  248. $this->assertEquals(false, $calendar->childExists('foo2'));
  249. }
  250. public function testCalendarQuery(): void {
  251. $backend = $this->createMock(CalDavBackend::class);
  252. $calendarInfo = [
  253. '{http://owncloud.org/ns}owner-principal' => 'user1',
  254. 'principaluri' => 'user2',
  255. 'id' => 666,
  256. 'uri' => 'cal',
  257. ];
  258. $backend->expects($this->once())
  259. ->method('calendarQuery')
  260. ->with(666, ['foo'], 1)
  261. ->willReturn([99]);
  262. $calendar = new CachedSubscription($backend, $calendarInfo);
  263. $this->assertEquals([99], $calendar->calendarQuery(['foo']));
  264. }
  265. }