MetaDataTest.php 3.2 KB

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