You are here

public function MoveRevisionMetadataFieldsUpdateTest::testAddingRequiredRevisionMetadataKeys in Drupal 8

Tests the addition of required revision metadata keys.

This test ensures that already cached entity instances will only return the required revision metadata keys they have been cached with and only new instances will return all the new required revision metadata keys.

@expectedDeprecation The revision_user revision metadata key is not set for entity type: entity_test_mul_revlog See: https://www.drupal.org/node/2831499 @expectedDeprecation The revision_created revision metadata key is not set for entity type: entity_test_mul_revlog See: https://www.drupal.org/node/2831499 @expectedDeprecation The revision_log_message revision metadata key is not set for entity type: entity_test_mul_revlog See: https://www.drupal.org/node/2831499

File

core/modules/system/tests/src/Functional/Entity/Update/MoveRevisionMetadataFieldsUpdateTest.php, line 103

Class

MoveRevisionMetadataFieldsUpdateTest
Tests the upgrade path for moving the revision metadata fields.

Namespace

Drupal\Tests\system\Functional\Entity\Update

Code

public function testAddingRequiredRevisionMetadataKeys() {

  // Ensure that cached entity types without required revision metadata keys
  // will not return any of the newly added required revision metadata keys.
  // Contains no revision metadata keys and the property holding the required
  // metadata keys is empty, the entity type id is "entity_test_mul_revlog".
  $cached_with_no_metadata_keys = 'Tzo4MjoiRHJ1cGFsXFRlc3RzXHN5c3RlbVxGdW5jdGlvbmFsXEVudGl0eVxVcGRhdGVcVGVzdFJldmlzaW9uTWV0YWRhdGFCY0xheWVyRW50aXR5VHlwZSI6Mzk6e3M6MjU6IgAqAHJldmlzaW9uX21ldGFkYXRhX2tleXMiO2E6MDp7fXM6MzE6IgAqAHJlcXVpcmVkUmV2aXNpb25NZXRhZGF0YUtleXMiO2E6MDp7fXM6MTU6IgAqAHN0YXRpY19jYWNoZSI7YjoxO3M6MTU6IgAqAHJlbmRlcl9jYWNoZSI7YjoxO3M6MTk6IgAqAHBlcnNpc3RlbnRfY2FjaGUiO2I6MTtzOjE0OiIAKgBlbnRpdHlfa2V5cyI7YTo1OntzOjg6InJldmlzaW9uIjtzOjA6IiI7czo2OiJidW5kbGUiO3M6MDoiIjtzOjg6Imxhbmdjb2RlIjtzOjA6IiI7cz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9jb2xsZWN0aW9uIjtzOjA6IiI7czoxNzoiACoAbGFiZWxfc2luZ3VsYXIiO3M6MDoiIjtzOjE1OiIAKgBsYWJlbF9wbHVyYWwiO3M6MDoiIjtzOjE0OiIAKgBsYWJlbF9jb3VudCI7YTowOnt9czoxNToiACoAdXJpX2NhbGxiYWNrIjtOO3M6ODoiACoAZ3JvdXAiO047czoxNDoiACoAZ3JvdXBfbGFiZWwiO047czoyMjoiACoAZmllbGRfdWlfYmFzZV9yb3V0ZSI7TjtzOjI2OiIAKgBjb21tb25fcmVmZXJlbmNlX3RhcmdldCI7YjowO3M6MjI6IgAqAGxpc3RfY2FjaGVfY29udGV4dHMiO2E6MDp7fXM6MTg6IgAqAGxpc3RfY2FjaGVfdGFncyI7YToxOntpOjA7czo5OiJ0ZXN0X2xpc3QiO31zOjE0OiIAKgBjb25zdHJhaW50cyI7YTowOnt9czoxMzoiACoAYWRkaXRpb25hbCI7YTowOnt9czo4OiIAKgBjbGFzcyI7TjtzOjExOiIAKgBwcm92aWRlciI7TjtzOjIwOiIAKgBzdHJpbmdUcmFuc2xhdGlvbiI7Tjt9';

  /** @var \Drupal\Tests\system\Functional\Entity\Update\TestRevisionMetadataBcLayerEntityType $entity_type */
  $entity_type = unserialize(base64_decode($cached_with_no_metadata_keys));
  $required_revision_metadata_keys_no_bc = [];
  $this
    ->assertEquals($required_revision_metadata_keys_no_bc, $entity_type
    ->getRevisionMetadataKeys(FALSE));
  $required_revision_metadata_keys_with_bc = $required_revision_metadata_keys_no_bc + [
    'revision_user' => 'revision_user',
    'revision_created' => 'revision_created',
    'revision_log_message' => 'revision_log_message',
  ];
  $this
    ->assertEquals($required_revision_metadata_keys_with_bc, $entity_type
    ->getRevisionMetadataKeys(TRUE));

  // Ensure that cached entity types with only one required revision metadata
  // key will return only that one after a second required revision metadata
  // key has been added.
  // Contains one revision metadata key - revision_default which is also
  // contained in the property holding the required revision metadata keys,
  // the entity type id is "entity_test_mul_revlog".
  $cached_with_metadata_key_rev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haW50cyI7YTowOnt9czoxMzoiACoAYWRkaXRpb25hbCI7YTowOnt9czo4OiIAKgBjbGFzcyI7TjtzOjExOiIAKgBwcm92aWRlciI7TjtzOjIwOiIAKgBzdHJpbmdUcmFuc2xhdGlvbiI7Tjt9';
  $entity_type = unserialize(base64_decode($cached_with_metadata_key_revision_default));
  $required_revision_metadata_keys_no_bc = [
    'revision_default' => 'revision_default',
  ];
  $this
    ->assertEquals($required_revision_metadata_keys_no_bc, $entity_type
    ->getRevisionMetadataKeys(FALSE));
  $required_revision_metadata_keys_with_bc = $required_revision_metadata_keys_no_bc + [
    'revision_user' => 'revision_user',
    'revision_created' => 'revision_created',
    'revision_log_message' => 'revision_log_message',
  ];
  $this
    ->assertEquals($required_revision_metadata_keys_with_bc, $entity_type
    ->getRevisionMetadataKeys(TRUE));

  // Ensure that newly instantiated entity types will return the two required
  // revision metadata keys.
  $entity_type = new TestRevisionMetadataBcLayerEntityType([
    'id' => 'test',
  ]);
  $required_revision_metadata_keys = [
    'revision_default' => 'revision_default',
    'second_required_key' => 'second_required_key',
  ];
  $this
    ->assertEquals($required_revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(FALSE));

  // Load an entity type from the cache with no revision metadata keys in the
  // annotation.
  $entity_last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
  $entity_type = $entity_last_installed_schema_repository
    ->getLastInstalledDefinition('entity_test_mul_revlog');
  $revision_metadata_keys = [];
  $this
    ->assertEquals($revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(FALSE));
  $revision_metadata_keys = [
    'revision_user' => 'revision_user',
    'revision_created' => 'revision_created',
    'revision_log_message' => 'revision_log_message',
  ];
  $this
    ->assertEquals($revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(TRUE));

  // Load an entity type without using the cache with no revision metadata
  // keys in the annotation.
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_type_manager
    ->useCaches(FALSE);
  $entity_type = $entity_type_manager
    ->getDefinition('entity_test_mul_revlog');
  $revision_metadata_keys = [
    'revision_default' => 'revision_default',
  ];
  $this
    ->assertEquals($revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(FALSE));
  $revision_metadata_keys = [
    'revision_user' => 'revision_user',
    'revision_created' => 'revision_created',
    'revision_log_message' => 'revision_log_message',
    'revision_default' => 'revision_default',
  ];
  $this
    ->assertEquals($revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(TRUE));

  // Ensure that the BC layer will not be triggered if one of the required
  // revision metadata keys is defined in the annotation.
  $definition = [
    'id' => 'entity_test_mul_revlog',
    'revision_metadata_keys' => [
      'revision_default' => 'revision_default',
    ],
  ];
  $entity_type = new ContentEntityType($definition);
  $revision_metadata_keys = [
    'revision_default' => 'revision_default',
  ];
  $this
    ->assertEquals($revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(TRUE));

  // Ensure that the BC layer will be triggered if no revision metadata keys
  // have been defined in the annotation.
  $definition = [
    'id' => 'entity_test_mul_revlog',
  ];
  $entity_type = new ContentEntityType($definition);
  $revision_metadata_keys = [
    'revision_default' => 'revision_default',
    'revision_user' => 'revision_user',
    'revision_created' => 'revision_created',
    'revision_log_message' => 'revision_log_message',
  ];
  $this
    ->assertEquals($revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(TRUE));

  // Ensure that the BC layer will break if a new revision metadata key is
  // only added to the revision_metadata_keys property.
  $definition = [
    'id' => 'entity_test_mul_revlog',
  ];
  $entity_type = new ContentEntityType($definition);
  $revision_metadata_keys = $entity_type
    ->get('revision_metadata_keys');
  $revision_metadata_keys['new_revision_metadata_key'] = 'new_revision_metadata_key';
  $entity_type
    ->set('revision_metadata_keys', $revision_metadata_keys);
  $expected_revision_metadata_keys = [
    'new_revision_metadata_key' => 'new_revision_metadata_key',
    'revision_default' => 'revision_default',
  ];
  $this
    ->assertEquals($expected_revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(TRUE));

  // Ensure that the BC layer will be triggered if a new revision metadata key
  // is added to both properties revision_metadata_keys and
  // requiredRevisionMetadataKeys.
  $definition = [
    'id' => 'entity_test_mul_revlog',
  ];
  $entity_type = new ContentEntityType($definition);
  $revision_metadata_keys = $entity_type
    ->get('revision_metadata_keys');
  $revision_metadata_keys['new_revision_metadata_key'] = 'new_revision_metadata_key';
  $entity_type
    ->set('revision_metadata_keys', $revision_metadata_keys);
  $required_revision_metadata_keys = $entity_type
    ->get('requiredRevisionMetadataKeys');
  $required_revision_metadata_keys['new_revision_metadata_key'] = 'new_revision_metadata_key';
  $entity_type
    ->set('requiredRevisionMetadataKeys', $required_revision_metadata_keys);
  $expected_revision_metadata_keys = [
    'new_revision_metadata_key' => 'new_revision_metadata_key',
    'revision_default' => 'revision_default',
    'revision_user' => 'revision_user',
    'revision_created' => 'revision_created',
    'revision_log_message' => 'revision_log_message',
  ];
  $this
    ->assertEquals($expected_revision_metadata_keys, $entity_type
    ->getRevisionMetadataKeys(TRUE));
}