123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <?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;
- class Manager extends \PHPUnit_Framework_TestCase {
- public function testUserExistsSingleBackendExists() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
- */
- $backend1 = $this->getMock('\OC_User_Dummy');
- $backend1->expects($this->once())
- ->method('userExists')
- ->with($this->equalTo('foo'))
- ->will($this->returnValue(false));
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
- */
- $backend2 = $this->getMock('\OC_User_Dummy');
- $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() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
- */
- $backend1 = $this->getMock('\OC_User_Dummy');
- $backend1->expects($this->once())
- ->method('userExists')
- ->with($this->equalTo('foo'))
- ->will($this->returnValue(true));
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
- */
- $backend2 = $this->getMock('\OC_User_Dummy');
- $backend2->expects($this->never())
- ->method('userExists');
- $manager = new \OC\User\Manager();
- $manager->registerBackend($backend1);
- $manager->registerBackend($backend2);
- $this->assertTrue($manager->userExists('foo'));
- }
- public function testCheckPassword() {
- /**
- * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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) {
- if ($actions === \OC_USER_BACKEND_CHECK_PASSWORD) {
- 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
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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'));
- }
- public function testGetOneBackendExists() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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());
- }
- public function testSearchTwoBackendLimitOffset() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
- */
- $backend1 = $this->getMock('\OC_User_Dummy');
- $backend1->expects($this->once())
- ->method('getUsers')
- ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
- ->will($this->returnValue(array('foo1', 'foo2')));
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
- */
- $backend2 = $this->getMock('\OC_User_Dummy');
- $backend2->expects($this->once())
- ->method('getUsers')
- ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
- ->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());
- }
- public function testCreateUserSingleBackendNotExists() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $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() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
- */
- $backend1 = $this->getMock('\OC_User_Dummy');
- $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));
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
- */
- $backend2 = $this->getMock('\OC_User_Dummy');
- $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');
- }
- public function testCountUsersNoBackend() {
- $manager = new \OC\User\Manager();
- $result = $manager->countUsers();
- $this->assertTrue(is_array($result));
- $this->assertTrue(empty($result));
- }
- public function testCountUsersOneBackend() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend = $this->getMock('\OC_User_Dummy');
- $backend->expects($this->once())
- ->method('countUsers')
- ->will($this->returnValue(7));
- $backend->expects($this->once())
- ->method('implementsActions')
- ->with(\OC_USER_BACKEND_COUNT_USERS)
- ->will($this->returnValue(true));
- $manager = new \OC\User\Manager();
- $manager->registerBackend($backend);
- $result = $manager->countUsers();
- $keys = array_keys($result);
- $this->assertTrue(strpos($keys[0], 'Mock_OC_User_Dummy') !== false);
- $users = array_shift($result);
- $this->assertEquals(7, $users);
- }
- public function testCountUsersTwoBackends() {
- /**
- * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
- */
- $backend1 = $this->getMock('\OC_User_Dummy');
- $backend1->expects($this->once())
- ->method('countUsers')
- ->will($this->returnValue(7));
- $backend1->expects($this->once())
- ->method('implementsActions')
- ->with(\OC_USER_BACKEND_COUNT_USERS)
- ->will($this->returnValue(true));
- $backend2 = $this->getMock('\OC_User_Dummy');
- $backend2->expects($this->once())
- ->method('countUsers')
- ->will($this->returnValue(16));
- $backend2->expects($this->once())
- ->method('implementsActions')
- ->with(\OC_USER_BACKEND_COUNT_USERS)
- ->will($this->returnValue(true));
- $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);
- $this->assertTrue(strpos($keys[0], 'Mock_OC_User_Dummy') !== false);
- $users = array_shift($result);
- //users from backends shall be summed up
- $this->assertEquals(7+16, $users);
- }
- }
|