Skip to content

Commit da10983

Browse files
authored
MQE-570: Sanitize MAGENTO_BASE_URL (.env file) and Selenium params
- addressed all base AC of issue. - Broke out sanitization into its own method, defined _resetConfig to prevent variables from being overwritten on test startup. - Added step to configure rest URL based on sanitized version of MAGENTO_BASE_URL.
1 parent bdb8340 commit da10983

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/Magento/FunctionalTestingFramework/Module/MagentoRestDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ class MagentoRestDriver extends REST
104104
public function _beforeSuite($settings = [])
105105
{
106106
parent::_beforeSuite($settings);
107-
108107
if (empty($this->config['url']) || empty($this->config['username']) || empty($this->config['password'])) {
109108
return;
110109
}
110+
$this->config['url'] = $_ENV['MAGENTO_BASE_URL'] . "rest/default/V1/";
111111

112112
$this->haveHttpHeader('Content-Type', 'application/json');
113113
$this->sendPOST(

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,96 @@ class MagentoWebDriver extends WebDriver
7676
LC_MESSAGES => null,
7777
];
7878

79+
public function _initialize()
80+
{
81+
$this->sanitizeConfig();
82+
parent::_initialize();
83+
}
84+
85+
public function _resetConfig()
86+
{
87+
parent::_resetConfig();
88+
$this->sanitizeConfig();
89+
}
90+
91+
/**
92+
* Sanitizes URL and Selenium Variables, then assigns them to the config array.
93+
* @return void
94+
*/
95+
private function sanitizeConfig()
96+
{
97+
if ($this->config['url'] === "") {
98+
trigger_error("MAGENTO_BASE_URL must be defined in .env", E_USER_ERROR);
99+
}
100+
101+
//Determine if url sanitize is required
102+
if (!preg_match("/(http|https):\/\/[\w.:]+\//", $this->config['url'])) {
103+
$urlParts = parse_url($this->config['url']);
104+
105+
if (!isset($urlParts['scheme'])) {
106+
$urlParts['scheme'] = "http";
107+
}
108+
if (!isset($urlParts['host'])) {
109+
$urlParts['host'] = rtrim($urlParts['path'], "/");
110+
unset($urlParts['path']);
111+
}
112+
113+
if (!isset($urlParts['path'])) {
114+
$urlParts['path'] = "/";
115+
} else {
116+
$urlParts['path'] = rtrim($urlParts['path'], "/") . "/";
117+
}
118+
119+
$_ENV['MAGENTO_BASE_URL'] = str_replace("///", "//", $this->build_url($urlParts));
120+
$this->config['url'] = str_replace("///", "//", $this->build_url($urlParts));
121+
}
122+
123+
//Assign default Values to Selenium configs if they are defined
124+
if ($this->config['protocol'] == '%SELENIUM_PROTOCOL%') {
125+
$this->config['protocol'] = "http";
126+
}
127+
if ($this->config['host'] == '%SELENIUM_HOST%') {
128+
$this->config['host'] = "127.0.0.1";
129+
}
130+
if ($this->config['port'] == '%SELENIUM_PORT%') {
131+
$this->config['port'] = "4444";
132+
}
133+
if ($this->config['path'] == '%SELENIUM_PATH%') {
134+
$this->config['path'] = "/wd/hub";
135+
}
136+
}
137+
138+
/**
139+
* Returns url from $parts given, used with parse_url output for convenience.
140+
* This only exists because of deprecation of http_build_url, which does the exact same thing as the code below.
141+
* @param array $parts
142+
* @return string
143+
*/
144+
private function build_url(array $parts) {
145+
$get = function ($key) use ($parts) {
146+
return isset($parts[$key]) ? $parts[$key] : null;
147+
};
148+
149+
$pass = $get('pass');
150+
$user = $get('user');
151+
$userinfo = $pass !== null ? "$user:$pass" : $user;
152+
$port = $get('port');
153+
$scheme = $get('scheme');
154+
$query = $get('query');
155+
$fragment = $get('fragment');
156+
$authority =
157+
($userinfo !== null ? "$userinfo@" : '') .
158+
$get('host') .
159+
($port ? ":$port" : '');
160+
161+
return
162+
(strlen($scheme) ? "$scheme:" : '') .
163+
(strlen($authority) ? "//$authority" : '') .
164+
$get('path') .
165+
(strlen($query) ? "?$query" : '') .
166+
(strlen($fragment) ? "#$fragment" : '');
167+
}
168+
79169
/**
80170
* Returns URL of a host.
81171
*

0 commit comments

Comments
 (0)