3 namespace Drupal\KernelTests\Core\Plugin\Condition;
5 use Drupal\Core\Path\CurrentPathStack;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\system\Tests\Routing\MockAliasManager;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\RequestStack;
12 * Tests that the Request Path Condition, provided by the system module, is
17 class RequestPathTest extends KernelTestBase {
20 * The condition plugin manager under test.
22 * @var \Drupal\Core\Condition\ConditionManager
24 protected $pluginManager;
27 * The path alias manager used for testing.
29 * @var \Drupal\system\Tests\Routing\MockAliasManager
31 protected $aliasManager;
34 * The request stack used for testing.
36 * @var \Symfony\Component\HttpFoundation\RequestStack
38 protected $requestStack;
45 public static $modules = ['system', 'user', 'field', 'path'];
50 * @var \Drupal\Core\Path\CurrentPathStack|\PHPUnit_Framework_MockObject_MockObject
52 protected $currentPath;
57 protected function setUp() {
60 $this->installSchema('system', ['sequences']);
62 $this->pluginManager = $this->container->get('plugin.manager.condition');
64 // Set a mock alias manager in the container.
65 $this->aliasManager = new MockAliasManager();
66 $this->container->set('path.alias_manager', $this->aliasManager);
68 // Set the test request stack in the container.
69 $this->requestStack = new RequestStack();
70 $this->container->set('request_stack', $this->requestStack);
72 $this->currentPath = new CurrentPathStack($this->requestStack);
73 $this->container->set('path.current', $this->currentPath);
77 * Tests the request path condition.
79 public function testConditions() {
81 // Get the request path condition and test and configure it to check against
82 // different patterns and requests.
84 $pages = "/my/pass/page\r\n/my/pass/page2\r\n/foo";
86 $request = Request::create('/my/pass/page2');
87 $this->requestStack->push($request);
89 /* @var \Drupal\system\Plugin\Condition\RequestPath $condition */
90 $condition = $this->pluginManager->createInstance('request_path');
91 $condition->setConfig('pages', $pages);
93 $this->aliasManager->addAlias('/my/pass/page2', '/my/pass/page2');
95 $this->assertTrue($condition->execute(), 'The request path matches a standard path');
96 $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');
98 // Test an aliased path.
99 $this->currentPath->setPath('/my/aliased/page', $request);
100 $this->requestStack->pop();
101 $this->requestStack->push($request);
103 $this->aliasManager->addAlias('/my/aliased/page', '/my/pass/page');
105 $this->assertTrue($condition->execute(), 'The request path matches an aliased path');
106 $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');
108 // Test a wildcard path.
109 $this->aliasManager->addAlias('/my/pass/page3', '/my/pass/page3');
110 $this->currentPath->setPath('/my/pass/page3', $request);
111 $this->requestStack->pop();
112 $this->requestStack->push($request);
114 $condition->setConfig('pages', '/my/pass/*');
116 $this->assertTrue($condition->evaluate(), 'The system_path my/pass/page3 passes for wildcard paths.');
117 $this->assertEqual($condition->summary(), 'Return true on the following pages: /my/pass/*', 'The condition summary matches for a wildcard path');
119 // Test a missing path.
120 $this->requestStack->pop();
121 $this->requestStack->push($request);
122 $this->currentPath->setPath('/my/fail/page4', $request);
124 $condition->setConfig('pages', '/my/pass/*');
126 $this->aliasManager->addAlias('/my/fail/page4', '/my/fail/page4');
128 $this->assertFalse($condition->evaluate(), 'The system_path /my/pass/page4 fails for a missing path.');
130 // Test a path of '/'.
131 $this->aliasManager->addAlias('/', '/my/pass/page3');
132 $this->currentPath->setPath('/', $request);
133 $this->requestStack->pop();
134 $this->requestStack->push($request);
136 $this->assertTrue($condition->evaluate(), 'The system_path my/pass/page3 passes for wildcard paths.');
137 $this->assertEqual($condition->summary(), 'Return true on the following pages: /my/pass/*', 'The condition summary matches for a wildcard path');