View source
<?php
namespace Drupal\Tests\subpathauto\Unit;
use Drupal\Core\Language\Language;
use Drupal\Core\Url;
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 $pathProcessor;
protected $aliases = [
'/content/first-node' => '/node/1',
'/content/first-node-test' => '/node/1/test',
'/malicious-path' => '/admin',
'' => '<front>',
];
public function setUp() {
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->subPathautoSettings = $this
->createMock('Drupal\\Core\\Config\\ConfigBase');
$this->configFactory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$this->configFactory
->expects($this
->any())
->method('get')
->with('subpathauto.settings')
->willReturn($this->subPathautoSettings);
$this->pathProcessor = new PathProcessor($this->aliasProcessor, $this->languageManager, $this->configFactory);
$this->pathProcessor
->setPathValidator($this->pathValidator);
}
public function testInboundSubPath() {
$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('/en/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() {
$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() {
$processed = $this->pathProcessor
->processInbound('node/1', Request::create('/content/first-node'));
$this
->assertEquals('node/1', $processed);
}
public function testOutboundSubPath() {
$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() {
$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() {
$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;
}
}