Skip to content

Commit 4babbdf

Browse files
committed
Use custom Backend domain, refactoring to Executors responsible for calling HTTP endpoints
1 parent a9e04a7 commit 4babbdf

File tree

7 files changed

+64
-56
lines changed

7 files changed

+64
-56
lines changed

etc/config/.env.example

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#*** Set the base URL for your Magento instance ***#
55
MAGENTO_BASE_URL=http://devdocs.magento.com/
66

7+
#*** Uncomment if you are running Admin Panel on separate domain (used with MAGENTO_BACKEND_NAME) ***#
8+
# MAGENTO_BACKEND_BASE_HOST=http://admin.example.com/
9+
710
#*** Set the Admin Username and Password for your Magento instance ***#
811
MAGENTO_BACKEND_NAME=admin
912
MAGENTO_ADMIN_USERNAME=admin
@@ -23,8 +26,9 @@ MAGENTO_ADMIN_PASSWORD=123123q
2326
BROWSER=chrome
2427

2528
#*** Uncomment and set host & port if your dev environment needs different value other than MAGENTO_BASE_URL for Rest API Requests ***#
26-
#MAGENTO_RESTAPI_SERVER_HOST=
27-
#MAGENTO_RESTAPI_SERVER_PORT=
29+
#MAGENTO_RESTAPI_SERVER_HOST=restapi.magento.com
30+
#MAGENTO_RESTAPI_SERVER_PORT=8080
31+
#MAGENTO_RESTAPI_SERVER_PROTOCOL=https
2832

2933
#*** Uncomment these properties to set up a dev environment with symlinked projects ***#
3034
#TESTS_BP=

etc/config/functional.suite.dist.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ modules:
2626
config:
2727
\Magento\FunctionalTestingFramework\Module\MagentoWebDriver:
2828
url: "%MAGENTO_BASE_URL%"
29+
backend_url: "%MAGENTO_BACKEND_BASE_URL%"
2930
backend_name: "%MAGENTO_BACKEND_NAME%"
3031
browser: 'chrome'
3132
restart: true

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AbstractExecutor.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,14 @@
1414
abstract class AbstractExecutor implements CurlInterface
1515
{
1616
/**
17-
* Base url.
17+
* Returns Magento base URL. Used as a fallback for other services (eg. WebApi, Backend)
1818
*
1919
* @var string
2020
*/
2121
protected static $baseUrl = null;
2222

23-
/**
24-
* Resolve base url.
25-
*
26-
* @return void
27-
*/
28-
protected static function resolveBaseUrl()
23+
public function getBaseUrl(): string
2924
{
30-
31-
if ((getenv('MAGENTO_RESTAPI_SERVER_HOST') !== false)
32-
&& (getenv('MAGENTO_RESTAPI_SERVER_HOST') !== '') ) {
33-
self::$baseUrl = getenv('MAGENTO_RESTAPI_SERVER_HOST');
34-
} else {
35-
self::$baseUrl = getenv('MAGENTO_BASE_URL');
36-
}
37-
38-
if ((getenv('MAGENTO_RESTAPI_SERVER_PORT') !== false)
39-
&& (getenv('MAGENTO_RESTAPI_SERVER_PORT') !== '')) {
40-
self::$baseUrl .= ':' . getenv('MAGENTO_RESTAPI_SERVER_PORT');
41-
}
42-
43-
self::$baseUrl = rtrim(self::$baseUrl, '/') . '/';
25+
return getenv('MAGENTO_BASE_URL');
4426
}
4527
}

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AdminExecutor.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,11 @@ class AdminExecutor extends AbstractExecutor implements CurlInterface
3737
private $response;
3838

3939
/**
40-
* Should executor remove backend_name from api url
40+
* Flag describes whether the request is to Magento Base URL, removes backend_name from api url
4141
* @var boolean
4242
*/
4343
private $removeBackend;
4444

45-
/**
46-
* Backend url.
47-
*
48-
* @var string
49-
*/
50-
private static $adminUrl;
51-
5245
/**
5346
* Constructor.
5447
* @param boolean $removeBackend
@@ -58,15 +51,17 @@ class AdminExecutor extends AbstractExecutor implements CurlInterface
5851
*/
5952
public function __construct($removeBackend)
6053
{
61-
if (!isset(parent::$baseUrl)) {
62-
parent::resolveBaseUrl();
63-
}
64-
self::$adminUrl = parent::$baseUrl . getenv('MAGENTO_BACKEND_NAME') . '/';
6554
$this->removeBackend = $removeBackend;
6655
$this->transport = new CurlTransport();
6756
$this->authorize();
6857
}
6958

59+
public function getBaseUrl(): string
60+
{
61+
$backendHost = getenv('MAGENTO_BACKEND_BASE_URL') ?: parent::getBaseUrl();
62+
return $backendHost . getenv('MAGENTO_BACKEND_NAME') . '/';
63+
}
64+
7065
/**
7166
* Authorize admin on backend.
7267
*
@@ -76,11 +71,11 @@ public function __construct($removeBackend)
7671
private function authorize()
7772
{
7873
// Perform GET to backend url so form_key is set
79-
$this->transport->write(self::$adminUrl, [], CurlInterface::GET);
74+
$this->transport->write($this->getBaseUrl(), [], CurlInterface::GET);
8075
$this->read();
8176

8277
// Authenticate admin user
83-
$authUrl = self::$adminUrl . 'admin/auth/login/';
78+
$authUrl = $this->getBaseUrl() . 'admin/auth/login/';
8479
$data = [
8580
'login[username]' => getenv('MAGENTO_ADMIN_USERNAME'),
8681
'login[password]' => getenv('MAGENTO_ADMIN_PASSWORD'),
@@ -110,19 +105,19 @@ private function setFormKey()
110105
* Send request to the remote server.
111106
*
112107
* @param string $url
113-
* @param array $data
108+
* @param array $data
114109
* @param string $method
115-
* @param array $headers
110+
* @param array $headers
116111
* @return void
117112
* @throws TestFrameworkException
118113
*/
119114
public function write($url, $data = [], $method = CurlInterface::POST, $headers = [])
120115
{
121116
$url = ltrim($url, "/");
122-
$apiUrl = self::$adminUrl . $url;
117+
$apiUrl = $this->getBaseUrl() . $url;
123118

124119
if ($this->removeBackend) {
125-
$apiUrl = parent::$baseUrl . $url;
120+
$apiUrl = parent::getBaseUrl() . $url;
126121
}
127122

128123
if ($this->formKey) {
@@ -168,7 +163,7 @@ public function read($successRegex = null, $returnRegex = null)
168163
/**
169164
* Add additional option to cURL.
170165
*
171-
* @param integer $option CURLOPT_* constants.
166+
* @param integer $option CURLOPT_* constants.
172167
* @param integer|string|boolean|array $value
173168
* @return void
174169
*/

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/FrontendExecutor.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ class FrontendExecutor extends AbstractExecutor implements CurlInterface
6767
*/
6868
public function __construct($customerEmail, $customerPassWord)
6969
{
70-
if (!isset(parent::$baseUrl)) {
71-
parent::resolveBaseUrl();
72-
}
7370
$this->transport = new CurlTransport();
7471
$this->customerEmail = $customerEmail;
7572
$this->customerPassword = $customerPassWord;
@@ -84,11 +81,11 @@ public function __construct($customerEmail, $customerPassWord)
8481
*/
8582
private function authorize()
8683
{
87-
$url = parent::$baseUrl . 'customer/account/login/';
84+
$url = $this->getBaseUrl() . 'customer/account/login/';
8885
$this->transport->write($url);
8986
$this->read();
9087

91-
$url = parent::$baseUrl . 'customer/account/loginPost/';
88+
$url = $this->getBaseUrl() . 'customer/account/loginPost/';
9289
$data = [
9390
'login[username]' => $this->customerEmail,
9491
'login[password]' => $this->customerPassword,
@@ -146,7 +143,7 @@ public function write($url, $data = [], $method = CurlInterface::POST, $headers
146143
if (isset($data['customer_password'])) {
147144
unset($data['customer_password']);
148145
}
149-
$apiUrl = parent::$baseUrl . $url;
146+
$apiUrl = $this->getBaseUrl() . $url;
150147
if ($this->formKey) {
151148
$data['form_key'] = $this->formKey;
152149
} else {

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/WebapiExecutor.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,34 @@ class WebapiExecutor extends AbstractExecutor implements CurlInterface
5959
*/
6060
public function __construct($storeCode = null)
6161
{
62-
if (!isset(parent::$baseUrl)) {
63-
parent::resolveBaseUrl();
64-
}
65-
6662
$this->storeCode = $storeCode;
6763
$this->transport = new CurlTransport();
6864
$this->authorize();
6965
}
7066

67+
/**
68+
* Returns WebApi base URL, fallback to Magento Base URL
69+
* @return string
70+
*/
71+
public function getBaseUrl(): string
72+
{
73+
$baseUrl = parent::getBaseUrl();
74+
75+
$webapiHost = getenv('MAGENTO_RESTAPI_SERVER_HOST');
76+
$webapiPort = getenv("MAGENTO_RESTAPI_SERVER_PORT");
77+
$webapiProtocol = getenv("MAGENTO_RESTAPI_SERVER_PROTOCOL");
78+
79+
if ($webapiHost) {
80+
$baseUrl = sprintf('%s://%s/', $webapiProtocol, $webapiHost);
81+
}
82+
83+
if ($webapiPort) {
84+
$baseUrl = rtrim($baseUrl,'/').':'.$webapiPort.'/';
85+
}
86+
87+
return $baseUrl;
88+
}
89+
7190
/**
7291
* Returns the authorization token needed for some requests via REST call.
7392
*
@@ -152,7 +171,7 @@ public function close()
152171
*/
153172
public function getFormattedUrl($resource)
154173
{
155-
$urlResult = parent::$baseUrl . 'rest/';
174+
$urlResult = $this->getBaseUrl() . 'rest/';
156175
if ($this->storeCode != null) {
157176
$urlResult .= $this->storeCode . "/";
158177
}

src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,17 +396,18 @@ protected function getAdminToken()
396396
{
397397
$login = $_ENV['MAGENTO_ADMIN_USERNAME'] ?? null;
398398
$password = $_ENV['MAGENTO_ADMIN_PASSWORD'] ?? null;
399-
if (!$login || !$password || !isset($_ENV['MAGENTO_BASE_URL'])) {
399+
if (!$login || !$password || !$this->getBackendUrl()) {
400400
$message = "Cannot retrieve API token without credentials and base url, please fill out .env.";
401401
$context = [
402402
"MAGENTO_BASE_URL" => getenv("MAGENTO_BASE_URL"),
403+
"MAGENTO_BACKEND_BASE_URL" => getenv("MAGENTO_BACKEND_BASE_URL"),
403404
"MAGENTO_ADMIN_USERNAME" => getenv("MAGENTO_ADMIN_USERNAME"),
404405
"MAGENTO_ADMIN_PASSWORD" => getenv("MAGENTO_ADMIN_PASSWORD"),
405406
];
406407
throw new TestFrameworkException($message, $context);
407408
}
408409

409-
$url = ConfigSanitizerUtil::sanitizeUrl($_ENV['MAGENTO_BASE_URL']) . $this->adminTokenUrl;
410+
$url = ConfigSanitizerUtil::sanitizeUrl($this->getBackendUrl()) . $this->adminTokenUrl;
410411
$data = [
411412
'username' => $login,
412413
'password' => $password
@@ -428,7 +429,7 @@ protected function getAdminToken()
428429

429430
if ($responseCode !== 200) {
430431
if ($responseCode == 0) {
431-
$details = "Could not find Magento Instance at given MAGENTO_BASE_URL";
432+
$details = "Could not find Magento Backend Instance at MAGENTO_BACKEND_BASE_URL or MAGENTO_BASE_URL";
432433
} else {
433434
$details = $responseCode . " " . Response::$statusTexts[$responseCode];
434435
}
@@ -563,4 +564,13 @@ private function getRegisteredModuleList()
563564
}
564565
return [];
565566
}
567+
568+
/**
569+
* Returns custom Backend URL if set, fallback to Magento Base URL
570+
* @return string|null
571+
*/
572+
private function getBackendUrl()
573+
{
574+
return getenv('MAGENTO_BACKEND_BASE_URL') ?: getenv('MAGENTO_BASE_URL');
575+
}
566576
}

0 commit comments

Comments
 (0)