You are here

protected function DeveloperSyncTest::verify in Apigee Edge 8

Verifies that the Drupal users and the Edge developers are synchronized.

3 calls to DeveloperSyncTest::verify()
DeveloperSyncTest::testCliDeveloperSync in tests/src/Functional/DeveloperSyncTest.php
Tests the developer synchronization started from the CLI.
DeveloperSyncTest::testDeveloperAsync in tests/src/Functional/DeveloperSyncTest.php
Tests scheduled developer synchronization.
DeveloperSyncTest::testDeveloperSync in tests/src/Functional/DeveloperSyncTest.php
Tests developer synchronization.

File

tests/src/Functional/DeveloperSyncTest.php, line 478

Class

DeveloperSyncTest
Developer-user synchronization test.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

protected function verify() {
  $developers_to_verify = array_merge($this->edgeDevelopers, $this->drupalUsers, $this->modifiedEdgeDevelopers, $this->modifiedDrupalUsers);
  foreach ($developers_to_verify as $email => $entity) {

    /** @var \Drupal\user\UserInterface $user */
    $user = user_load_by_mail($email);

    /** @var \Drupal\apigee_edge\Entity\DeveloperInterface $developer */
    $developer = Developer::load($email);
    $this
      ->assertNotEmpty($user, 'User found: ' . $email);
    $this
      ->assertNotEmpty($developer, 'Developer found: ' . $email);
    $this
      ->assertEquals($developer
      ->getUserName(), $user
      ->getAccountName());
    $this
      ->assertEquals($developer
      ->getFirstName(), $user
      ->get('first_name')->value);
    $this
      ->assertEquals($developer
      ->getLastName(), $user
      ->get('last_name')->value);
    if (array_key_exists($email, $this->modifiedDrupalUsers) || array_key_exists($email, $this->modifiedEdgeDevelopers)) {

      // Unlinked field/attribute should be unchanged on both sides.
      $this
        ->assertEquals($developer
        ->getAttributeValue('one_track_field'), 'developer');
      $this
        ->assertEquals($user
        ->get($this->fieldNamePrefix . 'one_track_field')->value, 'user');
      foreach ($this->fields as $field_type => $data) {
        $formatter = $this->formatManager
          ->lookupPluginForFieldType($field_type);
        if ($formatter instanceof JSON) {
          $this
            ->assertJsonStringEqualsJsonString($developer
            ->getAttributeValue($data['name']), $formatter
            ->encode($user
            ->get($this->fieldNamePrefix . $data['name'])
            ->getValue()));
          $this
            ->assertJsonStringEqualsJsonString($developer
            ->getAttributeValue($data['name']), $formatter
            ->encode($data['data_changed']));
        }
        elseif ($formatter instanceof CSV) {
          $this
            ->assertEquals($developer
            ->getAttributeValue($data['name']), $formatter
            ->encode($user
            ->get($this->fieldNamePrefix . $data['name'])
            ->getValue()));
          $this
            ->assertEquals($developer
            ->getAttributeValue($data['name']), $formatter
            ->encode($data['data_changed']));
        }
      }
    }
    else {
      foreach ($this->fields as $field_type => $data) {
        $formatter = $this->formatManager
          ->lookupPluginForFieldType($field_type);
        if ($formatter instanceof JSON) {
          $this
            ->assertJsonStringEqualsJsonString($developer
            ->getAttributeValue($data['name']), $formatter
            ->encode($user
            ->get($this->fieldNamePrefix . $data['name'])
            ->getValue()));
          $this
            ->assertJsonStringEqualsJsonString($developer
            ->getAttributeValue($data['name']), $formatter
            ->encode($data['data']));
        }
        elseif ($formatter instanceof CSV) {
          $this
            ->assertEquals($developer
            ->getAttributeValue($data['name']), $formatter
            ->encode($user
            ->get($this->fieldNamePrefix . $data['name'])
            ->getValue()));
          $this
            ->assertEquals($developer
            ->getAttributeValue($data['name']), $formatter
            ->encode($data['data']));
        }
      }
    }

    // Invalid email address should not be copied into the corresponding
    // Drupal user field.
    if ($developer
      ->hasAttribute('invalid_email')) {
      if (array_key_exists($email, $this->edgeDevelopers)) {
        $this
          ->assertNull($user
          ->get("{$this->fieldNamePrefix}invalid_email")->value);
      }
      elseif (array_key_exists($email, $this->modifiedEdgeDevelopers)) {
        $this
          ->assertEquals($user
          ->get("{$this->fieldNamePrefix}invalid_email")->value, 'valid.email@example.com');
      }
    }
  }

  // Developer with existing username is not copied into Drupal.
  $this
    ->assertFalse(user_load_by_mail("{$this->prefix}.reserved@example.com"));

  // Drupal user's status is active.

  /** @var \Drupal\user\UserInterface $active_user */
  $active_user = user_load_by_mail($this->inactiveDeveloper
    ->getEmail());
  $this
    ->assertTrue($active_user
    ->isActive());

  // Only the necessary test users were created in Drupal besides the
  // inactive developer's, anonymous and admin users.
  $this
    ->assertEquals(count(User::loadMultiple()), count($developers_to_verify) + 3);
}