123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- /*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace FOS\UserBundle\Tests\Util;
- use FOS\UserBundle\Util\UserManipulator;
- use FOS\UserBundle\Tests\TestUser;
- class UserManipulatorTest extends \PHPUnit_Framework_TestCase
- {
- public function testCreate()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $user = new TestUser();
- $username = 'test_username';
- $password = 'test_password';
- $email = 'test@email.org';
- $active = true; // it is enabled
- $superadmin = false;
- $userManagerMock->expects($this->once())
- ->method('createUser')
- ->will($this->returnValue($user));
- $userManagerMock->expects($this->once())
- ->method('updateUser')
- ->will($this->returnValue($user))
- ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->create($username, $password, $email, $active, $superadmin);
- $this->assertEquals($username, $user->getUsername());
- $this->assertEquals($password, $user->getPlainPassword());
- $this->assertEquals($email, $user->getEmail());
- $this->assertEquals($active, $user->isEnabled());
- $this->assertEquals($superadmin, $user->isSuperAdmin());
- }
- public function testActivateWithValidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $username = 'test_username';
- $user = new TestUser();
- $user->setUsername($username);
- $user->setEnabled(false);
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue($user))
- ->with($this->equalTo($username));
- $userManagerMock->expects($this->once())
- ->method('updateUser')
- ->will($this->returnValue($user))
- ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->activate($username);
- $this->assertEquals($username, $user->getUsername());
- $this->assertEquals(true, $user->isEnabled());
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testActivateWithInvalidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $invalidusername = 'invalid_username';
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue(null))
- ->with($this->equalTo($invalidusername));
- $userManagerMock->expects($this->never())
- ->method('updateUser');
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->activate($invalidusername);
- }
- public function testDeactivateWithValidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $username = 'test_username';
- $user = new TestUser();
- $user->setUsername($username);
- $user->setEnabled(true);
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue($user))
- ->with($this->equalTo($username));
- $userManagerMock->expects($this->once())
- ->method('updateUser')
- ->will($this->returnValue($user))
- ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->deactivate($username);
- $this->assertEquals($username, $user->getUsername());
- $this->assertEquals(false, $user->isEnabled());
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testDeactivateWithInvalidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $invalidusername = 'invalid_username';
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue(null))
- ->with($this->equalTo($invalidusername));
- $userManagerMock->expects($this->never())
- ->method('updateUser');
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->deactivate($invalidusername);
- }
- public function testPromoteWithValidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $username = 'test_username';
- $user = new TestUser();
- $user->setUsername($username);
- $user->setSuperAdmin(false);
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue($user))
- ->with($this->equalTo($username));
- $userManagerMock->expects($this->once())
- ->method('updateUser')
- ->will($this->returnValue($user))
- ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->promote($username);
- $this->assertEquals($username, $user->getUsername());
- $this->assertEquals(true, $user->isSuperAdmin());
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testPromoteWithInvalidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $invalidusername = 'invalid_username';
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue(null))
- ->with($this->equalTo($invalidusername));
- $userManagerMock->expects($this->never())
- ->method('updateUser');
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->promote($invalidusername);
- }
- public function testDemoteWithValidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $username = 'test_username';
- $user = new TestUser();
- $user->setUsername($username);
- $user->setSuperAdmin(true);
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue($user))
- ->with($this->equalTo($username));
- $userManagerMock->expects($this->once())
- ->method('updateUser')
- ->will($this->returnValue($user))
- ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->demote($username);
- $this->assertEquals($username, $user->getUsername());
- $this->assertEquals(false, $user->isSuperAdmin());
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testDemoteWithInvalidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $invalidusername = 'invalid_username';
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue(null))
- ->with($this->equalTo($invalidusername));
- $userManagerMock->expects($this->never())
- ->method('updateUser');
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->demote($invalidusername);
- }
- public function testChangePasswordWithValidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $user = new TestUser();
- $username = 'test_username';
- $password = 'test_password';
- $oldpassword = 'old_password';
- $user->setUsername($username);
- $user->setPlainPassword($oldpassword);
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue($user))
- ->with($this->equalTo($username));
- $userManagerMock->expects($this->once())
- ->method('updateUser')
- ->will($this->returnValue($user))
- ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->changePassword($username, $password);
- $this->assertEquals($username, $user->getUsername());
- $this->assertEquals($password, $user->getPlainPassword());
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testChangePasswordWithInvalidUsername()
- {
- $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
- $invalidusername = 'invalid_username';
- $password = 'test_password';
- $userManagerMock->expects($this->once())
- ->method('findUserByUsername')
- ->will($this->returnValue(null))
- ->with($this->equalTo($invalidusername));
- $userManagerMock->expects($this->never())
- ->method('updateUser');
- $manipulator = new UserManipulator($userManagerMock);
- $manipulator->changePassword($invalidusername, $password);
- }
- }
|