You are here

protected function ExportTest::validateUserCdfObject in Acquia Content Hub 8.2

Validates Drupal user CDF object.

Parameters

string $name_expected: Expected user name.

string $email_expected: Expected user email.

File

tests/src/Kernel/ExportTest.php, line 1578

Class

ExportTest
Tests entity exports.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

protected function validateUserCdfObject($name_expected, $email_expected) {
  $this
    ->validateBaseCdfObject();
  $cdf = $this->cdfObject;

  // Validate Type attribute.
  $type = $cdf
    ->getType();
  $this
    ->assertEquals('drupal8_content_entity', $type);

  // Validate Metadata attribute.
  $metadata = $cdf
    ->getMetadata();
  $metadata_expected = [
    'default_language' => 'en',
    'field' => [
      'uuid' => [
        'type' => 'uuid',
      ],
      'preferred_langcode' => [
        'type' => 'language',
      ],
      'preferred_admin_langcode' => [
        'type' => 'language',
      ],
      'name' => [
        'type' => 'string',
      ],
      'pass' => [
        'type' => 'password',
      ],
      'mail' => [
        'type' => 'email',
      ],
      'timezone' => [
        'type' => 'string',
      ],
      'status' => [
        'type' => 'boolean',
      ],
      'created' => [
        'type' => 'created',
      ],
      'changed' => [
        'type' => 'changed',
      ],
      'access' => [
        'type' => 'timestamp',
      ],
      'login' => [
        'type' => 'timestamp',
      ],
      'init' => [
        'type' => 'email',
      ],
      'roles' => [
        'type' => 'entity_reference',
        'target' => 'user_role',
      ],
      'default_langcode' => [
        'type' => 'boolean',
      ],
    ],
    'languages' => [
      'en',
    ],
    'version' => 2,
    'user_data' => [],
  ];
  unset($metadata['data']);
  unset($metadata['field']['user_picture']);
  $this
    ->assertEquals($metadata_expected, $metadata);

  // Validate "Data" attribute.
  $data = $this
    ->getCdfDataAttribute($cdf);
  $created_timestamp = $data['created']['value']['en']['value'];
  $is_timestamp = is_numeric($created_timestamp) && (int) $created_timestamp == $created_timestamp;
  $this
    ->assertTrue($is_timestamp);
  $data_expected = [
    'uuid' => [
      'value' => [
        'en' => [
          'value' => $cdf
            ->getUuid(),
        ],
      ],
    ],
    'preferred_langcode' => [
      'value' => [
        'en' => 'en',
      ],
    ],
    'preferred_admin_langcode' => [],
    'pass' => [
      'value' => [
        'en' => [
          'pre_hashed' => TRUE,
        ],
      ],
    ],
    'init' => [
      'value' => [
        'en' => [],
      ],
    ],
    'roles' => [
      'value' => [
        'en' => [],
      ],
    ],
    'name' => [
      'value' => [
        'en' => $name_expected,
      ],
    ],
    'mail' => [
      'value' => [
        'en' => [
          'value' => $email_expected,
        ],
      ],
    ],
    'timezone' => [
      'value' => [
        'en' => '',
      ],
    ],
    'status' => [
      'value' => [
        'en' => '0',
      ],
    ],
    'created' => [
      'value' => [
        'en' => [
          'value' => $created_timestamp,
        ],
      ],
    ],
    'changed' => [
      'value' => [
        'en' => [
          'value' => $created_timestamp,
        ],
      ],
    ],
    'access' => [
      'value' => [
        'en' => [
          'value' => '0',
        ],
      ],
    ],
    'login' => [
      'value' => [
        'en' => [
          'value' => '0',
        ],
      ],
    ],
    'default_langcode' => [
      'value' => [
        'en' => '1',
      ],
    ],
  ];
  unset($data['user_picture']);
  $this
    ->assertEquals($data_expected, $data);

  // Validate entity type.
  $entity_type = $this
    ->getCdfAttribute($cdf, 'entity_type');
  $this
    ->assertEquals('user', $entity_type);

  // Validate bundle.
  $bundle = $this
    ->getCdfAttribute($cdf, 'bundle');
  $this
    ->assertEquals('user', $bundle);

  // Validate username.
  $name = $this
    ->getCdfAttribute($cdf, 'username');
  $this
    ->assertEquals($name_expected, $name);
  $label = $this
    ->getCdfAttribute($cdf, 'label', 'en');
  $this
    ->assertEquals($name_expected, $label);

  // Validate email.
  $email = $this
    ->getCdfAttribute($cdf, 'mail');
  $this
    ->assertEquals($email_expected, $email);
}