CachedSubscriptionTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Georg Ehrke
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Tests\unit\CalDAV;
  25. use OCA\DAV\CalDAV\CachedSubscription;
  26. use OCA\DAV\CalDAV\CachedSubscriptionObject;
  27. use OCA\DAV\CalDAV\CalDavBackend;
  28. use Sabre\DAV\PropPatch;
  29. class CachedSubscriptionTest extends \Test\TestCase {
  30. public function testGetACL() {
  31. $backend = $this->createMock(CalDavBackend::class);
  32. $calendarInfo = [
  33. '{http://owncloud.org/ns}owner-principal' => 'user1',
  34. 'principaluri' => 'user2',
  35. 'id' => 666,
  36. 'uri' => 'cal',
  37. ];
  38. $calendar = new CachedSubscription($backend, $calendarInfo);
  39. $this->assertEquals([
  40. [
  41. 'privilege' => '{DAV:}read',
  42. 'principal' => 'user1',
  43. 'protected' => true,
  44. ],
  45. [
  46. 'privilege' => '{DAV:}read',
  47. 'principal' => 'user1/calendar-proxy-write',
  48. 'protected' => true,
  49. ],
  50. [
  51. 'privilege' => '{DAV:}read',
  52. 'principal' => 'user1/calendar-proxy-read',
  53. 'protected' => true,
  54. ],
  55. [
  56. 'privilege' => '{urn:ietf:params:xml:ns:caldav}read-free-busy',
  57. 'principal' => '{DAV:}authenticated',
  58. 'protected' => true,
  59. ],
  60. ], $calendar->getACL());
  61. }
  62. public function testGetChildACL() {
  63. $backend = $this->createMock(CalDavBackend::class);
  64. $calendarInfo = [
  65. '{http://owncloud.org/ns}owner-principal' => 'user1',
  66. 'principaluri' => 'user2',
  67. 'id' => 666,
  68. 'uri' => 'cal',
  69. ];
  70. $calendar = new CachedSubscription($backend, $calendarInfo);
  71. $this->assertEquals([
  72. [
  73. 'privilege' => '{DAV:}read',
  74. 'principal' => 'user1',
  75. 'protected' => true,
  76. ],
  77. [
  78. 'privilege' => '{DAV:}read',
  79. 'principal' => 'user1/calendar-proxy-write',
  80. 'protected' => true,
  81. ],
  82. [
  83. 'privilege' => '{DAV:}read',
  84. 'principal' => 'user1/calendar-proxy-read',
  85. 'protected' => true,
  86. ]
  87. ], $calendar->getChildACL());
  88. }
  89. public function testGetOwner() {
  90. $backend = $this->createMock(CalDavBackend::class);
  91. $calendarInfo = [
  92. '{http://owncloud.org/ns}owner-principal' => 'user1',
  93. 'principaluri' => 'user2',
  94. 'id' => 666,
  95. 'uri' => 'cal',
  96. ];
  97. $calendar = new CachedSubscription($backend, $calendarInfo);
  98. $this->assertEquals('user1', $calendar->getOwner());
  99. }
  100. public function testDelete() {
  101. $backend = $this->createMock(CalDavBackend::class);
  102. $calendarInfo = [
  103. '{http://owncloud.org/ns}owner-principal' => 'user1',
  104. 'principaluri' => 'user2',
  105. 'id' => 666,
  106. 'uri' => 'cal',
  107. ];
  108. $backend->expects($this->once())
  109. ->method('deleteSubscription')
  110. ->with(666);
  111. $calendar = new CachedSubscription($backend, $calendarInfo);
  112. $calendar->delete();
  113. }
  114. public function testPropPatch() {
  115. $backend = $this->createMock(CalDavBackend::class);
  116. $calendarInfo = [
  117. '{http://owncloud.org/ns}owner-principal' => 'user1',
  118. 'principaluri' => 'user2',
  119. 'id' => 666,
  120. 'uri' => 'cal',
  121. ];
  122. $propPatch = $this->createMock(PropPatch::class);
  123. $backend->expects($this->once())
  124. ->method('updateSubscription')
  125. ->with(666, $propPatch);
  126. $calendar = new CachedSubscription($backend, $calendarInfo);
  127. $calendar->propPatch($propPatch);
  128. }
  129. public function testGetChild() {
  130. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  131. $this->expectExceptionMessage('Calendar object not found');
  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. ->willReturn([
  143. 'id' => 99,
  144. 'uri' => 'foo1'
  145. ]);
  146. $backend->expects($this->at(1))
  147. ->method('getCalendarObject')
  148. ->with(666, 'foo2', 1)
  149. ->willReturn(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. ->willReturn([
  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. ->willReturn([
  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. public function testCreateFile() {
  210. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  211. $this->expectExceptionMessage('Creating objects in cached subscription is not allowed');
  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. $calendar = new CachedSubscription($backend, $calendarInfo);
  220. $calendar->createFile('foo', []);
  221. }
  222. public function testChildExists() {
  223. $backend = $this->createMock(CalDavBackend::class);
  224. $calendarInfo = [
  225. '{http://owncloud.org/ns}owner-principal' => 'user1',
  226. 'principaluri' => 'user2',
  227. 'id' => 666,
  228. 'uri' => 'cal',
  229. ];
  230. $backend->expects($this->at(0))
  231. ->method('getCalendarObject')
  232. ->with(666, 'foo1', 1)
  233. ->willReturn([
  234. 'id' => 99,
  235. 'uri' => 'foo1'
  236. ]);
  237. $backend->expects($this->at(1))
  238. ->method('getCalendarObject')
  239. ->with(666, 'foo2', 1)
  240. ->willReturn(null);
  241. $calendar = new CachedSubscription($backend, $calendarInfo);
  242. $this->assertEquals(true, $calendar->childExists('foo1'));
  243. $this->assertEquals(false, $calendar->childExists('foo2'));
  244. }
  245. public function testCalendarQuery() {
  246. $backend = $this->createMock(CalDavBackend::class);
  247. $calendarInfo = [
  248. '{http://owncloud.org/ns}owner-principal' => 'user1',
  249. 'principaluri' => 'user2',
  250. 'id' => 666,
  251. 'uri' => 'cal',
  252. ];
  253. $backend->expects($this->once())
  254. ->method('calendarQuery')
  255. ->with(666, ['foo'], 1)
  256. ->willReturn([99]);
  257. $calendar = new CachedSubscription($backend, $calendarInfo);
  258. $this->assertEquals([99], $calendar->calendarQuery(['foo']));
  259. }
  260. }