AddressBookTest.php 5.0 KB

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