AddressBookTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 Citharel <nextcloud@tcit.fr>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\CardDAV;
  28. use OCA\DAV\CardDAV\AddressBook;
  29. use OCA\DAV\CardDAV\Card;
  30. use OCA\DAV\CardDAV\CardDavBackend;
  31. use OCA\DAV\DAV\CustomPropertiesBackend;
  32. use OCP\IL10N;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. use Psr\Log\LoggerInterface;
  35. use Sabre\DAV\Exception\Forbidden;
  36. use Sabre\DAV\PropPatch;
  37. use Test\TestCase;
  38. class AddressBookTest extends TestCase {
  39. public function testMove(): void {
  40. $backend = $this->createMock(CardDavBackend::class);
  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. $card = new Card($backend, $addressBookInfo, ['id' => 5, 'carddata' => 'RANDOM VCF DATA', 'uri' => 'something', 'addressbookid' => 23]);
  52. $backend->expects($this->once())->method('moveCard')->with(23, 666, 'something', 'user1')->willReturn(true);
  53. $addressBook->moveInto('new', 'old', $card);
  54. }
  55. public function testDelete(): void {
  56. /** @var MockObject | CardDavBackend $backend */
  57. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  58. $backend->expects($this->once())->method('updateShares');
  59. $backend->expects($this->any())->method('getShares')->willReturn([
  60. ['href' => 'principal:user2']
  61. ]);
  62. $addressBookInfo = [
  63. '{http://owncloud.org/ns}owner-principal' => 'user1',
  64. '{DAV:}displayname' => 'Test address book',
  65. 'principaluri' => 'user2',
  66. 'id' => 666,
  67. 'uri' => 'default',
  68. ];
  69. $l10n = $this->createMock(IL10N::class);
  70. $logger = $this->createMock(LoggerInterface::class);
  71. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  72. $addressBook->delete();
  73. }
  74. public function testDeleteFromGroup(): void {
  75. $this->expectException(Forbidden::class);
  76. /** @var MockObject | CardDavBackend $backend */
  77. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  78. $backend->expects($this->never())->method('updateShares');
  79. $backend->expects($this->any())->method('getShares')->willReturn([
  80. ['href' => 'principal:group2']
  81. ]);
  82. $addressBookInfo = [
  83. '{http://owncloud.org/ns}owner-principal' => 'user1',
  84. '{DAV:}displayname' => 'Test address book',
  85. 'principaluri' => 'user2',
  86. 'id' => 666,
  87. 'uri' => 'default',
  88. ];
  89. $l10n = $this->createMock(IL10N::class);
  90. $logger = $this->createMock(LoggerInterface::class);
  91. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  92. $addressBook->delete();
  93. }
  94. public function testPropPatchShared(): void {
  95. /** @var MockObject | CardDavBackend $backend */
  96. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  97. $backend->expects($this->never())->method('updateAddressBook');
  98. $addressBookInfo = [
  99. '{http://owncloud.org/ns}owner-principal' => 'user1',
  100. '{DAV:}displayname' => 'Test address book',
  101. 'principaluri' => 'user2',
  102. 'id' => 666,
  103. 'uri' => 'default',
  104. ];
  105. $l10n = $this->createMock(IL10N::class);
  106. $logger = $this->createMock(LoggerInterface::class);
  107. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  108. $addressBook->propPatch(new PropPatch(['{DAV:}displayname' => 'Test address book']));
  109. }
  110. public function testPropPatchNotShared(): void {
  111. /** @var MockObject | CardDavBackend $backend */
  112. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  113. $backend->expects($this->atLeast(1))->method('updateAddressBook');
  114. $addressBookInfo = [
  115. '{DAV:}displayname' => 'Test address book',
  116. 'principaluri' => 'user1',
  117. 'id' => 666,
  118. 'uri' => 'default',
  119. ];
  120. $l10n = $this->createMock(IL10N::class);
  121. $logger = $this->createMock(LoggerInterface::class);
  122. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  123. $addressBook->propPatch(new PropPatch(['{DAV:}displayname' => 'Test address book']));
  124. }
  125. /**
  126. * @dataProvider providesReadOnlyInfo
  127. */
  128. public function testAcl($expectsWrite, $readOnlyValue, $hasOwnerSet): void {
  129. /** @var MockObject | CardDavBackend $backend */
  130. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  131. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  132. $addressBookInfo = [
  133. '{DAV:}displayname' => 'Test address book',
  134. 'principaluri' => 'user2',
  135. 'id' => 666,
  136. 'uri' => 'default'
  137. ];
  138. if (!is_null($readOnlyValue)) {
  139. $addressBookInfo['{http://owncloud.org/ns}read-only'] = $readOnlyValue;
  140. }
  141. if ($hasOwnerSet) {
  142. $addressBookInfo['{http://owncloud.org/ns}owner-principal'] = 'user1';
  143. }
  144. $l10n = $this->createMock(IL10N::class);
  145. $logger = $this->createMock(LoggerInterface::class);
  146. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  147. $acl = $addressBook->getACL();
  148. $childAcl = $addressBook->getChildACL();
  149. $expectedAcl = [[
  150. 'privilege' => '{DAV:}read',
  151. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  152. 'protected' => true
  153. ], [
  154. 'privilege' => '{DAV:}write',
  155. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  156. 'protected' => true
  157. ], [
  158. 'privilege' => '{DAV:}write-properties',
  159. 'principal' => '{DAV:}authenticated',
  160. 'protected' => true
  161. ]];
  162. if ($hasOwnerSet) {
  163. $expectedAcl[] = [
  164. 'privilege' => '{DAV:}read',
  165. 'principal' => 'user2',
  166. 'protected' => true
  167. ];
  168. if ($expectsWrite) {
  169. $expectedAcl[] = [
  170. 'privilege' => '{DAV:}write',
  171. 'principal' => 'user2',
  172. 'protected' => true
  173. ];
  174. }
  175. }
  176. $this->assertEquals($expectedAcl, $acl);
  177. $this->assertEquals($expectedAcl, $childAcl);
  178. }
  179. public function providesReadOnlyInfo(): array {
  180. return [
  181. 'read-only property not set' => [true, null, true],
  182. 'read-only property is false' => [true, false, true],
  183. 'read-only property is true' => [false, true, true],
  184. 'read-only property not set and no owner' => [true, null, false],
  185. 'read-only property is false and no owner' => [true, false, false],
  186. 'read-only property is true and no owner' => [false, true, false],
  187. ];
  188. }
  189. }