View source
<?php
namespace Drupal\KernelTests\Core\Plugin\Condition;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\KernelTests\KernelTestBase;
use Drupal\system\Tests\Routing\MockAliasManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class RequestPathTest extends KernelTestBase {
protected $pluginManager;
protected $aliasManager;
protected $requestStack;
public static $modules = [
'system',
'user',
'field',
'path',
];
protected $currentPath;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this->pluginManager = $this->container
->get('plugin.manager.condition');
$this->aliasManager = new MockAliasManager();
$this->container
->set('path_alias.manager', $this->aliasManager);
$this->requestStack = new RequestStack();
$this->container
->set('request_stack', $this->requestStack);
$this->currentPath = new CurrentPathStack($this->requestStack);
$this->container
->set('path.current', $this->currentPath);
}
public function testConditions() {
$pages = "/my/pass/page\r\n/my/pass/page2\r\n/foo";
$request = Request::create('/my/pass/page2');
$this->requestStack
->push($request);
$condition = $this->pluginManager
->createInstance('request_path');
$condition
->setConfig('pages', $pages);
$this->aliasManager
->addAlias('/my/pass/page2', '/my/pass/page2');
$this
->assertTrue($condition
->execute(), 'The request path matches a standard path');
$this
->assertEqual($condition
->summary(), 'Return true on the following pages: /my/pass/page, /my/pass/page2, /foo', 'The condition summary matches for a standard path');
$this->currentPath
->setPath('/my/aliased/page', $request);
$this->requestStack
->pop();
$this->requestStack
->push($request);
$this->aliasManager
->addAlias('/my/aliased/page', '/my/pass/page');
$this
->assertTrue($condition
->execute(), 'The request path matches an aliased path');
$this
->assertEqual($condition
->summary(), 'Return true on the following pages: /my/pass/page, /my/pass/page2, /foo', 'The condition summary matches for an aliased path');
$this->aliasManager
->addAlias('/my/pass/page3', '/my/pass/page3');
$this->currentPath
->setPath('/my/pass/page3', $request);
$this->requestStack
->pop();
$this->requestStack
->push($request);
$condition
->setConfig('pages', '/my/pass/*');
$this
->assertTrue($condition
->evaluate(), 'The system_path my/pass/page3 passes for wildcard paths.');
$this
->assertEqual($condition
->summary(), 'Return true on the following pages: /my/pass/*', 'The condition summary matches for a wildcard path');
$this->requestStack
->pop();
$this->requestStack
->push($request);
$this->currentPath
->setPath('/my/fail/page4', $request);
$condition
->setConfig('pages', '/my/pass/*');
$this->aliasManager
->addAlias('/my/fail/page4', '/my/fail/page4');
$this
->assertFalse($condition
->evaluate(), 'The system_path /my/pass/page4 fails for a missing path.');
$this->aliasManager
->addAlias('/', '/my/pass/page3');
$this->currentPath
->setPath('/', $request);
$this->requestStack
->pop();
$this->requestStack
->push($request);
$this
->assertTrue($condition
->evaluate(), 'The system_path my/pass/page3 passes for wildcard paths.');
$this
->assertEqual($condition
->summary(), 'Return true on the following pages: /my/pass/*', 'The condition summary matches for a wildcard path');
}
}