Reproducing Drupal Contact Module Issue


Read {count} times since 2020

This post contains information regarding the Drupal Core issue. This post will explain how the bug was reproduced and how the test patch was created.

The bug was “Wrong message displayed after contact form mail(s) cannot be sent”.

Requires

  • Drupal 8.0
  • Contact Module

Note that URLs mentioned in this post “http://mydrupalsite.com/" is the URL of your Drupal installation and “/mydrupalsite” is the folder location of Drupal installation.

Reproducing Bug

  1. First of all we need to make a contact form page. Go to the Contact Module configuration page :
http://mydrupalsite.com/admin/structure/contact

Click on “Add Contact Form” button and in the new window that you go to, fill up the form with the asked information. Example :

Drupal Contact Form Config Page

Drupal Contact Form Config Page

In the Recipients filed, type in an email address that doesn’t exist. Tick the “Make this the default form” and submit the form by “Save” button.

  1. Go to the Contact Page of your site :
http://mydrupalsite.com/contact

Fill up the contact form with some data and submit the form :

Filling up the Drupal Contact Form

Filling up the Drupal Contact Form

  1. Now, we will see the bug :

Drupal Bug

Drupal Bug

As you can see, the success message is shown even when the email was failed to send.

Reproducing Bug in Tests

  1. Create following test file :
/mydrupalsite/core/modules/contact/src/Tests/FailedContactPersonalTest.php
  1. I made a new test class by extending “WebTestBase” class and made the testPersonalContactFail() method to reproduce bug :
function testPersonalContactFail() {
  // Create a valid form.
  $this->admin_user = $this->drupalCreateUser(array('access site-wide contact form', 'administer contact forms', 'administer permissions', 'administer users'));

  $this->drupalLogin($this->admin_user);
  $this->addContactForm($id = Unicode::strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), "[email protected]", '', TRUE);
 $this->assertRaw(t('Contact form %label has been added.', array('%label' => $label)));

  // Submit the contact form.
  $this->drupalLogout();
  $this->drupalGet('/contact');
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
  $this->submitContact("Test", "[email protected]", "Sample Message", $id, "Just a sample message");

  // Check the bug messages
  $this->assertText(t('Your message has been sent.'), 'Displayed "Message sent"');
  /* The following code only works if emails can be sent in tests */
  //$this->assertText(t('Unable to send email. Contact the site administrator if the problem persists.'), 'Displayed "Unable to send email..."');
}

The messages due to the bug that are these :

Unable to send email. Contact the site administrator if the problem persists.
Your message has been sent.

was checked in the output using $this->assertText() function. But, checking of the “Unable to send email. Contact the site administrator if the problem persists.” is not possible, because email is not actually sent in testing phase, so to output that message. Therefore that line is commented.

  1. Go to “http://mydrupalsite.com/admin/config/development/testing", expand “contact” and tick the checkbox aside to “DrupalcontactTestsNotExistEmailContactTest” :

Choose the contact module for testing

Choose the contact module for testing

  1. Click on “Run Tests” button at the end of the page. Now the testing is started. It will take some time to finish.

  2. Now, the success test page is shown :

Success Test Page

Success Test Page

Patching

  1. By using git, create a patch. For this, go to the root of Drupal installation, open terminal and commit the changed file :
git branch 2348119-Reproduce-bug-test-case
git checkout 2348119-Reproduce-bug-test-case
git add core/modules/contact/src/Tests/NotExistEmailContactTest.php
git commit core/modules/contact/src/Tests/NotExistEmailContactTest.php -m "Reproduced the bug https://www.drupal.org/node/2348119"
  1. Create the patch file :
git diff 8.0.x > Reproduce-bug-test-case-2348119-38.patch

Uploading to Issue page

Went to the issue page and added a new comment with the Patch file “Reproduce-bug-test-case-2348119-38.patch” :

Uploading the Patch to Issue Page

Uploading the Patch to Issue Page

Here is the comment link : https://www.drupal.org/node/2348119#comment-9409977

Conclusion

This experience gave me new information about Testing in Drupal. I realised that emails being sent in tests doesn’t actually get sent. Instead it is gone into the Drupal mail inbox. I agree with this method as if it was not like this, many emails will go into the recipient’s or admin’s inbox.

This was my first patch into the Drupal Core. Felt a little scary before doing it as I’m taking part in the CORE of DRUPAL !

With this little experience, I gained the confidence to make patches for the Drupal Core more !

Show Comments