diff --git a/etc/config/.env.example b/etc/config/.env.example index 17ea384ca..899a52df4 100644 --- a/etc/config/.env.example +++ b/etc/config/.env.example @@ -4,6 +4,9 @@ #*** Set the base URL for your Magento instance ***# MAGENTO_BASE_URL=http://devdocs.magento.com/ +#*** Uncomment if you are running Admin Panel on separate domain (used with MAGENTO_BACKEND_NAME) ***# +# MAGENTO_BACKEND_BASE_HOST=http://admin.example.com/ + #*** Set the Admin Username and Password for your Magento instance ***# MAGENTO_BACKEND_NAME=admin MAGENTO_ADMIN_USERNAME=admin @@ -23,8 +26,9 @@ MAGENTO_ADMIN_PASSWORD=123123q BROWSER=chrome #*** Uncomment and set host & port if your dev environment needs different value other than MAGENTO_BASE_URL for Rest API Requests ***# -#MAGENTO_RESTAPI_SERVER_HOST= -#MAGENTO_RESTAPI_SERVER_PORT= +#MAGENTO_RESTAPI_SERVER_HOST=restapi.magento.com +#MAGENTO_RESTAPI_SERVER_PORT=8080 +#MAGENTO_RESTAPI_SERVER_PROTOCOL=https #*** Uncomment these properties to set up a dev environment with symlinked projects ***# #TESTS_BP= diff --git a/etc/config/functional.suite.dist.yml b/etc/config/functional.suite.dist.yml index a7a9f12bf..12efe5b50 100644 --- a/etc/config/functional.suite.dist.yml +++ b/etc/config/functional.suite.dist.yml @@ -20,6 +20,7 @@ modules: config: \Magento\FunctionalTestingFramework\Module\MagentoWebDriver: url: "%MAGENTO_BASE_URL%" + backend_url: "%MAGENTO_BACKEND_BASE_URL%" backend_name: "%MAGENTO_BACKEND_NAME%" browser: 'chrome' restart: true diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AbstractExecutor.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AbstractExecutor.php index 6adf87892..b6c3f29ec 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AbstractExecutor.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AbstractExecutor.php @@ -14,32 +14,18 @@ abstract class AbstractExecutor implements CurlInterface { /** - * Base url. + * Returns Magento base URL. Used as a fallback for other services (eg. WebApi, Backend) * * @var string */ protected static $baseUrl = null; /** - * Resolve base url. - * - * @return void + * Returns base URL for Magento instance + * @return string */ - protected static function resolveBaseUrl() + public function getBaseUrl(): string { - - if ((getenv('MAGENTO_RESTAPI_SERVER_HOST') !== false) - && (getenv('MAGENTO_RESTAPI_SERVER_HOST') !== '') ) { - self::$baseUrl = getenv('MAGENTO_RESTAPI_SERVER_HOST'); - } else { - self::$baseUrl = getenv('MAGENTO_BASE_URL'); - } - - if ((getenv('MAGENTO_RESTAPI_SERVER_PORT') !== false) - && (getenv('MAGENTO_RESTAPI_SERVER_PORT') !== '')) { - self::$baseUrl .= ':' . getenv('MAGENTO_RESTAPI_SERVER_PORT'); - } - - self::$baseUrl = rtrim(self::$baseUrl, '/') . '/'; + return getenv('MAGENTO_BASE_URL'); } } diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AdminExecutor.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AdminExecutor.php index cff7c04fb..1f7ae70d7 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AdminExecutor.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/AdminExecutor.php @@ -37,18 +37,11 @@ class AdminExecutor extends AbstractExecutor implements CurlInterface private $response; /** - * Should executor remove backend_name from api url + * Flag describes whether the request is to Magento Base URL, removes backend_name from api url * @var boolean */ private $removeBackend; - /** - * Backend url. - * - * @var string - */ - private static $adminUrl; - /** * Constructor. * @param boolean $removeBackend @@ -58,15 +51,21 @@ class AdminExecutor extends AbstractExecutor implements CurlInterface */ public function __construct($removeBackend) { - if (!isset(parent::$baseUrl)) { - parent::resolveBaseUrl(); - } - self::$adminUrl = parent::$baseUrl . getenv('MAGENTO_BACKEND_NAME') . '/'; $this->removeBackend = $removeBackend; $this->transport = new CurlTransport(); $this->authorize(); } + /** + * Returns base URL for Magento backend instance + * @return string + */ + public function getBaseUrl(): string + { + $backendHost = getenv('MAGENTO_BACKEND_BASE_URL') ?: parent::getBaseUrl(); + return $backendHost . getenv('MAGENTO_BACKEND_NAME') . '/'; + } + /** * Authorize admin on backend. * @@ -76,11 +75,11 @@ public function __construct($removeBackend) private function authorize() { // Perform GET to backend url so form_key is set - $this->transport->write(self::$adminUrl, [], CurlInterface::GET); + $this->transport->write($this->getBaseUrl(), [], CurlInterface::GET); $this->read(); // Authenticate admin user - $authUrl = self::$adminUrl . 'admin/auth/login/'; + $authUrl = $this->getBaseUrl() . 'admin/auth/login/'; $data = [ 'login[username]' => getenv('MAGENTO_ADMIN_USERNAME'), 'login[password]' => getenv('MAGENTO_ADMIN_PASSWORD'), @@ -119,10 +118,10 @@ private function setFormKey() public function write($url, $data = [], $method = CurlInterface::POST, $headers = []) { $url = ltrim($url, "/"); - $apiUrl = self::$adminUrl . $url; + $apiUrl = $this->getBaseUrl() . $url; if ($this->removeBackend) { - $apiUrl = parent::$baseUrl . $url; + $apiUrl = parent::getBaseUrl() . $url; } if ($this->formKey) { diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/FrontendExecutor.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/FrontendExecutor.php index aa1706ef3..0a82e88a6 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/FrontendExecutor.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/FrontendExecutor.php @@ -67,9 +67,6 @@ class FrontendExecutor extends AbstractExecutor implements CurlInterface */ public function __construct($customerEmail, $customerPassWord) { - if (!isset(parent::$baseUrl)) { - parent::resolveBaseUrl(); - } $this->transport = new CurlTransport(); $this->customerEmail = $customerEmail; $this->customerPassword = $customerPassWord; @@ -84,11 +81,11 @@ public function __construct($customerEmail, $customerPassWord) */ private function authorize() { - $url = parent::$baseUrl . 'customer/account/login/'; + $url = $this->getBaseUrl() . 'customer/account/login/'; $this->transport->write($url); $this->read(); - $url = parent::$baseUrl . 'customer/account/loginPost/'; + $url = $this->getBaseUrl() . 'customer/account/loginPost/'; $data = [ 'login[username]' => $this->customerEmail, 'login[password]' => $this->customerPassword, @@ -146,7 +143,7 @@ public function write($url, $data = [], $method = CurlInterface::POST, $headers if (isset($data['customer_password'])) { unset($data['customer_password']); } - $apiUrl = parent::$baseUrl . $url; + $apiUrl = $this->getBaseUrl() . $url; if ($this->formKey) { $data['form_key'] = $this->formKey; } else { diff --git a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/WebapiExecutor.php b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/WebapiExecutor.php index 16fef75f2..5eb877443 100644 --- a/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/WebapiExecutor.php +++ b/src/Magento/FunctionalTestingFramework/DataGenerator/Persist/Curl/WebapiExecutor.php @@ -59,15 +59,34 @@ class WebapiExecutor extends AbstractExecutor implements CurlInterface */ public function __construct($storeCode = null) { - if (!isset(parent::$baseUrl)) { - parent::resolveBaseUrl(); - } - $this->storeCode = $storeCode; $this->transport = new CurlTransport(); $this->authorize(); } + /** + * Returns base URL for Magento Web API instance + * @return string + */ + public function getBaseUrl(): string + { + $baseUrl = parent::getBaseUrl(); + + $webapiHost = getenv('MAGENTO_RESTAPI_SERVER_HOST'); + $webapiPort = getenv("MAGENTO_RESTAPI_SERVER_PORT"); + $webapiProtocol = getenv("MAGENTO_RESTAPI_SERVER_PROTOCOL"); + + if ($webapiHost) { + $baseUrl = sprintf('%s://%s/', $webapiProtocol, $webapiHost); + } + + if ($webapiPort) { + $baseUrl = rtrim($baseUrl, '/') . ':' . $webapiPort . '/'; + } + + return $baseUrl; + } + /** * Returns the authorization token needed for some requests via REST call. * @@ -152,11 +171,11 @@ public function close() */ public function getFormattedUrl($resource) { - $urlResult = parent::$baseUrl . 'rest/'; + $urlResult = $this->getBaseUrl() . 'rest/'; if ($this->storeCode != null) { $urlResult .= $this->storeCode . "/"; } - $urlResult.= trim($resource, "/"); + $urlResult .= trim($resource, "/"); return $urlResult; } } diff --git a/src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php b/src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php index d1574ffe6..16e6037fa 100644 --- a/src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php +++ b/src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php @@ -396,17 +396,18 @@ protected function getAdminToken() { $login = $_ENV['MAGENTO_ADMIN_USERNAME'] ?? null; $password = $_ENV['MAGENTO_ADMIN_PASSWORD'] ?? null; - if (!$login || !$password || !isset($_ENV['MAGENTO_BASE_URL'])) { + if (!$login || !$password || !$this->getBackendUrl()) { $message = "Cannot retrieve API token without credentials and base url, please fill out .env."; $context = [ "MAGENTO_BASE_URL" => getenv("MAGENTO_BASE_URL"), + "MAGENTO_BACKEND_BASE_URL" => getenv("MAGENTO_BACKEND_BASE_URL"), "MAGENTO_ADMIN_USERNAME" => getenv("MAGENTO_ADMIN_USERNAME"), "MAGENTO_ADMIN_PASSWORD" => getenv("MAGENTO_ADMIN_PASSWORD"), ]; throw new TestFrameworkException($message, $context); } - $url = ConfigSanitizerUtil::sanitizeUrl($_ENV['MAGENTO_BASE_URL']) . $this->adminTokenUrl; + $url = ConfigSanitizerUtil::sanitizeUrl($this->getBackendUrl()) . $this->adminTokenUrl; $data = [ 'username' => $login, 'password' => $password @@ -428,7 +429,7 @@ protected function getAdminToken() if ($responseCode !== 200) { if ($responseCode == 0) { - $details = "Could not find Magento Instance at given MAGENTO_BASE_URL"; + $details = "Could not find Magento Backend Instance at MAGENTO_BACKEND_BASE_URL or MAGENTO_BASE_URL"; } else { $details = $responseCode . " " . Response::$statusTexts[$responseCode]; } @@ -565,4 +566,13 @@ private function getRegisteredModuleList() } return []; } + + /** + * Returns custom Backend URL if set, fallback to Magento Base URL + * @return string|null + */ + private function getBackendUrl() + { + return getenv('MAGENTO_BACKEND_BASE_URL') ?: getenv('MAGENTO_BASE_URL'); + } }