AdminPathConfigTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Model;
  7. use Magento\Backend\Model\AdminPathConfig;
  8. use Magento\Store\Model\Store;
  9. class AdminPathConfigTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. protected $coreConfig;
  15. /**
  16. * @var \Magento\Backend\App\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $backendConfig;
  19. /**
  20. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $url;
  23. /**
  24. * @var AdminPathConfig
  25. */
  26. protected $adminPathConfig;
  27. protected function setUp()
  28. {
  29. $this->coreConfig = $this->getMockForAbstractClass(
  30. \Magento\Framework\App\Config\ScopeConfigInterface::class,
  31. [],
  32. '',
  33. false
  34. );
  35. $this->backendConfig = $this->getMockForAbstractClass(
  36. \Magento\Backend\App\ConfigInterface::class,
  37. [],
  38. '',
  39. false
  40. );
  41. $this->url = $this->getMockForAbstractClass(
  42. \Magento\Framework\UrlInterface::class,
  43. [],
  44. '',
  45. false,
  46. true,
  47. true,
  48. ['getBaseUrl']
  49. );
  50. $this->adminPathConfig = new AdminPathConfig($this->coreConfig, $this->backendConfig, $this->url);
  51. }
  52. public function testGetCurrentSecureUrl()
  53. {
  54. $request = $this->getMockForAbstractClass(
  55. \Magento\Framework\App\RequestInterface::class,
  56. [],
  57. '',
  58. false,
  59. true,
  60. true,
  61. ['getPathInfo']
  62. );
  63. $request->expects($this->once())->method('getPathInfo')->willReturn('/info');
  64. $this->url->expects($this->once())->method('getBaseUrl')->with('link', true)->willReturn('localhost/');
  65. $this->assertEquals('localhost/info', $this->adminPathConfig->getCurrentSecureUrl($request));
  66. }
  67. /**
  68. * @param $unsecureBaseUrl
  69. * @param $useSecureInAdmin
  70. * @param $secureBaseUrl
  71. * @param $useCustomUrl
  72. * @param $customUrl
  73. * @param $expected
  74. * @dataProvider shouldBeSecureDataProvider
  75. */
  76. public function testShouldBeSecure(
  77. $unsecureBaseUrl,
  78. $useSecureInAdmin,
  79. $secureBaseUrl,
  80. $useCustomUrl,
  81. $customUrl,
  82. $expected
  83. ) {
  84. $coreConfigValueMap = $this->returnValueMap([
  85. [\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL, 'default', null, $unsecureBaseUrl],
  86. [\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL, 'default', null, $secureBaseUrl],
  87. ['admin/url/custom', 'default', null, $customUrl],
  88. ]);
  89. $backendConfigFlagsMap = $this->returnValueMap([
  90. [\Magento\Store\Model\Store::XML_PATH_SECURE_IN_ADMINHTML, $useSecureInAdmin],
  91. ['admin/url/use_custom', $useCustomUrl],
  92. ]);
  93. $this->coreConfig->expects($this->atLeast(1))->method('getValue')
  94. ->will($coreConfigValueMap);
  95. $this->coreConfig->expects($this->atMost(2))->method('getValue')
  96. ->will($coreConfigValueMap);
  97. $this->backendConfig->expects($this->atMost(2))->method('isSetFlag')
  98. ->will($backendConfigFlagsMap);
  99. $this->assertEquals($expected, $this->adminPathConfig->shouldBeSecure(''));
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function shouldBeSecureDataProvider()
  105. {
  106. return [
  107. ['http://localhost/', false, 'default', false, '', false],
  108. ['http://localhost/', true, 'default', false, '', false],
  109. ['https://localhost/', false, 'default', false, '', true],
  110. ['https://localhost/', true, 'default', false, '', true],
  111. ['http://localhost/', false, 'https://localhost/', false, '', false],
  112. ['http://localhost/', true, 'https://localhost/', false, '', true],
  113. ['https://localhost/', true, 'https://localhost/', false, '', true],
  114. ];
  115. }
  116. public function testGetDefaultPath()
  117. {
  118. $this->backendConfig->expects($this->once())
  119. ->method('getValue')
  120. ->with('web/default/admin')
  121. ->willReturn('default/path');
  122. $this->assertEquals('default/path', $this->adminPathConfig->getDefaultPath());
  123. }
  124. }