View source
<?php
namespace Drupal\Tests\apigee_edge\Functional;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Form\DeveloperSettingsForm;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Core\Url;
class EmailTest extends ApigeeEdgeFunctionalTestBase {
use AssertMailTrait;
protected $developer;
protected static $mock_api_client_ready = TRUE;
protected function tearDown() {
try {
if ($this->developer !== NULL) {
$this
->queueDeveloperResponseFromDeveloper($this->developer);
$this->developer
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testEmailValidator() {
$name = strtolower($this
->randomMachineName());
$this->developer = Developer::create([
'email' => $name . '@example.com',
'userName' => $name,
'firstName' => $this
->getRandomGenerator()
->word(8),
'lastName' => $this
->getRandomGenerator()
->word(8),
]);
$this
->queueDeveloperResponseFromDeveloper($this->developer, 201);
$this->stack
->queueMockResponse('no_content');
$this->developer
->save();
$this
->addOrganizationMatchedResponse();
$this
->editUserWithAlreadyExistingEmailTest();
$this
->registerWithAlreadyExistingEmail();
}
public function editUserWithAlreadyExistingEmailTest() {
$this
->disableUserPresave();
$account = $this
->createAccount();
$this
->enableUserPresave();
$this
->drupalLogin($account);
$this
->queueDeveloperResponseFromDeveloper($this->developer);
$this
->drupalPostForm(Url::fromRoute('entity.user.edit_form', [
'user' => $account
->id(),
]), [
'mail' => $this->developer
->getEmail(),
'current_pass' => $account->passRaw,
], 'Save');
$this
->assertSession()
->pageTextContains('This email address already exists in our system. You can register a new account if you would like to use it on the Developer Portal.');
$this
->drupalLogin($this->rootUser);
$this
->drupalPostForm(Url::fromRoute('entity.user.edit_form', [
'user' => $account
->id(),
]), [
'mail' => $this->developer
->getEmail(),
], 'Save');
$this
->assertSession()
->pageTextContains('This email address already belongs to a developer on Apigee Edge.');
}
protected function registerWithAlreadyExistingEmail() {
$user_register_path = Url::fromRoute('user.register')
->toString();
$developer_settings_path = Url::fromRoute('apigee_edge.settings.developer')
->toString();
$edit = [
'name' => $this->developer
->getUserName(),
'mail' => $this->developer
->getEmail(),
'first_name[0][value]' => $this->developer
->getFirstName(),
'last_name[0][value]' => $this->developer
->getLastName(),
];
$this
->drupalLogin($this->rootUser);
$error_message = trim($this
->getRandomGenerator()
->paragraphs(1));
$this
->drupalPostForm($developer_settings_path, [
'verification_action' => DeveloperSettingsForm::VERIFICATION_ACTION_DISPLAY_ERROR_ONLY,
'display_only_error_message_content[value]' => $error_message,
], 'Save configuration');
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
$this
->drupalLogout();
$this
->drupalPostForm($user_register_path, $edit, 'Create new account');
$this
->assertSession()
->pageTextContains($error_message);
$this
->drupalLogin($this->rootUser);
$this
->drupalPostForm($developer_settings_path, [
'verification_action' => DeveloperSettingsForm::VERIFICATION_ACTION_VERIFY_EMAIL,
], 'Save configuration');
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
$this
->drupalLogout();
$this
->drupalPostForm($user_register_path, $edit, 'Create new account');
$this
->assertSession()
->pageTextContains("This email address already exists in our system. We have sent you an verification email to {$this->developer->getEmail()}.");
$this
->assertMail('id', 'apigee_edge_developer_email_verification');
$this
->assertMail('to', $this->developer
->getEmail());
$mails = $this
->getMails();
$mail = end($mails);
$matches = [];
preg_match('%https?://[^/]+/user/register\\?[^/\\s]+%', $mail['body'], $matches);
$link = $matches[0];
$this
->drupalPostForm($link, $edit, 'Create new account');
$this
->assertSession()
->pageTextContains('A welcome message with further instructions has been sent to your email address.');
}
}