Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigSanitizerUtil.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
22  public static function sanitizeWebDriverConfig($config, $params = ['url', 'selenium'])
23  {
24  self::validateConfigBasedVars($config);
25 
26  if (in_array('url', $params)) {
27  $config['url'] = self::sanitizeUrl($config['url']);
28  }
29 
30  if (in_array('selenium', $params)) {
31  $config = self::sanitizeSeleniumEnvs($config);
32  }
33 
34  return $config;
35  }
36 
42  private static function sanitizeSeleniumEnvs($config)
43  {
44  if ($config['protocol'] == '%SELENIUM_PROTOCOL%') {
45  $config['protocol'] = "http";
46  }
47  if ($config['host'] == '%SELENIUM_HOST%') {
48  $config['host'] = "127.0.0.1";
49  }
50  if ($config['port'] == '%SELENIUM_PORT%') {
51  $config['port'] = "4444";
52  }
53  if ($config['path'] == '%SELENIUM_PATH%') {
54  $config['path'] = "/wd/hub";
55  }
56  return $config;
57  }
58 
66  private static function validateConfigBasedVars($config)
67  {
68  $configStrings = array_filter($config, function ($value) {
69  return is_string($value);
70  });
71 
72  foreach ($configStrings as $configKey => $configValue) {
73  $var = trim((String)$configValue, '%');
74  if (array_key_exists($var, $_ENV)) {
75  trigger_error(
76  "Issue with setting configuration for test runs. Please make sure '{$var}' is "
77  . "not duplicated as a system level variable",
78  E_USER_ERROR
79  );
80  }
81  }
82  }
83 
89  public static function sanitizeUrl($url)
90  {
91  if (strlen($url) == 0 && !MftfApplicationConfig::getConfig()->forceGenerateEnabled()) {
92  trigger_error("MAGENTO_BASE_URL must be defined in .env", E_USER_ERROR);
93  }
94 
95  if (filter_var($url, FILTER_VALIDATE_URL) === true) {
96  return rtrim($url, "/") . "/";
97  }
98 
99  $urlParts = parse_url($url);
100 
101  if (!isset($urlParts['scheme'])) {
102  $urlParts['scheme'] = "http";
103  }
104  if (!isset($urlParts['host'])) {
105  $urlParts['host'] = rtrim($urlParts['path'], "/");
106  $urlParts['host'] = str_replace("//", "/", $urlParts['host']);
107  unset($urlParts['path']);
108  }
109 
110  if (!isset($urlParts['path'])) {
111  $urlParts['path'] = "/";
112  } else {
113  $urlParts['path'] = rtrim($urlParts['path'], "/") . "/";
114  }
115 
116  return str_replace("///", "//", self::buildUrl($urlParts));
117  }
118 
125  private static function buildUrl(array $parts)
126  {
127  $get = function ($key) use ($parts) {
128  return isset($parts[$key]) ? $parts[$key] : null;
129  };
130 
131  $pass = $get('pass');
132  $user = $get('user');
133  $userinfo = $pass !== null ? "$user:$pass" : $user;
134  $port = $get('port');
135  $scheme = $get('scheme');
136  $query = $get('query');
137  $fragment = $get('fragment');
138  $authority =
139  ($userinfo !== null ? "$userinfo@" : '') .
140  $get('host') .
141  ($port ? ":$port" : '');
142 
143  return
144  (strlen($scheme) ? "$scheme:" : '') .
145  (strlen($authority) ? "//$authority" : '') .
146  $get('path') .
147  (strlen($query) ? "?$query" : '') .
148  (strlen($fragment) ? "#$fragment" : '');
149  }
150 }
static sanitizeWebDriverConfig($config, $params=['url', 'selenium'])
$config
Definition: fraud_order.php:17
$value
Definition: gender.phtml:16
$user
Definition: dummy_user.php:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18