MetaDataTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. public function setUp() {
  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(9))
  51. ->method('getGID')
  52. ->will($this->onConsecutiveCalls(
  53. 'admin', 'admin', 'admin',
  54. 'g2', 'g2', 'g2',
  55. 'g3', 'g3', 'g3'));
  56. $group->expects($this->exactly($countCallCount))
  57. ->method('count')
  58. ->with('')
  59. ->will($this->onConsecutiveCalls(2, 3, 5));
  60. return $group;
  61. }
  62. public function testGet() {
  63. $group = $this->getGroupMock();
  64. $groups = array_fill(0, 3, $group);
  65. $this->groupManager->expects($this->once())
  66. ->method('search')
  67. ->with('')
  68. ->will($this->returnValue($groups));
  69. list($adminGroups, $ordinaryGroups) = $this->groupMetadata->get();
  70. $this->assertSame(1, count($adminGroups));
  71. $this->assertSame(2, count($ordinaryGroups));
  72. $this->assertSame('g2', $ordinaryGroups[0]['name']);
  73. // user count is not loaded
  74. $this->assertSame(0, $ordinaryGroups[0]['usercount']);
  75. }
  76. public function testGetWithSorting() {
  77. $this->groupMetadata->setSorting(1);
  78. $group = $this->getGroupMock(3);
  79. $groups = array_fill(0, 3, $group);
  80. $this->groupManager->expects($this->once())
  81. ->method('search')
  82. ->with('')
  83. ->will($this->returnValue($groups));
  84. list($adminGroups, $ordinaryGroups) = $this->groupMetadata->get();
  85. $this->assertSame(1, count($adminGroups));
  86. $this->assertSame(2, count($ordinaryGroups));
  87. $this->assertSame('g3', $ordinaryGroups[0]['name']);
  88. $this->assertSame(5, $ordinaryGroups[0]['usercount']);
  89. }
  90. public function testGetWithCache() {
  91. $group = $this->getGroupMock();
  92. $groups = array_fill(0, 3, $group);
  93. $this->groupManager->expects($this->once())
  94. ->method('search')
  95. ->with('')
  96. ->will($this->returnValue($groups));
  97. //two calls, if caching fails call counts for group and groupmanager
  98. //are exceeded
  99. $this->groupMetadata->get();
  100. $this->groupMetadata->get();
  101. }
  102. //get() does not need to be tested with search parameters, because they are
  103. //solely and only passed to GroupManager and Group.
  104. public function testGetGroupsAsAdmin() {
  105. $this->groupManager
  106. ->expects($this->once())
  107. ->method('search')
  108. ->with('Foo')
  109. ->will($this->returnValue(['DummyValue']));
  110. $expected = ['DummyValue'];
  111. $this->assertSame($expected, $this->invokePrivate($this->groupMetadata, 'getGroups', ['Foo']));
  112. }
  113. }