whitelabel-8.x-2.x-dev/tests/src/Functional/WhiteLabelInboundPathProcessingTest.php
tests/src/Functional/WhiteLabelInboundPathProcessingTest.php
<?php
namespace Drupal\Tests\whitelabel\Functional;
use Drupal\Core\Url;
use Drupal\Tests\user\Traits\UserCreationTrait;;
use Drupal\whitelabel\Plugin\WhiteLabelNegotiation\WhiteLabelNegotiationUrl;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Tests if the correct style sheets are applied to a page.
*
* @group whitelabel
*/
class WhiteLabelInboundPathProcessingTest extends WhiteLabelTestBase {
use UserCreationTrait {
createUser as drupalCreateUser;
}
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'field',
'text',
'options',
'user',
'file',
'image',
'whitelabel',
'node',
'views',
];
/**
* Holds the randomly generated token.
*
* @var string
*/
private $token;
/**
* Holds the randomly generated query string parameter.
*
* @var string
*/
private $queryStringIdentifier;
/**
* The PathProcessorManager.
*
* @var \Drupal\Core\PathProcessor\PathProcessorManager
*/
private $pathProcessorManager;
/**
* The WhiteLabelNegotiatorInterface.
*
* @var \Drupal\whitelabel\WhiteLabelNegotiatorInterface
*/
private $whiteLabelNegotiator;
/**
* Constant that holds the new white label token.
*/
const WHITE_LABEL_TOKEN = 'cool_whitelabel_token';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$user = $this->drupalCreateUser(['serve white label pages']);
$this->setCurrentUser($user);
$this->token = self::WHITE_LABEL_TOKEN;
$this->queryStringIdentifier = $this->randomMachineName(16);
$this->whiteLabel = $this->createWhiteLabel(['token' => $this->token]);
$this->whiteLabelNegotiator = \Drupal::service('whitelabel.whitelabel_negotiator');
// Set the global white label configuration.
$this->config('whitelabel.negotiation')
->set('negotiator_settings.url.settings.query_string_identifier', $this->queryStringIdentifier)
->set('negotiator_settings.url.settings.domain', 'http://localhost')
->save();
// Needed for correct resolving the system front page.
$this->config('system.site')
->set('page.front', '/node')
->save();
}
/**
* Test for the inbound query parameter mode.
*
* @dataProvider provideTokensBasicTests
*/
public function testSimpleInboundRequests($mode, $token = NULL, $path = '/', $expect_whitelabel = TRUE, $expect_404 = FALSE) {
// Query parameter mode will never throw an exception.
if ($mode === WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER) {
$expect_404 = FALSE;
}
// Ensure White Label is empty.
$this->assertNull($this->whiteLabelNegotiator->init());
// Set the test specific white label configuration.
$this->config('whitelabel.negotiation')->set('negotiator_settings.url.settings.mode', $mode)->save();
$path = $this->generateRequest($mode, $token, $path);
if ($expect_404) {
$this->expectException(NotFoundHttpException::class);
}
$this->drupalGet($path);
// Ensure White Label equals the expected one.
if ($expect_whitelabel) {
/** @var \Drupal\whitelabel\Entity\WhiteLabelInterface $whitelabel */
$whitelabel = $this->whiteLabelNegotiator->init();
$this->assertEquals($this->whiteLabel->id(), $whitelabel->id());
}
else {
$this->assertNull($this->whiteLabelNegotiator->init());
}
}
/**
* Data provider.
*
* @return array
* An array with a mode, token, expected white label and expected 404.
*/
public function provideTokensBasicTests() {
$return = [];
// Query parameter mode.
// No token.
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, NULL, '/', FALSE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, NULL, '/node', FALSE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, NULL, '/node/add', FALSE, FALSE];
// Existing token.
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, self::WHITE_LABEL_TOKEN, '/', TRUE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, self::WHITE_LABEL_TOKEN, '/node', TRUE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, self::WHITE_LABEL_TOKEN, '/node/add', TRUE, FALSE];
// Non-existing token.
// This combination can never cause an exception.
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, 'someFakeToken', '/', FALSE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, 'someFakeToken', '/node', FALSE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER, 'someFakeToken', '/node/add', FALSE, FALSE];
/* This negotiation method is disabled.
// Path prefix mode.
// No token.
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, NULL, '/', FALSE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, NULL, '/node', FALSE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, NULL, '/node/add', FALSE, FALSE];
// Existing token.
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, self::WHITE_LABEL_TOKEN, '/', TRUE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, self::WHITE_LABEL_TOKEN, '/node', TRUE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, self::WHITE_LABEL_TOKEN, '/node/add', TRUE, FALSE];
// Non-existing token.
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, 'someFakeToken', '/', FALSE, TRUE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, 'someFakeToken', '/node', FALSE, TRUE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX, 'someFakeToken', '/node/add', FALSE, TRUE];
*/
// Path prefix mode.
// No token.
// This scenario does not exist.
// Existing token.
$return[] = [WhiteLabelNegotiationUrl::CONFIG_DOMAIN, self::WHITE_LABEL_TOKEN, '/', TRUE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_DOMAIN, self::WHITE_LABEL_TOKEN, '/node', TRUE, FALSE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_DOMAIN, self::WHITE_LABEL_TOKEN, '/node/add', TRUE, FALSE];
// Non-existing token.
$return[] = [WhiteLabelNegotiationUrl::CONFIG_DOMAIN, 'someFakeToken', '/', FALSE, TRUE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_DOMAIN, 'someFakeToken', '/node', FALSE, TRUE];
$return[] = [WhiteLabelNegotiationUrl::CONFIG_DOMAIN, 'someFakeToken', '/node/add', FALSE, TRUE];
return $return;
}
/**
* Test to see if tokens based on system paths are correctly resolved.
*
* @dataProvider provideTokensTrickyTokenTests
*/
public function testTrickyTokens($mode, $tricky_token, $tricky_path) {
// Ensure White Label is empty.
$this->assertNull($this->whiteLabelNegotiator->init());
// Set the test specific white label configuration.
$this->config('whitelabel.negotiation')->set('negotiator_settings.url.settings.mode', $mode)->save();
$this->whiteLabel->setToken($tricky_token)->save();
$path = $this->generateRequest($mode, $tricky_token, $tricky_path);
$this->drupalGet($path);
/** @var \Drupal\whitelabel\Entity\WhiteLabelInterface $whitelabel */
$whitelabel = $this->whiteLabelNegotiator->init();
$this->assertEquals($this->whiteLabel->id(), $whitelabel->id(), 'Asserting token: ' . $tricky_token . ' for path: ' . $tricky_path);
}
/**
* Data provider.
*
* @return array
* An array with a mode, white label token and a path.
*/
public function provideTokensTrickyTokenTests() {
$modes = array_keys(WhiteLabelNegotiationUrl::getModes());
$tricky_tokens = [
'admin',
'node',
'system',
'user',
];
$return = [];
foreach ($modes as $mode) {
foreach ($tricky_tokens as $tricky_token) {
foreach ($tricky_tokens as $tricky_path) {
// Remove paths that do not serve a page.
if (in_array($tricky_path, ['system'])) {
break;
}
$return["$mode|$tricky_token|$tricky_path"] = [$mode, $tricky_token, '/' . $tricky_path];
}
}
}
return $return;
}
/**
* Test to see if less privileged user gets a no WL page.
*
* @dataProvider provideModes
*/
public function testViewerNoPermissions($mode) {
// Test with unprivileged user.
$white_label_viewer = $this->drupalCreateUser();
$this->setCurrentUser($white_label_viewer);
// Ensure that no White Label is provided.
$this->assertNull($this->whiteLabelNegotiator->init());
// Set the test specific white label configuration.
$this->config('whitelabel.negotiation')->set('negotiator_settings.url.settings.mode', $mode)->save();
$path = $this->generateRequest($mode, $this->token);
if ($mode == WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX || $mode == WhiteLabelNegotiationUrl::CONFIG_DOMAIN) {
$this->expectException(NotFoundHttpException::class);
}
$this->drupalGet($path);
// Ensure White Label is empty.
$this->assertNull($this->whiteLabelNegotiator->init());
}
/**
* Test to see if less privileged owner serves a no WL page.
*
* @dataProvider provideModes
*/
public function testOwnerNoPermissions($mode) {
// Test with regular viewer account.
$white_label_viewer = $this->drupalCreateUser(['view white label pages']);
$this->setCurrentUser($white_label_viewer);
// Change the owner of the white label to a less privileged one.
$white_label_owner = $this->drupalCreateUser();
$this->whiteLabel->setOwner($white_label_owner)->save();
// Ensure that no White Label is provided.
$this->assertNull($this->whiteLabelNegotiator->init());
// Set the test specific white label configuration.
$this->config('whitelabel.negotiation')->set('negotiator_settings.url.settings.mode', $mode)->save();
$path = $this->generateRequest($mode, $this->token);
if ($mode == WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX || $mode == WhiteLabelNegotiationUrl::CONFIG_DOMAIN) {
$this->expectException(NotFoundHttpException::class);
}
$this->drupalGet($path);
// Ensure White Label is empty.
$this->assertNull($this->whiteLabelNegotiator->init());
}
/**
* Data provider for tests.
*
* @return array
* Return an array with White label modes
*/
public static function provideModes() {
$modes = array_keys(WhiteLabelNegotiationUrl::getModes());
foreach ($modes as &$mode) {
$mode = [$mode];
}
return $modes;
}
/**
* Constructs an incoming request for the given mode and token.
*
* @param string $mode
* The white label mode to return the request for.
* @param string|null $token
* The token to use when constructing the request, or NULL to use default.
* @param string $path
* The path to take into account while generating the request.
*
* @return string
* A string with the generated path.
*/
protected function generateRequest($mode, $token = NULL, $path = '/') {
switch ($mode) {
case WhiteLabelNegotiationUrl::CONFIG_QUERY_PARAMETER:
$path = Url::fromUserInput($path, [
'query' => [$this->queryStringIdentifier => $token],
]);
break;
case WhiteLabelNegotiationUrl::CONFIG_PATH_PREFIX:
$path = $path ?? $token . '/' . $path;
$path = Url::fromUserInput($path);
break;
case WhiteLabelNegotiationUrl::CONFIG_DOMAIN:
$http_host = empty($token) ? 'localhost' : $token . '.localhost';
$request = Request::create($path, 'GET', [], [], [], ['HTTP_HOST' => $http_host]);
$this->container->get('request_stack')->push($request);
$path = Url::fromUserInput($path);
break;
}
return $path;
}
}
