annotate core/modules/contact/tests/src/Functional/ContactSitewideTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\contact\Functional;
Chris@0 4
Chris@18 5 use Drupal\Core\Url;
Chris@0 6 use Drupal\contact\Entity\ContactForm;
Chris@0 7 use Drupal\Core\Mail\MailFormatHelper;
Chris@0 8 use Drupal\Core\Test\AssertMailTrait;
Chris@0 9 use Drupal\Tests\BrowserTestBase;
Chris@0 10 use Drupal\Core\Entity\EntityTypeInterface;
Chris@17 11 use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
Chris@0 12 use Drupal\user\RoleInterface;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Tests site-wide contact form functionality.
Chris@0 16 *
Chris@0 17 * @see \Drupal\Tests\contact\Functional\ContactStorageTest
Chris@0 18 *
Chris@0 19 * @group contact
Chris@0 20 */
Chris@0 21 class ContactSitewideTest extends BrowserTestBase {
Chris@0 22
Chris@0 23 use FieldUiTestTrait;
Chris@0 24 use AssertMailTrait;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Modules to enable.
Chris@0 28 *
Chris@0 29 * @var array
Chris@0 30 */
Chris@0 31 public static $modules = ['text', 'contact', 'field_ui', 'contact_test', 'block', 'error_service_test', 'dblog'];
Chris@0 32
Chris@0 33 /**
Chris@0 34 * {@inheritdoc}
Chris@0 35 */
Chris@0 36 protected function setUp() {
Chris@0 37 parent::setUp();
Chris@0 38 $this->drupalPlaceBlock('system_breadcrumb_block');
Chris@0 39 $this->drupalPlaceBlock('local_actions_block');
Chris@0 40 $this->drupalPlaceBlock('page_title_block');
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Tests configuration options and the site-wide contact form.
Chris@0 45 */
Chris@0 46 public function testSiteWideContact() {
Chris@0 47 // Tests name and email fields for authenticated and anonymous users.
Chris@0 48 $this->drupalLogin($this->drupalCreateUser(['access site-wide contact form']));
Chris@0 49 $this->drupalGet('contact');
Chris@0 50
Chris@0 51 // Ensure that there is no textfield for name.
Chris@0 52 $this->assertFalse($this->xpath('//input[@name=:name]', [':name' => 'name']));
Chris@0 53
Chris@0 54 // Ensure that there is no textfield for email.
Chris@0 55 $this->assertFalse($this->xpath('//input[@name=:name]', [':name' => 'mail']));
Chris@0 56
Chris@0 57 // Logout and retrieve the page as an anonymous user
Chris@0 58 $this->drupalLogout();
Chris@0 59 user_role_grant_permissions('anonymous', ['access site-wide contact form']);
Chris@0 60 $this->drupalGet('contact');
Chris@0 61
Chris@0 62 // Ensure that there is textfield for name.
Chris@0 63 $this->assertTrue($this->xpath('//input[@name=:name]', [':name' => 'name']));
Chris@0 64
Chris@0 65 // Ensure that there is textfield for email.
Chris@0 66 $this->assertTrue($this->xpath('//input[@name=:name]', [':name' => 'mail']));
Chris@0 67
Chris@0 68 // Create and log in administrative user.
Chris@0 69 $admin_user = $this->drupalCreateUser([
Chris@0 70 'access site-wide contact form',
Chris@0 71 'administer contact forms',
Chris@0 72 'administer users',
Chris@0 73 'administer account settings',
Chris@0 74 'administer contact_message display',
Chris@0 75 'administer contact_message fields',
Chris@0 76 'administer contact_message form display',
Chris@0 77 ]);
Chris@0 78 $this->drupalLogin($admin_user);
Chris@0 79
Chris@0 80 // Check the presence of expected cache tags.
Chris@0 81 $this->drupalGet('contact');
Chris@0 82 $this->assertCacheTag('config:contact.settings');
Chris@0 83
Chris@0 84 $flood_limit = 3;
Chris@0 85 $this->config('contact.settings')
Chris@0 86 ->set('flood.limit', $flood_limit)
Chris@0 87 ->set('flood.interval', 600)
Chris@0 88 ->save();
Chris@0 89
Chris@0 90 // Set settings.
Chris@0 91 $edit = [];
Chris@0 92 $edit['contact_default_status'] = TRUE;
Chris@0 93 $this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
Chris@0 94 $this->assertText(t('The configuration options have been saved.'));
Chris@0 95
Chris@0 96 $this->drupalGet('admin/structure/contact');
Chris@0 97 // Default form exists.
Chris@0 98 $this->assertLinkByHref('admin/structure/contact/manage/feedback/delete');
Chris@0 99 // User form could not be changed or deleted.
Chris@0 100 // Cannot use ::assertNoLinkByHref as it does partial url matching and with
Chris@0 101 // field_ui enabled admin/structure/contact/manage/personal/fields exists.
Chris@0 102 // @todo: See https://www.drupal.org/node/2031223 for the above.
Chris@0 103 $edit_link = $this->xpath('//a[@href=:href]', [
Chris@18 104 ':href' => Url::fromRoute('entity.contact_form.edit_form', ['contact_form' => 'personal'])->toString(),
Chris@0 105 ]);
Chris@0 106 $this->assertTrue(empty($edit_link), format_string('No link containing href %href found.',
Chris@0 107 ['%href' => 'admin/structure/contact/manage/personal']
Chris@0 108 ));
Chris@0 109 $this->assertNoLinkByHref('admin/structure/contact/manage/personal/delete');
Chris@0 110
Chris@0 111 $this->drupalGet('admin/structure/contact/manage/personal');
Chris@0 112 $this->assertResponse(403);
Chris@0 113
Chris@0 114 // Delete old forms to ensure that new forms are used.
Chris@0 115 $this->deleteContactForms();
Chris@0 116 $this->drupalGet('admin/structure/contact');
Chris@0 117 $this->assertText('Personal', 'Personal form was not deleted');
Chris@0 118 $this->assertNoLinkByHref('admin/structure/contact/manage/feedback');
Chris@0 119
Chris@0 120 // Ensure that the contact form won't be shown without forms.
Chris@0 121 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
Chris@0 122 $this->drupalLogout();
Chris@0 123 $this->drupalGet('contact');
Chris@0 124 $this->assertResponse(404);
Chris@0 125
Chris@0 126 $this->drupalLogin($admin_user);
Chris@0 127 $this->drupalGet('contact');
Chris@0 128 $this->assertResponse(200);
Chris@0 129 $this->assertText(t('The contact form has not been configured.'));
Chris@0 130 // Test access personal form via site-wide contact page.
Chris@0 131 $this->drupalGet('contact/personal');
Chris@0 132 $this->assertResponse(403);
Chris@0 133
Chris@0 134 // Add forms.
Chris@0 135 // Test invalid recipients.
Chris@0 136 $invalid_recipients = ['invalid', 'invalid@', 'invalid@site.', '@site.', '@site.com'];
Chris@0 137 foreach ($invalid_recipients as $invalid_recipient) {
Chris@0 138 $this->addContactForm($this->randomMachineName(16), $this->randomMachineName(16), $invalid_recipient, '', FALSE);
Chris@0 139 $this->assertRaw(t('%recipient is an invalid email address.', ['%recipient' => $invalid_recipient]));
Chris@0 140 }
Chris@0 141
Chris@0 142 // Test validation of empty form and recipients fields.
Chris@0 143 $this->addContactForm('', '', '', '', TRUE);
Chris@0 144 $this->assertText(t('Label field is required.'));
Chris@0 145 $this->assertText(t('Machine-readable name field is required.'));
Chris@0 146 $this->assertText(t('Recipients field is required.'));
Chris@0 147
Chris@0 148 // Test validation of max_length machine name.
Chris@0 149 $recipients = ['simpletest&@example.com', 'simpletest2@example.com', 'simpletest3@example.com'];
Chris@0 150 $max_length = EntityTypeInterface::BUNDLE_MAX_LENGTH;
Chris@0 151 $max_length_exceeded = $max_length + 1;
Chris@17 152 $this->addContactForm($id = mb_strtolower($this->randomMachineName($max_length_exceeded)), $label = $this->randomMachineName($max_length_exceeded), implode(',', [$recipients[0]]), '', TRUE);
Chris@0 153 $this->assertText(format_string('Machine-readable name cannot be longer than @max characters but is currently @exceeded characters long.', ['@max' => $max_length, '@exceeded' => $max_length_exceeded]));
Chris@17 154 $this->addContactForm($id = mb_strtolower($this->randomMachineName($max_length)), $label = $this->randomMachineName($max_length), implode(',', [$recipients[0]]), '', TRUE);
Chris@0 155 $this->assertText(t('Contact form @label has been added.', ['@label' => $label]));
Chris@0 156
Chris@0 157 // Verify that the creation message contains a link to a contact form.
Chris@0 158 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'contact/']);
Chris@0 159 $this->assert(isset($view_link), 'The message area contains a link to a contact form.');
Chris@0 160
Chris@0 161 // Create first valid form.
Chris@17 162 $this->addContactForm($id = mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0]]), '', TRUE);
Chris@0 163 $this->assertText(t('Contact form @label has been added.', ['@label' => $label]));
Chris@0 164
Chris@0 165 // Verify that the creation message contains a link to a contact form.
Chris@0 166 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'contact/']);
Chris@0 167 $this->assert(isset($view_link), 'The message area contains a link to a contact form.');
Chris@0 168
Chris@0 169 // Check that the form was created in site default language.
Chris@0 170 $langcode = $this->config('contact.form.' . $id)->get('langcode');
Chris@0 171 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
Chris@0 172 $this->assertEqual($langcode, $default_langcode);
Chris@0 173
Chris@0 174 // Make sure the newly created form is included in the list of forms.
Chris@0 175 $this->assertNoUniqueText($label, 'New form included in forms list.');
Chris@0 176
Chris@0 177 // Ensure that the recipient email is escaped on the listing.
Chris@0 178 $this->drupalGet('admin/structure/contact');
Chris@0 179 $this->assertEscaped($recipients[0]);
Chris@0 180
Chris@0 181 // Test update contact form.
Chris@0 182 $this->updateContactForm($id, $label = $this->randomMachineName(16), $recipients_str = implode(',', [$recipients[0], $recipients[1]]), $reply = $this->randomMachineName(30), FALSE, 'Your message has been sent.', '/user');
Chris@0 183 $config = $this->config('contact.form.' . $id)->get();
Chris@0 184 $this->assertEqual($config['label'], $label);
Chris@0 185 $this->assertEqual($config['recipients'], [$recipients[0], $recipients[1]]);
Chris@0 186 $this->assertEqual($config['reply'], $reply);
Chris@0 187 $this->assertNotEqual($id, $this->config('contact.settings')->get('default_form'));
Chris@0 188 $this->assertText(t('Contact form @label has been updated.', ['@label' => $label]));
Chris@0 189 // Ensure the label is displayed on the contact page for this form.
Chris@0 190 $this->drupalGet('contact/' . $id);
Chris@0 191 $this->assertText($label);
Chris@0 192
Chris@0 193 // Reset the form back to be the default form.
Chris@0 194 $this->config('contact.settings')->set('default_form', $id)->save();
Chris@0 195
Chris@0 196 // Ensure that the contact form is shown without a form selection input.
Chris@0 197 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
Chris@0 198 $this->drupalLogout();
Chris@0 199 $this->drupalGet('contact');
Chris@0 200 $this->assertText(t('Your email address'));
Chris@0 201 $this->assertNoText(t('Form'));
Chris@0 202 $this->drupalLogin($admin_user);
Chris@0 203
Chris@0 204 // Add more forms.
Chris@17 205 $this->addContactForm(mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0], $recipients[1]]), '', FALSE);
Chris@0 206 $this->assertText(t('Contact form @label has been added.', ['@label' => $label]));
Chris@0 207
Chris@17 208 $this->addContactForm($name = mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0], $recipients[1], $recipients[2]]), '', FALSE);
Chris@0 209 $this->assertText(t('Contact form @label has been added.', ['@label' => $label]));
Chris@0 210
Chris@0 211 // Try adding a form that already exists.
Chris@0 212 $this->addContactForm($name, $label, '', '', FALSE);
Chris@0 213 $this->assertNoText(t('Contact form @label has been added.', ['@label' => $label]));
Chris@0 214 $this->assertRaw(t('The machine-readable name is already in use. It must be unique.'));
Chris@0 215
Chris@0 216 $this->drupalLogout();
Chris@0 217
Chris@0 218 // Check to see that anonymous user cannot see contact page without permission.
Chris@0 219 user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
Chris@0 220 $this->drupalGet('contact');
Chris@0 221 $this->assertResponse(403);
Chris@0 222
Chris@0 223 // Give anonymous user permission and see that page is viewable.
Chris@0 224 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
Chris@0 225 $this->drupalGet('contact');
Chris@0 226 $this->assertResponse(200);
Chris@0 227
Chris@0 228 // Submit contact form with invalid values.
Chris@0 229 $this->submitContact('', $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64));
Chris@0 230 $this->assertText(t('Your name field is required.'));
Chris@0 231
Chris@0 232 $this->submitContact($this->randomMachineName(16), '', $this->randomMachineName(16), $id, $this->randomMachineName(64));
Chris@0 233 $this->assertText(t('Your email address field is required.'));
Chris@0 234
Chris@0 235 $this->submitContact($this->randomMachineName(16), $invalid_recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64));
Chris@0 236 $this->assertRaw(t('The email address %mail is not valid.', ['%mail' => 'invalid']));
Chris@0 237
Chris@0 238 $this->submitContact($this->randomMachineName(16), $recipients[0], '', $id, $this->randomMachineName(64));
Chris@0 239 $this->assertText(t('Subject field is required.'));
Chris@0 240
Chris@0 241 $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, '');
Chris@0 242 $this->assertText(t('Message field is required.'));
Chris@0 243
Chris@0 244 // Test contact form with no default form selected.
Chris@0 245 $this->config('contact.settings')
Chris@0 246 ->set('default_form', '')
Chris@0 247 ->save();
Chris@0 248 $this->drupalGet('contact');
Chris@0 249 $this->assertResponse(404);
Chris@0 250
Chris@0 251 // Try to access contact form with non-existing form IDs.
Chris@0 252 $this->drupalGet('contact/0');
Chris@0 253 $this->assertResponse(404);
Chris@0 254 $this->drupalGet('contact/' . $this->randomMachineName());
Chris@0 255 $this->assertResponse(404);
Chris@0 256
Chris@0 257 // Submit contact form with correct values and check flood interval.
Chris@0 258 for ($i = 0; $i < $flood_limit; $i++) {
Chris@0 259 $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64));
Chris@0 260 $this->assertText(t('Your message has been sent.'));
Chris@0 261 }
Chris@0 262 // Submit contact form one over limit.
Chris@0 263 $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64));
Chris@0 264 $this->assertRaw(t('You cannot send more than %number messages in 10 min. Try again later.', ['%number' => $this->config('contact.settings')->get('flood.limit')]));
Chris@0 265
Chris@0 266 // Test listing controller.
Chris@0 267 $this->drupalLogin($admin_user);
Chris@0 268
Chris@0 269 $this->deleteContactForms();
Chris@0 270
Chris@0 271 $label = $this->randomMachineName(16);
Chris@0 272 $recipients = implode(',', [$recipients[0], $recipients[1], $recipients[2]]);
Chris@17 273 $contact_form = mb_strtolower($this->randomMachineName(16));
Chris@0 274 $this->addContactForm($contact_form, $label, $recipients, '', FALSE);
Chris@0 275 $this->drupalGet('admin/structure/contact');
Chris@0 276 $this->clickLink(t('Edit'));
Chris@0 277 $this->assertResponse(200);
Chris@0 278 $this->assertFieldByName('label', $label);
Chris@0 279
Chris@0 280 // Test field UI and field integration.
Chris@0 281 $this->drupalGet('admin/structure/contact');
Chris@0 282
Chris@0 283 $view_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and text()=:text]', [
Chris@18 284 ':href' => Url::fromRoute('entity.contact_form.canonical', ['contact_form' => $contact_form])->toString(),
Chris@0 285 ':text' => $label,
Chris@0 286 ]
Chris@0 287 );
Chris@0 288 $this->assertTrue(!empty($view_link), 'Contact listing links to contact form.');
Chris@0 289
Chris@0 290 // Find out in which row the form we want to add a field to is.
Chris@0 291 foreach ($this->xpath('//table/tbody/tr') as $row) {
Chris@0 292 if ($row->findLink($label)) {
Chris@0 293 $row->clickLink('Manage fields');
Chris@0 294 break;
Chris@0 295 }
Chris@0 296 }
Chris@0 297
Chris@0 298 $this->assertResponse(200);
Chris@0 299 $this->clickLink(t('Add field'));
Chris@0 300 $this->assertResponse(200);
Chris@0 301
Chris@0 302 // Create a simple textfield.
Chris@17 303 $field_name = mb_strtolower($this->randomMachineName());
Chris@0 304 $field_label = $this->randomMachineName();
Chris@0 305 $this->fieldUIAddNewField(NULL, $field_name, $field_label, 'text');
Chris@0 306 $field_name = 'field_' . $field_name;
Chris@0 307
Chris@0 308 // Check preview field can be ordered.
Chris@0 309 $this->drupalGet('admin/structure/contact/manage/' . $contact_form . '/form-display');
Chris@0 310 $this->assertText(t('Preview'));
Chris@0 311
Chris@0 312 // Check that the field is displayed.
Chris@0 313 $this->drupalGet('contact/' . $contact_form);
Chris@0 314 $this->assertText($field_label);
Chris@0 315
Chris@0 316 // Submit the contact form and verify the content.
Chris@0 317 $edit = [
Chris@0 318 'subject[0][value]' => $this->randomMachineName(),
Chris@0 319 'message[0][value]' => $this->randomMachineName(),
Chris@0 320 $field_name . '[0][value]' => $this->randomMachineName(),
Chris@0 321 ];
Chris@0 322 $this->drupalPostForm(NULL, $edit, t('Send message'));
Chris@0 323 $mails = $this->getMails();
Chris@0 324 $mail = array_pop($mails);
Chris@0 325 $this->assertEqual($mail['subject'], t('[@label] @subject', ['@label' => $label, '@subject' => $edit['subject[0][value]']]));
Chris@0 326 $this->assertTrue(strpos($mail['body'], $field_label));
Chris@0 327 $this->assertTrue(strpos($mail['body'], $edit[$field_name . '[0][value]']));
Chris@0 328
Chris@0 329 // Test messages and redirect.
Chris@0 330 /** @var \Drupal\contact\ContactFormInterface $form */
Chris@0 331 $form = ContactForm::load($contact_form);
Chris@0 332 $form->setMessage('Thanks for your submission.');
Chris@0 333 $form->setRedirectPath('/user/' . $admin_user->id());
Chris@0 334 $form->save();
Chris@0 335 // Check that the field is displayed.
Chris@0 336 $this->drupalGet('contact/' . $contact_form);
Chris@0 337
Chris@0 338 // Submit the contact form and verify the content.
Chris@0 339 $edit = [
Chris@0 340 'subject[0][value]' => $this->randomMachineName(),
Chris@0 341 'message[0][value]' => $this->randomMachineName(),
Chris@0 342 $field_name . '[0][value]' => $this->randomMachineName(),
Chris@0 343 ];
Chris@0 344 $this->drupalPostForm(NULL, $edit, t('Send message'));
Chris@0 345 $this->assertText('Thanks for your submission.');
Chris@0 346 $this->assertUrl('user/' . $admin_user->id());
Chris@0 347
Chris@0 348 // Test Empty message.
Chris@0 349 /** @var \Drupal\contact\ContactFormInterface $form */
Chris@0 350 $form = ContactForm::load($contact_form);
Chris@0 351 $form->setMessage('');
Chris@0 352 $form->setRedirectPath('/user/' . $admin_user->id());
Chris@0 353 $form->save();
Chris@0 354 $this->drupalGet('admin/structure/contact/manage/' . $contact_form);
Chris@0 355 // Check that the field is displayed.
Chris@0 356 $this->drupalGet('contact/' . $contact_form);
Chris@0 357
Chris@0 358 // Submit the contact form and verify the content.
Chris@0 359 $edit = [
Chris@0 360 'subject[0][value]' => $this->randomMachineName(),
Chris@0 361 'message[0][value]' => $this->randomMachineName(),
Chris@0 362 $field_name . '[0][value]' => $this->randomMachineName(),
Chris@0 363 ];
Chris@0 364 $this->drupalPostForm(NULL, $edit, t('Send message'));
Chris@0 365 $result = $this->xpath('//div[@role=:role]', [':role' => 'contentinfo']);
Chris@0 366 $this->assertEqual(count($result), 0, 'Messages not found.');
Chris@0 367 $this->assertUrl('user/' . $admin_user->id());
Chris@0 368
Chris@0 369 // Test preview and visibility of the message field and label. Submit the
Chris@0 370 // contact form and verify the content.
Chris@0 371 $edit = [
Chris@0 372 'subject[0][value]' => $this->randomMachineName(),
Chris@0 373 'message[0][value]' => $this->randomMachineName(),
Chris@0 374 $field_name . '[0][value]' => $this->randomMachineName(),
Chris@0 375 ];
Chris@0 376 $this->drupalPostForm($form->toUrl('canonical'), $edit, t('Preview'));
Chris@0 377
Chris@0 378 // Message is now by default displayed twice, once for the form element and
Chris@0 379 // once for the viewed message.
Chris@0 380 $page_text = $this->getSession()->getPage()->getText();
Chris@0 381 $this->assertGreaterThan(1, substr_count($page_text, t('Message')));
Chris@0 382 $this->assertSession()->responseContains('class="field field--name-message field--type-string-long field--label-above');
Chris@0 383 $this->assertSession()->pageTextContains($edit['message[0][value]']);
Chris@0 384
Chris@0 385 // Hide the message field label.
Chris@0 386 $display_edit = [
Chris@0 387 'fields[message][label]' => 'hidden',
Chris@0 388 ];
Chris@0 389 $this->drupalPostForm('admin/structure/contact/manage/' . $contact_form . '/display', $display_edit, t('Save'));
Chris@0 390
Chris@0 391 $this->drupalPostForm($form->toUrl('canonical'), $edit, t('Preview'));
Chris@0 392 // Message should only be displayed once now.
Chris@0 393 $page_text = $this->getSession()->getPage()->getText();
Chris@0 394 $this->assertEquals(1, substr_count($page_text, t('Message')));
Chris@0 395 $this->assertSession()->responseContains('class="field field--name-message field--type-string-long field--label-hidden field__item">');
Chris@0 396 $this->assertSession()->pageTextContains($edit['message[0][value]']);
Chris@0 397 }
Chris@0 398
Chris@0 399 /**
Chris@0 400 * Tests auto-reply on the site-wide contact form.
Chris@0 401 */
Chris@0 402 public function testAutoReply() {
Chris@0 403 // Create and log in administrative user.
Chris@0 404 $admin_user = $this->drupalCreateUser([
Chris@0 405 'access site-wide contact form',
Chris@0 406 'administer contact forms',
Chris@0 407 'administer permissions',
Chris@0 408 'administer users',
Chris@17 409 'access site reports',
Chris@0 410 ]);
Chris@0 411 $this->drupalLogin($admin_user);
Chris@0 412
Chris@0 413 // Set up three forms, 2 with an auto-reply and one without.
Chris@0 414 $foo_autoreply = $this->randomMachineName(40);
Chris@0 415 $bar_autoreply = $this->randomMachineName(40);
Chris@0 416 $this->addContactForm('foo', 'foo', 'foo@example.com', $foo_autoreply, FALSE);
Chris@0 417 $this->addContactForm('bar', 'bar', 'bar@example.com', $bar_autoreply, FALSE);
Chris@0 418 $this->addContactForm('no_autoreply', 'no_autoreply', 'bar@example.com', '', FALSE);
Chris@0 419
Chris@0 420 // Log the current user out in order to test the name and email fields.
Chris@0 421 $this->drupalLogout();
Chris@0 422 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
Chris@0 423
Chris@0 424 // Test the auto-reply for form 'foo'.
Chris@0 425 $email = $this->randomMachineName(32) . '@example.com';
Chris@0 426 $subject = $this->randomMachineName(64);
Chris@0 427 $this->submitContact($this->randomMachineName(16), $email, $subject, 'foo', $this->randomString(128));
Chris@0 428
Chris@0 429 // We are testing the auto-reply, so there should be one email going to the sender.
Chris@0 430 $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
Chris@0 431 $this->assertEqual(count($captured_emails), 1);
Chris@0 432 $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($foo_autoreply)));
Chris@0 433
Chris@0 434 // Test the auto-reply for form 'bar'.
Chris@0 435 $email = $this->randomMachineName(32) . '@example.com';
Chris@0 436 $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'bar', $this->randomString(128));
Chris@0 437
Chris@0 438 // Auto-reply for form 'bar' should result in one auto-reply email to the sender.
Chris@0 439 $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
Chris@0 440 $this->assertEqual(count($captured_emails), 1);
Chris@0 441 $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($bar_autoreply)));
Chris@0 442
Chris@0 443 // Verify that no auto-reply is sent when the auto-reply field is left blank.
Chris@0 444 $email = $this->randomMachineName(32) . '@example.com';
Chris@0 445 $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'no_autoreply', $this->randomString(128));
Chris@0 446 $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
Chris@0 447 $this->assertEqual(count($captured_emails), 0);
Chris@0 448
Chris@0 449 // Verify that the current error message doesn't show, that the auto-reply
Chris@0 450 // doesn't get sent and the correct silent error gets logged.
Chris@0 451 $email = '';
Chris@0 452 entity_get_form_display('contact_message', 'foo', 'default')
Chris@0 453 ->removeComponent('mail')
Chris@0 454 ->save();
Chris@0 455 $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'foo', $this->randomString(128));
Chris@0 456 $this->assertNoText('Unable to send email. Contact the site administrator if the problem persists.');
Chris@0 457 $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]);
Chris@0 458 $this->assertEqual(count($captured_emails), 0);
Chris@0 459 $this->drupalLogin($admin_user);
Chris@0 460 $this->drupalGet('admin/reports/dblog');
Chris@0 461 $this->assertRaw('Error sending auto-reply, missing sender e-mail address in foo');
Chris@0 462 }
Chris@0 463
Chris@0 464 /**
Chris@0 465 * Adds a form.
Chris@0 466 *
Chris@0 467 * @param string $id
Chris@0 468 * The form machine name.
Chris@0 469 * @param string $label
Chris@0 470 * The form label.
Chris@0 471 * @param string $recipients
Chris@0 472 * The list of recipient email addresses.
Chris@0 473 * @param string $reply
Chris@0 474 * The auto-reply text that is sent to a user upon completing the contact
Chris@0 475 * form.
Chris@0 476 * @param bool $selected
Chris@0 477 * A Boolean indicating whether the form should be selected by default.
Chris@0 478 * @param string $message
Chris@0 479 * The message that will be displayed to a user upon completing the contact
Chris@0 480 * form.
Chris@0 481 * @param array $third_party_settings
Chris@0 482 * Array of third party settings to be added to the posted form data.
Chris@0 483 */
Chris@0 484 public function addContactForm($id, $label, $recipients, $reply, $selected, $message = 'Your message has been sent.', $third_party_settings = []) {
Chris@0 485 $edit = [];
Chris@0 486 $edit['label'] = $label;
Chris@0 487 $edit['id'] = $id;
Chris@0 488 $edit['message'] = $message;
Chris@0 489 $edit['recipients'] = $recipients;
Chris@0 490 $edit['reply'] = $reply;
Chris@0 491 $edit['selected'] = ($selected ? TRUE : FALSE);
Chris@0 492 $edit += $third_party_settings;
Chris@0 493 $this->drupalPostForm('admin/structure/contact/add', $edit, t('Save'));
Chris@0 494 }
Chris@0 495
Chris@0 496 /**
Chris@0 497 * Updates a form.
Chris@0 498 *
Chris@0 499 * @param string $id
Chris@0 500 * The form machine name.
Chris@0 501 * @param string $label
Chris@0 502 * The form label.
Chris@0 503 * @param string $recipients
Chris@0 504 * The list of recipient email addresses.
Chris@0 505 * @param string $reply
Chris@0 506 * The auto-reply text that is sent to a user upon completing the contact
Chris@0 507 * form.
Chris@0 508 * @param bool $selected
Chris@0 509 * A Boolean indicating whether the form should be selected by default.
Chris@0 510 * @param string $message
Chris@0 511 * The message that will be displayed to a user upon completing the contact
Chris@0 512 * form.
Chris@0 513 * @param string $redirect
Chris@0 514 * The path where user will be redirect after this form has been submitted..
Chris@0 515 */
Chris@0 516 public function updateContactForm($id, $label, $recipients, $reply, $selected, $message = 'Your message has been sent.', $redirect = '/') {
Chris@0 517 $edit = [];
Chris@0 518 $edit['label'] = $label;
Chris@0 519 $edit['recipients'] = $recipients;
Chris@0 520 $edit['reply'] = $reply;
Chris@0 521 $edit['selected'] = ($selected ? TRUE : FALSE);
Chris@0 522 $edit['message'] = $message;
Chris@0 523 $edit['redirect'] = $redirect;
Chris@0 524 $this->drupalPostForm("admin/structure/contact/manage/$id", $edit, t('Save'));
Chris@0 525 }
Chris@0 526
Chris@0 527 /**
Chris@0 528 * Submits the contact form.
Chris@0 529 *
Chris@0 530 * @param string $name
Chris@0 531 * The name of the sender.
Chris@0 532 * @param string $mail
Chris@0 533 * The email address of the sender.
Chris@0 534 * @param string $subject
Chris@0 535 * The subject of the message.
Chris@0 536 * @param string $id
Chris@0 537 * The form ID of the message.
Chris@0 538 * @param string $message
Chris@0 539 * The message body.
Chris@0 540 */
Chris@0 541 public function submitContact($name, $mail, $subject, $id, $message) {
Chris@0 542 $edit = [];
Chris@0 543 $edit['name'] = $name;
Chris@0 544 $edit['mail'] = $mail;
Chris@0 545 $edit['subject[0][value]'] = $subject;
Chris@0 546 $edit['message[0][value]'] = $message;
Chris@0 547 if ($id == $this->config('contact.settings')->get('default_form')) {
Chris@0 548 $this->drupalPostForm('contact', $edit, t('Send message'));
Chris@0 549 }
Chris@0 550 else {
Chris@0 551 $this->drupalPostForm('contact/' . $id, $edit, t('Send message'));
Chris@0 552 }
Chris@0 553 }
Chris@0 554
Chris@0 555 /**
Chris@0 556 * Deletes all forms.
Chris@0 557 */
Chris@0 558 public function deleteContactForms() {
Chris@18 559 $contact_forms = ContactForm::loadMultiple();
Chris@0 560 foreach ($contact_forms as $id => $contact_form) {
Chris@0 561 if ($id == 'personal') {
Chris@0 562 // Personal form could not be deleted.
Chris@0 563 $this->drupalGet("admin/structure/contact/manage/$id/delete");
Chris@0 564 $this->assertResponse(403);
Chris@0 565 }
Chris@0 566 else {
Chris@0 567 $this->drupalPostForm("admin/structure/contact/manage/$id/delete", [], t('Delete'));
Chris@0 568 $this->assertRaw(t('The contact form %label has been deleted.', ['%label' => $contact_form->label()]));
Chris@0 569 $this->assertFalse(ContactForm::load($id), format_string('Form %contact_form not found', ['%contact_form' => $contact_form->label()]));
Chris@0 570 }
Chris@0 571 }
Chris@0 572 }
Chris@0 573
Chris@0 574 }