RegistryTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Framework\Acl\Test\Unit\Role;
  8. use \Magento\Framework\Acl\Role\Registry;
  9. class RegistryTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var Registry
  13. */
  14. protected $model;
  15. protected function setUp()
  16. {
  17. $this->model = new Registry();
  18. }
  19. /**
  20. * @param $roleId
  21. * @param $parentRoleId
  22. * @return array
  23. * @throws \Zend_Acl_Role_Registry_Exception
  24. */
  25. protected function initRoles($roleId, $parentRoleId)
  26. {
  27. $parentRole = $this->createMock(\Zend_Acl_Role_Interface::class);
  28. $parentRole->expects($this->any())->method('getRoleId')->will($this->returnValue($parentRoleId));
  29. $role = $this->createMock(\Zend_Acl_Role_Interface::class);
  30. $role->expects($this->any())->method('getRoleId')->will($this->returnValue($roleId));
  31. $this->model->add($role);
  32. $this->model->add($parentRole);
  33. return [$role, $parentRole];
  34. }
  35. public function testAddParent()
  36. {
  37. $roleId = 1;
  38. $parentRoleId = 2;
  39. list($role, $parentRole) = $this->initRoles($roleId, $parentRoleId);
  40. $this->assertEmpty($this->model->getParents($roleId));
  41. $this->model->addParent($role, $parentRole);
  42. $this->model->getParents($roleId);
  43. $this->assertEquals([$parentRoleId => $parentRole], $this->model->getParents($roleId));
  44. }
  45. public function testAddParentByIds()
  46. {
  47. $roleId = 14;
  48. $parentRoleId = 25;
  49. list(, $parentRole) = $this->initRoles($roleId, $parentRoleId);
  50. $this->assertEmpty($this->model->getParents($roleId));
  51. $this->model->addParent($roleId, $parentRoleId);
  52. $this->model->getParents($roleId);
  53. $this->assertEquals([$parentRoleId => $parentRole], $this->model->getParents($roleId));
  54. }
  55. /**
  56. * @expectedException \Zend_Acl_Role_Registry_Exception
  57. * @expectedExceptionMessage Child Role id '20' does not exist
  58. */
  59. public function testAddParentWrongChildId()
  60. {
  61. $roleId = 1;
  62. $parentRoleId = 2;
  63. list(, $parentRole) = $this->initRoles($roleId, $parentRoleId);
  64. $this->model->addParent(20, $parentRole);
  65. }
  66. /**
  67. * @expectedException \Zend_Acl_Role_Registry_Exception
  68. * @expectedExceptionMessage Parent Role id '26' does not exist
  69. */
  70. public function testAddParentWrongParentId()
  71. {
  72. $roleId = 1;
  73. $parentRoleId = 2;
  74. list($role,) = $this->initRoles($roleId, $parentRoleId);
  75. $this->model->addParent($role, 26);
  76. }
  77. }