You are here

protected function DeveloperTest::developerCreateByAdminTest in Apigee Edge 8

Tests creating, editing and deleting developer entity by admin.

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

File

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

Class

DeveloperTest
Create, delete, update developer entity tests.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

protected function developerCreateByAdminTest() {

  // Create blocked user by admin.
  $this
    ->drupalLogin($this->rootUser);
  $test_user = [
    'email' => $this
      ->randomMachineName() . '@example.com',
    'first_name' => $this
      ->getRandomGenerator()
      ->word(16),
    'last_name' => $this
      ->getRandomGenerator()
      ->word(16),
    'username' => $this
      ->randomMachineName(),
    'password' => user_password(),
    'status' => '0',
  ];
  $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'],
    'pass[pass1]' => $test_user['password'],
    'pass[pass2]' => $test_user['password'],
    'status' => $test_user['status'],
  ];

  // Try to register with incorrect API credentials.
  $this
    ->invalidateKey();
  $this
    ->drupalPostForm(Url::fromRoute('user.admin_create'), $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();

  // Stack responses needed to create a new developer account for the account.
  $this
    ->addOrganizationMatchedResponse();
  $this->stack
    ->queueMockResponse('get_not_found');
  $this->stack
    ->queueMockResponse('get_not_found');
  $this
    ->queueDeveloperResponse($account, 201);
  $this
    ->drupalPostForm(Url::fromRoute('user.admin_create'), $formdata, 'Create new account');

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

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

  // Unblock and edit the user's email, first name, last name by the admin.
  $test_user['email'] = "mod.{$test_user['email']}";
  $test_user['first_name'] = "(mod) {$test_user['first_name']}";
  $test_user['last_name'] = "(mod) {$test_user['last_name']}";
  $test_user['status'] = '1';
  $formdata['mail'] = $test_user['email'];
  $formdata['first_name[0][value]'] = $test_user['first_name'];
  $formdata['last_name[0][value]'] = $test_user['last_name'];
  $formdata['status'] = $test_user['status'];
  $this
    ->drupalPostForm(Url::fromRoute('entity.user.edit_form', [
    'user' => $account
      ->id(),
  ]), $formdata, 'Save');

  // Flush user entity cache to ensure the updated user gets loaded.
  // (Especially in apigee_edge_developer_app_storage_load().)
  $this->entityTypeManager
    ->getStorage('user')
    ->resetCache([
    $account
      ->id(),
  ]);
  $account = user_load_by_mail($test_user['email']);
  $this
    ->assertNotEmpty($account);

  // Queue a developer response to mock the loading of a developer.
  $this
    ->queueDeveloperResponse($account);

  // Ensure that entity static cache is also invalidated in this scope.
  $this->developerCreatedByAdmin = $this->developerStorage
    ->loadUnchanged($test_user['email']);
  $this
    ->assertNotEmpty($this->developerCreatedByAdmin);
  $this
    ->assertEquals($this->developerCreatedByAdmin
    ->getEmail(), $test_user['email']);
  $this
    ->assertEquals($this->developerCreatedByAdmin
    ->getFirstName(), $test_user['first_name']);
  $this
    ->assertEquals($this->developerCreatedByAdmin
    ->getLastName(), $test_user['last_name']);
  $this
    ->assertEquals($this->developerCreatedByAdmin
    ->getUserName(), $test_user['username']);
  $this
    ->assertEquals($this->developerCreatedByAdmin
    ->getStatus(), DeveloperInterface::STATUS_ACTIVE);

  // Block the user's account on the people form.
  $this
    ->drupalGet(Url::fromRoute('entity.user.collection'));
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('edit-action', 'user_block_user_action');
  $this
    ->getSession()
    ->getPage()
    ->checkField('edit-user-bulk-form-0');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('edit-submit');

  // Ensure that entity static cache is also invalidated in this scope
  // too.
  // - Stack response with the blocked developer.
  $modified_dev = clone $this->developerCreatedByAdmin;
  $modified_dev
    ->setStatus(DeveloperInterface::STATUS_INACTIVE);
  $this
    ->queueDeveloperResponseFromDeveloper($modified_dev);
  $this->developerCreatedByAdmin = $this->developerStorage
    ->loadUnchanged($test_user['email']);
  $this
    ->assertEquals($this->developerCreatedByAdmin
    ->getStatus(), DeveloperInterface::STATUS_INACTIVE);

  // Block user on the cancel form using the user_cancel_block method.
  $test_user['status'] = '1';
  $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'],
    'pass[pass1]' => $test_user['password'],
    'pass[pass2]' => $test_user['password'],
    'status' => $test_user['status'],
  ];
  $this
    ->drupalPostForm(Url::fromRoute('entity.user.edit_form', [
    'user' => $account
      ->id(),
  ]), $formdata, 'Save');
  $formdata = [
    'user_cancel_method' => 'user_cancel_block',
  ];
  $this
    ->drupalPostForm($account
    ->toUrl('cancel-form')
    ->toString(), $formdata, 'Cancel account');
  $this->developerCreatedByAdmin = $this->developerStorage
    ->load($test_user['email']);
  $this
    ->assertNotEmpty($this->developerCreatedByAdmin);
  $this
    ->assertEquals($this->developerCreatedByAdmin
    ->getStatus(), DeveloperInterface::STATUS_INACTIVE);

  // Block user on the cancel form using the user_cancel_reassign method.
  $test_user['status'] = '1';
  $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'],
    'pass[pass1]' => $test_user['password'],
    'pass[pass2]' => $test_user['password'],
    'status' => $test_user['status'],
  ];
  $this
    ->drupalPostForm(Url::fromRoute('entity.user.edit_form', [
    'user' => $account
      ->id(),
  ]), $formdata, 'Save');
  $formdata = [
    'user_cancel_method' => 'user_cancel_block_unpublish',
  ];
  $this
    ->drupalPostForm($account
    ->toUrl('cancel-form')
    ->toString(), $formdata, 'Cancel account');
  $this->developerCreatedByAdmin = $this->developerStorage
    ->load($test_user['email']);
  $this
    ->assertNotEmpty($this->developerCreatedByAdmin);
  $this
    ->assertEquals($this->developerCreatedByAdmin
    ->getStatus(), DeveloperInterface::STATUS_INACTIVE);

  // Delete user by admin.
  $formdata = [
    'user_cancel_method' => 'user_cancel_delete',
  ];
  $this
    ->drupalPostForm($account
    ->toUrl('cancel-form')
    ->toString(), $formdata, 'Cancel account');

  // Ensure that entity static cache is also invalidated in this scope
  // too.
  $this->stack
    ->queueMockResponse('get_not_found');
  $loaded = $this->developerStorage
    ->loadUnchanged($test_user['email']);
  $this
    ->assertEmpty($loaded, 'Developer does not exists anymore.');
}