AbstractBackendController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework\TestCase;
  7. /**
  8. * A parent class for backend controllers - contains directives for admin user creation and authentication.
  9. *
  10. * @SuppressWarnings(PHPMD.NumberOfChildren)
  11. */
  12. abstract class AbstractBackendController extends \Magento\TestFramework\TestCase\AbstractController
  13. {
  14. /**
  15. * @var \Magento\Backend\Model\Auth\Session
  16. */
  17. protected $_session;
  18. /**
  19. * @var \Magento\Backend\Model\Auth
  20. */
  21. protected $_auth;
  22. /**
  23. * The resource used to authorize action
  24. *
  25. * @var string
  26. */
  27. protected $resource = null;
  28. /**
  29. * The uri at which to access the controller
  30. *
  31. * @var string
  32. */
  33. protected $uri = null;
  34. /**
  35. * @var string|null
  36. */
  37. protected $httpMethod;
  38. /**
  39. * @inheritDoc
  40. *
  41. * @throws \Magento\Framework\Exception\AuthenticationException
  42. */
  43. protected function setUp()
  44. {
  45. parent::setUp();
  46. $this->_objectManager->get(\Magento\Backend\Model\UrlInterface::class)->turnOffSecretKey();
  47. $this->_auth = $this->_objectManager->get(\Magento\Backend\Model\Auth::class);
  48. $this->_session = $this->_auth->getAuthStorage();
  49. $credentials = $this->_getAdminCredentials();
  50. $this->_auth->login($credentials['user'], $credentials['password']);
  51. $this->_objectManager->get(\Magento\Security\Model\Plugin\Auth::class)->afterLogin($this->_auth);
  52. }
  53. /**
  54. * Get credentials to login admin user
  55. *
  56. * @return array
  57. */
  58. protected function _getAdminCredentials()
  59. {
  60. return [
  61. 'user' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  62. 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  63. ];
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. protected function tearDown()
  69. {
  70. $this->_auth->getAuthStorage()->destroy(['send_expire_cookie' => false]);
  71. $this->_auth = null;
  72. $this->_session = null;
  73. $this->_objectManager->get(\Magento\Backend\Model\UrlInterface::class)->turnOnSecretKey();
  74. parent::tearDown();
  75. }
  76. /**
  77. * Utilize backend session model by default
  78. *
  79. * @param \PHPUnit\Framework\Constraint\Constraint $constraint
  80. * @param string|null $messageType
  81. * @param string $messageManagerClass
  82. */
  83. public function assertSessionMessages(
  84. \PHPUnit\Framework\Constraint\Constraint $constraint,
  85. $messageType = null,
  86. $messageManagerClass = \Magento\Framework\Message\Manager::class
  87. ) {
  88. parent::assertSessionMessages($constraint, $messageType, $messageManagerClass);
  89. }
  90. /**
  91. * Test ACL configuration for action working.
  92. */
  93. public function testAclHasAccess()
  94. {
  95. if ($this->uri === null) {
  96. $this->markTestIncomplete('AclHasAccess test is not complete');
  97. }
  98. if ($this->httpMethod) {
  99. $this->getRequest()->setMethod($this->httpMethod);
  100. }
  101. $this->dispatch($this->uri);
  102. $this->assertNotSame(403, $this->getResponse()->getHttpResponseCode());
  103. $this->assertNotSame(404, $this->getResponse()->getHttpResponseCode());
  104. }
  105. /**
  106. * Test ACL actually denying access.
  107. */
  108. public function testAclNoAccess()
  109. {
  110. if ($this->resource === null || $this->uri === null) {
  111. $this->markTestIncomplete('Acl test is not complete');
  112. }
  113. if ($this->httpMethod) {
  114. $this->getRequest()->setMethod($this->httpMethod);
  115. }
  116. $this->_objectManager->get(\Magento\Framework\Acl\Builder::class)
  117. ->getAcl()
  118. ->deny(null, $this->resource);
  119. $this->dispatch($this->uri);
  120. $this->assertSame(403, $this->getResponse()->getHttpResponseCode());
  121. }
  122. }