email_confirmer-8.x-1.x-dev/tests/src/Functional/EmailConfirmerServiceTest.php
tests/src/Functional/EmailConfirmerServiceTest.php
<?php
namespace Drupal\Tests\email_confirmer\Functional;
use Drupal\email_confirmer\EmailConfirmationInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Tests the email confirmer service.
*
* @group email_confirmer
*/
class EmailConfirmerServiceTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'email_confirmer',
'email_confirmer_test',
];
/**
* The email confirmer service.
*
* @var \Drupal\email_confirmer\EmailConfirmerInterface
*/
protected $emailConfirmer;
/**
* The mail manager.
*
* @var \Drupal\email_confirmer_test\MailCollector
*/
protected $mailManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The email address to use for testing.
*
* @var string
*/
protected string $emailAddress = 'test@example.com';
/**
* The email confirmation.
*
* @var \Drupal\email_confirmer\EmailConfirmationInterface
*/
protected EmailConfirmationInterface $confirmation;
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->emailConfirmer = \Drupal::service('email_confirmer');
$this->mailManager = \Drupal::service('plugin.manager.mail');
$this->entityTypeManager = \Drupal::service('entity_type.manager');
$this->configFactory = \Drupal::service('config.factory');
$this->token = \Drupal::service('token');
// Initialize the confirmation and send it.
$this->confirmation = $this->emailConfirmer->confirm($this->emailAddress);
}
/**
* Tests that the confirmation email is sent.
*/
public function testConfirmationEmail() {
$mails = $this->mailManager->getMails();
// Check that an email is sent.
$this->assertCount(1, $mails);
$mail = $mails[0];
// Check that an email is sent to the confirmed email address.
$this->assertEquals($this->emailAddress, $mail['to']);
// Check that the email contains the expected text.
$this->assertStringContainsString('We have received a request to confirm ' . $this->emailAddress, $mail['body'] ?? '');
}
/**
* Tests that the confirmation response form is correct.
*/
public function testEmailConfirmationResponsePage() {
// Open the confirmation response page.
$this->drupalGet($url = $this->getConfirmationUrl());
// Check that the page is not accessible.
$this->assertSession()->statusCodeEquals(403);
// Grant access to anonymous users.
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('access email confirmation')
->save();
// Check that the page is now accessible.
$this->drupalGet($url);
$this->assertSession()->statusCodeEquals(200);
// Check that the page title is correct.
$this->assertSession()->titleEquals('Confirm ' . $this->emailAddress . ' | Drupal');
// Check that the form is present.
$this->assertSession()->elementExists('css', 'input[name="cancel"]');
$this->assertSession()->buttonExists('Send');
}
/**
* Test positive confirmation of an email address.
*/
public function testEmailConfirmationConfirm() {
// Go to the confirmation response form.
$this->grantConfirmationAccessPermission();
$this->drupalGet($url = $this->getConfirmationUrl());
// Submit the form.
$this->click('[data-drupal-selector="edit-submit"]');
// Positive confirmation message expected.
$this->assertSession()->statusMessageContains('Email confirmation confirmed.');
// Check that the email confirmation is confirmed.
$this->reloadConfirmation();
$this->assertTrue($this->confirmation->isConfirmed());
// Back to the confirmation page.
$this->drupalGet($url);
// Check that the page is still accessible.
$this->assertSession()->statusCodeEquals(200);
// Check the already confirmed message to the user is present.
$this->assertSession()->titleEquals('Confirmation done | Drupal');
$this->assertSession()->pageTextContains('Your email address was confirmed.');
}
/**
* Get the confirmation response URL.
*
* @return string
* The confirmation response URL.
*/
protected function getConfirmationUrl() : string {
return $this->token->replace('[email-confirmer:confirmation-url]', ['email_confirmer_confirmation' => $this->confirmation]);
}
/**
* Grants confirmation access permission to the anonymous role.
*/
protected function grantConfirmationAccessPermission() {
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('access email confirmation')
->save();
}
/**
* Reloads the confirmation.
*/
protected function reloadConfirmation() {
$this->confirmation = $this->entityTypeManager->getStorage('email_confirmer_confirmation')->load($this->confirmation->id());
}
}
