AddressBookTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 OCP\IL10N;
  32. use PHPUnit\Framework\MockObject\MockObject;
  33. use Psr\Log\LoggerInterface;
  34. use Sabre\DAV\Exception\Forbidden;
  35. use Sabre\DAV\PropPatch;
  36. use Test\TestCase;
  37. class AddressBookTest extends TestCase {
  38. public function testMove(): void {
  39. $backend = $this->createMock(CardDavBackend::class);
  40. $addressBookInfo = [
  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. $l10n = $this->createMock(IL10N::class);
  48. $logger = $this->createMock(LoggerInterface::class);
  49. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  50. $card = new Card($backend, $addressBookInfo, ['id' => 5, 'carddata' => 'RANDOM VCF DATA', 'uri' => 'something', 'addressbookid' => 23]);
  51. $backend->expects($this->once())->method('moveCard')->with(23, 666, 'something', 'user1')->willReturn(true);
  52. $addressBook->moveInto('new', 'old', $card);
  53. }
  54. public function testDelete(): void {
  55. /** @var MockObject | CardDavBackend $backend */
  56. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  57. $backend->expects($this->once())->method('updateShares');
  58. $backend->expects($this->any())->method('getShares')->willReturn([
  59. ['href' => 'principal:user2']
  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 testDeleteFromGroup(): void {
  74. $this->expectException(Forbidden::class);
  75. /** @var MockObject | CardDavBackend $backend */
  76. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  77. $backend->expects($this->never())->method('updateShares');
  78. $backend->expects($this->any())->method('getShares')->willReturn([
  79. ['href' => 'principal:group2']
  80. ]);
  81. $addressBookInfo = [
  82. '{http://owncloud.org/ns}owner-principal' => 'user1',
  83. '{DAV:}displayname' => 'Test address book',
  84. 'principaluri' => 'user2',
  85. 'id' => 666,
  86. 'uri' => 'default',
  87. ];
  88. $l10n = $this->createMock(IL10N::class);
  89. $logger = $this->createMock(LoggerInterface::class);
  90. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  91. $addressBook->delete();
  92. }
  93. public function testPropPatchShared(): void {
  94. /** @var MockObject | CardDavBackend $backend */
  95. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  96. $backend->expects($this->never())->method('updateAddressBook');
  97. $addressBookInfo = [
  98. '{http://owncloud.org/ns}owner-principal' => 'user1',
  99. '{DAV:}displayname' => 'Test address book',
  100. 'principaluri' => 'user2',
  101. 'id' => 666,
  102. 'uri' => 'default',
  103. ];
  104. $l10n = $this->createMock(IL10N::class);
  105. $logger = $this->createMock(LoggerInterface::class);
  106. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  107. $addressBook->propPatch(new PropPatch(['{DAV:}displayname' => 'Test address book']));
  108. }
  109. public function testPropPatchNotShared(): void {
  110. /** @var MockObject | CardDavBackend $backend */
  111. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  112. $backend->expects($this->atLeast(1))->method('updateAddressBook');
  113. $addressBookInfo = [
  114. '{DAV:}displayname' => 'Test address book',
  115. 'principaluri' => 'user1',
  116. 'id' => 666,
  117. 'uri' => 'default',
  118. ];
  119. $l10n = $this->createMock(IL10N::class);
  120. $logger = $this->createMock(LoggerInterface::class);
  121. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  122. $addressBook->propPatch(new PropPatch(['{DAV:}displayname' => 'Test address book']));
  123. }
  124. /**
  125. * @dataProvider providesReadOnlyInfo
  126. */
  127. public function testAcl($expectsWrite, $readOnlyValue, $hasOwnerSet): void {
  128. /** @var MockObject | CardDavBackend $backend */
  129. $backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  130. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  131. $addressBookInfo = [
  132. '{DAV:}displayname' => 'Test address book',
  133. 'principaluri' => 'user2',
  134. 'id' => 666,
  135. 'uri' => 'default'
  136. ];
  137. if (!is_null($readOnlyValue)) {
  138. $addressBookInfo['{http://owncloud.org/ns}read-only'] = $readOnlyValue;
  139. }
  140. if ($hasOwnerSet) {
  141. $addressBookInfo['{http://owncloud.org/ns}owner-principal'] = 'user1';
  142. }
  143. $l10n = $this->createMock(IL10N::class);
  144. $logger = $this->createMock(LoggerInterface::class);
  145. $addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
  146. $acl = $addressBook->getACL();
  147. $childAcl = $addressBook->getChildACL();
  148. $expectedAcl = [[
  149. 'privilege' => '{DAV:}read',
  150. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  151. 'protected' => true
  152. ], [
  153. 'privilege' => '{DAV:}write',
  154. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  155. 'protected' => true
  156. ], [
  157. 'privilege' => '{DAV:}write-properties',
  158. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  159. 'protected' => true
  160. ]];
  161. if ($hasOwnerSet) {
  162. $expectedAcl[] = [
  163. 'privilege' => '{DAV:}read',
  164. 'principal' => 'user2',
  165. 'protected' => true
  166. ];
  167. if ($expectsWrite) {
  168. $expectedAcl[] = [
  169. 'privilege' => '{DAV:}write',
  170. 'principal' => 'user2',
  171. 'protected' => true
  172. ];
  173. }
  174. }
  175. $this->assertEquals($expectedAcl, $acl);
  176. $this->assertEquals($expectedAcl, $childAcl);
  177. }
  178. public function providesReadOnlyInfo(): array {
  179. return [
  180. 'read-only property not set' => [true, null, true],
  181. 'read-only property is false' => [true, false, true],
  182. 'read-only property is true' => [false, true, true],
  183. 'read-only property not set and no owner' => [true, null, false],
  184. 'read-only property is false and no owner' => [true, false, false],
  185. 'read-only property is true and no owner' => [false, true, false],
  186. ];
  187. }
  188. }