UserTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Test\Unit\Model\ResourceModel;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. /**
  9. * Test class for \Magento\User\Model\ResourceModel\User testing
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class UserTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** @var \Magento\User\Model\ResourceModel\User */
  16. protected $model;
  17. /** @var \Magento\User\Model\User|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $userMock;
  19. /** @var \Magento\Framework\Model\ResourceModel\Db\Context|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $contextMock;
  21. /** @var \Magento\Authorization\Model\RoleFactory|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $roleFactoryMock;
  23. /** @var \Magento\Framework\Stdlib\DateTime|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $dateTimeMock;
  25. /** @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $resourceMock;
  27. /** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $dbAdapterMock;
  29. /** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */
  30. protected $selectMock;
  31. /** @var \Magento\Authorization\Model\Role|\PHPUnit_Framework_MockObject_MockObject */
  32. protected $roleMock;
  33. /**
  34. * @var \Magento\Framework\Acl\Data\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $aclDataCacheMock;
  37. protected function setUp()
  38. {
  39. $this->userMock = $this->getMockBuilder(\Magento\User\Model\User::class)
  40. ->disableOriginalConstructor()
  41. ->setMethods([])
  42. ->getMock();
  43. $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods([])
  46. ->getMock();
  47. $this->roleFactoryMock = $this->getMockBuilder(\Magento\Authorization\Model\RoleFactory::class)
  48. ->disableOriginalConstructor()
  49. ->setMethods(['create'])
  50. ->getMock();
  51. $this->roleMock = $this->getMockBuilder(\Magento\Authorization\Model\Role::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods([])
  54. ->getMock();
  55. $this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods([])
  58. ->getMock();
  59. $this->selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods([])
  62. ->getMock();
  63. $this->dbAdapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
  64. ->disableOriginalConstructor()
  65. ->setMethods([])
  66. ->getMock();
  67. $this->aclDataCacheMock = $this->getMockBuilder(\Magento\Framework\Acl\Data\CacheInterface::class)
  68. ->disableOriginalConstructor()
  69. ->setMethods([])
  70. ->getMock();
  71. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  72. $this->model = $helper->getObject(
  73. \Magento\User\Model\ResourceModel\User::class,
  74. [
  75. 'resource' => $this->resourceMock,
  76. 'roleFactory' => $this->roleFactoryMock,
  77. 'dateTime' => $this->dateTimeMock,
  78. 'aclDataCache' => $this->aclDataCacheMock,
  79. ]
  80. );
  81. }
  82. public function testIsUserUnique()
  83. {
  84. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  85. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  86. $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
  87. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  88. $this->dbAdapterMock->expects($this->once())->method('fetchRow')->willReturn([true]);
  89. $this->assertFalse($this->model->isUserUnique($this->userMock));
  90. }
  91. public function testRecordLogin()
  92. {
  93. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  94. $this->dbAdapterMock->expects($this->once())->method('update');
  95. $this->assertInstanceOf(
  96. \Magento\User\Model\ResourceModel\User::class,
  97. $this->model->recordLogin($this->userMock)
  98. );
  99. }
  100. public function testLoadByUsername()
  101. {
  102. $returnData = [1, 2, 3];
  103. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  104. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  105. $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
  106. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  107. $this->dbAdapterMock->expects($this->once())->method('fetchRow')->willReturn($returnData);
  108. $this->assertEquals($returnData, $this->model->loadByUsername('user1'));
  109. }
  110. public function testHasAssigned2Role()
  111. {
  112. $returnData = [1, 2, 3];
  113. $uid = 1234;
  114. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  115. $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
  116. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  117. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  118. $this->dbAdapterMock->expects($this->once())->method('fetchAll')->willReturn($returnData);
  119. $this->assertEquals($returnData, $this->model->hasAssigned2Role($uid));
  120. $this->assertNull($this->model->hasAssigned2Role(0));
  121. }
  122. public function testHasAssigned2RolePassAnObject()
  123. {
  124. $methodUserMock = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
  125. ->disableOriginalConstructor()
  126. ->setMethods(['getUserId'])
  127. ->getMock();
  128. $returnData = [1, 2, 3];
  129. $uid = 1234;
  130. $methodUserMock->expects($this->once())->method('getUserId')->willReturn($uid);
  131. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  132. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  133. $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
  134. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  135. $this->dbAdapterMock->expects($this->once())->method('fetchAll')->willReturn($returnData);
  136. $this->assertEquals($returnData, $this->model->hasAssigned2Role($methodUserMock));
  137. }
  138. public function testClearUserRoles()
  139. {
  140. $uid = 123;
  141. $this->userMock->expects($this->once())->method('getId')->willReturn($uid);
  142. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  143. $this->dbAdapterMock->expects($this->once())->method('delete');
  144. $this->model->_clearUserRoles($this->userMock);
  145. }
  146. public function testDeleteSuccess()
  147. {
  148. $uid = 123;
  149. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  150. $this->dbAdapterMock->expects($this->once())->method('beginTransaction');
  151. $this->userMock->expects($this->once())->method('getId')->willReturn($uid);
  152. $this->dbAdapterMock->expects($this->atLeastOnce())->method('delete');
  153. $this->assertTrue($this->model->delete($this->userMock));
  154. }
  155. public function testGetRolesEmptyId()
  156. {
  157. $this->assertEquals([], $this->model->getRoles($this->userMock));
  158. }
  159. public function testGetRolesReturnFilledArray()
  160. {
  161. $uid = 123;
  162. $this->userMock->expects($this->atLeastOnce())->method('getId')->willReturn($uid);
  163. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  164. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  165. $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
  166. $this->selectMock->expects($this->once())->method('joinLeft')->willReturn($this->selectMock);
  167. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  168. $this->dbAdapterMock->expects($this->once())->method('fetchCol')->willReturn([1, 2, 3]);
  169. $this->assertEquals([1, 2, 3], $this->model->getRoles($this->userMock));
  170. }
  171. public function testGetRolesFetchRowFailure()
  172. {
  173. $uid = 123;
  174. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  175. $this->userMock->expects($this->atLeastOnce())->method('getId')->willReturn($uid);
  176. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  177. $this->selectMock->expects($this->once())->method('from')->willReturn($this->selectMock);
  178. $this->selectMock->expects($this->once())->method('joinLeft')->willReturn($this->selectMock);
  179. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  180. $this->dbAdapterMock->expects($this->once())->method('fetchCol')->willReturn(false);
  181. $this->assertEquals([], $this->model->getRoles($this->userMock));
  182. }
  183. public function testSaveExtraEmptyId()
  184. {
  185. $this->resourceMock->expects($this->never())->method('getConnection');
  186. $this->assertInstanceOf(
  187. \Magento\User\Model\ResourceModel\User::class,
  188. $this->model->saveExtra($this->userMock, [1, 2, 3])
  189. );
  190. }
  191. public function testSaveExtraFilledId()
  192. {
  193. $uid = 123;
  194. $this->userMock->expects($this->atLeastOnce())->method('getId')->willReturn($uid);
  195. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  196. $this->dbAdapterMock->expects($this->once())->method('update');
  197. $this->assertInstanceOf(
  198. \Magento\User\Model\ResourceModel\User::class,
  199. $this->model->saveExtra($this->userMock, [1, 2, 3])
  200. );
  201. }
  202. public function testCountAll()
  203. {
  204. $returnData = 123;
  205. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  206. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  207. $this->dbAdapterMock->expects($this->once())->method('fetchOne')->willReturn($returnData);
  208. $this->assertEquals($returnData, $this->model->countAll());
  209. }
  210. public function testUpdateRoleUsersAclWithUsers()
  211. {
  212. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  213. $this->roleMock->expects($this->once())->method('getRoleUsers')->willReturn(['user1', 'user2']);
  214. $this->dbAdapterMock->expects($this->once())->method('update')->willReturn(1);
  215. $this->assertTrue($this->model->updateRoleUsersAcl($this->roleMock));
  216. }
  217. public function testUpdateRoleUsersAclNoUsers()
  218. {
  219. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  220. $this->roleMock->expects($this->once())->method('getRoleUsers')->willReturn([]);
  221. $this->dbAdapterMock->expects($this->never())->method('update');
  222. $this->assertFalse($this->model->updateRoleUsersAcl($this->roleMock));
  223. }
  224. public function testUpdateRoleUsersAclUpdateFail()
  225. {
  226. $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock);
  227. $this->roleMock->expects($this->once())->method('getRoleUsers')->willReturn(['user1', 'user2']);
  228. $this->dbAdapterMock->expects($this->once())->method('update')->willReturn(0);
  229. $this->assertFalse($this->model->updateRoleUsersAcl($this->roleMock));
  230. }
  231. public function testUnlock()
  232. {
  233. $inputData = [1, 2, 3];
  234. $returnData = 5;
  235. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  236. $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($returnData);
  237. $this->assertEquals($returnData, $this->model->unlock($inputData));
  238. }
  239. public function testUnlockWithInteger()
  240. {
  241. $inputData = 123;
  242. $returnData = 5;
  243. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  244. $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($returnData);
  245. $this->assertEquals($returnData, $this->model->unlock($inputData));
  246. }
  247. public function testLock()
  248. {
  249. $inputData = [1, 2, 3];
  250. $returnData = 5;
  251. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  252. $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($returnData);
  253. $this->assertEquals($returnData, $this->model->lock($inputData, 1, 1));
  254. }
  255. public function testLockWithInteger()
  256. {
  257. $inputData = 123;
  258. $returnData = 5;
  259. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  260. $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($returnData);
  261. $this->assertEquals($returnData, $this->model->lock($inputData, 1, 1));
  262. }
  263. public function testGetOldPassword()
  264. {
  265. $returnData = ['password1', 'password2'];
  266. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  267. $this->dbAdapterMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock);
  268. $this->selectMock->expects($this->atLeastOnce())->method('from')->willReturn($this->selectMock);
  269. $this->selectMock->expects($this->atLeastOnce())->method('order')->willReturn($this->selectMock);
  270. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  271. $this->dbAdapterMock->expects($this->atLeastOnce())->method('fetchCol')->willReturn($returnData);
  272. $this->assertEquals($returnData, $this->model->getOldPasswords($this->userMock));
  273. }
  274. public function testDeleteFromRole()
  275. {
  276. $methodUserMock = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
  277. ->disableOriginalConstructor()
  278. ->setMethods(['getUserId', 'getRoleId'])
  279. ->getMock();
  280. $uid = 1234;
  281. $roleId = 44;
  282. $methodUserMock->expects($this->once())->method('getUserId')->willReturn($uid);
  283. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  284. $methodUserMock->expects($this->atLeastOnce())->method('getRoleId')->willReturn($roleId);
  285. $this->dbAdapterMock->expects($this->once())->method('delete');
  286. $this->assertInstanceOf(
  287. \Magento\User\Model\ResourceModel\User::class,
  288. $this->model->deleteFromRole($methodUserMock)
  289. );
  290. }
  291. public function testRoleUserExists()
  292. {
  293. $methodUserMock = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
  294. ->disableOriginalConstructor()
  295. ->setMethods(['getUserId', 'getRoleId'])
  296. ->getMock();
  297. $uid = 1234;
  298. $roleId = 44;
  299. $returnData = [1, 2, 3];
  300. $methodUserMock->expects($this->atLeastOnce())->method('getUserId')->willReturn($uid);
  301. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  302. $methodUserMock->expects($this->once())->method('getRoleId')->willReturn($roleId);
  303. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  304. $this->selectMock->expects($this->atLeastOnce())->method('from')->willReturn($this->selectMock);
  305. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  306. $this->dbAdapterMock->expects($this->once())->method('fetchCol')->willReturn($returnData);
  307. $this->assertEquals($returnData, $this->model->roleUserExists($methodUserMock));
  308. $this->assertEquals([], $this->model->roleUserExists($this->userMock));
  309. }
  310. public function testGetValidationBeforeSave()
  311. {
  312. $this->assertInstanceOf('\Zend_Validate_Callback', $this->model->getValidationRulesBeforeSave());
  313. }
  314. public function testUpdateFailure()
  315. {
  316. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  317. $this->dbAdapterMock->expects($this->once())->method('update')->willReturn($this->selectMock);
  318. $this->dbAdapterMock->expects($this->once())->method('quoteInto')->willReturn($this->selectMock);
  319. $this->model->updateFailure($this->userMock, 1, 1);
  320. }
  321. public function testTrackPassword()
  322. {
  323. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  324. $this->dbAdapterMock->expects($this->once())->method('insert')->willReturn($this->selectMock);
  325. $this->model->trackPassword($this->userMock, "myPas#w0rd", 1);
  326. }
  327. public function testGetLatestPassword()
  328. {
  329. $uid = 123;
  330. $returnData = ['password1', 'password2'];
  331. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  332. $this->dbAdapterMock->expects($this->once())->method('fetchRow')->willReturn($returnData);
  333. $this->dbAdapterMock->expects($this->once())->method('select')->willReturn($this->selectMock);
  334. $this->selectMock->expects($this->atLeastOnce())->method('from')->willReturn($this->selectMock);
  335. $this->selectMock->expects($this->atLeastOnce())->method('where')->willReturn($this->selectMock);
  336. $this->selectMock->expects($this->atLeastOnce())->method('order')->willReturn($this->selectMock);
  337. $this->selectMock->expects($this->atLeastOnce())->method('limit')->willReturn($this->selectMock);
  338. $this->assertEquals($returnData, $this->model->getLatestPassword($uid));
  339. }
  340. public function testInitUniqueFields()
  341. {
  342. $this->assertInstanceOf(
  343. \Magento\User\Model\ResourceModel\User::class,
  344. $this->invokeMethod($this->model, '_initUniqueFields', [])
  345. );
  346. }
  347. public function testAfterSave()
  348. {
  349. $roleId = 123;
  350. $methodUserMock = $this->getMockBuilder(\Magento\User\Model\User::class)
  351. ->disableOriginalConstructor()
  352. ->setMethods(['hasRoleId', 'getRoleId', 'getExtra', 'setExtra'])
  353. ->getMock();
  354. $methodUserMock->expects($this->once())->method('hasRoleId')->willReturn(true);
  355. $methodUserMock->expects($this->once())->method('getRoleId')->willReturn($roleId);
  356. $extraData = ['user', 'extra', 'data'];
  357. $serializerMock = $this->createPartialMock(Json::class, ['serialize', 'unserialize']);
  358. $serializerMock->expects($this->once())
  359. ->method('unserialize')
  360. ->with(json_encode($extraData))
  361. ->will($this->returnValue($extraData));
  362. $methodUserMock->expects($this->once())
  363. ->method('getExtra')
  364. ->will($this->returnValue(json_encode($extraData)));
  365. $methodUserMock->expects($this->once())
  366. ->method('setExtra')
  367. ->with($extraData);
  368. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  369. $objectManager->setBackwardCompatibleProperty($this->model, 'serializer', $serializerMock);
  370. $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
  371. $this->roleFactoryMock->expects($this->once())->method('create')->willReturn($this->roleMock);
  372. $this->roleMock->expects($this->once())->method('load')->willReturn($this->roleMock);
  373. $this->roleMock->expects($this->atLeastOnce())->method('getId')->willReturn($roleId);
  374. $this->dbAdapterMock->expects($this->once())->method('describeTable')->willReturn([1, 2, 3]);
  375. $this->aclDataCacheMock->expects($this->once())->method('clean');
  376. $this->assertInstanceOf(
  377. \Magento\User\Model\ResourceModel\User::class,
  378. $this->invokeMethod($this->model, '_afterSave', [$methodUserMock])
  379. );
  380. }
  381. public function testAfterLoad()
  382. {
  383. $methodUserMock = $this->getMockBuilder(\Magento\User\Model\User::class)
  384. ->disableOriginalConstructor()
  385. ->setMethods(['getExtra', 'setExtra'])
  386. ->getMock();
  387. $extraData = ['user', 'extra', 'data'];
  388. $serializerMock = $this->createPartialMock(Json::class, ['serialize', 'unserialize']);
  389. $serializerMock->expects($this->once())
  390. ->method('unserialize')
  391. ->with(json_encode($extraData))
  392. ->will($this->returnValue($extraData));
  393. $methodUserMock->expects($this->exactly(2))
  394. ->method('getExtra')
  395. ->will($this->returnValue(json_encode($extraData)));
  396. $methodUserMock->expects($this->once())
  397. ->method('setExtra')
  398. ->with($extraData);
  399. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  400. $objectManager->setBackwardCompatibleProperty($this->model, 'serializer', $serializerMock);
  401. $this->assertInstanceOf(
  402. \Magento\User\Model\ResourceModel\User::class,
  403. $this->invokeMethod($this->model, '_afterLoad', [$methodUserMock])
  404. );
  405. }
  406. public function testAfterLoadNoExtra()
  407. {
  408. $methodUserMock = $this->getMockBuilder(\Magento\User\Model\User::class)
  409. ->disableOriginalConstructor()
  410. ->setMethods(['getExtra', 'setExtra'])
  411. ->getMock();
  412. $extraData = null;
  413. $serializerMock = $this->createPartialMock(Json::class, ['serialize', 'unserialize']);
  414. $serializerMock->expects($this->never())
  415. ->method('unserialize');
  416. $methodUserMock->expects($this->exactly(1))
  417. ->method('getExtra')
  418. ->will($this->returnValue($extraData));
  419. $methodUserMock->expects($this->never())
  420. ->method('setExtra');
  421. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  422. $objectManager->setBackwardCompatibleProperty($this->model, 'serializer', $serializerMock);
  423. $this->assertInstanceOf(
  424. \Magento\User\Model\ResourceModel\User::class,
  425. $this->invokeMethod($this->model, '_afterLoad', [$methodUserMock])
  426. );
  427. }
  428. /**
  429. * Call protected/private method of a class.
  430. *
  431. * @param $object
  432. * @param $methodName
  433. * @param array $parameters
  434. * @return mixed
  435. */
  436. public function invokeMethod(&$object, $methodName, array $parameters = [])
  437. {
  438. $reflection = new \ReflectionClass(get_class($object));
  439. $method = $reflection->getMethod($methodName);
  440. $method->setAccessible(true);
  441. return $method->invokeArgs($object, $parameters);
  442. }
  443. }