AddressBookTest.php 4.5 KB

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