Namespace
Drupal\Tests\subpathauto\Kernel
File
-
tests/src/Kernel/SubPathautoKernelTest.php
View source
<?php
namespace Drupal\Tests\subpathauto\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\path_alias\PathAliasInterface;
use Drupal\redirect\Entity\Redirect;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
class SubPathautoKernelTest extends KernelTestBase {
use NodeCreationTrait;
use TestFileCreationTrait;
use UserCreationTrait;
protected static $modules = [
'filter',
'language',
'link',
'node',
'path_alias',
'redirect',
'subpathauto',
'system',
'user',
];
protected $pathProcessor;
protected $pathAliasStorage;
protected $redirectStorage;
protected function setUp() : void {
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installEntitySchema('path_alias');
$this->installEntitySchema('redirect');
$this->installConfig('subpathauto');
$this->installConfig('filter');
$this->installConfig('language');
\Drupal::configFactory()->getEditable('redirect.settings')
->set('auto_redirect', TRUE)
->save();
NodeType::create([
'type' => 'page',
'name' => 'page',
])->save();
$this->pathAliasStorage = \Drupal::entityTypeManager()->getStorage('path_alias');
$this->redirectStorage = \Drupal::entityTypeManager()->getStorage('redirect');
$this->pathProcessor = $this->container
->get('path_processor_subpathauto');
$this->container
->get('path_alias.whitelist')
->set('node', TRUE);
User::create([
'uid' => 0,
'name' => 'anonymous user',
])->save();
$this->config('language.negotiation')
->set('url.source', LanguageNegotiationUrl::CONFIG_PATH_PREFIX)
->set('url.prefixes', [
'en' => 'default_language',
])
->save();
}
public function testProcessInbound() : void {
$this->createNode();
$path_alias = $this->createAlias('/node/1', '/kittens');
$processed = $this->pathProcessor
->processInbound('/kittens/are-fake', Request::create('/kittens/are-fake'));
$this->assertSame('/kittens/are-fake', $processed);
$processed = $this->pathProcessor
->processInbound('/kittens/edit', Request::create('/default_language/kittens/edit'));
$this->assertSame('/node/1/edit', $processed);
$processed = $this->pathProcessor
->processInbound('/kittens/edit', Request::create('/kittens/edit'));
$this->assertSame('/node/1/edit', $processed);
$admin_user = $this->createUser();
\Drupal::currentUser()->setAccount($admin_user);
$processed = $this->pathProcessor
->processInbound('/kittens/edit', Request::create('/kittens/edit'));
$this->assertSame('/node/1/edit', $processed);
$path_alias->setAlias('/more-kittens')
->save();
$processed = $this->pathProcessor
->processInbound('/kittens', Request::create('/default_language/kittens'));
$this->assertSame('/node/1', $processed);
$processed = $this->pathProcessor
->processInbound('/kittens/edit', Request::create('/default_language/kittens/edit'));
$this->assertSame('/node/1/edit', $processed);
$processed = $this->pathProcessor
->processInbound('/more-kittens/edit', Request::create('/default_language/more-kittens/edit'));
$this->assertSame('/node/1/edit', $processed);
}
public function testProcessOutbound() : void {
$this->createNode();
$this->createAlias('/node/1', '/kittens');
$processed = $this->pathProcessor
->processOutbound('/kittens/are-fake');
$this->assertSame('/kittens/are-fake', $processed);
$processed = $this->pathProcessor
->processOutbound('/node/1/edit');
$this->assertSame('/kittens/edit', $processed);
$admin_user = $this->createUser();
\Drupal::currentUser()->setAccount($admin_user);
$processed = $this->pathProcessor
->processOutbound('/node/1/edit');
$this->assertSame('/kittens/edit', $processed);
$options = [
'absolute' => TRUE,
];
$processed = $this->pathProcessor
->processOutbound('/node/1/edit', $options);
$this->assertSame('/kittens/edit', $processed);
$this->createNode();
$this->createAlias('/node/2', '/node/1/foo');
$processed = $this->pathProcessor
->processOutbound('/node/2/edit', $options);
$this->assertSame('/node/1/foo/edit', $processed);
}
public function testAliasesAndRedirects() {
$node = $this->createNode();
$alias = $this->createAlias('/' . $node->toUrl()
->getInternalPath(), '/node/foo');
$processed = $this->pathProcessor
->processInbound('/node/foo', Request::create('/node/foo'));
$this->assertSame("/node/foo", $processed);
$processed = $this->pathProcessor
->processInbound('/node/foo/edit', Request::create('/node/foo/edit'));
$this->assertSame("/node/{$node->id()}/edit", $processed);
$alias->setAlias('/node/foo/bar')
->save();
foreach ([
'/node/foo/edit',
'/node/foo/bar/edit',
] as $url) {
$processed = $this->pathProcessor
->processInbound($url, Request::create($url));
$this->assertSame("/node/{$node->id()}/edit", $processed);
}
$nodes_alias = $this->createAlias('/node', '/articles');
$processed = $this->pathProcessor
->processInbound('/node/foo/edit', Request::create('/node/foo/edit'));
$this->assertSame("/node/{$node->id()}/edit", $processed);
$nodes_alias->setAlias('/more-articles')
->save();
$alias->setAlias('/articles/foo')
->save();
foreach ([
'/articles/foo/edit',
'/node/foo/edit',
] as $url) {
$processed = $this->pathProcessor
->processInbound($url, Request::create($url));
$this->assertSame("/node/{$node->id()}/edit", $processed);
}
$another_node = $this->createNode();
$another_alias = $this->createAlias('/' . $another_node->toUrl()
->getInternalPath(), '/articles/foo/sub-pages/new-page');
$processed = $this->pathProcessor
->processInbound('/articles/foo/sub-pages/new-page/edit', Request::create('/articles/foo/sub-pages/new-page/edit'));
$this->assertSame("/node/{$another_node->id()}/edit", $processed);
$another_alias->setAlias('/bar')
->save();
$processed = $this->pathProcessor
->processInbound('/articles/foo/sub-pages/new-page/edit', Request::create('/articles/foo/sub-pages/new-page/edit'));
$this->assertSame("/node/{$another_node->id()}/edit", $processed);
$processed = $this->pathProcessor
->processInbound('/bar/edit', Request::create('/bar/edit'));
$this->assertSame("/node/{$another_node->id()}/edit", $processed);
}
public function testNoRedirectSupport(bool $redirect_support, string $expected_result) {
$this->createNode();
$alias = $this->createAlias('/node/1', '/foo/bar');
$alias->save();
$alias->setAlias('/xyz')
->save();
\Drupal::configFactory()->getEditable('subpathauto.settings')
->set('redirect_support', $redirect_support)
->save();
$processed = $this->pathProcessor
->processInbound('/foo/bar/edit', Request::create('/foo/bar/edit'));
$this->assertSame($expected_result, $processed);
}
public function noRedirectSupportDataProvider() : array {
return [
'redirect is supported' => [
TRUE,
'/node/1/edit',
],
'redirect is not supported' => [
FALSE,
'/foo/bar/edit',
],
];
}
public function testNoRedirectInstalled() {
$this->createNode();
$alias = $this->createAlias('/node/1', '/foo/bar');
$alias->save();
$alias->setAlias('/xyz')
->save();
\Drupal::configFactory()->getEditable('subpathauto.settings')
->set('redirect_support', TRUE)
->save();
$this->disableModules([
'redirect',
]);
$processed = $this->pathProcessor
->processInbound('/foo/bar/edit', Request::create('/foo/bar/edit'));
$this->assertSame('/foo/bar/edit', $processed);
}
public function testExternalRedirects() {
$this->createNode();
$this->createRedirect('/node/1', 'http://example.com');
$processed = $this->pathProcessor
->processInbound('/node/1/edit', Request::create('/node/1/edit'));
$this->assertSame("/node/1/edit", $processed);
$this->createAlias('/node', '/content');
$processed = $this->pathProcessor
->processInbound('/content/1/edit', Request::create('/content/1/edit'));
$this->assertSame("/node/1/edit", $processed);
}
public function testRedirectToFile() : void {
$source = str_replace('public://', '/sites/default/files/', current($this->getTestFiles('html'))->uri);
$this->createRedirect($source, '/foo/bar/baz/myfile.pdf');
$this->pathProcessor
->processInbound($source, Request::create($source));
}
protected function createAlias(string $path, string $alias) : PathAliasInterface {
$alias = $this->pathAliasStorage
->create([
'path' => $path,
'alias' => $alias,
]);
$alias->save();
return $alias;
}
protected function createRedirect(string $path, string $redirect_uri) : Redirect {
$redirect = $this->redirectStorage
->create();
$redirect->setSource($path);
$redirect->setRedirect($redirect_uri);
$redirect->setLanguage('en');
$redirect->setStatusCode(301);
$redirect->save();
return $redirect;
}
}
Classes
| Title |
Deprecated |
Summary |
| SubPathautoKernelTest |
|
@coversDefaultClass \Drupal\subpathauto\PathProcessor
@group subpathauto |