View source
<?php
namespace Drupal\Tests\language_neutral_aliases\Kernel;
use Drupal\Core\Language\LanguageInterface;
use Drupal\KernelTests\KernelTestBase;
class LanguageNeutralAliasesTest extends KernelTestBase {
const TABLE = 'path_alias';
protected $field;
public static $modules = [
'language_neutral_aliases',
'path_alias',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('path_alias');
$database = $this->container
->get('database');
$storage = $this->container
->get('entity_type.manager')
->getStorage('path_alias');
$paths = [
'/node/1' => '/path/first',
'/node/2' => '/path/second',
'/node/3' => '/path/third',
];
foreach ($paths as $path => $alias) {
$values = [
'path' => $path,
'alias' => $alias,
];
$alias = $storage
->create($values);
$storage
->save($alias);
}
$database
->update(self::TABLE)
->fields([
'langcode' => 'de',
])
->condition('path', '/node/1')
->execute();
$database
->update(self::TABLE)
->fields([
'langcode' => 'da',
])
->condition('path', '/node/2')
->execute();
$database
->update(self::TABLE . '_revision')
->fields([
'langcode' => 'de',
])
->condition('path', '/node/1')
->execute();
$database
->update(self::TABLE . '_revision')
->fields([
'langcode' => 'da',
])
->condition('path', '/node/2')
->execute();
}
public function testSave() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('path_alias');
$values = [
'path' => '/node/4',
'alias' => '/path/fourth',
'langcode' => 'de',
];
$alias = $storage
->create($values);
$storage
->save($alias);
$actual = $storage
->load($alias
->id());
$this
->assertEqual(4, $actual
->id());
$this
->assertEqual('/node/4', $actual
->getPath());
$this
->assertEqual('/path/fourth', $actual
->getAlias());
$this
->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $actual
->get('langcode')->value);
$alias = $storage
->load(1);
$alias
->setAlias('/path/fifth');
$storage
->save($alias);
$actual = $storage
->load($alias
->id());
$this
->assertEqual(1, $actual
->id());
$this
->assertEqual('/node/1', $actual
->getPath());
$this
->assertEqual('/path/fifth', $actual
->getAlias());
$this
->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $actual
->get('langcode')->value);
$alias = $storage
->load(4);
$alias
->setAlias('/path/sixth');
$actual = $storage
->load(4);
$this
->assertEqual(4, $actual
->id());
$this
->assertEqual('/node/4', $actual
->getPath());
$this
->assertEqual('/path/sixth', $actual
->getAlias());
$this
->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $actual
->get('langcode')->value);
}
public function testPreloadPathAlias() {
$repository = $this->container
->get('path_alias.repository');
$this
->assertEqual([
'/node/3' => '/path/third',
], $repository
->preloadPathAlias([
'/node/1',
'/node/3',
], LanguageInterface::LANGCODE_NOT_SPECIFIED));
$this
->assertEqual([
'/node/3' => '/path/third',
], $repository
->preloadPathAlias([
'/node/1',
'/node/3',
], 'de'));
}
public function testLookupBySystemPath() {
$repository = $this->container
->get('path_alias.repository');
$this
->assertEqual([
'id' => 3,
'path' => '/node/3',
'alias' => '/path/third',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
], $repository
->lookupBySystemPath('/node/3', LanguageInterface::LANGCODE_NOT_SPECIFIED));
$this
->assertEqual([
'id' => 3,
'path' => '/node/3',
'alias' => '/path/third',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
], $repository
->lookupBySystemPath('/node/3', 'de'));
$this
->assertNull($repository
->lookupBySystemPath('/node/1', LanguageInterface::LANGCODE_NOT_SPECIFIED));
$this
->assertNull($repository
->lookupBySystemPath('/node/1', 'de'));
}
public function testLookupByAlias() {
$repository = $this->container
->get('path_alias.repository');
$this
->assertEqual([
'id' => 3,
'path' => '/node/3',
'alias' => '/path/third',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
], $repository
->lookupByAlias('/path/third', LanguageInterface::LANGCODE_NOT_SPECIFIED));
$this
->assertEqual([
'id' => 3,
'path' => '/node/3',
'alias' => '/path/third',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
], $repository
->lookupByAlias('/path/third', 'de'));
$this
->assertNull($repository
->lookupByAlias('/path/first', LanguageInterface::LANGCODE_NOT_SPECIFIED));
$this
->assertNull($repository
->lookupByAlias('/path/first', 'de'));
}
public function testPathHasMatchingAlias() {
$repository = $this->container
->get('path_alias.repository');
$this
->assertFalse($repository
->pathHasMatchingAlias('/node/1'));
$this
->assertTrue($repository
->pathHasMatchingAlias('/node/3'));
}
}