You are here

protected function DeveloperSyncTest::setUp in Apigee Edge 8

Overrides ApigeeEdgeFunctionalTestBase::setUp

File

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

Class

DeveloperSyncTest
Developer-user synchronization test.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

protected function setUp() {
  parent::setUp();
  $this
    ->drupalPlaceBlock('system_breadcrumb_block');
  $this->fieldNamePrefix = $this
    ->config('field_ui.settings')
    ->get('field_prefix');
  $this->formatManager = $this->container
    ->get('plugin.manager.apigee_field_storage_format');
  $this->prefix = $this
    ->randomMachineName();
  $escaped_prefix = preg_quote($this->prefix);
  $this->filter = "/^{$escaped_prefix}\\.[a-zA-Z0-9]*@example\\.com\$/";
  $this->container
    ->get('config.factory')
    ->getEditable('apigee_edge.sync')
    ->set('filter', $this->filter)
    ->save();
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->setUpUserFields();

  // Create developers on Apigee Edge.
  for ($i = 0; $i < self::DEVELOPER_TO_CREATE_PER_TYPE; $i++) {
    $mail = "{$this->prefix}.{$this->randomMachineName()}@example.com";
    $this->edgeDevelopers[$mail] = Developer::create([
      'email' => $mail,
      'userName' => $this
        ->randomMachineName(),
      'firstName' => $this
        ->randomMachineName(64),
      'lastName' => $this
        ->randomMachineName(64),
    ]);
    foreach ($this->fields as $field_type => $data) {
      $formatter = $this->formatManager
        ->lookupPluginForFieldType($field_type);
      $this->edgeDevelopers[$mail]
        ->setAttribute($data['name'], $formatter
        ->encode($data['data']));
    }
    $this->edgeDevelopers[$mail]
      ->setAttribute('invalid_email', 'invalid_email_address');
    $this->edgeDevelopers[$mail]
      ->save();
  }

  // Create users in Drupal. Do not let run apigee_edge_user_presave(), so
  // the corresponding developer won't be created.
  $this
    ->disableUserPresave();
  for ($i = 0; $i < self::DEVELOPER_TO_CREATE_PER_TYPE; $i++) {
    $user = $this
      ->createAccount([], TRUE, $this->prefix);
    foreach ($this->fields as $field_type => $data) {
      $user
        ->set($this->fieldNamePrefix . $data['name'], $data['data']);
    }
    $user
      ->save();
    $this->drupalUsers[$user
      ->getEmail()] = $user;
  }
  $this
    ->enableUserPresave();

  // Create synchronized users and change attribute values only on Apigee
  // Edge.
  for ($i = 0; $i < self::DEVELOPER_TO_CREATE_PER_TYPE; $i++) {
    $user = $this
      ->createAccount([], TRUE, $this->prefix);
    foreach ($this->fields as $field_type => $data) {
      $user
        ->set($this->fieldNamePrefix . $data['name'], $data['data']);
    }

    // Set unlinked field on the user.
    $user
      ->set($this->fieldNamePrefix . 'invalid_email', 'valid.email@example.com');

    // Set valid email field on the user.
    $user
      ->set($this->fieldNamePrefix . 'one_track_field', 'user');
    $user
      ->setChangedTime($this->container
      ->get('datetime.time')
      ->getCurrentTime() - 100);
    $user
      ->save();
    $this->modifiedEdgeDevelopers[$user
      ->getEmail()] = Developer::load($user
      ->getEmail());
    foreach ($this->fields as $field_type => $data) {
      $formatter = $this->formatManager
        ->lookupPluginForFieldType($field_type);
      $this->modifiedEdgeDevelopers[$user
        ->getEmail()]
        ->setAttribute($data['name'], $formatter
        ->encode($data['data_changed']));
    }

    // Change first name and last name.
    $this->modifiedEdgeDevelopers[$user
      ->getEmail()]
      ->setFirstName($this->randomGenerator
      ->word(8));
    $this->modifiedEdgeDevelopers[$user
      ->getEmail()]
      ->setLastName($this->randomGenerator
      ->word(8));

    // Set unlinked attribute on the developer.
    $this->modifiedEdgeDevelopers[$user
      ->getEmail()]
      ->setAttribute('one_track_field', 'developer');

    // Set invalid email attribute value on the developer.
    $this->modifiedEdgeDevelopers[$user
      ->getEmail()]
      ->setAttribute('invalid_email', 'invalid_email_address');
    $this->modifiedEdgeDevelopers[$user
      ->getEmail()]
      ->save();
  }

  // Create synchronized users and change field values only in Drupal.
  for ($i = 0; $i < self::DEVELOPER_TO_CREATE_PER_TYPE; $i++) {
    $user = $this
      ->createAccount([], TRUE, $this->prefix);
    foreach ($this->fields as $field_type => $data) {
      $user
        ->set($this->fieldNamePrefix . $data['name'], $data['data']);
    }
    $user
      ->save();
    $this->modifiedDrupalUsers[$user
      ->getEmail()] = $user;

    // Set unlinked field on Apigee Edge.

    /** @var \Drupal\apigee_edge\Entity\DeveloperInterface $developer */
    $developer = Developer::load($user
      ->getEmail());
    $developer
      ->setAttribute('one_track_field', 'developer');
    $developer
      ->save();

    // Do not let run apigee_edge_user_presave(), so the corresponding
    // developer won't be updated.
    $this
      ->disableUserPresave();
    foreach ($this->fields as $field_type => $data) {
      $this->modifiedDrupalUsers[$user
        ->getEmail()]
        ->set($this->fieldNamePrefix . $data['name'], $data['data_changed']);
    }

    // Change first name, last name and username.
    $this->modifiedDrupalUsers[$user
      ->getEmail()]
      ->set('first_name', $this->randomGenerator
      ->word(8));
    $this->modifiedDrupalUsers[$user
      ->getEmail()]
      ->set('last_name', $this->randomGenerator
      ->word(8));
    $this->modifiedDrupalUsers[$user
      ->getEmail()]
      ->set('name', $this->randomGenerator
      ->word(8));

    // Set unlinked field in Drupal.
    $this->modifiedDrupalUsers[$user
      ->getEmail()]
      ->set($this->fieldNamePrefix . 'one_track_field', 'user');

    // It's necessary because changed time is automatically updated on the UI
    // only.
    $this->modifiedDrupalUsers[$user
      ->getEmail()]
      ->setChangedTime($this->container
      ->get('datetime.time')
      ->getCurrentTime() + 100);
    $this->modifiedDrupalUsers[$user
      ->getEmail()]
      ->save();
    $this
      ->enableUserPresave();
  }

  // Developer's username already exists. Should not be copied into Drupal.
  Developer::create([
    'email' => "{$this->prefix}.reserved@example.com",
    'userName' => reset($this->drupalUsers)
      ->getAccountName(),
    'firstName' => $this
      ->randomMachineName(),
    'lastName' => $this
      ->randomMachineName(),
  ])
    ->save();

  // Do not block Drupal user if the corresponding developer's status is
  // inactive.
  $active_user = $this
    ->createAccount([], TRUE, $this->prefix);
  $dc = new DeveloperController($this->container
    ->get('apigee_edge.sdk_connector')
    ->getOrganization(), $this->container
    ->get('apigee_edge.sdk_connector')
    ->getClient());
  $dc
    ->setStatus($active_user
    ->getEmail(), Developer::STATUS_INACTIVE);
  $this->inactiveDeveloper = Developer::load($active_user
    ->getEmail());
  $this
    ->assertEquals($this->inactiveDeveloper
    ->getStatus(), Developer::STATUS_INACTIVE);
}