Skip to content

Commit c5f211b

Browse files
committed
MQE-1352: bug fix in dev/tests/functional/lib/Magento/Mtf/Util/Command/Cli.php
1 parent ba6d90e commit c5f211b

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

dev/tests/functional/lib/Magento/Mtf/Util/Protocol/CurlTransport/BackendDecorator.php

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,54 @@ public function __construct(CurlTransport $transport, DataInterface $configurati
6363
*/
6464
protected function authorize()
6565
{
66-
// Perform GET to backend url so form_key is set
67-
$url = $_ENV['app_backend_url'];
68-
$this->transport->write($url, [], CurlInterface::GET);
69-
$this->read();
70-
71-
$url = $_ENV['app_backend_url'] . $this->configuration->get('application/0/backendLoginUrl/0/value');
72-
$data = [
73-
'login[username]' => $this->configuration->get('application/0/backendLogin/0/value'),
74-
'login[password]' => $this->configuration->get('application/0/backendPassword/0/value'),
75-
'form_key' => $this->formKey,
76-
];
77-
$this->transport->write($url, $data, CurlInterface::POST);
78-
$response = $this->read();
79-
if (strpos($response, 'login-form') !== false) {
80-
throw new \Exception(
81-
'Admin user cannot be logged in by curl handler!'
82-
);
66+
// There are situations where magento application backend url could be slightly different from the environment
67+
// variable we know. It could be intentionally (e.g. InstallTest) or unintentionally. We would still want tests
68+
// to run in this case.
69+
// When the original app_backend_url does not work, we will try 4 variants of the it. i.e. with and without
70+
// url rewrite, http and https.
71+
$urls = [];
72+
$originalUrl = rtrim($_ENV['app_backend_url'], '/') . '/';
73+
$urls[] = $originalUrl;
74+
if (strpos($originalUrl, '/index.php') !== false) {
75+
$url2 = str_replace('/index.php', '', $originalUrl);
76+
} else {
77+
$url2 = $originalUrl . 'index.php/';
78+
}
79+
$urls[] = $url2;
80+
if (strpos($originalUrl, 'https') !== false) {
81+
$urls[] = str_replace('https', 'http', $originalUrl);
82+
} else {
83+
$urls[] = str_replace('http', 'https', $url2);
84+
}
85+
86+
$isAuthorized = false;
87+
foreach ($urls as $url) {
88+
try {
89+
// Perform GET to backend url so form_key is set
90+
$this->transport->write($url, [], CurlInterface::GET);
91+
$this->read();
92+
93+
$authUrl = $url . $this->configuration->get('application/0/backendLoginUrl/0/value');
94+
$data = [
95+
'login[username]' => $this->configuration->get('application/0/backendLogin/0/value'),
96+
'login[password]' => $this->configuration->get('application/0/backendPassword/0/value'),
97+
'form_key' => $this->formKey,
98+
];
99+
100+
$this->transport->write($authUrl, $data, CurlInterface::POST);
101+
$response = $this->read();
102+
if (strpos($response, 'login-form')) {
103+
continue;
104+
}
105+
$isAuthorized = true;
106+
$_ENV['app_backend_url'] = $url;
107+
break;
108+
} catch (\Exception $e) {
109+
continue;
110+
}
111+
}
112+
if ($isAuthorized == false) {
113+
throw new \Exception('Admin user cannot be logged in by curl handler!');
83114
}
84115
}
85116

0 commit comments

Comments
 (0)