From 0d4f790383e832f7d6dd3b709fc736837d48c18a Mon Sep 17 00:00:00 2001 From: "dhruv.mittal" <dhruv210803@gmail.com> Date: Mon, 31 Mar 2025 11:08:50 +0530 Subject: [PATCH 1/5] Issue #3515924: Add "check your email" page. --- .../VerifyEmailThankYouController.php | 69 +++++++++++++++++++ src/Form/VerifyEmailForm.php | 5 +- src/Routing/VerifyEmailRoutes.php | 15 +++- .../src/Functional/VerifyEmailRoutingTest.php | 18 ++++- 4 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 src/Controller/VerifyEmailThankYouController.php diff --git a/src/Controller/VerifyEmailThankYouController.php b/src/Controller/VerifyEmailThankYouController.php new file mode 100644 index 0000000..bcaa5a6 --- /dev/null +++ b/src/Controller/VerifyEmailThankYouController.php @@ -0,0 +1,69 @@ +<?php + +// src/Controller/VerifyEmailThankYouController.php + +namespace Drupal\verify_email\Controller; + +use Drupal\Core\Controller\ControllerBase; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; + +/** + * Controller for handling the thank you page. + */ +class VerifyEmailThankYouController extends ControllerBase { + + /** + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * VerifyEmailThankYouController constructor. + * + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + */ + public function __construct(EntityTypeManagerInterface $entityTypeManager) { + $this->entityTypeManager = $entityTypeManager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity_type.manager') + ); + } + + /** + * Renders the thank you page. + * + * @param string $entity_id + * The entity ID of the verify email form. + * + * @return array + * A render array. + */ + public function thankYouPage($entity_id) { + // Load the verify_email entity using the entity manager + $entity = $this->entityTypeManager->getStorage('verify_email')->load($entity_id); + + if ($entity) { + // Display the custom message + $message = t('Please check your email and click on the included link to verify your email address.'); + + return [ + '#theme' => 'item_list', + '#items' => [$message], + '#title' => t('Thank you'), + ]; + } + + // Fallback message in case entity doesn't exist + return [ + '#markup' => t('Thank you for your submission.'), + '#title' => t('Thank you'), + ]; + } +} diff --git a/src/Form/VerifyEmailForm.php b/src/Form/VerifyEmailForm.php index 798a709..13c7765 100644 --- a/src/Form/VerifyEmailForm.php +++ b/src/Form/VerifyEmailForm.php @@ -10,6 +10,7 @@ use Drupal\verify_email\Service\VerifierInterface; use Drupal\verify_email\VerifyEmailInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Url; /** * Provides a Verify Email form. @@ -91,7 +92,9 @@ public function submitForm(array &$form, FormStateInterface $form_state): void { // Send email containing confirmation link. $this->verifier->sendVerificationEmail($entity, $email); $this->messenger()->addStatus($this->t('The message has been sent.')); - $form_state->setRedirect('<front>'); + + // Redirect to the 'thank you' page of the specific entity. + $form_state->setRedirectUrl(Url::fromRoute('verify_email.' . $entity->id() . '.thanks')); } } diff --git a/src/Routing/VerifyEmailRoutes.php b/src/Routing/VerifyEmailRoutes.php index e35bfb8..d7f0728 100644 --- a/src/Routing/VerifyEmailRoutes.php +++ b/src/Routing/VerifyEmailRoutes.php @@ -6,6 +6,7 @@ use Drupal\verify_email\VerifyEmailRepositoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Routing\Route; +use Drupal\verify_email\Controller\VerifyEmailThankYouController; /** * Provides dynamic routes for search. @@ -55,10 +56,22 @@ public function routes(): array { ], ], ); + + // New route for the "/thanks" path with the controller + $routes["verify_email.$entity_id.thanks"] = new Route( + path: '/' . $entity->getPath() . '/thanks', + defaults: [ + '_controller' => 'Drupal\verify_email\Controller\VerifyEmailThankYouController::thankYouPage', + '_title' => 'Thank you', + 'entity_id' => $entity_id, // Passing the entity_id as a route parameter + ], + requirements: [ + '_access' => 'TRUE', + ], + ); } return $routes; - } } diff --git a/tests/src/Functional/VerifyEmailRoutingTest.php b/tests/src/Functional/VerifyEmailRoutingTest.php index b5c13c5..bdc5b4b 100644 --- a/tests/src/Functional/VerifyEmailRoutingTest.php +++ b/tests/src/Functional/VerifyEmailRoutingTest.php @@ -106,12 +106,26 @@ public function testInvalidEmail(): void { public function testVerifyEmailForm(): void { $this->drupalGet('/verify-email-form'); $this->assertSession()->statusCodeEquals(200); - // Submit the form with an valid email address. + + // Submit the form with a valid email address. $this->submitForm([ 'email' => 'another@test.com', ], 'Verify'); + + // Check if we are redirected to the correct 'Thank You' page. + // We assume the entity ID for the VerifyEmail form is 'verify_email_test'. + // So the thank you page route should be like /verify-email-form/thanks. $this->assertSession()->statusCodeEquals(200); - $this->assertSession()->statusMessageContains('The message has been sent.'); + + // Check if the correct 'Thank You' page is loaded. + // Check the path of the page to ensure we are redirected to the expected 'thanks' route. + $this->assertSession()->addressEquals('/verify-email-form/thanks'); + + // Check that the title is 'Thank you'. + $this->assertSession()->titleEquals('Thank you'); + + // Check if the page text contains the message confirming the email verification. + $this->assertSession()->pageTextContains('Please check your email and click on the included link to verify your email address.'); } /** -- GitLab From 2ff858a5bd46d595009ab452aa1b8daf759130d0 Mon Sep 17 00:00:00 2001 From: "dhruv.mittal" <dhruv210803@gmail.com> Date: Tue, 1 Apr 2025 15:15:15 +0530 Subject: [PATCH 2/5] Issue #3515924: Done suggested changes. --- .../VerifyEmailThankYouController.php | 43 +++---------------- src/Routing/VerifyEmailRoutes.php | 9 +++- .../src/Functional/VerifyEmailRoutingTest.php | 24 ++++------- 3 files changed, 23 insertions(+), 53 deletions(-) diff --git a/src/Controller/VerifyEmailThankYouController.php b/src/Controller/VerifyEmailThankYouController.php index bcaa5a6..e983812 100644 --- a/src/Controller/VerifyEmailThankYouController.php +++ b/src/Controller/VerifyEmailThankYouController.php @@ -1,69 +1,40 @@ <?php -// src/Controller/VerifyEmailThankYouController.php - namespace Drupal\verify_email\Controller; use Drupal\Core\Controller\ControllerBase; -use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\verify_email\Entity\VerifyEmail; /** * Controller for handling the thank you page. */ class VerifyEmailThankYouController extends ControllerBase { - /** - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected $entityTypeManager; - - /** - * VerifyEmailThankYouController constructor. - * - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager - */ - public function __construct(EntityTypeManagerInterface $entityTypeManager) { - $this->entityTypeManager = $entityTypeManager; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity_type.manager') - ); - } - /** * Renders the thank you page. * - * @param string $entity_id - * The entity ID of the verify email form. + * @param \Drupal\verify_email\Entity\VerifyEmail|null $entity + * The verify email entity passed from the route. * * @return array * A render array. */ - public function thankYouPage($entity_id) { - // Load the verify_email entity using the entity manager - $entity = $this->entityTypeManager->getStorage('verify_email')->load($entity_id); + public function thankYouPage(VerifyEmail|null $entity = NULL) { if ($entity) { // Display the custom message $message = t('Please check your email and click on the included link to verify your email address.'); return [ - '#theme' => 'item_list', - '#items' => [$message], + '#markup' => $message, '#title' => t('Thank you'), ]; } // Fallback message in case entity doesn't exist return [ - '#markup' => t('Thank you for your submission.'), - '#title' => t('Thank you'), + '#markup' => t('Something has gone wrong. Please try submitting your email address again.'), + '#title' => t('Sorry'), ]; } } diff --git a/src/Routing/VerifyEmailRoutes.php b/src/Routing/VerifyEmailRoutes.php index d7f0728..e901732 100644 --- a/src/Routing/VerifyEmailRoutes.php +++ b/src/Routing/VerifyEmailRoutes.php @@ -63,11 +63,18 @@ public function routes(): array { defaults: [ '_controller' => 'Drupal\verify_email\Controller\VerifyEmailThankYouController::thankYouPage', '_title' => 'Thank you', - 'entity_id' => $entity_id, // Passing the entity_id as a route parameter + 'entity_id' => $entity_id, ], requirements: [ '_access' => 'TRUE', ], + options: [ + 'parameters' => [ + 'entity' => [ + 'type' => 'entity:verify_email', + ], + ], + ], ); } diff --git a/tests/src/Functional/VerifyEmailRoutingTest.php b/tests/src/Functional/VerifyEmailRoutingTest.php index bdc5b4b..ef6805b 100644 --- a/tests/src/Functional/VerifyEmailRoutingTest.php +++ b/tests/src/Functional/VerifyEmailRoutingTest.php @@ -105,29 +105,21 @@ public function testInvalidEmail(): void { */ public function testVerifyEmailForm(): void { $this->drupalGet('/verify-email-form'); - $this->assertSession()->statusCodeEquals(200); + $session = $this->assertSession(); // Store assertSession in a variable + + $session->statusCodeEquals(200); // Submit the form with a valid email address. $this->submitForm([ 'email' => 'another@test.com', ], 'Verify'); - // Check if we are redirected to the correct 'Thank You' page. - // We assume the entity ID for the VerifyEmail form is 'verify_email_test'. - // So the thank you page route should be like /verify-email-form/thanks. - $this->assertSession()->statusCodeEquals(200); - - // Check if the correct 'Thank You' page is loaded. - // Check the path of the page to ensure we are redirected to the expected 'thanks' route. - $this->assertSession()->addressEquals('/verify-email-form/thanks'); - - // Check that the title is 'Thank you'. - $this->assertSession()->titleEquals('Thank you'); - - // Check if the page text contains the message confirming the email verification. - $this->assertSession()->pageTextContains('Please check your email and click on the included link to verify your email address.'); + // Check redirection to the 'Thank You' page after form submission + $session->statusCodeEquals(200); + $session->addressEquals('/verify-email-form/thanks'); + $session->elementExists('xpath', '//h1[text() = "Thank you"]'); + $session->pageTextContains('Please check your email and click on the included link to verify your email address.'); } - /** * Tests clicking the non existent validation link. */ -- GitLab From 40e51f55be65cf29612143a72e937970e9f8d2bb Mon Sep 17 00:00:00 2001 From: Nidhi Patadia <nidhipatadia27@gmail.com> Date: Tue, 1 Apr 2025 18:08:33 +0530 Subject: [PATCH 3/5] Resolved phpcs issues. --- src/Controller/VerifyEmailThankYouController.php | 8 ++++---- src/Routing/VerifyEmailRoutes.php | 3 +-- tests/src/Functional/VerifyEmailRoutingTest.php | 10 ++++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Controller/VerifyEmailThankYouController.php b/src/Controller/VerifyEmailThankYouController.php index e983812..ac15ff5 100644 --- a/src/Controller/VerifyEmailThankYouController.php +++ b/src/Controller/VerifyEmailThankYouController.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Drupal\verify_email\Controller; @@ -19,10 +19,9 @@ class VerifyEmailThankYouController extends ControllerBase { * @return array * A render array. */ - public function thankYouPage(VerifyEmail|null $entity = NULL) { if ($entity) { - // Display the custom message + // Display the custom message. $message = t('Please check your email and click on the included link to verify your email address.'); return [ @@ -31,10 +30,11 @@ public function thankYouPage(VerifyEmail|null $entity = NULL) { ]; } - // Fallback message in case entity doesn't exist + // Fallback message in case entity doesn't exist. return [ '#markup' => t('Something has gone wrong. Please try submitting your email address again.'), '#title' => t('Sorry'), ]; } + } diff --git a/src/Routing/VerifyEmailRoutes.php b/src/Routing/VerifyEmailRoutes.php index e901732..97146aa 100644 --- a/src/Routing/VerifyEmailRoutes.php +++ b/src/Routing/VerifyEmailRoutes.php @@ -6,7 +6,6 @@ use Drupal\verify_email\VerifyEmailRepositoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Routing\Route; -use Drupal\verify_email\Controller\VerifyEmailThankYouController; /** * Provides dynamic routes for search. @@ -57,7 +56,7 @@ public function routes(): array { ], ); - // New route for the "/thanks" path with the controller + // New route for the "/thanks" path with the controller. $routes["verify_email.$entity_id.thanks"] = new Route( path: '/' . $entity->getPath() . '/thanks', defaults: [ diff --git a/tests/src/Functional/VerifyEmailRoutingTest.php b/tests/src/Functional/VerifyEmailRoutingTest.php index ef6805b..c549b5d 100644 --- a/tests/src/Functional/VerifyEmailRoutingTest.php +++ b/tests/src/Functional/VerifyEmailRoutingTest.php @@ -105,21 +105,23 @@ public function testInvalidEmail(): void { */ public function testVerifyEmailForm(): void { $this->drupalGet('/verify-email-form'); - $session = $this->assertSession(); // Store assertSession in a variable + // Store assertSession in a variable. + $session = $this->assertSession(); $session->statusCodeEquals(200); - + // Submit the form with a valid email address. $this->submitForm([ 'email' => 'another@test.com', ], 'Verify'); - - // Check redirection to the 'Thank You' page after form submission + + // Check redirection to the 'Thank You' page after form submission. $session->statusCodeEquals(200); $session->addressEquals('/verify-email-form/thanks'); $session->elementExists('xpath', '//h1[text() = "Thank you"]'); $session->pageTextContains('Please check your email and click on the included link to verify your email address.'); } + /** * Tests clicking the non existent validation link. */ -- GitLab From 5f3d1822aa98a7e1d74a681aa62478053d566aa0 Mon Sep 17 00:00:00 2001 From: Nidhi Patadia <nidhipatadia27@gmail.com> Date: Tue, 1 Apr 2025 19:01:10 +0530 Subject: [PATCH 4/5] Resolved phpcs issues. --- src/Routing/VerifyEmailRoutes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Routing/VerifyEmailRoutes.php b/src/Routing/VerifyEmailRoutes.php index 97146aa..edac468 100644 --- a/src/Routing/VerifyEmailRoutes.php +++ b/src/Routing/VerifyEmailRoutes.php @@ -62,7 +62,7 @@ public function routes(): array { defaults: [ '_controller' => 'Drupal\verify_email\Controller\VerifyEmailThankYouController::thankYouPage', '_title' => 'Thank you', - 'entity_id' => $entity_id, + 'entity' => $entity_id, ], requirements: [ '_access' => 'TRUE', -- GitLab From b3ece269232c2150dd39e12e5af7659d08bb4024 Mon Sep 17 00:00:00 2001 From: Nidhi Patadia <nidhipatadia27@gmail.com> Date: Tue, 1 Apr 2025 19:45:34 +0530 Subject: [PATCH 5/5] Renamed the controller. --- .../{VerifyEmailThankYouController.php => ThankYou.php} | 2 +- src/Routing/VerifyEmailRoutes.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/Controller/{VerifyEmailThankYouController.php => ThankYou.php} (93%) diff --git a/src/Controller/VerifyEmailThankYouController.php b/src/Controller/ThankYou.php similarity index 93% rename from src/Controller/VerifyEmailThankYouController.php rename to src/Controller/ThankYou.php index ac15ff5..2e5af86 100644 --- a/src/Controller/VerifyEmailThankYouController.php +++ b/src/Controller/ThankYou.php @@ -8,7 +8,7 @@ /** * Controller for handling the thank you page. */ -class VerifyEmailThankYouController extends ControllerBase { +class ThankYou extends ControllerBase { /** * Renders the thank you page. diff --git a/src/Routing/VerifyEmailRoutes.php b/src/Routing/VerifyEmailRoutes.php index edac468..f6c32f2 100644 --- a/src/Routing/VerifyEmailRoutes.php +++ b/src/Routing/VerifyEmailRoutes.php @@ -60,7 +60,7 @@ public function routes(): array { $routes["verify_email.$entity_id.thanks"] = new Route( path: '/' . $entity->getPath() . '/thanks', defaults: [ - '_controller' => 'Drupal\verify_email\Controller\VerifyEmailThankYouController::thankYouPage', + '_controller' => 'Drupal\verify_email\Controller\ThankYou::thankYouPage', '_title' => 'Thank you', 'entity' => $entity_id, ], -- GitLab