FrontNameResolverTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\App\Area;
  7. use Magento\Backend\App\Area\FrontNameResolver;
  8. use Magento\Backend\Setup\ConfigOptionsList;
  9. use Magento\Framework\App\DeploymentConfig;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Store\Model\Store;
  12. class FrontNameResolverTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Backend\App\Area\FrontNameResolver
  16. */
  17. protected $model;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Backend\App\Config
  20. */
  21. protected $configMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Config\ScopeConfigInterface
  24. */
  25. protected $scopeConfigMock;
  26. /**
  27. * @var string
  28. */
  29. protected $_defaultFrontName = 'defaultFrontName';
  30. protected function setUp()
  31. {
  32. /** @var \PHPUnit_Framework_MockObject_MockObject|DeploymentConfig $deploymentConfigMock */
  33. $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  34. $deploymentConfigMock->expects($this->once())
  35. ->method('get')
  36. ->with(ConfigOptionsList::CONFIG_PATH_BACKEND_FRONTNAME)
  37. ->will($this->returnValue($this->_defaultFrontName));
  38. $this->configMock = $this->createMock(\Magento\Backend\App\Config::class);
  39. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  40. $this->model = new FrontNameResolver($this->configMock, $deploymentConfigMock, $this->scopeConfigMock);
  41. }
  42. public function testIfCustomPathUsed()
  43. {
  44. $this->configMock->expects(
  45. $this->at(0)
  46. )->method(
  47. 'getValue'
  48. )->with(
  49. 'admin/url/use_custom_path'
  50. )->will(
  51. $this->returnValue(true)
  52. );
  53. $this->configMock->expects(
  54. $this->at(1)
  55. )->method(
  56. 'getValue'
  57. )->with(
  58. 'admin/url/custom_path'
  59. )->will(
  60. $this->returnValue('expectedValue')
  61. );
  62. $this->assertEquals('expectedValue', $this->model->getFrontName());
  63. }
  64. public function testIfCustomPathNotUsed()
  65. {
  66. $this->configMock->expects(
  67. $this->once()
  68. )->method(
  69. 'getValue'
  70. )->with(
  71. 'admin/url/use_custom_path'
  72. )->will(
  73. $this->returnValue(false)
  74. );
  75. $this->assertEquals($this->_defaultFrontName, $this->model->getFrontName());
  76. }
  77. /**
  78. * @param string $url
  79. * @param string $host
  80. * @param string $useCustomAdminUrl
  81. * @param string $customAdminUrl
  82. * @param string $expectedValue
  83. * @dataProvider hostsDataProvider
  84. */
  85. public function testIsHostBackend($url, $host, $useCustomAdminUrl, $customAdminUrl, $expectedValue)
  86. {
  87. $_SERVER['HTTP_HOST'] = $host;
  88. $this->scopeConfigMock->expects($this->exactly(2))
  89. ->method('getValue')
  90. ->will(
  91. $this->returnValueMap(
  92. [
  93. [Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, $url],
  94. [
  95. FrontNameResolver::XML_PATH_USE_CUSTOM_ADMIN_URL,
  96. ScopeInterface::SCOPE_STORE,
  97. null,
  98. $useCustomAdminUrl
  99. ],
  100. [
  101. FrontNameResolver::XML_PATH_CUSTOM_ADMIN_URL,
  102. ScopeInterface::SCOPE_STORE,
  103. null,
  104. $customAdminUrl
  105. ],
  106. ]
  107. )
  108. );
  109. $this->assertEquals($this->model->isHostBackend(), $expectedValue);
  110. }
  111. /**
  112. * @return array
  113. */
  114. public function hostsDataProvider()
  115. {
  116. return [
  117. 'withoutPort' => [
  118. 'url' => 'http://magento2.loc/',
  119. 'host' => 'magento2.loc',
  120. 'useCustomAdminUrl' => '0',
  121. 'customAdminUrl' => '',
  122. 'expectedValue' => true
  123. ],
  124. 'withPort' => [
  125. 'url' => 'http://magento2.loc:8080/',
  126. 'host' => 'magento2.loc:8080',
  127. 'useCustomAdminUrl' => '0',
  128. 'customAdminUrl' => '',
  129. 'expectedValue' => true
  130. ],
  131. 'withStandartPortInUrlWithoutPortInHost' => [
  132. 'url' => 'http://magento2.loc:80/',
  133. 'host' => 'magento2.loc',
  134. 'useCustomAdminUrl' => '0',
  135. 'customAdminUrl' => '',
  136. 'expectedValue' => true
  137. ],
  138. 'withoutStandartPortInUrlWithPortInHost' => [
  139. 'url' => 'https://magento2.loc/',
  140. 'host' => 'magento2.loc:443',
  141. 'useCustomAdminUrl' => '0',
  142. 'customAdminUrl' => '',
  143. 'expectedValue' => true
  144. ],
  145. 'differentHosts' => [
  146. 'url' => 'http://m2.loc/',
  147. 'host' => 'magento2.loc',
  148. 'useCustomAdminUrl' => '0',
  149. 'customAdminUrl' => '',
  150. 'expectedValue' => false
  151. ],
  152. 'differentPortsOnOneHost' => [
  153. 'url' => 'http://magento2.loc/',
  154. 'host' => 'magento2.loc:8080',
  155. 'useCustomAdminUrl' => '0',
  156. 'customAdminUrl' => '',
  157. 'expectedValue' => false
  158. ],
  159. 'withCustomAdminUrl' => [
  160. 'url' => 'http://magento2.loc/',
  161. 'host' => 'myhost.loc',
  162. 'useCustomAdminUrl' => '1',
  163. 'customAdminUrl' => 'https://myhost.loc/',
  164. 'expectedValue' => true
  165. ],
  166. 'withCustomAdminUrlWrongHost' => [
  167. 'url' => 'http://magento2.loc/',
  168. 'host' => 'SomeOtherHost.loc',
  169. 'useCustomAdminUrl' => '1',
  170. 'customAdminUrl' => 'https://myhost.loc/',
  171. 'expectedValue' => false
  172. ]
  173. ];
  174. }
  175. }