MetaDataTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Group;
  8. use OCP\IUserSession;
  9. class MetaDataTest extends \Test\TestCase {
  10. private \OC\Group\Manager $groupManager;
  11. private IUserSession $userSession;
  12. private \OC\Group\MetaData $groupMetadata;
  13. private bool $isAdmin = true;
  14. private bool $isDelegatedAdmin = true;
  15. protected function setUp(): void {
  16. parent::setUp();
  17. $this->groupManager = $this->getMockBuilder('\OC\Group\Manager')
  18. ->disableOriginalConstructor()
  19. ->getMock();
  20. $this->userSession = $this->createMock(IUserSession::class);
  21. $this->groupMetadata = new \OC\Group\MetaData(
  22. 'foo',
  23. $this->isAdmin,
  24. $this->isDelegatedAdmin,
  25. $this->groupManager,
  26. $this->userSession
  27. );
  28. }
  29. private function getGroupMock($countCallCount = 0) {
  30. $group = $this->getMockBuilder('\OC\Group\Group')
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $group->expects($this->exactly(6))
  34. ->method('getGID')
  35. ->will($this->onConsecutiveCalls(
  36. 'admin', 'admin',
  37. 'g2', 'g2',
  38. 'g3', 'g3'));
  39. $group->expects($this->exactly(3))
  40. ->method('getDisplayName')
  41. ->will($this->onConsecutiveCalls(
  42. 'admin',
  43. 'g2',
  44. 'g3'));
  45. $group->expects($this->exactly($countCallCount))
  46. ->method('count')
  47. ->with('')
  48. ->will($this->onConsecutiveCalls(2, 3, 5));
  49. return $group;
  50. }
  51. public function testGet() {
  52. $group = $this->getGroupMock();
  53. $groups = array_fill(0, 3, $group);
  54. $this->groupManager->expects($this->once())
  55. ->method('search')
  56. ->with('')
  57. ->willReturn($groups);
  58. [$adminGroups, $ordinaryGroups] = $this->groupMetadata->get();
  59. $this->assertSame(1, count($adminGroups));
  60. $this->assertSame(2, count($ordinaryGroups));
  61. $this->assertSame('g2', $ordinaryGroups[0]['name']);
  62. // user count is not loaded
  63. $this->assertSame(0, $ordinaryGroups[0]['usercount']);
  64. }
  65. public function testGetWithSorting() {
  66. $this->groupMetadata->setSorting(1);
  67. $group = $this->getGroupMock(3);
  68. $groups = array_fill(0, 3, $group);
  69. $this->groupManager->expects($this->once())
  70. ->method('search')
  71. ->with('')
  72. ->willReturn($groups);
  73. [$adminGroups, $ordinaryGroups] = $this->groupMetadata->get();
  74. $this->assertSame(1, count($adminGroups));
  75. $this->assertSame(2, count($ordinaryGroups));
  76. $this->assertSame('g3', $ordinaryGroups[0]['name']);
  77. $this->assertSame(5, $ordinaryGroups[0]['usercount']);
  78. }
  79. public function testGetWithCache() {
  80. $group = $this->getGroupMock();
  81. $groups = array_fill(0, 3, $group);
  82. $this->groupManager->expects($this->once())
  83. ->method('search')
  84. ->with('')
  85. ->willReturn($groups);
  86. //two calls, if caching fails call counts for group and groupmanager
  87. //are exceeded
  88. $this->groupMetadata->get();
  89. $this->groupMetadata->get();
  90. }
  91. //get() does not need to be tested with search parameters, because they are
  92. //solely and only passed to GroupManager and Group.
  93. public function testGetGroupsAsAdmin() {
  94. $this->groupManager
  95. ->expects($this->once())
  96. ->method('search')
  97. ->with('Foo')
  98. ->willReturn(['DummyValue']);
  99. $expected = ['DummyValue'];
  100. $this->assertSame($expected, $this->invokePrivate($this->groupMetadata, 'getGroups', ['Foo']));
  101. }
  102. }