View source
<?php
namespace Drupal\Tests\apigee_edge\Functional;
use Apigee\Edge\Api\Management\Controller\CompanyController;
use Apigee\Edge\Api\Management\Controller\CompanyMembersController;
use Apigee\Edge\Api\Management\Entity\Company;
use Apigee\Edge\Api\Management\Structure\CompanyMembership;
use Drupal\apigee_edge\Entity\DeveloperInterface;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
class DeveloperTest extends ApigeeEdgeFunctionalTestBase {
const USER_REGISTRATION_UNAVAILABLE = 'User registration is temporarily unavailable. Try again later or contact the site administrator.';
protected static $mock_api_client_ready = TRUE;
protected static $modules = [
'views',
];
protected $developerStorage;
protected $sdkConnector;
protected $developer;
protected $company;
protected $developerRegistered;
protected $developerCreatedByAdmin;
protected function setUp() {
parent::setUp();
$user_settings = $this
->config('user.settings');
$user_settings
->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
->save(TRUE);
$this->developerStorage = $this->container
->get('entity_type.manager')
->getStorage('developer');
$this->sdkConnector = $this->container
->get('apigee_edge.sdk_connector');
}
protected function tearDown() {
$this->stack
->reset();
try {
if ($this->developer !== NULL) {
$this
->queueDeveloperResponseFromDeveloper($this->developer);
$this->developer
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
try {
if ($this->company !== NULL) {
$this->stack
->queueMockResponse([
'company' => [
'company' => $this->company,
],
]);
$company_controller = new CompanyController($this->sdkConnector
->getOrganization(), $this->sdkConnector
->getClient());
$company_controller
->delete($this->company
->id());
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
try {
if ($this->developerRegistered !== NULL) {
$this
->queueDeveloperResponseFromDeveloper($this->developerRegistered);
$this->developerRegistered
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
try {
if ($this->developerCreatedByAdmin !== NULL) {
$this
->queueDeveloperResponseFromDeveloper($this->developerCreatedByAdmin);
$this->developerCreatedByAdmin
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testDeveloperRegisterAndCreate() {
$this
->developerRegisterTest();
$this
->developerCreateByAdminTest();
$this
->developerGetCompanyListTest();
}
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'],
];
$this
->invalidateKey();
$this
->drupalPostForm(Url::fromRoute('user.register'), $formdata, 'Create new account');
$this
->assertSession()
->pageTextContains(self::USER_REGISTRATION_UNAVAILABLE);
$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();
$this
->addOrganizationMatchedResponse();
$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');
$account = user_load_by_mail($test_user['email']);
$this
->assertNotEmpty($account, 'Account is created');
$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);
$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');
$this->entityTypeManager
->getStorage('user')
->resetCache();
$account = user_load_by_mail($test_user['email']);
$this
->assertTrue($account
->isActive());
$this->developerStorage
->resetCache([
$test_user['email'],
]);
$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);
}
protected function developerCreateByAdminTest() {
$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'],
];
$this
->invalidateKey();
$this
->drupalPostForm(Url::fromRoute('user.admin_create'), $formdata, 'Create new account');
$this
->assertSession()
->pageTextContains(self::USER_REGISTRATION_UNAVAILABLE);
$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();
$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');
$account = user_load_by_mail($test_user['email']);
$this
->assertNotEmpty($account);
$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);
$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');
$this->entityTypeManager
->getStorage('user')
->resetCache([
$account
->id(),
]);
$account = user_load_by_mail($test_user['email']);
$this
->assertNotEmpty($account);
$this
->queueDeveloperResponse($account);
$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);
$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');
$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);
$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);
$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);
$formdata = [
'user_cancel_method' => 'user_cancel_delete',
];
$this
->drupalPostForm($account
->toUrl('cancel-form')
->toString(), $formdata, 'Cancel account');
$this->stack
->queueMockResponse('get_not_found');
$loaded = $this->developerStorage
->loadUnchanged($test_user['email']);
$this
->assertEmpty($loaded, 'Developer does not exists anymore.');
}
public function developerGetCompanyListTest() {
$this
->addOrganizationMatchedResponse();
$name = strtolower($this
->randomMachineName());
$this->developer = $this->developerStorage
->create([
'email' => $name . '@example.com',
'userName' => $name,
'firstName' => $this
->getRandomGenerator()
->word(8),
'lastName' => $this
->getRandomGenerator()
->word(8),
]);
$account = $this
->queueDeveloperResponseFromDeveloper($this->developer, 201);
$this->stack
->queueMockResponse('no_content');
$this->developer
->save();
$this
->queueDeveloperResponse($account);
$this->developer
->getCompanies();
$this
->assertNotNull($this->developer
->getCompanies());
$this
->assertEmpty($this->developer
->getCompanies());
$this->company = new Company([
'name' => $this
->getRandomGenerator()
->name(),
'displayName' => $this
->getRandomGenerator()
->name(),
]);
$company_controller = new CompanyController($this->sdkConnector
->getOrganization(), $this->sdkConnector
->getClient());
$this
->queueCompanyResponse($this->company, 201);
$company_controller
->create($this->company);
$this
->queueDevsInCompanyResponse([
[
'email' => $this->developer
->getEmail(),
'role' => '',
],
]);
$company_membership_controller = new CompanyMembersController($this->company
->getName(), $this->sdkConnector
->getOrganization(), $this->sdkConnector
->getClient());
$company_membership = new CompanyMembership([
$this->developer
->getEmail() => NULL,
]);
$company_membership_controller
->setMembers($company_membership);
$this->developerStorage
->resetCache([
$this->developer
->id(),
]);
$developer_cache = $this->container
->get('apigee_edge.controller.cache.developer');
$developer_cache
->removeEntities([
$this->developer
->id(),
]);
$this->stack
->queueMockResponse([
'developers' => [
'developers' => [
$account,
],
'expand' => TRUE,
],
]);
$this
->queueDeveloperResponse($account, NULL, [
'companies' => [
$this->company
->getName(),
],
]);
$developer = $this->developerStorage
->loadMultiple()[$this->developer
->id()];
$this
->assertContains($this->company
->getName(), $developer
->getCompanies());
self::assertTrue($developer
->hasCompany($this->company
->getName()));
$this->stack
->queueMockResponse('no_content');
$company_membership_controller
->removeMember($this->developer
->getEmail());
$developer_cache
->removeEntities([
$this->developer
->id(),
]);
$this
->queueDeveloperResponse($account);
$developer = $this->developerStorage
->loadUnchanged($this->developer
->id());
$this
->queueDeveloperResponse($account);
$this
->assertNotContains($this->company
->getName(), $developer
->getCompanies());
self::assertFalse($developer
->hasCompany($this->company
->getName()));
}
}