diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2016-05-20 15:38:20 +0200 |
---|---|---|
committer | Thomas Müller <DeepDiver1975@users.noreply.github.com> | 2016-05-20 15:38:20 +0200 |
commit | 94ad54ec9b96d41a614fbbad4a97b34c41a6901f (patch) | |
tree | f3eb7cdda2704aaf0cd59d58efe66bcbd34cb67d /tests/lib/User | |
parent | 2ef751b1ec28f7b5c7113af60ec8c9fa0ae1cf87 (diff) | |
download | nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.tar.gz nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.zip |
Move tests/ to PSR-4 (#24731)
* Move a-b to PSR-4
* Move c-d to PSR-4
* Move e+g to PSR-4
* Move h-l to PSR-4
* Move m-r to PSR-4
* Move s-u to PSR-4
* Move files/ to PSR-4
* Move remaining tests to PSR-4
* Remove Test\ from old autoloader
Diffstat (limited to 'tests/lib/User')
-rw-r--r-- | tests/lib/User/AvatarUserDummy.php | 29 | ||||
-rw-r--r-- | tests/lib/User/Backend.php | 117 | ||||
-rw-r--r-- | tests/lib/User/DatabaseTest.php | 54 | ||||
-rw-r--r-- | tests/lib/User/Dummy.php | 30 | ||||
-rw-r--r-- | tests/lib/User/ManagerTest.php | 474 | ||||
-rw-r--r-- | tests/lib/User/SessionTest.php | 512 | ||||
-rw-r--r-- | tests/lib/User/UserTest.php | 479 |
7 files changed, 1695 insertions, 0 deletions
diff --git a/tests/lib/User/AvatarUserDummy.php b/tests/lib/User/AvatarUserDummy.php new file mode 100644 index 00000000000..123825de50f --- /dev/null +++ b/tests/lib/User/AvatarUserDummy.php @@ -0,0 +1,29 @@ +<?php +/** +* ownCloud +* +* @author Arthur Schiwon +* @copyright 2013 Arthur Schiwon blizzz@owncloud.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace Test\User; + +class AvatarUserDummy extends \Test\Util\User\Dummy { + public function canChangeAvatar($uid) { + return true; + } +} diff --git a/tests/lib/User/Backend.php b/tests/lib/User/Backend.php new file mode 100644 index 00000000000..85ccbac913c --- /dev/null +++ b/tests/lib/User/Backend.php @@ -0,0 +1,117 @@ +<?php +/** +* ownCloud +* +* @author Robin Appelman +* @copyright 2012 Robin Appelman icewind@owncloud.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace Test\User; + +/** + * Abstract class to provide the basis of backend-specific unit test classes. + * + * All subclasses MUST assign a backend property in setUp() which implements + * user operations (add, remove, etc.). Test methods in this class will then be + * run on each separate subclass and backend therein. + * + * For an example see /tests/lib/user/dummy.php + */ + +abstract class Backend extends \Test\TestCase { + /** + * @var \OC\User\Backend $backend + */ + protected $backend; + + /** + * get a new unique user name + * test cases can override this in order to clean up created user + * @return string + */ + public function getUser() { + return $this->getUniqueID('test_'); + } + + public function testAddRemove() { + //get the number of groups we start with, in case there are exising groups + $startCount=count($this->backend->getUsers()); + + $name1=$this->getUser(); + $name2=$this->getUser(); + $this->backend->createUser($name1, ''); + $count=count($this->backend->getUsers())-$startCount; + $this->assertEquals(1, $count); + $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false)); + $this->assertFalse((array_search($name2, $this->backend->getUsers())!==false)); + $this->backend->createUser($name2, ''); + $count=count($this->backend->getUsers())-$startCount; + $this->assertEquals(2, $count); + $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false)); + $this->assertTrue((array_search($name2, $this->backend->getUsers())!==false)); + + $this->backend->deleteUser($name2); + $count=count($this->backend->getUsers())-$startCount; + $this->assertEquals(1, $count); + $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false)); + $this->assertFalse((array_search($name2, $this->backend->getUsers())!==false)); + } + + public function testLogin() { + $name1=$this->getUser(); + $name2=$this->getUser(); + + $this->assertFalse($this->backend->userExists($name1)); + $this->assertFalse($this->backend->userExists($name2)); + + $this->backend->createUser($name1, 'pass1'); + $this->backend->createUser($name2, 'pass2'); + + $this->assertTrue($this->backend->userExists($name1)); + $this->assertTrue($this->backend->userExists($name2)); + + $this->assertSame($name1, $this->backend->checkPassword($name1, 'pass1')); + $this->assertSame($name2, $this->backend->checkPassword($name2, 'pass2')); + + $this->assertFalse($this->backend->checkPassword($name1, 'pass2')); + $this->assertFalse($this->backend->checkPassword($name2, 'pass1')); + + $this->assertFalse($this->backend->checkPassword($name1, 'dummy')); + $this->assertFalse($this->backend->checkPassword($name2, 'foobar')); + + $this->backend->setPassword($name1, 'newpass1'); + $this->assertFalse($this->backend->checkPassword($name1, 'pass1')); + $this->assertSame($name1, $this->backend->checkPassword($name1, 'newpass1')); + $this->assertFalse($this->backend->checkPassword($name2, 'newpass1')); + } + + public function testSearch() { + $name1 = 'foobarbaz'; + $name2 = 'bazbarfoo'; + $name3 = 'notme'; + + $this->backend->createUser($name1, 'pass1'); + $this->backend->createUser($name2, 'pass2'); + $this->backend->createUser($name3, 'pass3'); + + $result = $this->backend->getUsers('bar'); + $this->assertSame(2, count($result)); + + $result = $this->backend->getDisplayNames('bar'); + $this->assertSame(2, count($result)); + } +} diff --git a/tests/lib/User/DatabaseTest.php b/tests/lib/User/DatabaseTest.php new file mode 100644 index 00000000000..270d90b35bc --- /dev/null +++ b/tests/lib/User/DatabaseTest.php @@ -0,0 +1,54 @@ +<?php +/** +* ownCloud +* +* @author Robin Appelman +* @copyright 2012 Robin Appelman icewind@owncloud.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace Test\User; + +/** + * Class DatabaseTest + * + * @group DB + */ +class DatabaseTest extends Backend { + /** @var array */ + private $users; + + public function getUser() { + $user = parent::getUser(); + $this->users[]=$user; + return $user; + } + + protected function setUp() { + parent::setUp(); + $this->backend=new \OC\User\Database(); + } + + protected function tearDown() { + if(!isset($this->users)) { + return; + } + foreach($this->users as $user) { + $this->backend->deleteUser($user); + } + parent::tearDown(); + } +} diff --git a/tests/lib/User/Dummy.php b/tests/lib/User/Dummy.php new file mode 100644 index 00000000000..50382aa8fe6 --- /dev/null +++ b/tests/lib/User/Dummy.php @@ -0,0 +1,30 @@ +<?php +/** +* ownCloud +* +* @author Robin Appelman +* @copyright 2012 Robin Appelman icewind@owncloud.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace Test\User; + +class Dummy extends Backend { + protected function setUp() { + parent::setUp(); + $this->backend=new \Test\Util\User\Dummy(); + } +} diff --git a/tests/lib/User/ManagerTest.php b/tests/lib/User/ManagerTest.php new file mode 100644 index 00000000000..2a6cdbc1355 --- /dev/null +++ b/tests/lib/User/ManagerTest.php @@ -0,0 +1,474 @@ +<?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 ManagerTest + * + * @group DB + * + * @package Test\User + */ +class ManagerTest extends \Test\TestCase { + public function testGetBackends() { + $userDummyBackend = $this->getMock('\Test\Util\User\Dummy'); + $manager = new \OC\User\Manager(); + $manager->registerBackend($userDummyBackend); + $this->assertEquals([$userDummyBackend], $manager->getBackends()); + $dummyDatabaseBackend = $this->getMock('\OC_User_Database'); + $manager->registerBackend($dummyDatabaseBackend); + $this->assertEquals([$userDummyBackend, $dummyDatabaseBackend], $manager->getBackends()); + } + + + public function testUserExistsSingleBackendExists() { + /** + * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 + */ + $backend1 = $this->getMock('\Test\Util\User\Dummy'); + $backend1->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(false)); + + /** + * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 + */ + $backend2 = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 + */ + $backend1 = $this->getMock('\Test\Util\User\Dummy'); + $backend1->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(true)); + + /** + * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 + */ + $backend2 = $this->getMock('\Test\Util\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('\Test\Util\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('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 + */ + $backend1 = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 + */ + $backend2 = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 + */ + $backend1 = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 + */ + $backend2 = $this->getMock('\Test\Util\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 \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\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)); + + $backend->expects($this->once()) + ->method('getBackendName') + ->will($this->returnValue('Mock_Test_Util_User_Dummy')); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend); + + $result = $manager->countUsers(); + $keys = array_keys($result); + $this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false); + + $users = array_shift($result); + $this->assertEquals(7, $users); + } + + public function testCountUsersTwoBackends() { + /** + * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend1 = $this->getMock('\Test\Util\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)); + $backend1->expects($this->once()) + ->method('getBackendName') + ->will($this->returnValue('Mock_Test_Util_User_Dummy')); + + $backend2 = $this->getMock('\Test\Util\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)); + $backend2->expects($this->once()) + ->method('getBackendName') + ->will($this->returnValue('Mock_Test_Util_User_Dummy')); + + $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_Test_Util_User_Dummy') !== false); + + $users = array_shift($result); + //users from backends shall be summed up + $this->assertEquals(7 + 16, $users); + } + + public function testDeleteUser() { + $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); + $backend = new \Test\Util\User\Dummy(); + + $manager->registerBackend($backend); + $backend->createUser('foo', 'bar'); + $this->assertTrue($manager->userExists('foo')); + $manager->get('foo')->delete(); + $this->assertFalse($manager->userExists('foo')); + } +} diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php new file mode 100644 index 00000000000..4438487e2a0 --- /dev/null +++ b/tests/lib/User/SessionTest.php @@ -0,0 +1,512 @@ +<?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; + +use OC\Session\Memory; +use OC\User\User; + +/** + * @group DB + * @package Test\User + */ +class SessionTest extends \Test\TestCase { + + /** @var \OCP\AppFramework\Utility\ITimeFactory */ + private $timeFactory; + + /** @var \OC\Authentication\Token\DefaultTokenProvider */ + protected $defaultProvider; + + protected function setUp() { + parent::setUp(); + + $this->timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory'); + $this->timeFactory->expects($this->any()) + ->method('getTime') + ->will($this->returnValue(10000)); + $this->defaultProvider = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenProvider') + ->disableOriginalConstructor() + ->getMock(); + } + + public function testGetUser() { + $token = new \OC\Authentication\Token\DefaultToken(); + + $expectedUser = new User('foo', null); + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->at(0)) + ->method('get') + ->with('user_id') + ->will($this->returnValue($expectedUser->getUID())); + $sessionId = 'abcdef12345'; + + $manager = $this->getMockBuilder('\OC\User\Manager') + ->disableOriginalConstructor() + ->getMock(); + $session->expects($this->once()) + ->method('getId') + ->will($this->returnValue($sessionId)); + $this->defaultProvider->expects($this->once()) + ->method('getToken') + ->will($this->returnValue($token)); + $session->expects($this->at(2)) + ->method('get') + ->with('last_login_check') + ->will($this->returnValue(null)); // No check has been run yet + $this->defaultProvider->expects($this->once()) + ->method('getPassword') + ->with($token, $sessionId) + ->will($this->returnValue('password123')); + $manager->expects($this->once()) + ->method('checkPassword') + ->with($expectedUser->getUID(), 'password123') + ->will($this->returnValue(true)); + $session->expects($this->at(3)) + ->method('set') + ->with('last_login_check', 10000); + + $session->expects($this->at(4)) + ->method('get') + ->with('last_token_update') + ->will($this->returnValue(null)); // No check run so far + $this->defaultProvider->expects($this->once()) + ->method('updateToken') + ->with($token); + $session->expects($this->at(5)) + ->method('set') + ->with('last_token_update', $this->equalTo(10000)); + + $manager->expects($this->any()) + ->method('get') + ->with($expectedUser->getUID()) + ->will($this->returnValue($expectedUser)); + + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $user = $userSession->getUser(); + $this->assertSame($expectedUser, $user); + } + + public function isLoggedInData() { + return [ + [true], + [false], + ]; + } + + /** + * @dataProvider isLoggedInData + */ + public function testIsLoggedIn($isLoggedIn) { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + + $manager = $this->getMockBuilder('\OC\User\Manager') + ->disableOriginalConstructor() + ->getMock(); + + $userSession = $this->getMockBuilder('\OC\User\Session') + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider]) + ->setMethods([ + 'getUser' + ]) + ->getMock(); + $user = new User('sepp', null); + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($isLoggedIn ? $user : null)); + $this->assertEquals($isLoggedIn, $userSession->isLoggedIn()); + } + + public function testSetUser() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->once()) + ->method('set') + ->with('user_id', 'foo'); + + $manager = $this->getMock('\OC\User\Manager'); + + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('foo')); + + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession->setUser($user); + } + + public function testLoginValidPasswordEnabled() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->once()) + ->method('regenerateId'); + $session->expects($this->exactly(2)) + ->method('set') + ->with($this->callback(function ($key) { + switch ($key) { + case 'user_id': + case 'loginname': + return true; + break; + default: + return false; + break; + } + }, 'foo')); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach ($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if ($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + $user->expects($this->any()) + ->method('isEnabled') + ->will($this->returnValue(true)); + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('foo')); + $user->expects($this->once()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('checkPassword') + ->with('foo', 'bar') + ->will($this->returnValue($user)); + + $userSession = $this->getMockBuilder('\OC\User\Session') + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider]) + ->setMethods([ + 'prepareUserLogin' + ]) + ->getMock(); + $userSession->expects($this->once()) + ->method('prepareUserLogin'); + $userSession->login('foo', 'bar'); + $this->assertEquals($user, $userSession->getUser()); + } + + /** + * @expectedException \OC\User\LoginException + */ + public function testLoginValidPasswordDisabled() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->never()) + ->method('set'); + $session->expects($this->once()) + ->method('regenerateId'); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach ($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if ($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + $user->expects($this->any()) + ->method('isEnabled') + ->will($this->returnValue(false)); + $user->expects($this->never()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('checkPassword') + ->with('foo', 'bar') + ->will($this->returnValue($user)); + + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession->login('foo', 'bar'); + } + + public function testLoginInvalidPassword() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->never()) + ->method('set'); + $session->expects($this->once()) + ->method('regenerateId'); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach ($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if ($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + $user->expects($this->never()) + ->method('isEnabled'); + $user->expects($this->never()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('checkPassword') + ->with('foo', 'bar') + ->will($this->returnValue(false)); + + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession->login('foo', 'bar'); + } + + public function testLoginNonExisting() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->never()) + ->method('set'); + $session->expects($this->once()) + ->method('regenerateId'); + + $manager = $this->getMock('\OC\User\Manager'); + + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $manager->expects($this->once()) + ->method('checkPassword') + ->with('foo', 'bar') + ->will($this->returnValue(false)); + + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession->login('foo', 'bar'); + } + + public function testRememberLoginValidToken() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->exactly(1)) + ->method('set') + ->with($this->callback(function ($key) { + switch ($key) { + case 'user_id': + return true; + default: + return false; + } + }, 'foo')); + $session->expects($this->once()) + ->method('regenerateId'); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach ($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if ($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('foo')); + $user->expects($this->once()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue($user)); + + //prepare login token + $token = 'goodToken'; + \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time()); + + $userSession = $this->getMock( + '\OC\User\Session', + //override, otherwise tests will fail because of setcookie() + array('setMagicInCookie'), + //there are passed as parameters to the constructor + array($manager, $session, $this->timeFactory, $this->defaultProvider)); + + $granted = $userSession->loginWithCookie('foo', $token); + + $this->assertSame($granted, true); + } + + public function testRememberLoginInvalidToken() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->never()) + ->method('set'); + $session->expects($this->once()) + ->method('regenerateId'); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach ($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if ($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('foo')); + $user->expects($this->never()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue($user)); + + //prepare login token + $token = 'goodToken'; + \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time()); + + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $granted = $userSession->loginWithCookie('foo', 'badToken'); + + $this->assertSame($granted, false); + } + + public function testRememberLoginInvalidUser() { + $session = $this->getMock('\OC\Session\Memory', array(), array('')); + $session->expects($this->never()) + ->method('set'); + $session->expects($this->once()) + ->method('regenerateId'); + + $managerMethods = get_class_methods('\OC\User\Manager'); + //keep following methods intact in order to ensure hooks are + //working + $doNotMock = array('__construct', 'emit', 'listen'); + foreach ($doNotMock as $methodName) { + $i = array_search($methodName, $managerMethods, true); + if ($i !== false) { + unset($managerMethods[$i]); + } + } + $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); + + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); + + $user->expects($this->never()) + ->method('getUID'); + $user->expects($this->never()) + ->method('updateLastLoginTimestamp'); + + $manager->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue(null)); + + //prepare login token + $token = 'goodToken'; + \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time()); + + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $granted = $userSession->loginWithCookie('foo', $token); + + $this->assertSame($granted, false); + } + + public function testActiveUserAfterSetSession() { + $users = array( + 'foo' => new User('foo', null), + 'bar' => new User('bar', null) + ); + + $manager = $this->getMockBuilder('\OC\User\Manager') + ->disableOriginalConstructor() + ->getMock(); + + $manager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function ($uid) use ($users) { + return $users[$uid]; + })); + + $session = new Memory(''); + $session->set('user_id', 'foo'); + $userSession = $this->getMockBuilder('\OC\User\Session') + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider]) + ->setMethods([ + 'validateSession' + ]) + ->getMock(); + $userSession->expects($this->any()) + ->method('validateSession'); + + $this->assertEquals($users['foo'], $userSession->getUser()); + + $session2 = new Memory(''); + $session2->set('user_id', 'bar'); + $userSession->setSession($session2); + $this->assertEquals($users['bar'], $userSession->getUser()); + } + + public function testTryTokenLoginWithDisabledUser() { + $manager = $this->getMockBuilder('\OC\User\Manager') + ->disableOriginalConstructor() + ->getMock(); + $session = new Memory(''); + $token = $this->getMock('\OC\Authentication\Token\IToken'); + $user = $this->getMock('\OCP\IUser'); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $request = $this->getMock('\OCP\IRequest'); + + $request->expects($this->once()) + ->method('getHeader') + ->with('Authorization') + ->will($this->returnValue('token xxxxx')); + $this->defaultProvider->expects($this->once()) + ->method('validateToken') + ->with('xxxxx') + ->will($this->returnValue($token)); + $token->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('user123')); + $manager->expects($this->once()) + ->method('get') + ->with('user123') + ->will($this->returnValue($user)); + $user->expects($this->once()) + ->method('isEnabled') + ->will($this->returnValue(false)); + + $this->assertFalse($userSession->tryTokenLogin($request)); + } + +} diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php new file mode 100644 index 00000000000..a49bddde9eb --- /dev/null +++ b/tests/lib/User/UserTest.php @@ -0,0 +1,479 @@ +<?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; + +use OC\Hooks\PublicEmitter; + +/** + * Class UserTest + * + * @group DB + * + * @package Test\User + */ +class UserTest extends \Test\TestCase { + public function testDisplayName() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC\User\Backend'); + $backend->expects($this->once()) + ->method('getDisplayName') + ->with($this->equalTo('foo')) + ->will($this->returnValue('Foo')); + + $backend->expects($this->any()) + ->method('implementsActions') + ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME)) + ->will($this->returnValue(true)); + + $user = new \OC\User\User('foo', $backend); + $this->assertEquals('Foo', $user->getDisplayName()); + } + + /** + * if the display name contain whitespaces only, we expect the uid as result + */ + public function testDisplayNameEmpty() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC\User\Backend'); + $backend->expects($this->once()) + ->method('getDisplayName') + ->with($this->equalTo('foo')) + ->will($this->returnValue(' ')); + + $backend->expects($this->any()) + ->method('implementsActions') + ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME)) + ->will($this->returnValue(true)); + + $user = new \OC\User\User('foo', $backend); + $this->assertEquals('foo', $user->getDisplayName()); + } + + public function testDisplayNameNotSupported() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC\User\Backend'); + $backend->expects($this->never()) + ->method('getDisplayName'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME)) + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertEquals('foo', $user->getDisplayName()); + } + + public function testSetPassword() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + $backend->expects($this->once()) + ->method('setPassword') + ->with($this->equalTo('foo'), $this->equalTo('bar')); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::SET_PASSWORD) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->setPassword('bar','')); + } + + public function testSetPasswordNotSupported() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + $backend->expects($this->never()) + ->method('setPassword'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->setPassword('bar','')); + } + + public function testChangeAvatarSupportedYes() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('Test\User\AvatarUserDummy'); + $backend->expects($this->once()) + ->method('canChangeAvatar') + ->with($this->equalTo('foo')) + ->will($this->returnValue(true)); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::PROVIDE_AVATAR) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->canChangeAvatar()); + } + + public function testChangeAvatarSupportedNo() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('Test\User\AvatarUserDummy'); + $backend->expects($this->once()) + ->method('canChangeAvatar') + ->with($this->equalTo('foo')) + ->will($this->returnValue(false)); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::PROVIDE_AVATAR) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->canChangeAvatar()); + } + + public function testChangeAvatarNotSupported() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('Test\User\AvatarUserDummy'); + $backend->expects($this->never()) + ->method('canChangeAvatar'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + return false; + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->canChangeAvatar()); + } + + public function testDelete() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + $backend->expects($this->once()) + ->method('deleteUser') + ->with($this->equalTo('foo')); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->delete()); + } + + public function testGetHome() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + $backend->expects($this->once()) + ->method('getHome') + ->with($this->equalTo('foo')) + ->will($this->returnValue('/home/foo')); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::GET_HOME) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertEquals('/home/foo', $user->getHome()); + } + + public function testGetBackendClassName() { + $user = new \OC\User\User('foo', new \Test\Util\User\Dummy()); + $this->assertEquals('Dummy', $user->getBackendClassName()); + $user = new \OC\User\User('foo', new \OC\User\Database()); + $this->assertEquals('Database', $user->getBackendClassName()); + } + + public function testGetHomeNotSupported() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + $backend->expects($this->never()) + ->method('getHome'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $allConfig = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $allConfig->expects($this->any()) + ->method('getUserValue') + ->will($this->returnValue(true)); + $allConfig->expects($this->any()) + ->method('getSystemValue') + ->with($this->equalTo('datadirectory')) + ->will($this->returnValue('arbitrary/path')); + + $user = new \OC\User\User('foo', $backend, null, $allConfig); + $this->assertEquals('arbitrary/path/foo', $user->getHome()); + } + + public function testCanChangePassword() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::SET_PASSWORD) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->canChangePassword()); + } + + public function testCanChangePasswordNotSupported() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->canChangePassword()); + } + + public function testCanChangeDisplayName() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::SET_DISPLAYNAME) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->canChangeDisplayName()); + } + + public function testCanChangeDisplayNameNotSupported() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->canChangeDisplayName()); + } + + public function testSetDisplayNameSupported() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC\User\Database'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::SET_DISPLAYNAME) { + return true; + } else { + return false; + } + })); + + $backend->expects($this->once()) + ->method('setDisplayName') + ->with('foo','Foo') + ->willReturn(true); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->setDisplayName('Foo')); + $this->assertEquals('Foo',$user->getDisplayName()); + } + + /** + * don't allow display names containing whitespaces only + */ + public function testSetDisplayNameEmpty() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC\User\Database'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::SET_DISPLAYNAME) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->setDisplayName(' ')); + $this->assertEquals('foo',$user->getDisplayName()); + } + + public function testSetDisplayNameNotSupported() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC\User\Database'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + return false; + })); + + $backend->expects($this->never()) + ->method('setDisplayName'); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->setDisplayName('Foo')); + $this->assertEquals('foo',$user->getDisplayName()); + } + + public function testSetPasswordHooks() { + $hooksCalled = 0; + $test = $this; + + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + $backend->expects($this->once()) + ->method('setPassword'); + + /** + * @param \OC\User\User $user + * @param string $password + */ + $hook = function ($user, $password) use ($test, &$hooksCalled) { + $hooksCalled++; + $test->assertEquals('foo', $user->getUID()); + $test->assertEquals('bar', $password); + }; + + $emitter = new PublicEmitter(); + $emitter->listen('\OC\User', 'preSetPassword', $hook); + $emitter->listen('\OC\User', 'postSetPassword', $hook); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC\User\Backend::SET_PASSWORD) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend, $emitter); + + $user->setPassword('bar',''); + $this->assertEquals(2, $hooksCalled); + } + + public function testDeleteHooks() { + $hooksCalled = 0; + $test = $this; + + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + $backend->expects($this->once()) + ->method('deleteUser'); + + /** + * @param \OC\User\User $user + */ + $hook = function ($user) use ($test, &$hooksCalled) { + $hooksCalled++; + $test->assertEquals('foo', $user->getUID()); + }; + + $emitter = new PublicEmitter(); + $emitter->listen('\OC\User', 'preDelete', $hook); + $emitter->listen('\OC\User', 'postDelete', $hook); + + $user = new \OC\User\User('foo', $backend, $emitter); + $this->assertTrue($user->delete()); + $this->assertEquals(2, $hooksCalled); + } + + public function testGetCloudId() { + /** + * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\Test\Util\User\Dummy'); + $urlGenerator = $this->getMockBuilder('\OC\URLGenerator') + ->setMethods(['getAbsoluteURL']) + ->disableOriginalConstructor()->getMock(); + $urlGenerator + ->expects($this->any()) + ->method('getAbsoluteURL') + ->withAnyParameters() + ->willReturn('http://localhost:8888/owncloud'); + $user = new \OC\User\User('foo', $backend, null, null, $urlGenerator); + $this->assertEquals("foo@localhost:8888/owncloud", $user->getCloudId()); + } +} |