CalendarTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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\BirthdayService;
  24. use OCA\DAV\CalDAV\CalDavBackend;
  25. use OCA\DAV\CalDAV\Calendar;
  26. use OCP\IL10N;
  27. use Sabre\DAV\PropPatch;
  28. use Test\TestCase;
  29. class CalendarTest extends TestCase {
  30. /** @var IL10N */
  31. private $l10n;
  32. public function setUp() {
  33. parent::setUp();
  34. $this->l10n = $this->getMockBuilder('\OCP\IL10N')
  35. ->disableOriginalConstructor()->getMock();
  36. $this->l10n
  37. ->expects($this->any())
  38. ->method('t')
  39. ->will($this->returnCallback(function ($text, $parameters = array()) {
  40. return vsprintf($text, $parameters);
  41. }));
  42. }
  43. public function testDelete() {
  44. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  45. $backend = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')->disableOriginalConstructor()->getMock();
  46. $backend->expects($this->once())->method('updateShares');
  47. $backend->expects($this->any())->method('getShares')->willReturn([
  48. ['href' => 'principal:user2']
  49. ]);
  50. $calendarInfo = [
  51. '{http://owncloud.org/ns}owner-principal' => 'user1',
  52. 'principaluri' => 'user2',
  53. 'id' => 666,
  54. 'uri' => 'cal',
  55. ];
  56. $c = new Calendar($backend, $calendarInfo, $this->l10n);
  57. $c->delete();
  58. }
  59. /**
  60. * @expectedException \Sabre\DAV\Exception\Forbidden
  61. */
  62. public function testDeleteFromGroup() {
  63. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  64. $backend = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')->disableOriginalConstructor()->getMock();
  65. $backend->expects($this->never())->method('updateShares');
  66. $backend->expects($this->any())->method('getShares')->willReturn([
  67. ['href' => 'principal:group2']
  68. ]);
  69. $calendarInfo = [
  70. '{http://owncloud.org/ns}owner-principal' => 'user1',
  71. 'principaluri' => 'user2',
  72. 'id' => 666,
  73. 'uri' => 'cal',
  74. ];
  75. $c = new Calendar($backend, $calendarInfo, $this->l10n);
  76. $c->delete();
  77. }
  78. public function dataPropPatch() {
  79. return [
  80. [[], true],
  81. [[
  82. '{http://owncloud.org/ns}calendar-enabled' => true,
  83. ], false],
  84. [[
  85. '{DAV:}displayname' => true,
  86. ], true],
  87. [[
  88. '{DAV:}displayname' => true,
  89. '{http://owncloud.org/ns}calendar-enabled' => true,
  90. ], true],
  91. ];
  92. }
  93. /**
  94. * @dataProvider dataPropPatch
  95. */
  96. public function testPropPatch($mutations, $throws) {
  97. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  98. $backend = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')->disableOriginalConstructor()->getMock();
  99. $calendarInfo = [
  100. '{http://owncloud.org/ns}owner-principal' => 'user1',
  101. 'principaluri' => 'user2',
  102. 'id' => 666,
  103. 'uri' => 'default'
  104. ];
  105. $c = new Calendar($backend, $calendarInfo, $this->l10n);
  106. if ($throws) {
  107. $this->setExpectedException('\Sabre\DAV\Exception\Forbidden');
  108. }
  109. $c->propPatch(new PropPatch($mutations));
  110. if (!$throws) {
  111. $this->assertTrue(true);
  112. }
  113. }
  114. /**
  115. * @dataProvider providesReadOnlyInfo
  116. */
  117. public function testAcl($expectsWrite, $readOnlyValue, $hasOwnerSet, $uri = 'default') {
  118. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  119. $backend = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')->disableOriginalConstructor()->getMock();
  120. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  121. $calendarInfo = [
  122. 'principaluri' => 'user2',
  123. 'id' => 666,
  124. 'uri' => $uri
  125. ];
  126. if (!is_null($readOnlyValue)) {
  127. $calendarInfo['{http://owncloud.org/ns}read-only'] = $readOnlyValue;
  128. }
  129. if ($hasOwnerSet) {
  130. $calendarInfo['{http://owncloud.org/ns}owner-principal'] = 'user1';
  131. }
  132. $c = new Calendar($backend, $calendarInfo, $this->l10n);
  133. $acl = $c->getACL();
  134. $childAcl = $c->getChildACL();
  135. $expectedAcl = [[
  136. 'privilege' => '{DAV:}read',
  137. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  138. 'protected' => true
  139. ], [
  140. 'privilege' => '{DAV:}write',
  141. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  142. 'protected' => true
  143. ]];
  144. if ($uri === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  145. $expectedAcl = [[
  146. 'privilege' => '{DAV:}read',
  147. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  148. 'protected' => true
  149. ]];
  150. }
  151. if ($hasOwnerSet) {
  152. $expectedAcl[] = [
  153. 'privilege' => '{DAV:}read',
  154. 'principal' => 'user2',
  155. 'protected' => true
  156. ];
  157. if ($expectsWrite) {
  158. $expectedAcl[] = [
  159. 'privilege' => '{DAV:}write',
  160. 'principal' => 'user2',
  161. 'protected' => true
  162. ];
  163. }
  164. }
  165. $this->assertEquals($expectedAcl, $acl);
  166. $this->assertEquals($expectedAcl, $childAcl);
  167. }
  168. public function providesReadOnlyInfo() {
  169. return [
  170. 'read-only property not set' => [true, null, true],
  171. 'read-only property is false' => [true, false, true],
  172. 'read-only property is true' => [false, true, true],
  173. 'read-only property not set and no owner' => [true, null, false],
  174. 'read-only property is false and no owner' => [true, false, false],
  175. 'read-only property is true and no owner' => [false, true, false],
  176. 'birthday calendar' => [false, false, false, BirthdayService::BIRTHDAY_CALENDAR_URI]
  177. ];
  178. }
  179. }