PathConfigTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Test\Unit\Model;
  7. use Magento\Store\Model\StoreManagerInterface;
  8. use Magento\Store\Model\ScopeInterface;
  9. use Magento\Store\Model\Store;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. class PathConfigTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Framework\App\Config\ScopeConfigInterface | \PHPUnit_Framework_MockObject_MockObject*/
  14. private $scopeConfigMock;
  15. /** @var \Magento\Framework\Url\SecurityInfoInterface | \PHPUnit_Framework_MockObject_MockObject*/
  16. private $urlSecurityInfoMock;
  17. /** @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject*/
  18. private $storeManagerMock;
  19. /** @var Store | \PHPUnit_Framework_MockObject_MockObject*/
  20. private $storeMock;
  21. /** @var \Magento\Store\Model\RouteConfig */
  22. protected $model;
  23. protected function setUp()
  24. {
  25. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->urlSecurityInfoMock = $this->getMockBuilder(\Magento\Framework\Url\SecurityInfoInterface::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $mockArgs = [
  38. 'scopeConfig' => $this->scopeConfigMock,
  39. 'urlSecurityInfo' => $this->urlSecurityInfoMock,
  40. 'storeManager' => $this->storeManagerMock,
  41. ];
  42. $this->model = (new ObjectManager($this))->getObject(\Magento\Store\Model\PathConfig::class, $mockArgs);
  43. }
  44. public function testGetCurrentSecureUrlNoAlias()
  45. {
  46. $baseUrl = 'base-store.url/';
  47. $pathInfo = 'path/to/action';
  48. $this->storeMock->expects($this->once())->method('getBaseUrl')->with('link', true)->willReturn($baseUrl);
  49. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  50. $request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $request->expects($this->once())->method('getAlias')->willReturn(null);
  54. $request->expects($this->once())->method('getPathInfo')->willReturn($pathInfo);
  55. $this->assertSame($baseUrl . $pathInfo, $this->model->getCurrentSecureUrl($request));
  56. }
  57. public function testGetCurrentSecureUrlWithAlias()
  58. {
  59. $baseUrl = 'base-store.url/';
  60. $alias = 'action-alias';
  61. $this->storeMock->expects($this->once())->method('getBaseUrl')->with('link', true)->willReturn($baseUrl);
  62. $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
  63. $request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $request->expects($this->once())->method('getAlias')->willReturn($alias);
  67. $request->expects($this->never())->method('getPathInfo');
  68. $this->assertSame($baseUrl . $alias, $this->model->getCurrentSecureUrl($request));
  69. }
  70. /**
  71. * @dataProvider urlSchemeProvider
  72. * @param string $base Base Url
  73. * @param bool $secure Expected return value
  74. */
  75. public function testShouldBeSecureUnsecureBaseUrl($base, $secure)
  76. {
  77. $this->scopeConfigMock->expects($this->once())
  78. ->method('getValue')
  79. ->with(Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE)
  80. ->willReturn($base);
  81. $this->assertSame($secure, $this->model->shouldBeSecure('path/to/action'));
  82. }
  83. /**
  84. * @dataProvider urlSchemeProvider
  85. * @param string $base Base Url
  86. * @param bool $secure Expected return value
  87. */
  88. public function testShouldBeSecureSecureBaseUrl($base, $secure)
  89. {
  90. $path = 'path/to/action';
  91. $this->scopeConfigMock->expects($this->once())->method('isSetFlag')
  92. ->with(Store::XML_PATH_SECURE_IN_FRONTEND, ScopeInterface::SCOPE_STORE)
  93. ->willReturn($secure);
  94. $getValueReturnMap = [
  95. [Store::XML_PATH_SECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, $base],
  96. [Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, 'http://unsecure.url'],
  97. ];
  98. $this->scopeConfigMock->expects($this->any())
  99. ->method('getValue')
  100. ->will($this->returnValueMap($getValueReturnMap));
  101. if ($secure) {
  102. $this->urlSecurityInfoMock->expects($this->once())->method('isSecure')->with($path)->willReturn($secure);
  103. }
  104. $this->assertSame($secure, $this->model->shouldBeSecure($path));
  105. }
  106. /**
  107. * @return array
  108. */
  109. public function urlSchemeProvider()
  110. {
  111. return [
  112. ['https://base.url', true],
  113. ['http://base.url', false]
  114. ];
  115. }
  116. }