RequestDomainTest.php in Context 8.4
File
tests/src/Kernel/RequestDomainTest.php
View source
<?php
namespace Drupal\Tests\context\Kernel;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class RequestDomainTest extends KernelTestBase {
protected static $modules = [
'context',
];
protected $pluginManager;
protected $requestStack;
protected $currentPath;
protected function setUp() : void {
parent::setUp();
$this->pluginManager = $this->container
->get('plugin.manager.condition');
$this->requestStack = new RequestStack();
$this->container
->set('request_stack', $this->requestStack);
$this->currentPath = new CurrentPathStack($this->requestStack);
$this->container
->set('path.current', $this->currentPath);
$this->container
->set('current_route_match', new CurrentRouteMatch($this->requestStack));
}
public function testRequestDomain() {
$request = Request::create('/');
$this->requestStack
->push($request);
$domain = $request
->getHost();
$condition = $this->pluginManager
->createInstance('request_domain');
$condition
->setConfig('domains', $domain);
$this
->assertTrue($condition
->execute(), 'Domains match');
$this
->assertEquals($condition
->summary(), 'Return true on the following domains: ' . $domain, 'Domains match.');
$condition
->setConfig('domains', 'example.com');
$this
->assertFalse($condition
->execute(), 'Domains do not match');
}
public function testRequestDomainNegate() {
$request = Request::create('/');
$this->requestStack
->push($request);
$domain = $request
->getHost();
$condition = $this->pluginManager
->createInstance('request_domain');
$condition
->setConfig('negate', 1);
$condition
->setConfig('domains', $domain);
$this
->assertFalse($condition
->execute(), 'Domains do not match');
$this
->assertEquals($condition
->summary(), 'Do not return true on the following domains: ' . $domain, 'Domains match.');
$condition
->setConfig('domains', 'example.com');
$this
->assertTrue($condition
->execute(), 'Domains match');
}
}