You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CachedSubscriptionTest.php 7.7KB

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