Namespace
Drupal\Tests\subpathauto\Unit
Fichier
-
tests/src/Unit/SubPathautoTest.php
View source
<?php
namespace Drupal\Tests\subpathauto\Unit;
use Drupal\Core\Language\Language;
use Drupal\Core\Url;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\subpathauto\PathProcessor;
class SubPathautoTest extends UnitTestCase {
protected $aliasProcessor;
protected $languageManager;
protected $pathValidator;
protected $configFactory;
protected $subPathautoSettings;
protected $languageNegotiation;
protected $languageNegotiationSettings = [
'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
'prefixes' => [
'en' => 'default_language',
],
];
protected $pathProcessor;
protected $aliases = [
'/content/first-node' => '/node/1',
'/content/first-node-test' => '/node/1/test',
'/malicious-path' => '/admin',
'' => '<front>',
];
public function setUp() : void {
parent::setUp();
$this->aliasProcessor = $this->getMockBuilder('Drupal\\path_alias\\PathProcessor\\AliasPathProcessor')
->disableOriginalConstructor()
->getMock();
$this->languageManager = $this->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->languageManager
->expects($this->any())
->method('getCurrentLanguage')
->willReturn(new Language(Language::$defaultValues));
$this->pathValidator = $this->createMock('Drupal\\Core\\Path\\PathValidatorInterface');
$this->languageNegotiation = $this->createMock('Drupal\\Core\\Config\\ConfigBase');
$this->subPathautoSettings = $this->createMock('Drupal\\Core\\Config\\ConfigBase');
$this->configFactory = $this->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->configFactory
->expects($this->any())
->method('get')
->with($this->logicalOr($this->equalTo('subpathauto.settings'), $this->equalTo('language.negotiation')))
->will($this->returnCallback(function ($param) {
$config = func_get_arg(0);
if ($config == 'subpathauto.settings') {
return $this->subPathautoSettings;
}
elseif ($config == 'language.negotiation') {
return $this->languageNegotiation;
}
return NULL;
}));
$this->languageNegotiation
->expects($this->any())
->method('get')
->willReturn($this->languageNegotiationSettings);
$module_handler = $this->getMockBuilder('Drupal\\Core\\Extension\\ModuleHandler')
->disableOriginalConstructor()
->getMock();
$module_handler->expects($this->any())
->method('moduleExists')
->willReturn(FALSE);
$this->pathProcessor = new PathProcessor($this->aliasProcessor, $this->languageManager, $this->configFactory, $module_handler);
$this->pathProcessor
->setPathValidator($this->pathValidator);
}
public function testInboundSubPath() : void {
$this->aliasProcessor
->expects($this->any())
->method('processInbound')
->willReturnCallback([
$this,
'pathAliasCallback',
]);
$this->pathValidator
->expects($this->any())
->method('getUrlIfValidWithoutAccessCheck')
->willReturn(new Url('any_route'));
$this->subPathautoSettings
->expects($this->atLeastOnce())
->method('get')
->willReturn(0);
$processed = $this->pathProcessor
->processInbound('/content/first-node/a', Request::create('/content/first-node/a'));
$this->assertEquals('/node/1/a', $processed);
$processed = $this->pathProcessor
->processInbound('/content/first-node/a', Request::create('/default_language/content/first-node/a'));
$this->assertEquals('/node/1/a', $processed);
$processed = $this->pathProcessor
->processInbound('/content/first-node/kittens/more-kittens', Request::create('/content/first-node/kittens/more-kittens'));
$this->assertEquals('/node/1/kittens/more-kittens', $processed);
$processed = $this->pathProcessor
->processInbound('/content/first-node-test/a', Request::create('/content/first-node-test/a'));
$this->assertEquals('/node/1/test/a', $processed);
$processed = $this->pathProcessor
->processInbound('/content/first-node/edit', Request::create('/content/first-node/edit'));
$this->assertEquals('/node/1/edit', $processed);
$processed = $this->pathProcessor
->processInbound('/malicious-path/modules', Request::create('/malicious-path/modules'));
$this->assertEquals('/admin/modules', $processed);
}
public function testInboundPathProcessorMaxDepth() : void {
$this->pathValidator
->expects($this->any())
->method('getUrlIfValidWithoutAccessCheck')
->willReturn(new Url('any_route'));
$this->subPathautoSettings
->expects($this->exactly(2))
->method('get')
->willReturn(3);
$this->aliasProcessor
->expects($this->any())
->method('processInbound')
->willReturnCallback([
$this,
'pathAliasCallback',
]);
$processed = $this->pathProcessor
->processInbound('/content/first-node/first/second/third/fourth', Request::create('/content/first-node/first/second/third/fourth'));
$this->assertEquals('/content/first-node/first/second/third/fourth', $processed);
$processed = $this->pathProcessor
->processInbound('/content/first-node/first/second/third', Request::create('/content/first-node/first/second/third'));
$this->assertEquals('/node/1/first/second/third', $processed);
}
public function testInboundAlreadyProcessed() : void {
$processed = $this->pathProcessor
->processInbound('node/1', Request::create('/content/first-node'));
$this->assertEquals('node/1', $processed);
}
public function testOutboundSubPath() : void {
$this->aliasProcessor
->expects($this->any())
->method('processOutbound')
->willReturnCallback([
$this,
'aliasByPathCallback',
]);
$this->subPathautoSettings
->expects($this->atLeastOnce())
->method('get')
->willReturn(0);
$processed = $this->pathProcessor
->processOutbound('/node/1/a');
$this->assertEquals('/content/first-node/a', $processed);
$processed = $this->pathProcessor
->processOutbound('/node/1/kittens/more-kittens');
$this->assertEquals('/content/first-node/kittens/more-kittens', $processed);
$processed = $this->pathProcessor
->processOutbound('/node/1/test/a');
$this->assertEquals('/content/first-node-test/a', $processed);
$processed = $this->pathProcessor
->processOutbound('/node/1/edit');
$this->assertEquals('/content/first-node/edit', $processed);
$processed = $this->pathProcessor
->processOutbound('/admin/modules');
$this->assertEquals('/malicious-path/modules', $processed);
}
public function testOutboundPathProcessorMaxDepth() : void {
$this->pathValidator
->expects($this->any())
->method('getUrlIfValidWithoutAccessCheck')
->willReturn(new Url('any_route'));
$this->subPathautoSettings
->expects($this->exactly(2))
->method('get')
->willReturn(3);
$this->aliasProcessor
->expects($this->any())
->method('processOutbound')
->willReturnCallback([
$this,
'aliasByPathCallback',
]);
$processed = $this->pathProcessor
->processOutbound('/node/1/first/second/third/fourth');
$this->assertEquals('/node/1/first/second/third/fourth', $processed);
$processed = $this->pathProcessor
->processOutbound('/node/1/first/second/third');
$this->assertEquals('/content/first-node/first/second/third', $processed);
}
public function testOutboundAbsoluteUrl() : void {
$options = [
'absolute' => TRUE,
];
$processed = $this->pathProcessor
->processOutbound('node/1', $options);
$this->assertEquals('node/1', $processed);
}
public function pathAliasCallback($path) {
return $this->aliases[$path] ?? $path;
}
public function aliasByPathCallback($path) {
$aliases = array_flip($this->aliases);
return $aliases[$path] ?? $path;
}
}
Classes
| Titre |
Deprecated |
Résumé |
| SubPathautoTest |
|
@coversDefaultClass \Drupal\subpathauto\PathProcessor
@group subpathauto |