Pull merge.
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigEntityStorageTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
5 use Drupal\Core\Config\ConfigDuplicateUUIDException;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests sync and importing config entities with IDs and UUIDs that match
10  * existing config.
11  *
12  * @group config
13  */
14 class ConfigEntityStorageTest extends KernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['config_test'];
22
23   /**
24    * Tests creating configuration entities with changed UUIDs.
25    */
26   public function testUUIDConflict() {
27     $entity_type = 'config_test';
28     $id = 'test_1';
29     // Load the original configuration entity.
30     $storage = $this->container->get('entity_type.manager')
31       ->getStorage($entity_type);
32     $storage->create(['id' => $id])->save();
33     $entity = $storage->load($id);
34
35     $original_properties = $entity->toArray();
36
37     // Override with a new UUID and try to save.
38     $new_uuid = $this->container->get('uuid')->generate();
39     $entity->set('uuid', $new_uuid);
40
41     try {
42       $entity->save();
43       $this->fail('Exception thrown when attempting to save a configuration entity with a UUID that does not match the existing UUID.');
44     }
45     catch (ConfigDuplicateUUIDException $e) {
46       $this->pass(format_string('Exception thrown when attempting to save a configuration entity with a UUID that does not match existing data: %e.', ['%e' => $e]));
47     }
48
49     // Ensure that the config entity was not corrupted.
50     $entity = entity_load('config_test', $entity->id(), TRUE);
51     $this->assertIdentical($entity->toArray(), $original_properties);
52   }
53
54   /**
55    * Tests the hasData() method for config entity storage.
56    *
57    * @covers \Drupal\Core\Config\Entity\ConfigEntityStorage::hasData
58    */
59   public function testHasData() {
60     $storage = \Drupal::entityTypeManager()->getStorage('config_test');
61     $this->assertFalse($storage->hasData());
62
63     // Add a test config entity and check again.
64     $storage->create(['id' => $this->randomMachineName()])->save();
65     $this->assertTrue($storage->hasData());
66   }
67
68 }