HstsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\HeaderProvider;
  7. use \Magento\Store\Model\HeaderProvider\Hsts;
  8. use \Magento\Store\Model\Store;
  9. use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. use \Magento\Framework\App\Config\ScopeConfigInterface;
  11. class HstsTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** Strict-Transport-Security (HSTS) Header name */
  14. const HEADER_NAME = 'Strict-Transport-Security';
  15. /**
  16. * Strict-Transport-Security (HSTS) header value
  17. */
  18. const HEADER_VALUE = 'max-age=31536000';
  19. /**
  20. * @var Hsts
  21. */
  22. protected $object;
  23. /**
  24. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $scopeConfigMock;
  27. protected function setUp()
  28. {
  29. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config::class)
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $objectManager = new ObjectManagerHelper($this);
  33. $this->object = $objectManager->getObject(
  34. \Magento\Store\Model\HeaderProvider\Hsts::class,
  35. ['scopeConfig' => $this->scopeConfigMock]
  36. );
  37. }
  38. public function testGetName()
  39. {
  40. $this->assertEquals($this::HEADER_NAME, $this->object->getName(), 'Wrong header name');
  41. }
  42. public function testGetValue()
  43. {
  44. $this->assertEquals($this::HEADER_VALUE, $this->object->getValue(), 'Wrong header value');
  45. }
  46. /**
  47. * @param [] $configValuesMap
  48. * @param bool $expected
  49. * @dataProvider canApplyDataProvider
  50. */
  51. public function testCanApply($configValuesMap, $expected)
  52. {
  53. $this->scopeConfigMock->expects($this->any())->method('isSetFlag')->will(
  54. $this->returnValueMap($configValuesMap)
  55. );
  56. $this->assertEquals($expected, $this->object->canApply(), 'Incorrect canApply result');
  57. }
  58. /**
  59. * Data provider for testCanApply test
  60. *
  61. * @return array
  62. */
  63. public function canApplyDataProvider()
  64. {
  65. return [
  66. [
  67. [
  68. [Store::XML_PATH_SECURE_IN_FRONTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true],
  69. [Store::XML_PATH_SECURE_IN_ADMINHTML, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true],
  70. [Store::XML_PATH_ENABLE_HSTS, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true]
  71. ],
  72. true
  73. ],
  74. [
  75. [
  76. [Store::XML_PATH_SECURE_IN_FRONTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false],
  77. [Store::XML_PATH_SECURE_IN_ADMINHTML, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true],
  78. [Store::XML_PATH_ENABLE_HSTS, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true]
  79. ],
  80. false
  81. ],
  82. [
  83. [
  84. [Store::XML_PATH_SECURE_IN_FRONTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true],
  85. [Store::XML_PATH_SECURE_IN_ADMINHTML, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false],
  86. [Store::XML_PATH_ENABLE_HSTS, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true]
  87. ],
  88. false
  89. ],
  90. [
  91. [
  92. [Store::XML_PATH_SECURE_IN_FRONTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true],
  93. [Store::XML_PATH_SECURE_IN_ADMINHTML, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true],
  94. [Store::XML_PATH_ENABLE_HSTS, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false]
  95. ],
  96. false
  97. ],
  98. [
  99. [
  100. [Store::XML_PATH_SECURE_IN_FRONTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false],
  101. [Store::XML_PATH_SECURE_IN_ADMINHTML, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true],
  102. [Store::XML_PATH_ENABLE_HSTS, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false]
  103. ],
  104. false
  105. ],
  106. [
  107. [
  108. [Store::XML_PATH_SECURE_IN_FRONTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true],
  109. [Store::XML_PATH_SECURE_IN_ADMINHTML, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false],
  110. [Store::XML_PATH_ENABLE_HSTS, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false]
  111. ],
  112. false
  113. ],
  114. [
  115. [
  116. [Store::XML_PATH_SECURE_IN_FRONTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false],
  117. [Store::XML_PATH_SECURE_IN_ADMINHTML, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false],
  118. [Store::XML_PATH_ENABLE_HSTS, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false]
  119. ],
  120. false
  121. ],
  122. [
  123. [
  124. [Store::XML_PATH_SECURE_IN_FRONTEND, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false],
  125. [Store::XML_PATH_SECURE_IN_ADMINHTML, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, false],
  126. [Store::XML_PATH_ENABLE_HSTS, ScopeConfigInterface::SCOPE_TYPE_DEFAULT , null, true]
  127. ],
  128. false
  129. ],
  130. ];
  131. }
  132. }