AddressBookTest.php 4.8 KB

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