nextcloud/tests/lib/User/ManagerTest.php

476 lines
14 KiB
PHP
Raw Normal View History

2013-05-29 01:46:57 +04:00
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\User;
2016-09-12 22:53:21 +03:00
use OC\User\Database;
2013-05-29 01:46:57 +04:00
2015-11-19 16:39:05 +03:00
/**
* Class ManagerTest
2015-11-19 16:39:05 +03:00
*
* @group DB
*
* @package Test\User
*/
class ManagerTest extends \Test\TestCase {
public function testGetBackends() {
2016-09-12 22:53:21 +03:00
$userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class);
$manager = new \OC\User\Manager();
$manager->registerBackend($userDummyBackend);
$this->assertEquals([$userDummyBackend], $manager->getBackends());
2016-09-12 22:53:21 +03:00
$dummyDatabaseBackend = $this->createMock(Database::class);
$manager->registerBackend($dummyDatabaseBackend);
$this->assertEquals([$userDummyBackend, $dummyDatabaseBackend], $manager->getBackends());
}
2013-05-29 01:46:57 +04:00
public function testUserExistsSingleBackendExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$this->assertTrue($manager->userExists('foo'));
}
public function testUserExistsSingleBackendNotExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(false));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$this->assertFalse($manager->userExists('foo'));
}
public function testUserExistsNoBackends() {
$manager = new \OC\User\Manager();
$this->assertFalse($manager->userExists('foo'));
}
public function testUserExistsTwoBackendsSecondExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend1 = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend1->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(false));
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend2 = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend2->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
$this->assertTrue($manager->userExists('foo'));
}
public function testUserExistsTwoBackendsFirstExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend1 = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend1->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend2 = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend2->expects($this->never())
->method('userExists');
$manager = new \OC\User\Manager();
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
$this->assertTrue($manager->userExists('foo'));
}
2013-09-24 19:10:01 +04:00
public function testCheckPassword() {
/**
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
2013-09-24 19:10:01 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-09-24 19:10:01 +04:00
$backend->expects($this->once())
->method('checkPassword')
->with($this->equalTo('foo'), $this->equalTo('bar'))
->will($this->returnValue(true));
$backend->expects($this->any())
->method('implementsActions')
->will($this->returnCallback(function ($actions) {
2016-07-13 10:26:19 +03:00
if ($actions === \OC\USER\BACKEND::CHECK_PASSWORD) {
2013-09-24 19:10:01 +04:00
return true;
} else {
return false;
}
}));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$user = $manager->checkPassword('foo', 'bar');
$this->assertTrue($user instanceof \OC\User\User);
}
public function testCheckPasswordNotSupported() {
/**
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
2013-09-24 19:10:01 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-09-24 19:10:01 +04:00
$backend->expects($this->never())
->method('checkPassword');
$backend->expects($this->any())
->method('implementsActions')
->will($this->returnValue(false));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$this->assertFalse($manager->checkPassword('foo', 'bar'));
}
2013-05-29 01:46:57 +04:00
public function testGetOneBackendExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$this->assertEquals('foo', $manager->get('foo')->getUID());
}
public function testGetOneBackendNotExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(false));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$this->assertEquals(null, $manager->get('foo'));
}
public function testSearchOneBackend() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend->expects($this->once())
->method('getUsers')
->with($this->equalTo('fo'))
->will($this->returnValue(array('foo', 'afoo')));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$result = $manager->search('fo');
$this->assertEquals(2, count($result));
$this->assertEquals('afoo', array_shift($result)->getUID());
$this->assertEquals('foo', array_shift($result)->getUID());
2013-05-29 01:46:57 +04:00
}
public function testSearchTwoBackendLimitOffset() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend1 = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend1->expects($this->once())
->method('getUsers')
->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
->will($this->returnValue(array('foo1', 'foo2')));
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend2 = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend2->expects($this->once())
->method('getUsers')
2014-04-15 19:53:57 +04:00
->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
2013-05-29 01:46:57 +04:00
->will($this->returnValue(array('foo3')));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
$result = $manager->search('fo', 3, 1);
$this->assertEquals(3, count($result));
$this->assertEquals('foo1', array_shift($result)->getUID());
$this->assertEquals('foo2', array_shift($result)->getUID());
$this->assertEquals('foo3', array_shift($result)->getUID());
2013-05-29 01:46:57 +04:00
}
public function testCreateUserSingleBackendNotExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend->expects($this->any())
->method('implementsActions')
->will($this->returnValue(true));
$backend->expects($this->once())
->method('createUser')
->with($this->equalTo('foo'), $this->equalTo('bar'));
$backend->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(false));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$user = $manager->createUser('foo', 'bar');
$this->assertEquals('foo', $user->getUID());
}
/**
* @expectedException \Exception
*/
public function testCreateUserSingleBackendExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend->expects($this->any())
->method('implementsActions')
->will($this->returnValue(true));
$backend->expects($this->never())
->method('createUser');
$backend->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$manager->createUser('foo', 'bar');
}
public function testCreateUserSingleBackendNotSupported() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend->expects($this->any())
->method('implementsActions')
->will($this->returnValue(false));
$backend->expects($this->never())
->method('createUser');
$backend->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(false));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$this->assertFalse($manager->createUser('foo', 'bar'));
}
public function testCreateUserNoBackends() {
$manager = new \OC\User\Manager();
$this->assertFalse($manager->createUser('foo', 'bar'));
}
/**
* @expectedException \Exception
*/
public function testCreateUserTwoBackendExists() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend1 = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend1->expects($this->any())
->method('implementsActions')
->will($this->returnValue(true));
$backend1->expects($this->never())
->method('createUser');
$backend1->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(false));
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
2013-05-29 01:46:57 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend2 = $this->createMock(\Test\Util\User\Dummy::class);
2013-05-29 01:46:57 +04:00
$backend2->expects($this->any())
->method('implementsActions')
->will($this->returnValue(true));
$backend2->expects($this->never())
->method('createUser');
$backend2->expects($this->once())
->method('userExists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
$manager = new \OC\User\Manager();
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
$manager->createUser('foo', 'bar');
}
2014-01-08 16:26:48 +04:00
public function testCountUsersNoBackend() {
$manager = new \OC\User\Manager();
$result = $manager->countUsers();
$this->assertTrue(is_array($result));
$this->assertTrue(empty($result));
}
public function testCountUsersOneBackend() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2014-01-08 16:26:48 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend = $this->createMock(\Test\Util\User\Dummy::class);
2014-01-08 16:26:48 +04:00
$backend->expects($this->once())
->method('countUsers')
->will($this->returnValue(7));
$backend->expects($this->once())
->method('implementsActions')
2016-07-13 10:26:19 +03:00
->with(\OC\USER\BACKEND::COUNT_USERS)
2014-01-08 16:26:48 +04:00
->will($this->returnValue(true));
$backend->expects($this->once())
->method('getBackendName')
2015-09-22 01:56:36 +03:00
->will($this->returnValue('Mock_Test_Util_User_Dummy'));
2014-01-08 16:26:48 +04:00
$manager = new \OC\User\Manager();
$manager->registerBackend($backend);
$result = $manager->countUsers();
$keys = array_keys($result);
2015-09-22 01:56:36 +03:00
$this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false);
2014-01-08 16:26:48 +04:00
$users = array_shift($result);
$this->assertEquals(7, $users);
}
public function testCountUsersTwoBackends() {
/**
2015-09-22 01:56:36 +03:00
* @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
2014-01-08 16:26:48 +04:00
*/
2016-09-12 22:53:21 +03:00
$backend1 = $this->createMock(\Test\Util\User\Dummy::class);
2014-01-08 16:26:48 +04:00
$backend1->expects($this->once())
->method('countUsers')
->will($this->returnValue(7));
$backend1->expects($this->once())
->method('implementsActions')
2016-07-13 10:26:19 +03:00
->with(\OC\USER\BACKEND::COUNT_USERS)
2014-01-08 16:26:48 +04:00
->will($this->returnValue(true));
$backend1->expects($this->once())
->method('getBackendName')
2015-09-22 01:56:36 +03:00
->will($this->returnValue('Mock_Test_Util_User_Dummy'));
2014-01-08 16:26:48 +04:00
2016-09-12 22:53:21 +03:00
$backend2 = $this->createMock(\Test\Util\User\Dummy::class);
2014-01-08 16:26:48 +04:00
$backend2->expects($this->once())
->method('countUsers')
->will($this->returnValue(16));
$backend2->expects($this->once())
->method('implementsActions')
2016-07-13 10:26:19 +03:00
->with(\OC\USER\BACKEND::COUNT_USERS)
2014-01-08 16:26:48 +04:00
->will($this->returnValue(true));
$backend2->expects($this->once())
->method('getBackendName')
2015-09-22 01:56:36 +03:00
->will($this->returnValue('Mock_Test_Util_User_Dummy'));
2014-01-08 16:26:48 +04:00
$manager = new \OC\User\Manager();
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
$result = $manager->countUsers();
//because the backends have the same class name, only one value expected
$this->assertEquals(1, count($result));
$keys = array_keys($result);
2015-09-22 01:56:36 +03:00
$this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false);
2014-01-08 16:26:48 +04:00
$users = array_shift($result);
//users from backends shall be summed up
$this->assertEquals(7 + 16, $users);
}
public function testDeleteUser() {
2015-11-19 16:39:05 +03:00
$config = $this->getMockBuilder('OCP\IConfig')
->disableOriginalConstructor()
->getMock();
$config
->expects($this->at(0))
->method('getUserValue')
->with('foo', 'core', 'enabled')
->will($this->returnValue(true));
$config
->expects($this->at(1))
->method('getUserValue')
->with('foo', 'login', 'lastLogin')
->will($this->returnValue(0));
$manager = new \OC\User\Manager($config);
2015-09-22 01:56:36 +03:00
$backend = new \Test\Util\User\Dummy();
$manager->registerBackend($backend);
2015-11-19 16:39:05 +03:00
$backend->createUser('foo', 'bar');
$this->assertTrue($manager->userExists('foo'));
$manager->get('foo')->delete();
$this->assertFalse($manager->userExists('foo'));
2014-01-08 16:26:48 +04:00
}
2013-05-29 01:46:57 +04:00
}