RequestPathTest.php 4.5 KB

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