whitelabel-8.x-2.x-dev/tests/src/Functional/WhiteLabelOutboundPathProcessingTest.php
tests/src/Functional/WhiteLabelOutboundPathProcessingTest.php
<?php
namespace Drupal\Tests\whitelabel\Functional;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\whitelabel\Plugin\WhiteLabelNegotiation\WhiteLabelNegotiationUrl;
/**
* Tests to see if outbound urls contain the right token in the right place.
*
* @group whitelabel
*
* @todo Should assert what happens if the user does not have sufficient
* permissions. (Mitigated by inbound processor detecting it).
*/
class WhiteLabelOutboundPathProcessingTest extends WhiteLabelTestBase {
use UserCreationTrait {
createUser as drupalCreateUser;
}
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'text',
'options',
'user',
'file',
'image',
'whitelabel',
'language',
];
/**
* Holds the randomly generated token.
*
* @var string
*/
private $token;
/**
* Holds the randomly generated query string parameter.
*
* @var string
*/
private $queryStringIdentifier;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
ConfigurableLanguage::create(['id' => 'es'])->save();
// This is somehow required...
drupal_flush_all_caches();
$user = $this->drupalCreateUser(['serve white label pages']);
$this->setCurrentUser($user);
$this->token = $this->randomMachineName(16);
$this->queryStringIdentifier = $this->randomMachineName(16);
$this->whiteLabel = $this->createWhiteLabel(['token' => $this->token]);
$this->config('whitelabel.negotiation')
->set('negotiator_settings.url.settings.query_string_identifier', $this->queryStringIdentifier)
->set('negotiator_settings.url.settings.domain', 'http://localhost')
->save();
$this->setCurrentWhiteLabel($this->whiteLabel);
}
/**
* Test white label URL creation.
*/
public function testOutboundUrls() {
$modes = array_keys(WhiteLabelNegotiationUrl::getModes());
$expected_patterns = [
WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER =>
Url::fromUserInput('/', [
'query' => [$this->queryStringIdentifier => $this->token],
])->setAbsolute()->toString(),
WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX =>
Url::fromUserInput('/' . $this->token)->setAbsolute()->toString(),
WhiteLabelNegotiationUrl::CONFIG_DOMAIN =>
str_replace(
'http://localhost/',
"http://{$this->token}.localhost/",
Url::fromUserInput('/')->setAbsolute()->toString()
),
];
foreach ($modes as $mode) {
// Configure the white label mode.
$this->config('whitelabel.negotiation')
->set('negotiator_settings.url.settings.mode', $mode)
->save();
// Build the URL.
$url = Url::fromRoute('<front>')->setAbsolute();
// Check if the white label is in the URL in the expected pattern.
$this->assertSame($expected_patterns[$mode], $url->toString());
}
}
/**
* Test white label URL creation with the language module enabled.
*/
public function testOutboundUrlsLanguage() {
$this->config('language.negotiation')
->set('url.prefixes', ['en' => 'en', 'es' => 'es'])
->save();
// Obtain white label modes and test them.
$modes = array_keys(WhiteLabelNegotiationUrl::getModes());
$expected_patterns = [
WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER =>
Url::fromUserInput('/', [
'query' => [$this->queryStringIdentifier => $this->token],
'language' => 'en',
])->setAbsolute()->toString(),
WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX =>
Url::fromUserInput('/' . $this->token, [
'language' => 'en',
])->setAbsolute()->toString(),
WhiteLabelNegotiationUrl::CONFIG_DOMAIN =>
str_replace(
'http://localhost/',
"http://{$this->token}.localhost/",
Url::fromUserInput('/')->setAbsolute()->toString()
),
];
foreach ($modes as $mode) {
// Configure the white label mode.
$this->config('whitelabel.negotiation')
->set('negotiator_settings.url.settings.mode', $mode)
->save();
// Build the URL.
$url = Url::fromRoute('<front>')->setAbsolute();
// Check if the white label is in the URL in the expected pattern.
$this->assertSame($expected_patterns[$mode], $url->toString());
}
}
}
