View source
<?php
namespace Drupal\Tests\Core\PathProcessor;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\PathProcessor\PathProcessorAlias;
use Drupal\Core\PathProcessor\PathProcessorDecode;
use Drupal\Core\PathProcessor\PathProcessorFront;
use Drupal\Core\PathProcessor\PathProcessorManager;
use Drupal\language\HttpKernel\PathProcessorLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Tests\UnitTestCase;
class PathProcessorTest extends UnitTestCase {
protected $languages;
protected $languageManager;
protected function setUp() {
$languages = array();
foreach (array(
'en',
'fr',
) as $langcode) {
$language = new Language(array(
'id' => $langcode,
));
$languages[$langcode] = $language;
}
$this->languages = $languages;
$language_prefixes = array_keys($this->languages);
$config = array(
'url' => array(
'prefixes' => array_combine($language_prefixes, $language_prefixes),
),
);
$method_definitions = array(
LanguageNegotiationUrl::METHOD_ID => array(
'class' => '\\Drupal\\language\\Plugin\\LanguageNegotiation\\LanguageNegotiationUrl',
'weight' => 9,
),
);
$method_instance = new LanguageNegotiationUrl($config);
$language_manager = $this
->getMockBuilder('Drupal\\language\\ConfigurableLanguageManagerInterface')
->getMock();
$language_manager
->expects($this
->any())
->method('getCurrentLanguage')
->will($this
->returnValue($languages['en']));
$language_manager
->expects($this
->any())
->method('getLanguages')
->will($this
->returnValue($this->languages));
$language_manager
->expects($this
->any())
->method('getLanguageTypes')
->will($this
->returnValue(array(
LanguageInterface::TYPE_INTERFACE,
)));
$language_manager
->expects($this
->any())
->method('getNegotiationMethods')
->will($this
->returnValue($method_definitions));
$language_manager
->expects($this
->any())
->method('getNegotiationMethodInstance')
->will($this
->returnValue($method_instance));
$method_instance
->setLanguageManager($language_manager);
$this->languageManager = $language_manager;
}
function testProcessInbound() {
$alias_manager = $this
->getMockBuilder('Drupal\\Core\\Path\\AliasManager')
->disableOriginalConstructor()
->getMock();
$system_path_map = array(
array(
'/foo',
NULL,
'/user/1',
),
array(
'/fr/foo',
NULL,
'/fr/foo',
),
array(
'/fr',
NULL,
'/fr',
),
array(
'/user/login',
NULL,
'/user/login',
),
);
$alias_manager
->expects($this
->any())
->method('getPathByAlias')
->will($this
->returnValueMap($system_path_map));
$config_factory_stub = $this
->getConfigFactoryStub(array(
'system.site' => array(
'page.front' => '/user/login',
),
'language.negotiation' => array(
'url' => array(
'prefixes' => array(
'fr' => 'fr',
),
),
),
));
$negotiator = $this
->getMockBuilder('Drupal\\language\\LanguageNegotiatorInterface')
->getMock();
$negotiator
->expects($this
->any())
->method('getNegotiationMethods')
->will($this
->returnValue(array(
LanguageNegotiationUrl::METHOD_ID => array(
'class' => 'Drupal\\language\\Plugin\\LanguageNegotiation\\LanguageNegotiationUrl',
'weight' => 9,
),
)));
$method = new LanguageNegotiationUrl();
$method
->setConfig($config_factory_stub);
$method
->setLanguageManager($this->languageManager);
$negotiator
->expects($this
->any())
->method('getNegotiationMethodInstance')
->will($this
->returnValue($method));
$current_user = $this
->getMockBuilder('Drupal\\Core\\Session\\AccountInterface')
->getMock();
$config_subscriber = $this
->getMockBuilder('Drupal\\language\\EventSubscriber\\ConfigSubscriber')
->disableOriginalConstructor()
->getMock();
$alias_processor = new PathProcessorAlias($alias_manager);
$decode_processor = new PathProcessorDecode();
$front_processor = new PathProcessorFront($config_factory_stub);
$language_processor = new PathProcessorLanguage($config_factory_stub, $this->languageManager, $negotiator, $current_user, $config_subscriber);
$priorities = array(
1000 => $alias_processor,
500 => $decode_processor,
300 => $front_processor,
200 => $language_processor,
);
$processor_manager = new PathProcessorManager();
foreach ($priorities as $priority => $processor) {
$processor_manager
->addInbound($processor, $priority);
}
$test_path = '/fr';
$request = Request::create($test_path);
$processed = $processor_manager
->processInbound($test_path, $request);
$this
->assertEquals('/', $processed, 'Processing in the incorrect order fails to resolve the system path from the empty path');
$test_path = '/fr/foo';
$request = Request::create($test_path);
$processed = $processor_manager
->processInbound($test_path, $request);
$this
->assertEquals('/foo', $processed, 'Processing in the incorrect order fails to resolve the system path from an alias');
$processor_manager = new PathProcessorManager();
$priorities = array(
1000 => $decode_processor,
500 => $language_processor,
300 => $front_processor,
200 => $alias_processor,
);
foreach ($priorities as $priority => $processor) {
$processor_manager
->addInbound($processor, $priority);
}
$test_path = '/fr';
$request = Request::create($test_path);
$processed = $processor_manager
->processInbound($test_path, $request);
$this
->assertEquals('/user/login', $processed, 'Processing in the correct order resolves the system path from the empty path.');
$test_path = '/fr/foo';
$request = Request::create($test_path);
$processed = $processor_manager
->processInbound($test_path, $request);
$this
->assertEquals('/user/1', $processed, 'Processing in the correct order resolves the system path from an alias.');
}
}