View source
<?php
namespace Drupal\Tests\locale\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
class LocalePathTest extends BrowserTestBase {
use PathAliasTestTrait;
protected static $modules = [
'node',
'locale',
'path',
'views',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->config('system.site')
->set('page.front', '/node')
->save();
}
public function testPathLanguageConfiguration() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'create page content',
'administer url aliases',
'create url aliases',
'access administration pages',
'access content overview',
]);
$this
->drupalLogin($admin_user);
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$prefix = $langcode;
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add custom language');
$edit = [
"prefix[{$langcode}]" => $prefix,
];
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet($prefix);
$this
->assertSession()
->pageTextContains('Welcome!');
$node = $this
->drupalCreateNode([
'type' => 'page',
]);
$path = 'admin/config/search/path/add';
$english_path = $this
->randomMachineName(8);
$edit = [
'path[0][value]' => '/node/' . $node
->id(),
'alias[0][value]' => '/' . $english_path,
'langcode[0][value]' => 'en',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save');
$custom_language_path = $this
->randomMachineName(8);
$edit = [
'path[0][value]' => '/node/' . $node
->id(),
'alias[0][value]' => '/' . $custom_language_path,
'langcode[0][value]' => $langcode,
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save');
$this
->drupalGet($english_path);
$this
->assertSession()
->pageTextContains($node
->label());
$this
->drupalGet($prefix . '/' . $custom_language_path);
$this
->assertSession()
->pageTextContains($node
->label());
$custom_path = $this
->randomMachineName(8);
$path_alias = $this
->createPathAlias('/node/' . $node
->id(), '/' . $custom_path, LanguageInterface::LANGCODE_NOT_SPECIFIED);
$lookup_path = $this->container
->get('path_alias.manager')
->getAliasByPath('/node/' . $node
->id(), 'en');
$this
->assertEquals('/' . $english_path, $lookup_path, 'English language alias has priority.');
$lookup_path = $this->container
->get('path_alias.manager')
->getAliasByPath('/node/' . $node
->id(), $prefix);
$this
->assertEquals('/' . $custom_language_path, $lookup_path, 'Custom language alias has priority.');
$path_alias
->delete();
$first_node = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 1,
'langcode' => 'en',
]);
$second_node = $this
->drupalCreateNode([
'type' => 'page',
'promote' => 1,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this
->createPathAlias('/node/' . $first_node
->id(), '/' . $custom_path, $first_node
->language()
->getId());
$this
->createPathAlias('/node/' . $second_node
->id(), '/' . $custom_path, $second_node
->language()
->getId());
$this
->drupalGet('admin/content');
$custom_path_url = Url::fromUserInput('/' . $custom_path)
->toString();
$this
->assertSession()
->elementExists('xpath', $this
->assertSession()
->buildXPathQuery('//a[@href=:href and normalize-space(text())=:title]', [
':href' => $custom_path_url,
':title' => $first_node
->label(),
]));
$this
->assertSession()
->elementExists('xpath', $this
->assertSession()
->buildXPathQuery('//a[@href=:href and normalize-space(text())=:title]', [
':href' => $custom_path_url,
':title' => $second_node
->label(),
]));
$this
->drupalGet($custom_path);
$this
->assertSession()
->pageTextContains($first_node
->label());
$this
->drupalGet($prefix . '/' . $custom_path);
$this
->assertSession()
->pageTextContains($second_node
->label());
}
}