diff --git a/core/lib/Drupal/Core/Condition/ConditionManager.php b/core/lib/Drupal/Core/Condition/ConditionManager.php index cf35044..ce735e7 100644 --- a/core/lib/Drupal/Core/Condition/ConditionManager.php +++ b/core/lib/Drupal/Core/Condition/ConditionManager.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Condition; use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; -use Drupal\Component\Plugin\Factory\DefaultFactory; +use Drupal\Component\Plugin\Factory\ContainerFactory; use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Core\Executable\ExecutableInterface; use Drupal\Core\Executable\ExecutableManagerInterface; @@ -35,7 +35,7 @@ public function __construct(\Traversable $namespaces) { $this->discovery = new AlterDecorator($this->discovery, 'condition_info'); $this->discovery = new CacheDecorator($this->discovery, 'condition:' . language(Language::TYPE_INTERFACE)->langcode); - $this->factory = new DefaultFactory($this); + $this->factory = new ContainerFactory($this); } /** diff --git a/core/lib/Drupal/Core/Plugin/ContainerFactoryPluginInterface.php b/core/lib/Drupal/Core/Plugin/ContainerFactoryPluginInterface.php new file mode 100644 index 0000000..76dc2d7 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/ContainerFactoryPluginInterface.php @@ -0,0 +1,33 @@ + 'textarea', - '#title' => t('Pages'), - '#default_value' => !empty($this->configuration['pages']) ? $this->configuration['pages'] : '', - '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %user for the current user's page and %user-wildcard for every user page. %front is the front page.", array('%user' => 'user', '%user-wildcard' => 'user/*', '%front' => '')), - ); - return parent::form($form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function submit($form, &$form_state) { - $this->configuration['pages'] = $form_state['values']['pages']; - parent::submit($form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function summary() { - $pages = array(); - foreach (explode("\n", $this->configuration['pages']) as $page) { - $pages[] = trim($page); - } - $pages = implode(', ', $pages); - if (!empty($this->configuration['negate'])) { - return t('Do not return true on the following pages: @pages', array('@pages' => $pages)); - } - return t('Return true on the following pages: @pages', array('@pages' => $pages)); - } - - /** - * {@inheritdoc} - */ - public function evaluate() { - // Convert path to lowercase. This allows comparison of the same path - // with different case. Ex: /Page, /page, /PAGE. - $pages = Unicode::strtolower($this->configuration['pages']); - // Compare the lowercase path alias (if any) and internal path. - $request = $this->getContextValue('request'); - $pathMatcher = $this->getContextValue('path_matcher'); - $aliasManager = $this->getContextValue('alias_manager'); - $path = $request->attributes->get('system_path'); - $path_alias = Unicode::strtolower($aliasManager->getPathAlias($path)); - - return $pathMatcher->matchPath($path_alias, $pages) || (($path != $path_alias) && $pathMatcher->matchPath($path, $pages)); - } - -} diff --git a/core/modules/system/lib/Drupal/system/Plugin/Core/Condition/RequestPath.php b/core/modules/system/lib/Drupal/system/Plugin/Core/Condition/RequestPath.php new file mode 100644 index 0000000..bf0661c --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/Core/Condition/RequestPath.php @@ -0,0 +1,122 @@ +get('request'), $container->get('alias_manager'), $container->get('path_matcher'), $configuration, $plugin_id, $plugin_definition); + } + + /** + * Constructs a RequestPath condition plugin. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The HttpRequest object representing the current request. + * + * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager + * An alias manager to find the alias for the current system path. + * + * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher + * A path matcher for matching the path against a pattern. + */ + public function __construct(Request $request, AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, array $configuration, $plugin_id, array $plugin_definition) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->request = $request; + $this->aliasManager = $alias_manager; + $this->pathMatcher = $path_matcher; + } + + /** + * {@inheritdoc} + */ + public function form($form, &$form_state) { + $form['pages'] = array( + '#type' => 'textarea', + '#title' => t('Pages'), + '#default_value' => !empty($this->configuration['pages']) ? $this->configuration['pages'] : '', + '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %user for the current user's page and %user-wildcard for every user page. %front is the front page.", array('%user' => 'user', '%user-wildcard' => 'user/*', '%front' => '')), + ); + return parent::form($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submit($form, &$form_state) { + $this->configuration['pages'] = $form_state['values']['pages']; + parent::submit($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function summary() { + $pages = array(); + foreach (explode("\n", $this->configuration['pages']) as $page) { + $pages[] = trim($page); + } + $pages = implode(', ', $pages); + if (!empty($this->configuration['negate'])) { + return t('Do not return true on the following pages: @pages', array('@pages' => $pages)); + } + return t('Return true on the following pages: @pages', array('@pages' => $pages)); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + // Convert path to lowercase. This allows comparison of the same path + // with different case. Ex: /Page, /page, /PAGE. + $pages = Unicode::strtolower($this->configuration['pages']); + // Compare the lowercase path alias (if any) and internal path. + $path = $this->request->attributes->get('system_path'); + $path_alias = Unicode::strtolower($this->aliasManager->getPathAlias($path)); + return $this->pathMatcher->matchPath($path_alias, $pages) || (($path != $path_alias) && $this->pathMatcher->matchPath($path, $pages)); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Condition/RequestPathConditionTest.php b/core/modules/system/lib/Drupal/system/Tests/Condition/RequestPathConditionTest.php deleted file mode 100644 index 8bf71d8..0000000 --- a/core/modules/system/lib/Drupal/system/Tests/Condition/RequestPathConditionTest.php +++ /dev/null @@ -1,85 +0,0 @@ - 'Request Path Condition Plugin', - 'description' => 'Tests that the Request Path Condition, provided by the system module, is working properly.', - 'group' => 'Condition API', - ); - } - - protected function setUp() { - parent::setUp(); - - $this->manager = $this->container->get('plugin.manager.condition'); - } - - /** - * Test the RequestPath condition. - */ - public function testConditions() { - // Fabricate a new request. - $new_request = Request::create('http://test.com/my/pass/page'); - // The condition checks the system_path attribute of the request so we must - // set that to the path we want to evaluate. - $new_request->attributes->set('system_path', 'my/pass/page'); - - $pages = "my/pass/page\r\nmy/pass/page2"; - $condition = $this->manager->createInstance('request_path') - ->setConfig('pages', $pages) - ->setContextValue('request', $new_request) - ->setContextValue('alias_manager', $this->container->get('path.alias_manager')) - ->setContextValue('path_matcher', $this->container->get('path_matcher')); - - // Check the first configured path and summary. - $this->assertTrue($condition->execute(), 'The system_path my/pass/page passes.'); - $this->assertIdentical($condition->summary(), 'Return true on the following pages: my/pass/page, my/pass/page2'); - - // Check the second configured path. - $new_request->attributes->set('system_path', 'my/pass/page2'); - $this->assertTrue($condition->execute(), 'The system_path my/pass/page2 passes.'); - - $condition->setConfig('pages', 'my/pass/*'); - $this->assertTrue($condition->execute(), 'The system_path my/pass/page2 passes for wildcard paths.'); - - // Set the pages to a new path. - $condition->setConfig('pages', 'my/fail/page'); - $this->assertFalse($condition->execute(), 'The system_path my/fail/page fails.'); - $this->assertIdentical($condition->summary(), 'Return true on the following pages: my/fail/page'); - - // Negate the condition; check that execution and summary are still proper. - $condition->setConfig('negate', TRUE); - $this->assertTrue($condition->execute(), 'The system_path my/fail/page is properly negated.'); - $this->assertIdentical($condition->summary(), 'Do not return true on the following pages: my/fail/page'); - } - -} diff --git a/core/modules/system/tests/Drupal/system/Tests/Condition/RequestPathConditionTest.php b/core/modules/system/tests/Drupal/system/Tests/Condition/RequestPathConditionTest.php new file mode 100644 index 0000000..f92fd47 --- /dev/null +++ b/core/modules/system/tests/Drupal/system/Tests/Condition/RequestPathConditionTest.php @@ -0,0 +1,95 @@ + 'Request Path Condition Plugin', + 'description' => 'Tests that the Request Path Condition, provided by the system module, is working properly.', + 'group' => 'Condition API', + ); + } + + /** + * Tests the RequestPath condition. + */ + function testConditions() { + + $condition_manager = $this->getMockBuilder('Drupal\Core\Condition\ConditionManager') + ->disableOriginalConstructor() + ->getMock(); + + // Create an alias manager stub. + $alias_manager = $this->getMockBuilder('Drupal\Core\Path\AliasManager') + ->disableOriginalConstructor() + ->getMock(); + + $system_path_map = array( + // Set up one proper alias that can be resolved to a system path. + array('my/aliased/page', NULL, 'foo'), + ); + + $alias_manager->expects($this->any()) + ->method('getPathAlias') + ->will($this->returnValueMap($system_path_map)); + + // Create a stub config factory with all config settings that will be checked + // during this test. + $config_factory_stub = $this->getConfigFactoryStub( + array( + 'system.site' => array( + 'page.front' => 'user' + ), + ) + ); + $path_matcher = new PathMatcher($config_factory_stub); + $new_request = Request::create('http://test.com/my/pass/page'); + // The condition checks the system_path attribute of the request so we must + // set that to the path we want to evaluate. + $new_request->attributes->set('system_path', 'my/pass/page'); + + $condition = new RequestPath($new_request, $alias_manager, $path_matcher, array(), 'request_path', array()); + $condition->setExecutableManager($condition_manager); + + $pages = "my/pass/page\r\nmy/pass/page2\r\nfoo"; + $condition->setConfig('pages', $pages); + + // Check the first configured path and summary. + $this->assertTrue($condition->evaluate(), 'The system_path my/pass/page passes.'); + + // Check the second configured path. + $new_request->attributes->set('system_path', 'my/pass/page2'); + $this->assertTrue($condition->evaluate(), 'The system_path my/pass/page2 passes.'); + + // Check that a system path that is not included in the pages config, but whose + // alias is, passes. + $new_request->attributes->set('system_path', 'my/aliased/page'); + $this->assertTrue($condition->evaluate(), 'The system_path my/aliased/page passes.'); + + $new_request->attributes->set('system_path', 'my/pass/page3'); + $condition->setConfig('pages', 'my/pass/*'); + $this->assertTrue($condition->evaluate(), 'The system_path my/pass/page3 passes for wildcard paths.'); + + // Check that a system path that is not included in the pages config, either + // directly or via an alias, fails. + $new_request->attributes->set('system_path', 'my/fail/page4'); + $this->assertFalse($condition->evaluate(), 'The system_path my/fail/page4 fails.'); + + } +}