3 namespace Drupal\Tests\pathauto\Kernel;
5 use Drupal\Component\Serialization\PhpSerialize;
6 use Drupal\Component\Utility\Crypt;
7 use Drupal\Core\DependencyInjection\ContainerBuilder;
8 use Drupal\Core\KeyValueStore\KeyValueDatabaseFactory;
9 use Drupal\pathauto\PathautoState;
10 use Drupal\pathauto\Tests\PathautoTestHelperTrait;
11 use Drupal\KernelTests\KernelTestBase;
12 use Drupal\pathauto_string_id_test\Entity\PathautoStringIdTest;
15 * Tests auto-aliasing of entities that use string IDs.
19 class PathautoEntityWithStringIdTest extends KernelTestBase {
21 use PathautoTestHelperTrait;
24 * The alias type plugin instance.
26 * @var \Drupal\pathauto\AliasTypeBatchUpdateInterface
33 protected static $modules = [
40 'pathauto_string_id_test',
46 public function register(ContainerBuilder $container) {
47 parent::register($container);
48 // Kernel tests are using the 'keyvalue.memory' store but we want to test
49 // against the 'keyvalue.database'.
51 ->register('keyvalue.database', KeyValueDatabaseFactory::class)
52 ->addArgument(new PhpSerialize())
53 ->addArgument($container->get('database'))
55 $container->setAlias('keyvalue', 'keyvalue.database');
61 protected function setUp() {
63 $this->installSchema('system', ['key_value']);
64 $this->installConfig(['system', 'pathauto']);
65 $this->installEntitySchema('pathauto_string_id_test');
66 $this->createPattern('pathauto_string_id_test', '/[pathauto_string_id_test:name]');
67 /** @var \Drupal\pathauto\AliasTypeManager $alias_type_manager */
68 $alias_type_manager = $this->container->get('plugin.manager.alias_type');
69 $this->aliasType = $alias_type_manager->createInstance('canonical_entities:pathauto_string_id_test');
73 * Test aliasing entities with long string ID.
75 * @dataProvider entityWithStringIdProvider
77 * @param string|int $id
79 * @param string $expected_key
80 * The expected key for 'pathauto_state.*' collections.
82 public function testEntityWithStringId($id, $expected_key) {
83 $entity = PathautoStringIdTest::create([
85 'name' => $name = $this->randomMachineName(),
89 // Check that the path was generated.
90 $this->assertEntityAlias($entity, mb_strtolower("/$name"));
91 // Check that the path auto state was saved with the expected key.
92 $value = \Drupal::keyValue('pathauto_state.pathauto_string_id_test')->get($expected_key);
93 $this->assertEquals(PathautoState::CREATE, $value);
96 // Batch delete uses the key-value store collection 'pathauto_state.*. We
97 // test that after a bulk delete all aliases are removed. Running only once
98 // the batch delete process is enough as the batch size is 100.
99 $this->aliasType->batchDelete($context);
101 // Check that the paths were removed on batch delete.
102 $this->assertNoEntityAliasExists($entity, "/$name");
106 * Provides test cases for ::testEntityWithStringId().
108 * @see \Drupal\Tests\pathauto\Kernel\PathautoEntityWithStringIdTest::testEntityWithStringId()
110 public function entityWithStringIdProvider() {
112 'ascii with less or equal 128 chars' => [
113 str_repeat('a', 128), str_repeat('a', 128)
115 'ascii with over 128 chars' => [
116 str_repeat('a', 191), Crypt::hashBase64(str_repeat('a', 191))
118 'non-ascii with less or equal 128 chars' => [
119 str_repeat('社', 128), Crypt::hashBase64(str_repeat('社', 128))
121 'non-ascii with over 128 chars' => [
122 str_repeat('社', 191), Crypt::hashBase64(str_repeat('社', 191))
124 'simulating an integer id' => [