HhvmCompatibilityTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Hhvm ini_get/ini_set compatibility test
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. *
  8. */
  9. namespace Magento\Test\Integrity;
  10. use Magento\Framework\App\Utility\Files;
  11. class HhvmCompatibilityTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $allowedDirectives = [
  17. 'session.cookie_secure',
  18. 'session.cookie_httponly',
  19. 'session.use_cookies',
  20. 'session.use_only_cookies',
  21. 'session.referer_check',
  22. 'session.save_path',
  23. 'session.save_handler',
  24. 'session.cookie_lifetime',
  25. 'session.cookie_secure',
  26. 'date.timezone',
  27. 'memory_limit',
  28. 'max_execution_time',
  29. 'short_open_tag',
  30. 'disable_functions',
  31. 'asp_tags',
  32. 'apc.enabled',
  33. 'eaccelerator.enable',
  34. 'mime_magic.magicfile',
  35. 'display_errors',
  36. 'default_socket_timeout',
  37. 'pcre.recursion_limit',
  38. 'default_charset',
  39. /*
  40. There is not way to specify calculation/serialization precision in hhvm.
  41. Adding to whitelist in order to align precisions in php.
  42. */
  43. 'precision',
  44. 'serialize_precision',
  45. ];
  46. /**
  47. * Whitelist of variables allowed in files.
  48. *
  49. * @var array
  50. */
  51. private $whitelistVarsInFiles = [
  52. 'max_input_vars' => [
  53. 'integration/testsuite/Magento/Swatches/Controller/Adminhtml/Product/AttributeTest.php',
  54. 'integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/AttributeTest.php',
  55. ]
  56. ];
  57. /**
  58. * Test allowed directives.
  59. *
  60. * @SuppressWarnings(PHPMD.NPathComplexity)
  61. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  62. */
  63. public function testAllowedIniGetSetDirectives()
  64. {
  65. $deniedDirectives = [];
  66. foreach ($this->getFiles() as $file) {
  67. $fileDirectives = $this->parseDirectives($file);
  68. if ($fileDirectives) {
  69. $fileDeniedDirectives = array_diff($fileDirectives, $this->allowedDirectives);
  70. if ($fileDeniedDirectives) {
  71. $deniedDirectivesInFile = array_unique($fileDeniedDirectives);
  72. foreach ($deniedDirectivesInFile as $key => $deniedDirective) {
  73. if (isset($this->whitelistVarsInFiles[$deniedDirective])) {
  74. foreach ($this->whitelistVarsInFiles[$deniedDirective] as $whitelistFile) {
  75. if (strpos($file, $whitelistFile) !== false) {
  76. unset($deniedDirectivesInFile[$key]);
  77. }
  78. }
  79. }
  80. }
  81. if ($deniedDirectivesInFile) {
  82. $deniedDirectives[$file] = $deniedDirectivesInFile;
  83. }
  84. }
  85. }
  86. }
  87. if ($deniedDirectives) {
  88. $this->fail($this->createMessage($deniedDirectives));
  89. }
  90. }
  91. /**
  92. * @return array
  93. */
  94. protected function getFiles()
  95. {
  96. return \array_merge(
  97. Files::init()->getPhpFiles(
  98. Files::INCLUDE_APP_CODE
  99. | Files::INCLUDE_PUB_CODE
  100. | Files::INCLUDE_LIBS
  101. | Files::INCLUDE_TEMPLATES
  102. | Files::INCLUDE_TESTS
  103. | Files::INCLUDE_NON_CLASSES
  104. ),
  105. Files::init()->getPhtmlFiles(false, false),
  106. Files::init()->getFiles([BP . '/dev/'], '*.php')
  107. );
  108. }
  109. /**
  110. * @param string $file
  111. * @return null|array
  112. */
  113. protected function parseDirectives($file)
  114. {
  115. $content = file_get_contents($file);
  116. $pattern = '/ini_[g|s]et\(\s*[\'|"]([\w\._]+?)[\'|"][\s\w,\'"]*\)/';
  117. preg_match_all($pattern, $content, $matches);
  118. return $matches ? $matches[1] : null;
  119. }
  120. /**
  121. * @param array $deniedDirectives
  122. * @return string
  123. */
  124. protected function createMessage($deniedDirectives)
  125. {
  126. $message = 'HHVM-incompatible ini_get/ini_set options were found:';
  127. foreach ($deniedDirectives as $file => $fileDeniedDirectives) {
  128. $message .= "\n" . $file . ': [' . implode(', ', $fileDeniedDirectives) . ']';
  129. }
  130. return $message;
  131. }
  132. }