metadata.php 3.8 KB

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