You are here

protected function DeveloperTest::developerRegisterTest in Apigee Edge 8

Tests user/developer registration and edit.

1 call to DeveloperTest::developerRegisterTest()
DeveloperTest::testDeveloperRegisterAndCreate in tests/src/Functional/DeveloperTest.php
Tests developer registration and create by admin.

File

tests/src/Functional/DeveloperTest.php, line 163

Class

DeveloperTest
Create, delete, update developer entity tests.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

protected function developerRegisterTest() {
  $test_user = [
    'email' => $this
      ->randomMachineName() . '@example.com',
    'username' => $this
      ->randomMachineName(),
    'first_name' => $this
      ->getRandomGenerator()
      ->word(16),
    'last_name' => $this
      ->getRandomGenerator()
      ->word(16),
  ];
  $formdata = [
    'mail' => $test_user['email'],
    'first_name[0][value]' => $test_user['first_name'],
    'last_name[0][value]' => $test_user['last_name'],
    'name' => $test_user['username'],
  ];

  // Try to register with incorrect API credentials.
  $this
    ->invalidateKey();
  $this
    ->drupalPostForm(Url::fromRoute('user.register'), $formdata, 'Create new account');
  $this
    ->assertSession()
    ->pageTextContains(self::USER_REGISTRATION_UNAVAILABLE);

  // Try to register with correct API credentials.
  $this
    ->restoreKey();
  $account = $this->entityTypeManager
    ->getStorage('user')
    ->create([
    'mail' => $test_user['email'],
    'name' => $test_user['username'],
    'first_name' => $test_user['first_name'],
    'last_name' => $test_user['last_name'],
  ]);
  $this->entityTypeManager
    ->getStorage('user')
    ->resetCache();
  $this->developerStorage
    ->resetCache();

  // Add matched organization response so it returns the org whenever called.
  $this
    ->addOrganizationMatchedResponse();

  // Add other responses needed to create an account. In order:
  // - 'get_not_found' - when creating an account `apigee_edge` module checks
  //   that the email does not already exist in Edge.
  // - 'get_not_found' - @to-do: why is this needed again?
  // - stack a mocked developer created response.
  $this->stack
    ->queueMockResponse('get_not_found');
  $this->stack
    ->queueMockResponse('get_not_found');
  $this
    ->queueDeveloperResponse($account, 201);
  $this
    ->drupalPostForm(Url::fromRoute('user.register'), $formdata, 'Create new account');

  /** @var \Drupal\user\Entity\User $account */
  $account = user_load_by_mail($test_user['email']);
  $this
    ->assertNotEmpty($account, 'Account is created');

  // Queue a developer response to mock the loading of a developer.
  $this
    ->queueDeveloperResponse($account);
  $this->developerRegistered = $this->developerStorage
    ->load($test_user['email']);
  $this
    ->assertNotEmpty($this->developerRegistered);
  $this
    ->assertEquals($this->developerRegistered
    ->getEmail(), $test_user['email']);
  $this
    ->assertEquals($this->developerRegistered
    ->getFirstName(), $test_user['first_name']);
  $this
    ->assertEquals($this->developerRegistered
    ->getLastName(), $test_user['last_name']);
  $this
    ->assertEquals($this->developerRegistered
    ->getUserName(), $test_user['username']);
  $this
    ->assertEquals($this->developerRegistered
    ->getStatus(), DeveloperInterface::STATUS_INACTIVE);

  // Attribute is set by mock twig template.
  $this
    ->assertEqual($this->developerRegistered
    ->getAttributeValue('IS_MOCK_CLIENT'), !$this->integration_enabled);
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->drupalPostForm(Url::fromRoute('entity.user.edit_form', [
    'user' => $account
      ->id(),
  ]), [
    'status' => '1',
  ], 'Save');

  // Ensure status change was saved.
  $this->entityTypeManager
    ->getStorage('user')
    ->resetCache();
  $account = user_load_by_mail($test_user['email']);
  $this
    ->assertTrue($account
    ->isActive());

  // Ensure that entity static cache is also invalidated in this scope too.
  $this->developerStorage
    ->resetCache([
    $test_user['email'],
  ]);

  // Queue a developer response to mock the loading of a developer.
  $this
    ->queueDeveloperResponse($account);
  $this->developerRegistered = $this->developerStorage
    ->loadUnchanged($test_user['email']);
  $this
    ->assertEquals($this->developerRegistered
    ->getEmail(), $test_user['email']);
  $this
    ->assertEquals($this->developerRegistered
    ->getFirstName(), $test_user['first_name']);
  $this
    ->assertEquals($this->developerRegistered
    ->getLastName(), $test_user['last_name']);
  $this
    ->assertEquals($this->developerRegistered
    ->getUserName(), $test_user['username']);
  $this
    ->assertEquals($this->developerRegistered
    ->getStatus(), DeveloperInterface::STATUS_ACTIVE);
}