AddressBookTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\CardDAV;
  27. use OCA\DAV\CardDAV\AddressBook;
  28. use OCA\DAV\CardDAV\CardDavBackend;
  29. use OCP\IL10N;
  30. use Sabre\DAV\PropPatch;
  31. use Test\TestCase;
  32. class AddressBookTest extends TestCase {
  33. public function testDelete() {
  34. /** @var \PHPUnit\Framework\MockObject\MockObject | CardDavBackend $backend */
  35. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  36. $backend->expects($this->once())->method('updateShares');
  37. $backend->expects($this->any())->method('getShares')->willReturn([
  38. ['href' => 'principal:user2']
  39. ]);
  40. $calendarInfo = [
  41. '{http://owncloud.org/ns}owner-principal' => 'user1',
  42. '{DAV:}displayname' => 'Test address book',
  43. 'principaluri' => 'user2',
  44. 'id' => 666,
  45. 'uri' => 'default',
  46. ];
  47. $l = $this->createMock(IL10N::class);
  48. $c = new AddressBook($backend, $calendarInfo, $l);
  49. $c->delete();
  50. }
  51. public function testDeleteFromGroup() {
  52. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  53. /** @var \PHPUnit\Framework\MockObject\MockObject | CardDavBackend $backend */
  54. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  55. $backend->expects($this->never())->method('updateShares');
  56. $backend->expects($this->any())->method('getShares')->willReturn([
  57. ['href' => 'principal:group2']
  58. ]);
  59. $calendarInfo = [
  60. '{http://owncloud.org/ns}owner-principal' => 'user1',
  61. '{DAV:}displayname' => 'Test address book',
  62. 'principaluri' => 'user2',
  63. 'id' => 666,
  64. 'uri' => 'default',
  65. ];
  66. $l = $this->createMock(IL10N::class);
  67. $c = new AddressBook($backend, $calendarInfo, $l);
  68. $c->delete();
  69. }
  70. public function testPropPatch() {
  71. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  72. /** @var \PHPUnit\Framework\MockObject\MockObject | CardDavBackend $backend */
  73. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  74. $calendarInfo = [
  75. '{http://owncloud.org/ns}owner-principal' => 'user1',
  76. '{DAV:}displayname' => 'Test address book',
  77. 'principaluri' => 'user2',
  78. 'id' => 666,
  79. 'uri' => 'default',
  80. ];
  81. $l = $this->createMock(IL10N::class);
  82. $c = new AddressBook($backend, $calendarInfo, $l);
  83. $c->propPatch(new PropPatch([]));
  84. }
  85. /**
  86. * @dataProvider providesReadOnlyInfo
  87. */
  88. public function testAcl($expectsWrite, $readOnlyValue, $hasOwnerSet) {
  89. /** @var \PHPUnit\Framework\MockObject\MockObject | CardDavBackend $backend */
  90. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  91. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  92. $calendarInfo = [
  93. '{DAV:}displayname' => 'Test address book',
  94. 'principaluri' => 'user2',
  95. 'id' => 666,
  96. 'uri' => 'default'
  97. ];
  98. if (!is_null($readOnlyValue)) {
  99. $calendarInfo['{http://owncloud.org/ns}read-only'] = $readOnlyValue;
  100. }
  101. if ($hasOwnerSet) {
  102. $calendarInfo['{http://owncloud.org/ns}owner-principal'] = 'user1';
  103. }
  104. $l = $this->createMock(IL10N::class);
  105. $c = new AddressBook($backend, $calendarInfo, $l);
  106. $acl = $c->getACL();
  107. $childAcl = $c->getChildACL();
  108. $expectedAcl = [[
  109. 'privilege' => '{DAV:}read',
  110. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  111. 'protected' => true
  112. ], [
  113. 'privilege' => '{DAV:}write',
  114. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  115. 'protected' => true
  116. ]];
  117. if ($hasOwnerSet) {
  118. $expectedAcl[] = [
  119. 'privilege' => '{DAV:}read',
  120. 'principal' => 'user2',
  121. 'protected' => true
  122. ];
  123. if ($expectsWrite) {
  124. $expectedAcl[] = [
  125. 'privilege' => '{DAV:}write',
  126. 'principal' => 'user2',
  127. 'protected' => true
  128. ];
  129. }
  130. }
  131. $this->assertEquals($expectedAcl, $acl);
  132. $this->assertEquals($expectedAcl, $childAcl);
  133. }
  134. public function providesReadOnlyInfo() {
  135. return [
  136. 'read-only property not set' => [true, null, true],
  137. 'read-only property is false' => [true, false, true],
  138. 'read-only property is true' => [false, true, true],
  139. 'read-only property not set and no owner' => [true, null, false],
  140. 'read-only property is false and no owner' => [true, false, false],
  141. 'read-only property is true and no owner' => [false, true, false],
  142. ];
  143. }
  144. }