View source
<?php
namespace Drupal\Tests\pathauto\FunctionalJavascript;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\pathauto\PathautoState;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\pathauto\Functional\PathautoTestHelperTrait;
class PathautoLocaleTest extends WebDriverTestBase {
use PathautoTestHelperTrait;
protected $defaultTheme = 'stable';
public static $modules = [
'node',
'pathauto',
'locale',
'content_translation',
];
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
public function testLanguageAliases() {
$this
->createPattern('node', '/content/[node:title]');
ConfigurableLanguage::createFromLangcode('fr')
->save();
$node = [
'title' => 'English node',
'langcode' => 'en',
'path' => [
[
'alias' => '/english-node',
'pathauto' => FALSE,
],
],
];
$node = $this
->drupalCreateNode($node);
$english_alias = $this
->loadPathAliasByConditions([
'alias' => '/english-node',
'langcode' => 'en',
]);
$this
->assertNotEmpty($english_alias, 'Alias created with proper language.');
$this
->saveEntityAlias($node, '/french-node', 'fr');
$this
->createPathAlias('/node/invalid', '/content/english-node', Language::LANGCODE_NOT_SPECIFIED);
$node->path->pathauto = PathautoState::CREATE;
$node
->save();
$this
->assertEntityAlias($node, '/content/english-node-0', 'en');
$this
->assertEntityAlias($node, '/french-node', 'fr');
$this
->assertAliasExists([
'id' => $english_alias
->id(),
'alias' => '/content/english-node-0',
]);
$node = $this
->drupalCreateNode([
'title' => 'English node',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this
->assertEntityAlias($node, '/content/english-node-0', LanguageInterface::LANGCODE_NOT_SPECIFIED);
}
public function testLanguagePatterns() {
$permissions = [
'administer pathauto',
'administer url aliases',
'create url aliases',
'bypass node access',
'access content overview',
'administer languages',
'translate any entity',
'administer content translation',
];
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$this
->enableArticleTranslation();
$this
->drupalGet('admin/config/search/path/patterns/add');
$session = $this
->getSession();
$page = $session
->getPage();
$page
->fillField('type', 'canonical_entities:node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->fillField('pattern', '/the-articles/[node:title]');
$page
->fillField('bundles[article]', TRUE);
$page
->fillField('languages[en]', TRUE);
$page
->fillField('label', 'English articles');
$this
->assertSession()
->waitForElementVisible('css', '#edit-label-machine-name-suffix .machine-name-value');
$page
->pressButton('Save');
$this
->assertSession()
->pageTextContains('Pattern English articles saved.');
$this
->drupalGet('admin/config/search/path/patterns/add');
$page
->fillField('type', 'canonical_entities:node');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$page
->fillField('pattern', '/les-articles/[node:title]');
$page
->fillField('bundles[article]', TRUE);
$page
->fillField('languages[fr]', TRUE);
$page
->fillField('label', 'French articles');
$this
->assertSession()
->waitForElementVisible('css', '#edit-label-machine-name-suffix .machine-name-value');
$page
->pressButton('Save');
$this
->assertSession()
->pageTextContains('Pattern French articles saved.');
$edit = [
'title[0][value]' => 'English node',
'langcode[0][value]' => 'en',
];
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$english_node = $this
->drupalGetNodeByTitle('English node');
return;
$this
->assertAlias('/node/' . $english_node
->id(), '/the-articles/english-node', 'en');
$this
->drupalGet('node/' . $english_node
->id() . '/translations');
$this
->clickLink(t('Add'));
$edit = [
'title[0][value]' => 'French node',
];
$this
->drupalPostForm(NULL, $edit, t('Save (this translation)'));
$this
->rebuildContainer();
$english_node = $this
->drupalGetNodeByTitle('English node');
$french_node = $english_node
->getTranslation('fr');
$this
->assertAlias('/node/' . $french_node
->id(), '/les-articles/french-node', 'fr');
$this
->deleteAllAliases();
$edit = [
'update[canonical_entities:node]' => TRUE,
];
$this
->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
$this
->assertSession()
->pageTextContains(t('Generated 2 URL aliases.'));
$this
->assertAlias('/node/' . $english_node
->id(), '/the-articles/english-node', 'en');
$this
->assertAlias('/node/' . $french_node
->id(), '/les-articles/french-node', 'fr');
}
public function testLanguageNotApplicable() {
$this
->drupalLogin($this->rootUser);
$this
->enableArticleTranslation();
$pattern = $this
->createPattern('node', '/content/[node:title]', -1);
$pattern
->save();
$node = $this
->createNode([
'type' => 'article',
'title' => 'Test node',
'langcode' => LanguageInterface::LANGCODE_NOT_APPLICABLE,
]);
$alias = \Drupal::service('pathauto.alias_storage_helper')
->loadBySource('/node/' . $node
->id());
$this
->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $alias['langcode'], 'PathautoGenerator::createEntityAlias() adjusts the alias langcode from Not Applicable to Not Specified.');
$this
->drupalGet('content/test-node');
$this
->assertSession()
->pageTextContains(t('Test node'));
}
protected function enableArticleTranslation() {
$this
->drupalGet('admin/config/regional/content-language');
$edit = [
'entity_types[node]' => TRUE,
'settings[node][article][translatable]' => TRUE,
'settings[node][article][settings][language][language_alterable]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
}
}