1
0

AddressBookTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\CardDAV;
  8. use OCA\DAV\CardDAV\AddressBook;
  9. use OCA\DAV\CardDAV\Card;
  10. use OCA\DAV\CardDAV\CardDavBackend;
  11. use OCP\IL10N;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Psr\Log\LoggerInterface;
  14. use Sabre\DAV\Exception\Forbidden;
  15. use Sabre\DAV\PropPatch;
  16. use Test\TestCase;
  17. class AddressBookTest extends TestCase {
  18. public function testMove(): void {
  19. $backend = $this->createMock(CardDavBackend::class);
  20. $addressBookInfo = [
  21. '{http://owncloud.org/ns}owner-principal' => 'user1',
  22. '{DAV:}displayname' => 'Test address book',
  23. 'principaluri' => 'user2',
  24. 'id' => 666,
  25. 'uri' => 'default',
  26. ];
  27. $l10n = $this->createMock(IL10N::class);
  28. $logger = $this->createMock(LoggerInterface::class);
  29. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  30. $card = new Card($backend, $addressBookInfo, ['id' => 5, 'carddata' => 'RANDOM VCF DATA', 'uri' => 'something', 'addressbookid' => 23]);
  31. $backend->expects($this->once())->method('moveCard')->with(23, 666, 'something', 'user1')->willReturn(true);
  32. $addressBook->moveInto('new', 'old', $card);
  33. }
  34. public function testDelete(): void {
  35. /** @var MockObject | CardDavBackend $backend */
  36. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  37. $backend->expects($this->once())->method('updateShares');
  38. $backend->expects($this->any())->method('getShares')->willReturn([
  39. ['href' => 'principal:user2']
  40. ]);
  41. $addressBookInfo = [
  42. '{http://owncloud.org/ns}owner-principal' => 'user1',
  43. '{DAV:}displayname' => 'Test address book',
  44. 'principaluri' => 'user2',
  45. 'id' => 666,
  46. 'uri' => 'default',
  47. ];
  48. $l10n = $this->createMock(IL10N::class);
  49. $logger = $this->createMock(LoggerInterface::class);
  50. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  51. $addressBook->delete();
  52. }
  53. public function testDeleteFromGroup(): void {
  54. $this->expectException(Forbidden::class);
  55. /** @var MockObject | CardDavBackend $backend */
  56. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  57. $backend->expects($this->never())->method('updateShares');
  58. $backend->expects($this->any())->method('getShares')->willReturn([
  59. ['href' => 'principal:group2']
  60. ]);
  61. $addressBookInfo = [
  62. '{http://owncloud.org/ns}owner-principal' => 'user1',
  63. '{DAV:}displayname' => 'Test address book',
  64. 'principaluri' => 'user2',
  65. 'id' => 666,
  66. 'uri' => 'default',
  67. ];
  68. $l10n = $this->createMock(IL10N::class);
  69. $logger = $this->createMock(LoggerInterface::class);
  70. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  71. $addressBook->delete();
  72. }
  73. public function testPropPatchShared(): void {
  74. /** @var MockObject | CardDavBackend $backend */
  75. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  76. $backend->expects($this->never())->method('updateAddressBook');
  77. $addressBookInfo = [
  78. '{http://owncloud.org/ns}owner-principal' => 'user1',
  79. '{DAV:}displayname' => 'Test address book',
  80. 'principaluri' => 'user2',
  81. 'id' => 666,
  82. 'uri' => 'default',
  83. ];
  84. $l10n = $this->createMock(IL10N::class);
  85. $logger = $this->createMock(LoggerInterface::class);
  86. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  87. $addressBook->propPatch(new PropPatch(['{DAV:}displayname' => 'Test address book']));
  88. }
  89. public function testPropPatchNotShared(): void {
  90. /** @var MockObject | CardDavBackend $backend */
  91. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  92. $backend->expects($this->atLeast(1))->method('updateAddressBook');
  93. $addressBookInfo = [
  94. '{DAV:}displayname' => 'Test address book',
  95. 'principaluri' => 'user1',
  96. 'id' => 666,
  97. 'uri' => 'default',
  98. ];
  99. $l10n = $this->createMock(IL10N::class);
  100. $logger = $this->createMock(LoggerInterface::class);
  101. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  102. $addressBook->propPatch(new PropPatch(['{DAV:}displayname' => 'Test address book']));
  103. }
  104. /**
  105. * @dataProvider providesReadOnlyInfo
  106. */
  107. public function testAcl($expectsWrite, $readOnlyValue, $hasOwnerSet): void {
  108. /** @var MockObject | CardDavBackend $backend */
  109. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  110. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  111. $addressBookInfo = [
  112. '{DAV:}displayname' => 'Test address book',
  113. 'principaluri' => 'user2',
  114. 'id' => 666,
  115. 'uri' => 'default'
  116. ];
  117. if (!is_null($readOnlyValue)) {
  118. $addressBookInfo['{http://owncloud.org/ns}read-only'] = $readOnlyValue;
  119. }
  120. if ($hasOwnerSet) {
  121. $addressBookInfo['{http://owncloud.org/ns}owner-principal'] = 'user1';
  122. }
  123. $l10n = $this->createMock(IL10N::class);
  124. $logger = $this->createMock(LoggerInterface::class);
  125. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  126. $acl = $addressBook->getACL();
  127. $childAcl = $addressBook->getChildACL();
  128. $expectedAcl = [[
  129. 'privilege' => '{DAV:}read',
  130. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  131. 'protected' => true
  132. ], [
  133. 'privilege' => '{DAV:}write',
  134. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  135. 'protected' => true
  136. ], [
  137. 'privilege' => '{DAV:}write-properties',
  138. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  139. 'protected' => true
  140. ]];
  141. if ($hasOwnerSet) {
  142. $expectedAcl[] = [
  143. 'privilege' => '{DAV:}read',
  144. 'principal' => 'user2',
  145. 'protected' => true
  146. ];
  147. if ($expectsWrite) {
  148. $expectedAcl[] = [
  149. 'privilege' => '{DAV:}write',
  150. 'principal' => 'user2',
  151. 'protected' => true
  152. ];
  153. }
  154. }
  155. $this->assertEquals($expectedAcl, $acl);
  156. $this->assertEquals($expectedAcl, $childAcl);
  157. }
  158. public function providesReadOnlyInfo(): array {
  159. return [
  160. 'read-only property not set' => [true, null, true],
  161. 'read-only property is false' => [true, false, true],
  162. 'read-only property is true' => [false, true, true],
  163. 'read-only property not set and no owner' => [true, null, false],
  164. 'read-only property is false and no owner' => [true, false, false],
  165. 'read-only property is true and no owner' => [false, true, false],
  166. ];
  167. }
  168. }