diff --git a/core/modules/system/lib/Drupal/system/Plugin/Condition/RequestPath.php b/core/modules/system/src/Plugin/Condition/RequestPath.php similarity index 89% rename from core/modules/system/lib/Drupal/system/Plugin/Condition/RequestPath.php rename to core/modules/system/src/Plugin/Condition/RequestPath.php index 32ba4c3..b29e46c 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/Condition/RequestPath.php +++ b/core/modules/system/src/Plugin/Condition/RequestPath.php @@ -13,6 +13,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * Provides a 'Request Path' condition. @@ -20,11 +21,6 @@ * @Condition( * id = "request_path", * label = @Translation("Request Path"), - * context = { - * "request" = { - * "class" = "Symfony\Component\HttpFoundation\Request" - * } - * } * ) */ class RequestPath extends ConditionPluginBase implements ContainerFactoryPluginInterface { @@ -37,10 +33,19 @@ class RequestPath extends ConditionPluginBase implements ContainerFactoryPluginI protected $aliasManager; /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** * Constructs a RequestPath condition plugin. * * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager * An alias manager to find the alias for the current system path. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id @@ -48,9 +53,10 @@ class RequestPath extends ConditionPluginBase implements ContainerFactoryPluginI * @param array $plugin_definition * The plugin implementation definition. */ - public function __construct(AliasManagerInterface $alias_manager, array $configuration, $plugin_id, array $plugin_definition) { + public function __construct(AliasManagerInterface $alias_manager, RequestStack $request_stack, array $configuration, $plugin_id, array $plugin_definition) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->aliasManager = $alias_manager; + $this->requestStack = $request_stack; } /** @@ -59,6 +65,7 @@ public function __construct(AliasManagerInterface $alias_manager, array $configu public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $container->get('path.alias_manager.cached'), + $container->get('request_stack'), $configuration, $plugin_id, $plugin_definition); @@ -116,8 +123,7 @@ public function evaluate() { // with different case. Ex: /Page, /page, /PAGE. $pages = Unicode::strtolower($this->configuration['pages']); - /* @var \Symfony\Component\HttpFoundation\Request $request */ - $request = $this->getContextValue('request'); + $request = $this->requestStack->getCurrentRequest(); // Compare the lowercase path alias (if any) and internal path. $path = $request->attributes->get('_system_path'); $path_alias = Unicode::strtolower($this->aliasManager->getAliasByPath($path)); diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/Condition/RequestPathTest.php b/core/modules/system/src/Tests/Plugin/Condition/RequestPathTest.php similarity index 82% rename from core/modules/system/lib/Drupal/system/Tests/Plugin/Condition/RequestPathTest.php rename to core/modules/system/src/Tests/Plugin/Condition/RequestPathTest.php index 60640e5..e8c6a5e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/Condition/RequestPathTest.php +++ b/core/modules/system/src/Tests/Plugin/Condition/RequestPathTest.php @@ -10,6 +10,7 @@ use Drupal\simpletest\KernelTestBase; use Drupal\system\Tests\Routing\MockAliasManager; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; /** * Tests the Request Path condition plugin. @@ -31,6 +32,13 @@ class RequestPathTest extends KernelTestBase { protected $aliasManager; /** + * The request stack used for testing. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** * Modules to enable. * * @var array @@ -61,6 +69,10 @@ protected function setUp() { // Set a mock alias manager in the container. $this->aliasManager = new MockAliasManager(); $this->container->set('path.alias_manager.cached', $this->aliasManager); + + // Set the test request stack in the container. + $this->requestStack = new RequestStack(); + $this->container->set('request_stack', $this->requestStack); } /** @@ -75,11 +87,11 @@ public function testConditions() { $request = Request::create('/my/pass/page2'); $request->attributes->set('_system_path', 'my/pass/page2'); + $this->requestStack->push($request); /* @var \Drupal\system\Plugin\Condition\RequestPath $condition */ $condition = $this->pluginManager->createInstance('request_path'); - $condition->setConfig('pages', $pages) - ->setContextValue('request', $request); + $condition->setConfig('pages', $pages); $this->aliasManager->addAlias('my/pass/page2', 'my/pass/page2'); @@ -88,9 +100,10 @@ public function testConditions() { // Test an aliased path. $request->attributes->set('_system_path', 'my/aliased/page'); - $this->aliasManager->addAlias('my/aliased/page', 'my/pass/page'); + $this->requestStack->pop(); + $this->requestStack->push($request); - $condition->setContextValue('request', $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'); @@ -98,16 +111,20 @@ public function testConditions() { // Test a wildcard path. $this->aliasManager->addAlias('my/pass/page3', 'my/pass/page3'); $request->attributes->set('_system_path', 'my/pass/page3'); - $condition->setConfig('pages', 'my/pass/*') - ->setContextValue('request', $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'); // Test a missing path. $request->attributes->set('_system_path', 'my/fail/page4'); - $condition->setConfig('pages', 'my/pass/*') - ->setContextValue('request', $request); + $this->requestStack->pop(); + $this->requestStack->push($request); + + $condition->setConfig('pages', 'my/pass/*'); $this->aliasManager->addAlias('my/fail/page4', 'my/fail/page4');