Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Plugin / Condition / RequestPathTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Plugin\Condition;
4
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;
10
11 /**
12  * Tests that the Request Path Condition, provided by the system module, is
13  * working properly.
14  *
15  * @group Plugin
16  */
17 class RequestPathTest extends KernelTestBase {
18
19   /**
20    * The condition plugin manager under test.
21    *
22    * @var \Drupal\Core\Condition\ConditionManager
23    */
24   protected $pluginManager;
25
26   /**
27    * The path alias manager used for testing.
28    *
29    * @var \Drupal\system\Tests\Routing\MockAliasManager
30    */
31   protected $aliasManager;
32
33   /**
34    * The request stack used for testing.
35    *
36    * @var \Symfony\Component\HttpFoundation\RequestStack
37    */
38   protected $requestStack;
39
40   /**
41    * Modules to enable.
42    *
43    * @var array
44    */
45   public static $modules = ['system', 'user', 'field', 'path'];
46
47   /**
48    * The current path.
49    *
50    * @var \Drupal\Core\Path\CurrentPathStack|\PHPUnit_Framework_MockObject_MockObject
51    */
52   protected $currentPath;
53
54   /**
55    * {@inheritdoc}
56    */
57   protected function setUp() {
58     parent::setUp();
59
60     $this->installSchema('system', ['sequences']);
61
62     $this->pluginManager = $this->container->get('plugin.manager.condition');
63
64     // Set a mock alias manager in the container.
65     $this->aliasManager = new MockAliasManager();
66     $this->container->set('path.alias_manager', $this->aliasManager);
67
68     // Set the test request stack in the container.
69     $this->requestStack = new RequestStack();
70     $this->container->set('request_stack', $this->requestStack);
71
72     $this->currentPath = new CurrentPathStack($this->requestStack);
73     $this->container->set('path.current', $this->currentPath);
74   }
75
76   /**
77    * Tests the request path condition.
78    */
79   public function testConditions() {
80
81     // Get the request path condition and test and configure it to check against
82     // different patterns and requests.
83
84     $pages = "/my/pass/page\r\n/my/pass/page2\r\n/foo";
85
86     $request = Request::create('/my/pass/page2');
87     $this->requestStack->push($request);
88
89     /* @var \Drupal\system\Plugin\Condition\RequestPath $condition */
90     $condition = $this->pluginManager->createInstance('request_path');
91     $condition->setConfig('pages', $pages);
92
93     $this->aliasManager->addAlias('/my/pass/page2', '/my/pass/page2');
94
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');
97
98     // Test an aliased path.
99     $this->currentPath->setPath('/my/aliased/page', $request);
100     $this->requestStack->pop();
101     $this->requestStack->push($request);
102
103     $this->aliasManager->addAlias('/my/aliased/page', '/my/pass/page');
104
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');
107
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);
113
114     $condition->setConfig('pages', '/my/pass/*');
115
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');
118
119     // Test a missing path.
120     $this->requestStack->pop();
121     $this->requestStack->push($request);
122     $this->currentPath->setPath('/my/fail/page4', $request);
123
124     $condition->setConfig('pages', '/my/pass/*');
125
126     $this->aliasManager->addAlias('/my/fail/page4', '/my/fail/page4');
127
128     $this->assertFalse($condition->evaluate(), 'The system_path /my/pass/page4 fails for a missing path.');
129
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);
135
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');
138
139   }
140
141 }