PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorTest.php

https://gitlab.com/reasonat/test8
PHP | 213 lines | 141 code | 30 blank | 42 comment | 0 complexity | 4861717e70563a709693f23b7a5ce05b MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\Core\PathProcessor;
  3. use Drupal\Core\Language\Language;
  4. use Drupal\Core\Language\LanguageInterface;
  5. use Drupal\Core\PathProcessor\PathProcessorAlias;
  6. use Drupal\Core\PathProcessor\PathProcessorDecode;
  7. use Drupal\Core\PathProcessor\PathProcessorFront;
  8. use Drupal\Core\PathProcessor\PathProcessorManager;
  9. use Drupal\language\HttpKernel\PathProcessorLanguage;
  10. use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Drupal\Tests\UnitTestCase;
  13. /**
  14. * Tests processing of the inbound path.
  15. *
  16. * @group PathProcessor
  17. */
  18. class PathProcessorTest extends UnitTestCase {
  19. /**
  20. * Configuration for the languageManager stub.
  21. *
  22. * @var \Drupal\Core\Language\LanguageInterface[]
  23. */
  24. protected $languages;
  25. /**
  26. * The language manager stub used to construct a PathProcessorLanguage object.
  27. *
  28. * @var \Drupal\language\ConfigurableLanguageManagerInterface|\PHPUnit_Framework_MockObject_MockBuilder
  29. */
  30. protected $languageManager;
  31. protected function setUp() {
  32. // Set up some languages to be used by the language-based path processor.
  33. $languages = array();
  34. foreach (array('en', 'fr') as $langcode) {
  35. $language = new Language(array('id' => $langcode));
  36. $languages[$langcode] = $language;
  37. }
  38. $this->languages = $languages;
  39. // Create a stub configuration.
  40. $language_prefixes = array_keys($this->languages);
  41. $config = array(
  42. 'url' => array(
  43. 'prefixes' => array_combine($language_prefixes, $language_prefixes)
  44. )
  45. );
  46. // Create a URL-based language negotiation method definition.
  47. $method_definitions = array(
  48. LanguageNegotiationUrl::METHOD_ID => array(
  49. 'class' => '\Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl',
  50. 'weight' => 9,
  51. ),
  52. );
  53. // Create a URL-based language negotiation method.
  54. $method_instance = new LanguageNegotiationUrl($config);
  55. // Create a language manager stub.
  56. $language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface')
  57. ->getMock();
  58. $language_manager->expects($this->any())
  59. ->method('getCurrentLanguage')
  60. ->will($this->returnValue($languages['en']));
  61. $language_manager->expects($this->any())
  62. ->method('getLanguages')
  63. ->will($this->returnValue($this->languages));
  64. $language_manager->expects($this->any())
  65. ->method('getLanguageTypes')
  66. ->will($this->returnValue(array(LanguageInterface::TYPE_INTERFACE)));
  67. $language_manager->expects($this->any())
  68. ->method('getNegotiationMethods')
  69. ->will($this->returnValue($method_definitions));
  70. $language_manager->expects($this->any())
  71. ->method('getNegotiationMethodInstance')
  72. ->will($this->returnValue($method_instance));
  73. $method_instance->setLanguageManager($language_manager);
  74. $this->languageManager = $language_manager;
  75. }
  76. /**
  77. * Tests resolving the inbound path to the system path.
  78. */
  79. function testProcessInbound() {
  80. // Create an alias manager stub.
  81. $alias_manager = $this->getMockBuilder('Drupal\Core\Path\AliasManager')
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $system_path_map = array(
  85. // Set up one proper alias that can be resolved to a system path.
  86. array('/foo', NULL, '/user/1'),
  87. // Passing in anything else should return the same string.
  88. array('/fr/foo', NULL, '/fr/foo'),
  89. array('/fr', NULL, '/fr'),
  90. array('/user/login', NULL, '/user/login'),
  91. );
  92. $alias_manager->expects($this->any())
  93. ->method('getPathByAlias')
  94. ->will($this->returnValueMap($system_path_map));
  95. // Create a stub config factory with all config settings that will be checked
  96. // during this test.
  97. $config_factory_stub = $this->getConfigFactoryStub(
  98. array(
  99. 'system.site' => array(
  100. 'page.front' => '/user/login'
  101. ),
  102. 'language.negotiation' => array(
  103. 'url' => array(
  104. 'prefixes' => array('fr' => 'fr'),
  105. ),
  106. ),
  107. )
  108. );
  109. // Create a language negotiator stub.
  110. $negotiator = $this->getMockBuilder('Drupal\language\LanguageNegotiatorInterface')
  111. ->getMock();
  112. $negotiator->expects($this->any())
  113. ->method('getNegotiationMethods')
  114. ->will($this->returnValue(array(LanguageNegotiationUrl::METHOD_ID => array(
  115. 'class' => 'Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl',
  116. 'weight' => 9,
  117. ))));
  118. $method = new LanguageNegotiationUrl();
  119. $method->setConfig($config_factory_stub);
  120. $method->setLanguageManager($this->languageManager);
  121. $negotiator->expects($this->any())
  122. ->method('getNegotiationMethodInstance')
  123. ->will($this->returnValue($method));
  124. // Create a user stub.
  125. $current_user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')
  126. ->getMock();
  127. // Create a config event subscriber stub.
  128. $config_subscriber = $this->getMockBuilder('Drupal\language\EventSubscriber\ConfigSubscriber')
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. // Create the processors.
  132. $alias_processor = new PathProcessorAlias($alias_manager);
  133. $decode_processor = new PathProcessorDecode();
  134. $front_processor = new PathProcessorFront($config_factory_stub);
  135. $language_processor = new PathProcessorLanguage($config_factory_stub, $this->languageManager, $negotiator, $current_user, $config_subscriber);
  136. // First, test the processor manager with the processors in the incorrect
  137. // order. The alias processor will run before the language processor, meaning
  138. // aliases will not be found.
  139. $priorities = array(
  140. 1000 => $alias_processor,
  141. 500 => $decode_processor,
  142. 300 => $front_processor,
  143. 200 => $language_processor,
  144. );
  145. // Create the processor manager and add the processors.
  146. $processor_manager = new PathProcessorManager();
  147. foreach ($priorities as $priority => $processor) {
  148. $processor_manager->addInbound($processor, $priority);
  149. }
  150. // Test resolving the French homepage using the incorrect processor order.
  151. $test_path = '/fr';
  152. $request = Request::create($test_path);
  153. $processed = $processor_manager->processInbound($test_path, $request);
  154. $this->assertEquals('/', $processed, 'Processing in the incorrect order fails to resolve the system path from the empty path');
  155. // Test resolving an existing alias using the incorrect processor order.
  156. $test_path = '/fr/foo';
  157. $request = Request::create($test_path);
  158. $processed = $processor_manager->processInbound($test_path, $request);
  159. $this->assertEquals('/foo', $processed, 'Processing in the incorrect order fails to resolve the system path from an alias');
  160. // Now create a new processor manager and add the processors, this time in
  161. // the correct order.
  162. $processor_manager = new PathProcessorManager();
  163. $priorities = array(
  164. 1000 => $decode_processor,
  165. 500 => $language_processor,
  166. 300 => $front_processor,
  167. 200 => $alias_processor,
  168. );
  169. foreach ($priorities as $priority => $processor) {
  170. $processor_manager->addInbound($processor, $priority);
  171. }
  172. // Test resolving the French homepage using the correct processor order.
  173. $test_path = '/fr';
  174. $request = Request::create($test_path);
  175. $processed = $processor_manager->processInbound($test_path, $request);
  176. $this->assertEquals('/user/login', $processed, 'Processing in the correct order resolves the system path from the empty path.');
  177. // Test resolving an existing alias using the correct processor order.
  178. $test_path = '/fr/foo';
  179. $request = Request::create($test_path);
  180. $processed = $processor_manager->processInbound($test_path, $request);
  181. $this->assertEquals('/user/1', $processed, 'Processing in the correct order resolves the system path from an alias.');
  182. }
  183. }