You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GroupPrincipalTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\DAV;
  23. use OCA\DAV\DAV\GroupPrincipalBackend;
  24. use OCP\IGroupManager;
  25. use PHPUnit_Framework_MockObject_MockObject;
  26. use \Sabre\DAV\PropPatch;
  27. class GroupPrincipalTest extends \Test\TestCase {
  28. /** @var IGroupManager | PHPUnit_Framework_MockObject_MockObject */
  29. private $groupManager;
  30. /** @var GroupPrincipalBackend */
  31. private $connector;
  32. public function setUp() {
  33. $this->groupManager = $this->getMockBuilder('\OCP\IGroupManager')
  34. ->disableOriginalConstructor()->getMock();
  35. $this->connector = new GroupPrincipalBackend($this->groupManager);
  36. parent::setUp();
  37. }
  38. public function testGetPrincipalsByPrefixWithoutPrefix() {
  39. $response = $this->connector->getPrincipalsByPrefix('');
  40. $this->assertSame([], $response);
  41. }
  42. public function testGetPrincipalsByPrefixWithUsers() {
  43. $group1 = $this->mockGroup('foo');
  44. $group2 = $this->mockGroup('bar');
  45. $this->groupManager
  46. ->expects($this->once())
  47. ->method('search')
  48. ->with('')
  49. ->will($this->returnValue([$group1, $group2]));
  50. $expectedResponse = [
  51. 0 => [
  52. 'uri' => 'principals/groups/foo',
  53. '{DAV:}displayname' => 'foo'
  54. ],
  55. 1 => [
  56. 'uri' => 'principals/groups/bar',
  57. '{DAV:}displayname' => 'bar',
  58. ]
  59. ];
  60. $response = $this->connector->getPrincipalsByPrefix('principals/groups');
  61. $this->assertSame($expectedResponse, $response);
  62. }
  63. public function testGetPrincipalsByPrefixEmpty() {
  64. $this->groupManager
  65. ->expects($this->once())
  66. ->method('search')
  67. ->with('')
  68. ->will($this->returnValue([]));
  69. $response = $this->connector->getPrincipalsByPrefix('principals/groups');
  70. $this->assertSame([], $response);
  71. }
  72. public function testGetPrincipalsByPathWithoutMail() {
  73. $group1 = $this->mockGroup('foo');
  74. $this->groupManager
  75. ->expects($this->once())
  76. ->method('get')
  77. ->with('foo')
  78. ->will($this->returnValue($group1));
  79. $expectedResponse = [
  80. 'uri' => 'principals/groups/foo',
  81. '{DAV:}displayname' => 'foo'
  82. ];
  83. $response = $this->connector->getPrincipalByPath('principals/groups/foo');
  84. $this->assertSame($expectedResponse, $response);
  85. }
  86. public function testGetPrincipalsByPathWithMail() {
  87. $fooUser = $this->mockGroup('foo');
  88. $this->groupManager
  89. ->expects($this->once())
  90. ->method('get')
  91. ->with('foo')
  92. ->will($this->returnValue($fooUser));
  93. $expectedResponse = [
  94. 'uri' => 'principals/groups/foo',
  95. '{DAV:}displayname' => 'foo',
  96. ];
  97. $response = $this->connector->getPrincipalByPath('principals/groups/foo');
  98. $this->assertSame($expectedResponse, $response);
  99. }
  100. public function testGetPrincipalsByPathEmpty() {
  101. $this->groupManager
  102. ->expects($this->once())
  103. ->method('get')
  104. ->with('foo')
  105. ->will($this->returnValue(null));
  106. $response = $this->connector->getPrincipalByPath('principals/groups/foo');
  107. $this->assertSame(null, $response);
  108. }
  109. public function testGetGroupMemberSet() {
  110. $response = $this->connector->getGroupMemberSet('principals/groups/foo');
  111. $this->assertSame([], $response);
  112. }
  113. public function testGetGroupMembership() {
  114. $response = $this->connector->getGroupMembership('principals/groups/foo');
  115. $this->assertSame([], $response);
  116. }
  117. /**
  118. * @expectedException \Sabre\DAV\Exception
  119. * @expectedExceptionMessage Setting members of the group is not supported yet
  120. */
  121. public function testSetGroupMembership() {
  122. $this->connector->setGroupMemberSet('principals/groups/foo', ['foo']);
  123. }
  124. public function testUpdatePrincipal() {
  125. $this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch(array())));
  126. }
  127. public function testSearchPrincipals() {
  128. $this->assertSame([], $this->connector->searchPrincipals('principals/groups', []));
  129. }
  130. /**
  131. * @return PHPUnit_Framework_MockObject_MockObject
  132. */
  133. private function mockGroup($gid) {
  134. $fooUser = $this->getMockBuilder('\OC\Group\Group')
  135. ->disableOriginalConstructor()->getMock();
  136. $fooUser
  137. ->expects($this->exactly(1))
  138. ->method('getGID')
  139. ->will($this->returnValue($gid));
  140. return $fooUser;
  141. }
  142. }