123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\User\Test\Unit\Model\ResourceModel;
- use Magento\Framework\Serialize\Serializer\Json;
- /**
- * Test class for \Magento\User\Model\ResourceModel\User testing
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class UserTest extends \PHPUnit\Framework\TestCase
- {
- /** @var \Magento\User\Model\ResourceModel\User */
- protected $model;
- /** @var \Magento\User\Model\User|\PHPUnit_Framework_MockObject_MockObject */
- protected $userMock;
- /** @var \Magento\Framework\Model\ResourceModel\Db\Context|\PHPUnit_Framework_MockObject_MockObject */
- protected $contextMock;
- /** @var \Magento\Authorization\Model\RoleFactory|\PHPUnit_Framework_MockObject_MockObject */
- protected $roleFactoryMock;
- /** @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject */
- protected $dateTimeMock;
- /** @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */
- protected $resourceMock;
- /** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $dbAdapterMock;
- /** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */
- protected $selectMock;
- /** @var \Magento\Authorization\Model\Role|\PHPUnit_Framework_MockObject_MockObject */
- protected $roleMock;
- /**
- * @var \Magento\Framework\Acl\Data\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $aclDataCacheMock;
- protected function setUp()
- {
- $this->userMock = $this->getMockBuilder(\Magento\User\Model\User::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->roleFactoryMock = $this->getMockBuilder(\Magento\Authorization\Model\RoleFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->roleMock = $this->getMockBuilder(\Magento\Authorization\Model\Role::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->dbAdapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->aclDataCacheMock = $this->getMockBuilder(\Magento\Framework\Acl\Data\CacheInterface::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->model = $helper->getObject(
- \Magento\User\Model\ResourceModel\User::class,
- [
- 'resource' => $this->resourceMock,
- 'roleFactory' => $this->roleFactoryMock,
- 'dateTime' => $this->dateTimeMock,
- 'aclDataCache' => $this->aclDataCacheMock,
- ]
- );
- }
- public function testIsUserUnique()
- {
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchRow')->willReturn([true]);
- $this->assertFalse($this->model->isUserUnique($this->userMock));
- }
- public function testRecordLogin()
- {
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('update');
- $this->assertInstanceOf(
- \Magento\User\Model\ResourceModel\User::class,
- $this->model->recordLogin($this->userMock)
- );
- }
- public function testLoadByUsername()
- {
- $returnData = [1, 2, 3];
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchRow')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->loadByUsername('user1'));
- }
- public function testHasAssigned2Role()
- {
- $returnData = [1, 2, 3];
- $uid = 1234;
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchAll')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->hasAssigned2Role($uid));
- $this->assertNull($this->model->hasAssigned2Role(0));
- }
- public function testHasAssigned2RolePassAnObject()
- {
- $methodUserMock = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
- ->disableOriginalConstructor()
- ->setMethods(['getUserId'])
- ->getMock();
- $returnData = [1, 2, 3];
- $uid = 1234;
- $methodUserMock->expects($this->once())->method('getUserId')->willReturn($uid);
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchAll')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->hasAssigned2Role($methodUserMock));
- }
- public function testClearUserRoles()
- {
- $uid = 123;
- $this->userMock->expects($this->once())->method('getId')->willReturn($uid);
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('delete');
- $this->model->_clearUserRoles($this->userMock);
- }
- public function testDeleteSuccess()
- {
- $uid = 123;
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('beginTransaction');
- $this->userMock->expects($this->once())->method('getId')->willReturn($uid);
- $this->dbAdapterMock->expects($this->atLeastOnce())->method('delete');
- $this->assertTrue($this->model->delete($this->userMock));
- }
- public function testGetRolesEmptyId()
- {
- $this->assertEquals([], $this->model->getRoles($this->userMock));
- }
- public function testGetRolesReturnFilledArray()
- {
- $uid = 123;
- $this->userMock->expects($this->atLeastOnce())->method('getId')->willReturn($uid);
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
- $this->selectMock->expects($this->once())->method('joinLeft')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchCol')->willReturn([1, 2, 3]);
- $this->assertEquals([1, 2, 3], $this->model->getRoles($this->userMock));
- }
- public function testGetRolesFetchRowFailure()
- {
- $uid = 123;
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->userMock->expects($this->atLeastOnce())->method('getId')->willReturn($uid);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
- $this->selectMock->expects($this->once())->method('joinLeft')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchCol')->willReturn(false);
- $this->assertEquals([], $this->model->getRoles($this->userMock));
- }
- public function testSaveExtraEmptyId()
- {
- $this->resourceMock->expects($this->never())->method('getConnection');
- $this->assertInstanceOf(
- \Magento\User\Model\ResourceModel\User::class,
- $this->model->saveExtra($this->userMock, [1, 2, 3])
- );
- }
- public function testSaveExtraFilledId()
- {
- $uid = 123;
- $this->userMock->expects($this->atLeastOnce())->method('getId')->willReturn($uid);
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('update');
- $this->assertInstanceOf(
- \Magento\User\Model\ResourceModel\User::class,
- $this->model->saveExtra($this->userMock, [1, 2, 3])
- );
- }
- public function testCountAll()
- {
- $returnData = 123;
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchOne')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->countAll());
- }
- public function testUpdateRoleUsersAclWithUsers()
- {
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->roleMock->expects($this->once())->method('getRoleUsers')->willReturn(['user1', 'user2']);
- $this->dbAdapterMock->expects($this->once())->method('update')->willReturn(1);
- $this->assertTrue($this->model->updateRoleUsersAcl($this->roleMock));
- }
- public function testUpdateRoleUsersAclNoUsers()
- {
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->roleMock->expects($this->once())->method('getRoleUsers')->willReturn([]);
- $this->dbAdapterMock->expects($this->never())->method('update');
- $this->assertFalse($this->model->updateRoleUsersAcl($this->roleMock));
- }
- public function testUpdateRoleUsersAclUpdateFail()
- {
- $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->roleMock->expects($this->once())->method('getRoleUsers')->willReturn(['user1', 'user2']);
- $this->dbAdapterMock->expects($this->once())->method('update')->willReturn(0);
- $this->assertFalse($this->model->updateRoleUsersAcl($this->roleMock));
- }
- public function testUnlock()
- {
- $inputData = [1, 2, 3];
- $returnData = 5;
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->unlock($inputData));
- }
- public function testUnlockWithInteger()
- {
- $inputData = 123;
- $returnData = 5;
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->unlock($inputData));
- }
- public function testLock()
- {
- $inputData = [1, 2, 3];
- $returnData = 5;
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->lock($inputData, 1, 1));
- }
- public function testLockWithInteger()
- {
- $inputData = 123;
- $returnData = 5;
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->lock($inputData, 1, 1));
- }
- public function testGetOldPassword()
- {
- $returnData = ['password1', 'password2'];
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('from')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('order')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->atLeastOnce())->method('fetchCol')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->getOldPasswords($this->userMock));
- }
- public function testDeleteFromRole()
- {
- $methodUserMock = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
- ->disableOriginalConstructor()
- ->setMethods(['getUserId', 'getRoleId'])
- ->getMock();
- $uid = 1234;
- $roleId = 44;
- $methodUserMock->expects($this->once())->method('getUserId')->willReturn($uid);
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $methodUserMock->expects($this->atLeastOnce())->method('getRoleId')->willReturn($roleId);
- $this->dbAdapterMock->expects($this->once())->method('delete');
- $this->assertInstanceOf(
- \Magento\User\Model\ResourceModel\User::class,
- $this->model->deleteFromRole($methodUserMock)
- );
- }
- public function testRoleUserExists()
- {
- $methodUserMock = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
- ->disableOriginalConstructor()
- ->setMethods(['getUserId', 'getRoleId'])
- ->getMock();
- $uid = 1234;
- $roleId = 44;
- $returnData = [1, 2, 3];
- $methodUserMock->expects($this->atLeastOnce())->method('getUserId')->willReturn($uid);
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $methodUserMock->expects($this->once())->method('getRoleId')->willReturn($roleId);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('from')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchCol')->willReturn($returnData);
- $this->assertEquals($returnData, $this->model->roleUserExists($methodUserMock));
- $this->assertEquals([], $this->model->roleUserExists($this->userMock));
- }
- public function testGetValidationBeforeSave()
- {
- $this->assertInstanceOf('\Zend_Validate_Callback', $this->model->getValidationRulesBeforeSave());
- }
- public function testUpdateFailure()
- {
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($this->selectMock);
- $this->dbAdapterMock->expects($this->once())->method('quoteInto')->willReturn($this->selectMock);
- $this->model->updateFailure($this->userMock, 1, 1);
- }
- public function testTrackPassword()
- {
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('insert')->willReturn($this->selectMock);
- $this->model->trackPassword($this->userMock, "myPas#w0rd", 1);
- }
- public function testGetLatestPassword()
- {
- $uid = 123;
- $returnData = ['password1', 'password2'];
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->dbAdapterMock->expects($this->once())->method('fetchRow')->willReturn($returnData);
- $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('from')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('order')->willReturn($this->selectMock);
- $this->selectMock->expects($this->atLeastOnce())->method('limit')->willReturn($this->selectMock);
- $this->assertEquals($returnData, $this->model->getLatestPassword($uid));
- }
- public function testInitUniqueFields()
- {
- $this->assertInstanceOf(
- \Magento\User\Model\ResourceModel\User::class,
- $this->invokeMethod($this->model, '_initUniqueFields', [])
- );
- }
- public function testAfterSave()
- {
- $roleId = 123;
- $methodUserMock = $this->getMockBuilder(\Magento\User\Model\User::class)
- ->disableOriginalConstructor()
- ->setMethods(['hasRoleId', 'getRoleId', 'getExtra', 'setExtra'])
- ->getMock();
- $methodUserMock->expects($this->once())->method('hasRoleId')->willReturn(true);
- $methodUserMock->expects($this->once())->method('getRoleId')->willReturn($roleId);
- $extraData = ['user', 'extra', 'data'];
- $serializerMock = $this->createPartialMock(Json::class, ['serialize', 'unserialize']);
- $serializerMock->expects($this->once())
- ->method('unserialize')
- ->with(json_encode($extraData))
- ->will($this->returnValue($extraData));
- $methodUserMock->expects($this->once())
- ->method('getExtra')
- ->will($this->returnValue(json_encode($extraData)));
- $methodUserMock->expects($this->once())
- ->method('setExtra')
- ->with($extraData);
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $objectManager->setBackwardCompatibleProperty($this->model, 'serializer', $serializerMock);
- $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
- $this->roleFactoryMock->expects($this->once())->method('create')->willReturn($this->roleMock);
- $this->roleMock->expects($this->once())->method('load')->willReturn($this->roleMock);
- $this->roleMock->expects($this->atLeastOnce())->method('getId')->willReturn($roleId);
- $this->dbAdapterMock->expects($this->once())->method('describeTable')->willReturn([1, 2, 3]);
- $this->aclDataCacheMock->expects($this->once())->method('clean');
- $this->assertInstanceOf(
- \Magento\User\Model\ResourceModel\User::class,
- $this->invokeMethod($this->model, '_afterSave', [$methodUserMock])
- );
- }
- public function testAfterLoad()
- {
- $methodUserMock = $this->getMockBuilder(\Magento\User\Model\User::class)
- ->disableOriginalConstructor()
- ->setMethods(['getExtra', 'setExtra'])
- ->getMock();
- $extraData = ['user', 'extra', 'data'];
- $serializerMock = $this->createPartialMock(Json::class, ['serialize', 'unserialize']);
- $serializerMock->expects($this->once())
- ->method('unserialize')
- ->with(json_encode($extraData))
- ->will($this->returnValue($extraData));
- $methodUserMock->expects($this->exactly(2))
- ->method('getExtra')
- ->will($this->returnValue(json_encode($extraData)));
- $methodUserMock->expects($this->once())
- ->method('setExtra')
- ->with($extraData);
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $objectManager->setBackwardCompatibleProperty($this->model, 'serializer', $serializerMock);
- $this->assertInstanceOf(
- \Magento\User\Model\ResourceModel\User::class,
- $this->invokeMethod($this->model, '_afterLoad', [$methodUserMock])
- );
- }
- public function testAfterLoadNoExtra()
- {
- $methodUserMock = $this->getMockBuilder(\Magento\User\Model\User::class)
- ->disableOriginalConstructor()
- ->setMethods(['getExtra', 'setExtra'])
- ->getMock();
- $extraData = null;
- $serializerMock = $this->createPartialMock(Json::class, ['serialize', 'unserialize']);
- $serializerMock->expects($this->never())
- ->method('unserialize');
- $methodUserMock->expects($this->exactly(1))
- ->method('getExtra')
- ->will($this->returnValue($extraData));
- $methodUserMock->expects($this->never())
- ->method('setExtra');
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $objectManager->setBackwardCompatibleProperty($this->model, 'serializer', $serializerMock);
- $this->assertInstanceOf(
- \Magento\User\Model\ResourceModel\User::class,
- $this->invokeMethod($this->model, '_afterLoad', [$methodUserMock])
- );
- }
- /**
- * Call protected/private method of a class.
- *
- * @param $object
- * @param $methodName
- * @param array $parameters
- * @return mixed
- */
- public function invokeMethod(&$object, $methodName, array $parameters = [])
- {
- $reflection = new \ReflectionClass(get_class($object));
- $method = $reflection->getMethod($methodName);
- $method->setAccessible(true);
- return $method->invokeArgs($object, $parameters);
- }
- }
|