MetaDataTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, 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 Test\Group;
  23. use OCP\IUserSession;
  24. class MetaDataTest extends \Test\TestCase {
  25. /** @var \OC\Group\Manager */
  26. private $groupManager;
  27. /** @var \OCP\IUserSession */
  28. private $userSession;
  29. /** @var \OC\Group\MetaData */
  30. private $groupMetadata;
  31. /** @var bool */
  32. private $isAdmin = true;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->groupManager = $this->getMockBuilder('\OC\Group\Manager')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->userSession = $this->createMock(IUserSession::class);
  39. $this->groupMetadata = new \OC\Group\MetaData(
  40. 'foo',
  41. $this->isAdmin,
  42. $this->groupManager,
  43. $this->userSession
  44. );
  45. }
  46. private function getGroupMock($countCallCount = 0) {
  47. $group = $this->getMockBuilder('\OC\Group\Group')
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $group->expects($this->exactly(6))
  51. ->method('getGID')
  52. ->will($this->onConsecutiveCalls(
  53. 'admin', 'admin',
  54. 'g2', 'g2',
  55. 'g3', 'g3'));
  56. $group->expects($this->exactly(3))
  57. ->method('getDisplayName')
  58. ->will($this->onConsecutiveCalls(
  59. 'admin',
  60. 'g2',
  61. 'g3'));
  62. $group->expects($this->exactly($countCallCount))
  63. ->method('count')
  64. ->with('')
  65. ->will($this->onConsecutiveCalls(2, 3, 5));
  66. return $group;
  67. }
  68. public function testGet() {
  69. $group = $this->getGroupMock();
  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('g2', $ordinaryGroups[0]['name']);
  79. // user count is not loaded
  80. $this->assertSame(0, $ordinaryGroups[0]['usercount']);
  81. }
  82. public function testGetWithSorting() {
  83. $this->groupMetadata->setSorting(1);
  84. $group = $this->getGroupMock(3);
  85. $groups = array_fill(0, 3, $group);
  86. $this->groupManager->expects($this->once())
  87. ->method('search')
  88. ->with('')
  89. ->willReturn($groups);
  90. [$adminGroups, $ordinaryGroups] = $this->groupMetadata->get();
  91. $this->assertSame(1, count($adminGroups));
  92. $this->assertSame(2, count($ordinaryGroups));
  93. $this->assertSame('g3', $ordinaryGroups[0]['name']);
  94. $this->assertSame(5, $ordinaryGroups[0]['usercount']);
  95. }
  96. public function testGetWithCache() {
  97. $group = $this->getGroupMock();
  98. $groups = array_fill(0, 3, $group);
  99. $this->groupManager->expects($this->once())
  100. ->method('search')
  101. ->with('')
  102. ->willReturn($groups);
  103. //two calls, if caching fails call counts for group and groupmanager
  104. //are exceeded
  105. $this->groupMetadata->get();
  106. $this->groupMetadata->get();
  107. }
  108. //get() does not need to be tested with search parameters, because they are
  109. //solely and only passed to GroupManager and Group.
  110. public function testGetGroupsAsAdmin() {
  111. $this->groupManager
  112. ->expects($this->once())
  113. ->method('search')
  114. ->with('Foo')
  115. ->willReturn(['DummyValue']);
  116. $expected = ['DummyValue'];
  117. $this->assertSame($expected, $this->invokePrivate($this->groupMetadata, 'getGroups', ['Foo']));
  118. }
  119. }