1
0

CachedSubscriptionTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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->at(0))
  141. ->method('getCalendarObject')
  142. ->with(666, 'foo1', 1)
  143. ->willReturn([
  144. 'id' => 99,
  145. 'uri' => 'foo1'
  146. ]);
  147. $backend->expects($this->at(1))
  148. ->method('getCalendarObject')
  149. ->with(666, 'foo2', 1)
  150. ->willReturn(null);
  151. $calendar = new CachedSubscription($backend, $calendarInfo);
  152. $first = $calendar->getChild('foo1');
  153. $this->assertInstanceOf(CachedSubscriptionObject::class, $first);
  154. $calendar->getChild('foo2');
  155. }
  156. public function testGetChildren() {
  157. $backend = $this->createMock(CalDavBackend::class);
  158. $calendarInfo = [
  159. '{http://owncloud.org/ns}owner-principal' => 'user1',
  160. 'principaluri' => 'user2',
  161. 'id' => 666,
  162. 'uri' => 'cal',
  163. ];
  164. $backend->expects($this->at(0))
  165. ->method('getCalendarObjects')
  166. ->with(666, 1)
  167. ->willReturn([
  168. [
  169. 'id' => 99,
  170. 'uri' => 'foo1'
  171. ],
  172. [
  173. 'id' => 100,
  174. 'uri' => 'foo2'
  175. ],
  176. ]);
  177. $calendar = new CachedSubscription($backend, $calendarInfo);
  178. $res = $calendar->getChildren();
  179. $this->assertCount(2, $res);
  180. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[0]);
  181. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[1]);
  182. }
  183. public function testGetMultipleChildren() {
  184. $backend = $this->createMock(CalDavBackend::class);
  185. $calendarInfo = [
  186. '{http://owncloud.org/ns}owner-principal' => 'user1',
  187. 'principaluri' => 'user2',
  188. 'id' => 666,
  189. 'uri' => 'cal',
  190. ];
  191. $backend->expects($this->at(0))
  192. ->method('getMultipleCalendarObjects')
  193. ->with(666, ['foo1', 'foo2'], 1)
  194. ->willReturn([
  195. [
  196. 'id' => 99,
  197. 'uri' => 'foo1'
  198. ],
  199. [
  200. 'id' => 100,
  201. 'uri' => 'foo2'
  202. ],
  203. ]);
  204. $calendar = new CachedSubscription($backend, $calendarInfo);
  205. $res = $calendar->getMultipleChildren(['foo1', 'foo2']);
  206. $this->assertCount(2, $res);
  207. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[0]);
  208. $this->assertInstanceOf(CachedSubscriptionObject::class, $res[1]);
  209. }
  210. public function testCreateFile() {
  211. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  212. $this->expectExceptionMessage('Creating objects in cached subscription is not allowed');
  213. $backend = $this->createMock(CalDavBackend::class);
  214. $calendarInfo = [
  215. '{http://owncloud.org/ns}owner-principal' => 'user1',
  216. 'principaluri' => 'user2',
  217. 'id' => 666,
  218. 'uri' => 'cal',
  219. ];
  220. $calendar = new CachedSubscription($backend, $calendarInfo);
  221. $calendar->createFile('foo', []);
  222. }
  223. public function testChildExists() {
  224. $backend = $this->createMock(CalDavBackend::class);
  225. $calendarInfo = [
  226. '{http://owncloud.org/ns}owner-principal' => 'user1',
  227. 'principaluri' => 'user2',
  228. 'id' => 666,
  229. 'uri' => 'cal',
  230. ];
  231. $backend->expects($this->at(0))
  232. ->method('getCalendarObject')
  233. ->with(666, 'foo1', 1)
  234. ->willReturn([
  235. 'id' => 99,
  236. 'uri' => 'foo1'
  237. ]);
  238. $backend->expects($this->at(1))
  239. ->method('getCalendarObject')
  240. ->with(666, 'foo2', 1)
  241. ->willReturn(null);
  242. $calendar = new CachedSubscription($backend, $calendarInfo);
  243. $this->assertEquals(true, $calendar->childExists('foo1'));
  244. $this->assertEquals(false, $calendar->childExists('foo2'));
  245. }
  246. public function testCalendarQuery() {
  247. $backend = $this->createMock(CalDavBackend::class);
  248. $calendarInfo = [
  249. '{http://owncloud.org/ns}owner-principal' => 'user1',
  250. 'principaluri' => 'user2',
  251. 'id' => 666,
  252. 'uri' => 'cal',
  253. ];
  254. $backend->expects($this->once())
  255. ->method('calendarQuery')
  256. ->with(666, ['foo'], 1)
  257. ->willReturn([99]);
  258. $calendar = new CachedSubscription($backend, $calendarInfo);
  259. $this->assertEquals([99], $calendar->calendarQuery(['foo']));
  260. }
  261. }