Thanks to visit codestin.com
Credit goes to github.com

Skip to content

#389 Fix invalid bahaviour of MAGENTO_BACKEND_BASE_URL #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dev/tests/verification/Resources/PageReplacementTest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class PageReplacementTestCest
$I->amOnPage("/John/StringLiteral2.html"); // stepKey: twoParamPageStringData
$I->amOnPage("/John/" . $I->retrieveEntityField('datakey', 'firstname', 'test') . ".html"); // stepKey: twoParamPageDataPersist
$I->amOnPage("/" . $I->retrieveEntityField('datakey', 'firstname', 'test') . "/StringLiteral2.html"); // stepKey: twoParamPagePersistString
$I->amOnPage("/" . getenv("MAGENTO_BACKEND_NAME") . "/backend"); // stepKey: onAdminPage
$I->amOnPage("/" . getenv("MAGENTO_BACKEND_NAME") . "/StringLiteral/page.html"); // stepKey: oneParamAdminPageString
$I->amOnPage((getenv("MAGENTO_BACKEND_BASE_URL") ? rtrim(getenv("MAGENTO_BACKEND_BASE_URL"), "/") : "") . "/" . getenv("MAGENTO_BACKEND_NAME") . "/backend"); // stepKey: onAdminPage
$I->amOnPage((getenv("MAGENTO_BACKEND_BASE_URL") ? rtrim(getenv("MAGENTO_BACKEND_BASE_URL"), "/") : "") . "/" . getenv("MAGENTO_BACKEND_NAME") . "/StringLiteral/page.html"); // stepKey: oneParamAdminPageString
$I->amOnUrl("http://myFullUrl.com/"); // stepKey: onExternalPage
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl;

use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
use Magento\FunctionalTestingFramework\Provider\UrlProvider;
use Magento\FunctionalTestingFramework\Util\Protocol\CurlInterface;

/**
Expand All @@ -29,6 +29,6 @@ abstract class AbstractExecutor implements CurlInterface
*/
public function getBaseUrl(): string
{
return UrlFormatter::format(getenv('MAGENTO_BASE_URL'));
return UrlProvider::getBaseUrl();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl;

use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
use Magento\FunctionalTestingFramework\Page\Objects\PageObject;
use Magento\FunctionalTestingFramework\Provider\UrlProvider;
use Magento\FunctionalTestingFramework\Util\Protocol\CurlInterface;
use Magento\FunctionalTestingFramework\Util\Protocol\CurlTransport;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
Expand Down Expand Up @@ -64,16 +65,7 @@ public function __construct($removeBackend)
*/
public function getBaseUrl(): string
{
$backendHost = getenv('MAGENTO_BACKEND_BASE_URL')
?
UrlFormatter::format(getenv('MAGENTO_BACKEND_BASE_URL'))
:
parent::getBaseUrl();
return empty(getenv('MAGENTO_BACKEND_NAME'))
?
$backendHost
:
$backendHost . getenv('MAGENTO_BACKEND_NAME') . '/';
return UrlProvider::getBaseUrl(PageObject::ADMIN_AREA);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,8 @@ public function saveScreenshot()
*/
public function amOnPage($page)
{
parent::amOnPage($page);
(0 === strpos($page, 'http')) ? parent::amOnUrl($page) : parent::amOnPage($page);

$this->waitForPageLoad();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace Magento\FunctionalTestingFramework\Module;

use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Page\Objects\PageObject;
use Magento\FunctionalTestingFramework\Provider\UrlProvider;
use Facebook\WebDriver\Remote\RemoteWebDriver;

/**
Expand Down Expand Up @@ -47,16 +49,14 @@ public function _initialize()
}

try {
$adminUrl = rtrim(getenv('MAGENTO_BACKEND_BASE_URL'), '/')
?: rtrim(getenv('MAGENTO_BASE_URL'), '/')
. '/' . getenv('MAGENTO_BACKEND_NAME') . '/admin';
$adminUrl = UrlProvider::getBaseUrl(PageObject::ADMIN_AREA);
$this->loadPageAtUrl($adminUrl);
} catch (\Exception $e) {
$context[self::EXCEPTION_CONTEXT_ADMIN] = $e->getMessage();
}

try {
$storeUrl = getenv('MAGENTO_BASE_URL');
$storeUrl = UrlProvider::getBaseUrl();
$this->loadPageAtUrl($storeUrl);
} catch (\Exception $e) {
$context[self::EXCEPTION_CONTEXT_STOREFRONT] = $e->getMessage();
Expand Down
49 changes: 49 additions & 0 deletions src/Magento/FunctionalTestingFramework/Provider/UrlProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\FunctionalTestingFramework\Provider;

use Magento\FunctionalTestingFramework\Page\Objects\PageObject;

/**
* Provider responsible for returning right URLs for provided scope.
*/
class UrlProvider
{
/**
* Returns proper Base URL for specified Area.
*
* @param string|null $customArea
* @return string
*/
public static function getBaseUrl($customArea = null): string
{
$baseUrl = getenv('MAGENTO_BASE_URL');

switch ($customArea) {
case PageObject::ADMIN_AREA:
$backendName = getenv('MAGENTO_BACKEND_NAME');
$baseUrl = self::getBackendBaseUrl() ?: $baseUrl;

return rtrim($baseUrl, '/') . '/' . rtrim($backendName, '/') . '/';
}

return $baseUrl;
}

/**
* Returns MAGENTO_BACKEND_BASE_URL if set or null
*
* @return string|null
*/
public static function getBackendBaseUrl()
{
$backendBaseUrl = getenv('MAGENTO_BACKEND_BASE_URL');

return $backendBaseUrl ?: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,15 @@ private function resolveParameterization($isParameterized, $replacement, $match,
} else {
$resolvedReplacement = $replacement;
}
if (get_class($object) == PageObject::class && $object->getArea() == PageObject::ADMIN_AREA) {
$resolvedReplacement = "/{{_ENV.MAGENTO_BACKEND_NAME}}/" . $resolvedReplacement;

if (get_class($object) === PageObject::class && $object->getArea() === PageObject::ADMIN_AREA) {
$urlSegments = [
'{{_ENV.MAGENTO_BACKEND_BASE_URL}}',
'{{_ENV.MAGENTO_BACKEND_NAME}}',
$resolvedReplacement
];

$resolvedReplacement = implode('/', $urlSegments);
}
return $resolvedReplacement;
}
Expand Down
35 changes: 27 additions & 8 deletions src/Magento/FunctionalTestingFramework/Util/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1942,20 +1942,23 @@ private function resolveRuntimeReference($args, $regex, $func)
$newArgs = [];

foreach ($args as $key => $arg) {
$newArgs[$key] = $arg;

preg_match_all($regex, $arg, $matches);
if (!empty($matches[0])) {
$fullMatch = $matches[0][0];
$refVariable = $matches[1][0];
unset($matches);
$replacement = "{$func}(\"{$refVariable}\")";
foreach ($matches[0] as $matchKey => $fullMatch) {
$refVariable = $matches[1][$matchKey];

$replacement = $this->getReplacement($func, $refVariable);

$outputArg = $this->processQuoteBreaks($fullMatch, $newArgs[$key], $replacement);
$newArgs[$key] = $outputArg;
}

$outputArg = $this->processQuoteBreaks($fullMatch, $arg, $replacement);
$newArgs[$key] = $outputArg;
unset($matches);
continue;
}
$newArgs[$key] = $arg;
}

// override passed in args for use later.
return $newArgs;
}
Expand Down Expand Up @@ -2202,4 +2205,20 @@ private function hasDecimalPoint(string $outStr)
{
return strpos($outStr, localeconv()['decimal_point']) !== false;
}

/**
* Supports fallback for BACKEND URL
*
* @param string $func
* @param string $refVariable
* @return string
*/
private function getReplacement($func, $refVariable): string
{
if ($refVariable === 'MAGENTO_BACKEND_BASE_URL') {
return "({$func}(\"{$refVariable}\") ? rtrim({$func}(\"{$refVariable}\"), \"/\") : \"\")";
}

return "{$func}(\"{$refVariable}\")";
}
}