2014-07-09 14:19:50 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-10-27 16:09:45 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-07-09 14:19:50 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Group;
|
|
|
|
|
2016-09-07 21:02:11 +03:00
|
|
|
use OCP\IUserSession;
|
|
|
|
|
2016-05-19 09:44:41 +03:00
|
|
|
class MetaDataTest extends \Test\TestCase {
|
2015-10-27 16:09:45 +03:00
|
|
|
/** @var \OC\Group\Manager */
|
|
|
|
private $groupManager;
|
|
|
|
/** @var \OCP\IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
/** @var \OC\Group\MetaData */
|
|
|
|
private $groupMetadata;
|
|
|
|
/** @var bool */
|
|
|
|
private $isAdmin = true;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-10-27 16:09:45 +03:00
|
|
|
parent::setUp();
|
|
|
|
$this->groupManager = $this->getMockBuilder('\OC\Group\Manager')
|
2014-07-09 14:19:50 +04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-07 21:02:11 +03:00
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupMetadata = new \OC\Group\MetaData(
|
|
|
|
'foo',
|
|
|
|
$this->isAdmin,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->userSession
|
|
|
|
);
|
2014-07-09 14:19:50 +04:00
|
|
|
}
|
|
|
|
|
2015-05-18 13:23:12 +03:00
|
|
|
private function getGroupMock($countCallCount = 0) {
|
2014-07-09 14:19:50 +04:00
|
|
|
$group = $this->getMockBuilder('\OC\Group\Group')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2018-03-03 14:22:27 +03:00
|
|
|
$group->expects($this->exactly(6))
|
2014-07-09 14:19:50 +04:00
|
|
|
->method('getGID')
|
|
|
|
->will($this->onConsecutiveCalls(
|
2018-03-05 12:10:05 +03:00
|
|
|
'admin', 'admin',
|
|
|
|
'g2', 'g2',
|
|
|
|
'g3', 'g3'));
|
2014-07-09 14:19:50 +04:00
|
|
|
|
2018-03-03 14:22:27 +03:00
|
|
|
$group->expects($this->exactly(3))
|
|
|
|
->method('getDisplayName')
|
|
|
|
->will($this->onConsecutiveCalls(
|
|
|
|
'admin',
|
|
|
|
'g2',
|
|
|
|
'g3'));
|
|
|
|
|
2015-05-18 13:23:12 +03:00
|
|
|
$group->expects($this->exactly($countCallCount))
|
2014-07-09 14:19:50 +04:00
|
|
|
->method('count')
|
|
|
|
->with('')
|
|
|
|
->will($this->onConsecutiveCalls(2, 3, 5));
|
|
|
|
|
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testGet() {
|
|
|
|
$group = $this->getGroupMock();
|
|
|
|
$groups = array_fill(0, 3, $group);
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupManager->expects($this->once())
|
2014-07-09 14:19:50 +04:00
|
|
|
->method('search')
|
|
|
|
->with('')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($groups);
|
2014-07-09 14:19:50 +04:00
|
|
|
|
2021-01-12 12:15:48 +03:00
|
|
|
[$adminGroups, $ordinaryGroups] = $this->groupMetadata->get();
|
2014-07-09 14:19:50 +04:00
|
|
|
|
|
|
|
$this->assertSame(1, count($adminGroups));
|
|
|
|
$this->assertSame(2, count($ordinaryGroups));
|
|
|
|
|
|
|
|
$this->assertSame('g2', $ordinaryGroups[0]['name']);
|
2015-05-18 13:23:12 +03:00
|
|
|
// user count is not loaded
|
|
|
|
$this->assertSame(0, $ordinaryGroups[0]['usercount']);
|
2014-07-09 14:19:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithSorting() {
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupMetadata->setSorting(1);
|
2015-05-18 13:23:12 +03:00
|
|
|
$group = $this->getGroupMock(3);
|
2014-07-09 14:19:50 +04:00
|
|
|
$groups = array_fill(0, 3, $group);
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupManager->expects($this->once())
|
2014-07-09 14:19:50 +04:00
|
|
|
->method('search')
|
|
|
|
->with('')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($groups);
|
2014-07-09 14:19:50 +04:00
|
|
|
|
2021-01-12 12:15:48 +03:00
|
|
|
[$adminGroups, $ordinaryGroups] = $this->groupMetadata->get();
|
2014-07-09 14:19:50 +04:00
|
|
|
|
|
|
|
$this->assertSame(1, count($adminGroups));
|
|
|
|
$this->assertSame(2, count($ordinaryGroups));
|
|
|
|
|
|
|
|
$this->assertSame('g3', $ordinaryGroups[0]['name']);
|
|
|
|
$this->assertSame(5, $ordinaryGroups[0]['usercount']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWithCache() {
|
|
|
|
$group = $this->getGroupMock();
|
|
|
|
$groups = array_fill(0, 3, $group);
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupManager->expects($this->once())
|
2014-07-09 14:19:50 +04:00
|
|
|
->method('search')
|
|
|
|
->with('')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($groups);
|
2014-07-09 14:19:50 +04:00
|
|
|
|
|
|
|
//two calls, if caching fails call counts for group and groupmanager
|
|
|
|
//are exceeded
|
2015-10-27 16:09:45 +03:00
|
|
|
$this->groupMetadata->get();
|
|
|
|
$this->groupMetadata->get();
|
2014-07-09 14:19:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//get() does not need to be tested with search parameters, because they are
|
|
|
|
//solely and only passed to GroupManager and Group.
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
public function testGetGroupsAsAdmin() {
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('search')
|
|
|
|
->with('Foo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(['DummyValue']);
|
2015-10-27 16:09:45 +03:00
|
|
|
|
|
|
|
$expected = ['DummyValue'];
|
|
|
|
$this->assertSame($expected, $this->invokePrivate($this->groupMetadata, 'getGroups', ['Foo']));
|
|
|
|
}
|
2014-07-09 14:19:50 +04:00
|
|
|
}
|