Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / tests / src / Kernel / PathautoEntityWithStringIdTest.php
1 <?php
2
3 namespace Drupal\Tests\pathauto\Kernel;
4
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;
13
14 /**
15  * Tests auto-aliasing of entities that use string IDs.
16  *
17  * @group pathauto
18  */
19 class PathautoEntityWithStringIdTest extends KernelTestBase {
20
21   use PathautoTestHelperTrait;
22
23   /**
24    * The alias type plugin instance.
25    *
26    * @var \Drupal\pathauto\AliasTypeBatchUpdateInterface
27    */
28   protected $aliasType;
29
30   /**
31    * {@inheritdoc}
32    */
33   protected static $modules = [
34     'system',
35     'user',
36     'field',
37     'token',
38     'path',
39     'pathauto',
40     'pathauto_string_id_test',
41   ];
42
43   /**
44    * {@inheritdoc}
45    */
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'.
50     $container
51       ->register('keyvalue.database', KeyValueDatabaseFactory::class)
52       ->addArgument(new PhpSerialize())
53       ->addArgument($container->get('database'))
54       ->addTag('persist');
55     $container->setAlias('keyvalue', 'keyvalue.database');
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function setUp() {
62     parent::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');
70   }
71
72   /**
73    * Test aliasing entities with long string ID.
74    *
75    * @dataProvider entityWithStringIdProvider
76    *
77    * @param string|int $id
78    *   The entity ID
79    * @param string $expected_key
80    *   The expected key for 'pathauto_state.*' collections.
81    */
82   public function testEntityWithStringId($id, $expected_key) {
83     $entity = PathautoStringIdTest::create([
84       'id' => $id,
85       'name' => $name = $this->randomMachineName(),
86     ]);
87     $entity->save();
88
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);
94
95     $context = [];
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);
100
101     // Check that the paths were removed on batch delete.
102     $this->assertNoEntityAliasExists($entity, "/$name");
103   }
104
105   /**
106    * Provides test cases for ::testEntityWithStringId().
107    *
108    * @see \Drupal\Tests\pathauto\Kernel\PathautoEntityWithStringIdTest::testEntityWithStringId()
109    */
110   public function entityWithStringIdProvider() {
111     return [
112       'ascii with less or equal 128 chars' => [
113         str_repeat('a', 128), str_repeat('a', 128)
114       ],
115       'ascii with over 128 chars' => [
116         str_repeat('a', 191), Crypt::hashBase64(str_repeat('a', 191))
117       ],
118       'non-ascii with less or equal 128 chars' => [
119         str_repeat('社', 128), Crypt::hashBase64(str_repeat('社', 128))
120       ],
121       'non-ascii with over 128 chars' => [
122         str_repeat('社', 191), Crypt::hashBase64(str_repeat('社', 191))
123       ],
124       'simulating an integer id' => [
125         123, '123'
126       ],
127     ];
128   }
129
130 }