Chris@0: drupalPlaceBlock('system_breadcrumb_block'); Chris@0: $this->drupalPlaceBlock('local_actions_block'); Chris@0: $this->drupalPlaceBlock('page_title_block'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests configuration options and the site-wide contact form. Chris@0: */ Chris@0: public function testSiteWideContact() { Chris@0: // Tests name and email fields for authenticated and anonymous users. Chris@0: $this->drupalLogin($this->drupalCreateUser(['access site-wide contact form'])); Chris@0: $this->drupalGet('contact'); Chris@0: Chris@0: // Ensure that there is no textfield for name. Chris@0: $this->assertFalse($this->xpath('//input[@name=:name]', [':name' => 'name'])); Chris@0: Chris@0: // Ensure that there is no textfield for email. Chris@0: $this->assertFalse($this->xpath('//input[@name=:name]', [':name' => 'mail'])); Chris@0: Chris@0: // Logout and retrieve the page as an anonymous user Chris@0: $this->drupalLogout(); Chris@0: user_role_grant_permissions('anonymous', ['access site-wide contact form']); Chris@0: $this->drupalGet('contact'); Chris@0: Chris@0: // Ensure that there is textfield for name. Chris@0: $this->assertTrue($this->xpath('//input[@name=:name]', [':name' => 'name'])); Chris@0: Chris@0: // Ensure that there is textfield for email. Chris@0: $this->assertTrue($this->xpath('//input[@name=:name]', [':name' => 'mail'])); Chris@0: Chris@0: // Create and log in administrative user. Chris@0: $admin_user = $this->drupalCreateUser([ Chris@0: 'access site-wide contact form', Chris@0: 'administer contact forms', Chris@0: 'administer users', Chris@0: 'administer account settings', Chris@0: 'administer contact_message display', Chris@0: 'administer contact_message fields', Chris@0: 'administer contact_message form display', Chris@0: ]); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: // Check the presence of expected cache tags. Chris@0: $this->drupalGet('contact'); Chris@0: $this->assertCacheTag('config:contact.settings'); Chris@0: Chris@0: $flood_limit = 3; Chris@0: $this->config('contact.settings') Chris@0: ->set('flood.limit', $flood_limit) Chris@0: ->set('flood.interval', 600) Chris@0: ->save(); Chris@0: Chris@0: // Set settings. Chris@0: $edit = []; Chris@0: $edit['contact_default_status'] = TRUE; Chris@0: $this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration')); Chris@0: $this->assertText(t('The configuration options have been saved.')); Chris@0: Chris@0: $this->drupalGet('admin/structure/contact'); Chris@0: // Default form exists. Chris@0: $this->assertLinkByHref('admin/structure/contact/manage/feedback/delete'); Chris@0: // User form could not be changed or deleted. Chris@0: // Cannot use ::assertNoLinkByHref as it does partial url matching and with Chris@0: // field_ui enabled admin/structure/contact/manage/personal/fields exists. Chris@0: // @todo: See https://www.drupal.org/node/2031223 for the above. Chris@0: $edit_link = $this->xpath('//a[@href=:href]', [ Chris@18: ':href' => Url::fromRoute('entity.contact_form.edit_form', ['contact_form' => 'personal'])->toString(), Chris@0: ]); Chris@0: $this->assertTrue(empty($edit_link), format_string('No link containing href %href found.', Chris@0: ['%href' => 'admin/structure/contact/manage/personal'] Chris@0: )); Chris@0: $this->assertNoLinkByHref('admin/structure/contact/manage/personal/delete'); Chris@0: Chris@0: $this->drupalGet('admin/structure/contact/manage/personal'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Delete old forms to ensure that new forms are used. Chris@0: $this->deleteContactForms(); Chris@0: $this->drupalGet('admin/structure/contact'); Chris@0: $this->assertText('Personal', 'Personal form was not deleted'); Chris@0: $this->assertNoLinkByHref('admin/structure/contact/manage/feedback'); Chris@0: Chris@0: // Ensure that the contact form won't be shown without forms. Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']); Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet('contact'); Chris@0: $this->assertResponse(404); Chris@0: Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->drupalGet('contact'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText(t('The contact form has not been configured.')); Chris@0: // Test access personal form via site-wide contact page. Chris@0: $this->drupalGet('contact/personal'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Add forms. Chris@0: // Test invalid recipients. Chris@0: $invalid_recipients = ['invalid', 'invalid@', 'invalid@site.', '@site.', '@site.com']; Chris@0: foreach ($invalid_recipients as $invalid_recipient) { Chris@0: $this->addContactForm($this->randomMachineName(16), $this->randomMachineName(16), $invalid_recipient, '', FALSE); Chris@0: $this->assertRaw(t('%recipient is an invalid email address.', ['%recipient' => $invalid_recipient])); Chris@0: } Chris@0: Chris@0: // Test validation of empty form and recipients fields. Chris@0: $this->addContactForm('', '', '', '', TRUE); Chris@0: $this->assertText(t('Label field is required.')); Chris@0: $this->assertText(t('Machine-readable name field is required.')); Chris@0: $this->assertText(t('Recipients field is required.')); Chris@0: Chris@0: // Test validation of max_length machine name. Chris@0: $recipients = ['simpletest&@example.com', 'simpletest2@example.com', 'simpletest3@example.com']; Chris@0: $max_length = EntityTypeInterface::BUNDLE_MAX_LENGTH; Chris@0: $max_length_exceeded = $max_length + 1; Chris@17: $this->addContactForm($id = mb_strtolower($this->randomMachineName($max_length_exceeded)), $label = $this->randomMachineName($max_length_exceeded), implode(',', [$recipients[0]]), '', TRUE); Chris@0: $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: $this->addContactForm($id = mb_strtolower($this->randomMachineName($max_length)), $label = $this->randomMachineName($max_length), implode(',', [$recipients[0]]), '', TRUE); Chris@0: $this->assertText(t('Contact form @label has been added.', ['@label' => $label])); Chris@0: Chris@0: // Verify that the creation message contains a link to a contact form. Chris@0: $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'contact/']); Chris@0: $this->assert(isset($view_link), 'The message area contains a link to a contact form.'); Chris@0: Chris@0: // Create first valid form. Chris@17: $this->addContactForm($id = mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0]]), '', TRUE); Chris@0: $this->assertText(t('Contact form @label has been added.', ['@label' => $label])); Chris@0: Chris@0: // Verify that the creation message contains a link to a contact form. Chris@0: $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'contact/']); Chris@0: $this->assert(isset($view_link), 'The message area contains a link to a contact form.'); Chris@0: Chris@0: // Check that the form was created in site default language. Chris@0: $langcode = $this->config('contact.form.' . $id)->get('langcode'); Chris@0: $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); Chris@0: $this->assertEqual($langcode, $default_langcode); Chris@0: Chris@0: // Make sure the newly created form is included in the list of forms. Chris@0: $this->assertNoUniqueText($label, 'New form included in forms list.'); Chris@0: Chris@0: // Ensure that the recipient email is escaped on the listing. Chris@0: $this->drupalGet('admin/structure/contact'); Chris@0: $this->assertEscaped($recipients[0]); Chris@0: Chris@0: // Test update contact form. Chris@0: $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: $config = $this->config('contact.form.' . $id)->get(); Chris@0: $this->assertEqual($config['label'], $label); Chris@0: $this->assertEqual($config['recipients'], [$recipients[0], $recipients[1]]); Chris@0: $this->assertEqual($config['reply'], $reply); Chris@0: $this->assertNotEqual($id, $this->config('contact.settings')->get('default_form')); Chris@0: $this->assertText(t('Contact form @label has been updated.', ['@label' => $label])); Chris@0: // Ensure the label is displayed on the contact page for this form. Chris@0: $this->drupalGet('contact/' . $id); Chris@0: $this->assertText($label); Chris@0: Chris@0: // Reset the form back to be the default form. Chris@0: $this->config('contact.settings')->set('default_form', $id)->save(); Chris@0: Chris@0: // Ensure that the contact form is shown without a form selection input. Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']); Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet('contact'); Chris@0: $this->assertText(t('Your email address')); Chris@0: $this->assertNoText(t('Form')); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: // Add more forms. Chris@17: $this->addContactForm(mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0], $recipients[1]]), '', FALSE); Chris@0: $this->assertText(t('Contact form @label has been added.', ['@label' => $label])); Chris@0: Chris@17: $this->addContactForm($name = mb_strtolower($this->randomMachineName(16)), $label = $this->randomMachineName(16), implode(',', [$recipients[0], $recipients[1], $recipients[2]]), '', FALSE); Chris@0: $this->assertText(t('Contact form @label has been added.', ['@label' => $label])); Chris@0: Chris@0: // Try adding a form that already exists. Chris@0: $this->addContactForm($name, $label, '', '', FALSE); Chris@0: $this->assertNoText(t('Contact form @label has been added.', ['@label' => $label])); Chris@0: $this->assertRaw(t('The machine-readable name is already in use. It must be unique.')); Chris@0: Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Check to see that anonymous user cannot see contact page without permission. Chris@0: user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']); Chris@0: $this->drupalGet('contact'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Give anonymous user permission and see that page is viewable. Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']); Chris@0: $this->drupalGet('contact'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Submit contact form with invalid values. Chris@0: $this->submitContact('', $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64)); Chris@0: $this->assertText(t('Your name field is required.')); Chris@0: Chris@0: $this->submitContact($this->randomMachineName(16), '', $this->randomMachineName(16), $id, $this->randomMachineName(64)); Chris@0: $this->assertText(t('Your email address field is required.')); Chris@0: Chris@0: $this->submitContact($this->randomMachineName(16), $invalid_recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64)); Chris@0: $this->assertRaw(t('The email address %mail is not valid.', ['%mail' => 'invalid'])); Chris@0: Chris@0: $this->submitContact($this->randomMachineName(16), $recipients[0], '', $id, $this->randomMachineName(64)); Chris@0: $this->assertText(t('Subject field is required.')); Chris@0: Chris@0: $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, ''); Chris@0: $this->assertText(t('Message field is required.')); Chris@0: Chris@0: // Test contact form with no default form selected. Chris@0: $this->config('contact.settings') Chris@0: ->set('default_form', '') Chris@0: ->save(); Chris@0: $this->drupalGet('contact'); Chris@0: $this->assertResponse(404); Chris@0: Chris@0: // Try to access contact form with non-existing form IDs. Chris@0: $this->drupalGet('contact/0'); Chris@0: $this->assertResponse(404); Chris@0: $this->drupalGet('contact/' . $this->randomMachineName()); Chris@0: $this->assertResponse(404); Chris@0: Chris@0: // Submit contact form with correct values and check flood interval. Chris@0: for ($i = 0; $i < $flood_limit; $i++) { Chris@0: $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64)); Chris@0: $this->assertText(t('Your message has been sent.')); Chris@0: } Chris@0: // Submit contact form one over limit. Chris@0: $this->submitContact($this->randomMachineName(16), $recipients[0], $this->randomMachineName(16), $id, $this->randomMachineName(64)); Chris@0: $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: Chris@0: // Test listing controller. Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: $this->deleteContactForms(); Chris@0: Chris@0: $label = $this->randomMachineName(16); Chris@0: $recipients = implode(',', [$recipients[0], $recipients[1], $recipients[2]]); Chris@17: $contact_form = mb_strtolower($this->randomMachineName(16)); Chris@0: $this->addContactForm($contact_form, $label, $recipients, '', FALSE); Chris@0: $this->drupalGet('admin/structure/contact'); Chris@0: $this->clickLink(t('Edit')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertFieldByName('label', $label); Chris@0: Chris@0: // Test field UI and field integration. Chris@0: $this->drupalGet('admin/structure/contact'); Chris@0: Chris@0: $view_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and text()=:text]', [ Chris@18: ':href' => Url::fromRoute('entity.contact_form.canonical', ['contact_form' => $contact_form])->toString(), Chris@0: ':text' => $label, Chris@0: ] Chris@0: ); Chris@0: $this->assertTrue(!empty($view_link), 'Contact listing links to contact form.'); Chris@0: Chris@0: // Find out in which row the form we want to add a field to is. Chris@0: foreach ($this->xpath('//table/tbody/tr') as $row) { Chris@0: if ($row->findLink($label)) { Chris@0: $row->clickLink('Manage fields'); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->assertResponse(200); Chris@0: $this->clickLink(t('Add field')); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Create a simple textfield. Chris@17: $field_name = mb_strtolower($this->randomMachineName()); Chris@0: $field_label = $this->randomMachineName(); Chris@0: $this->fieldUIAddNewField(NULL, $field_name, $field_label, 'text'); Chris@0: $field_name = 'field_' . $field_name; Chris@0: Chris@0: // Check preview field can be ordered. Chris@0: $this->drupalGet('admin/structure/contact/manage/' . $contact_form . '/form-display'); Chris@0: $this->assertText(t('Preview')); Chris@0: Chris@0: // Check that the field is displayed. Chris@0: $this->drupalGet('contact/' . $contact_form); Chris@0: $this->assertText($field_label); Chris@0: Chris@0: // Submit the contact form and verify the content. Chris@0: $edit = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'message[0][value]' => $this->randomMachineName(), Chris@0: $field_name . '[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Send message')); Chris@0: $mails = $this->getMails(); Chris@0: $mail = array_pop($mails); Chris@0: $this->assertEqual($mail['subject'], t('[@label] @subject', ['@label' => $label, '@subject' => $edit['subject[0][value]']])); Chris@0: $this->assertTrue(strpos($mail['body'], $field_label)); Chris@0: $this->assertTrue(strpos($mail['body'], $edit[$field_name . '[0][value]'])); Chris@0: Chris@0: // Test messages and redirect. Chris@0: /** @var \Drupal\contact\ContactFormInterface $form */ Chris@0: $form = ContactForm::load($contact_form); Chris@0: $form->setMessage('Thanks for your submission.'); Chris@0: $form->setRedirectPath('/user/' . $admin_user->id()); Chris@0: $form->save(); Chris@0: // Check that the field is displayed. Chris@0: $this->drupalGet('contact/' . $contact_form); Chris@0: Chris@0: // Submit the contact form and verify the content. Chris@0: $edit = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'message[0][value]' => $this->randomMachineName(), Chris@0: $field_name . '[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Send message')); Chris@0: $this->assertText('Thanks for your submission.'); Chris@0: $this->assertUrl('user/' . $admin_user->id()); Chris@0: Chris@0: // Test Empty message. Chris@0: /** @var \Drupal\contact\ContactFormInterface $form */ Chris@0: $form = ContactForm::load($contact_form); Chris@0: $form->setMessage(''); Chris@0: $form->setRedirectPath('/user/' . $admin_user->id()); Chris@0: $form->save(); Chris@0: $this->drupalGet('admin/structure/contact/manage/' . $contact_form); Chris@0: // Check that the field is displayed. Chris@0: $this->drupalGet('contact/' . $contact_form); Chris@0: Chris@0: // Submit the contact form and verify the content. Chris@0: $edit = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'message[0][value]' => $this->randomMachineName(), Chris@0: $field_name . '[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Send message')); Chris@0: $result = $this->xpath('//div[@role=:role]', [':role' => 'contentinfo']); Chris@0: $this->assertEqual(count($result), 0, 'Messages not found.'); Chris@0: $this->assertUrl('user/' . $admin_user->id()); Chris@0: Chris@0: // Test preview and visibility of the message field and label. Submit the Chris@0: // contact form and verify the content. Chris@0: $edit = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'message[0][value]' => $this->randomMachineName(), Chris@0: $field_name . '[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm($form->toUrl('canonical'), $edit, t('Preview')); Chris@0: Chris@0: // Message is now by default displayed twice, once for the form element and Chris@0: // once for the viewed message. Chris@0: $page_text = $this->getSession()->getPage()->getText(); Chris@0: $this->assertGreaterThan(1, substr_count($page_text, t('Message'))); Chris@0: $this->assertSession()->responseContains('class="field field--name-message field--type-string-long field--label-above'); Chris@0: $this->assertSession()->pageTextContains($edit['message[0][value]']); Chris@0: Chris@0: // Hide the message field label. Chris@0: $display_edit = [ Chris@0: 'fields[message][label]' => 'hidden', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/contact/manage/' . $contact_form . '/display', $display_edit, t('Save')); Chris@0: Chris@0: $this->drupalPostForm($form->toUrl('canonical'), $edit, t('Preview')); Chris@0: // Message should only be displayed once now. Chris@0: $page_text = $this->getSession()->getPage()->getText(); Chris@0: $this->assertEquals(1, substr_count($page_text, t('Message'))); Chris@0: $this->assertSession()->responseContains('class="field field--name-message field--type-string-long field--label-hidden field__item">'); Chris@0: $this->assertSession()->pageTextContains($edit['message[0][value]']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests auto-reply on the site-wide contact form. Chris@0: */ Chris@0: public function testAutoReply() { Chris@0: // Create and log in administrative user. Chris@0: $admin_user = $this->drupalCreateUser([ Chris@0: 'access site-wide contact form', Chris@0: 'administer contact forms', Chris@0: 'administer permissions', Chris@0: 'administer users', Chris@17: 'access site reports', Chris@0: ]); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: // Set up three forms, 2 with an auto-reply and one without. Chris@0: $foo_autoreply = $this->randomMachineName(40); Chris@0: $bar_autoreply = $this->randomMachineName(40); Chris@0: $this->addContactForm('foo', 'foo', 'foo@example.com', $foo_autoreply, FALSE); Chris@0: $this->addContactForm('bar', 'bar', 'bar@example.com', $bar_autoreply, FALSE); Chris@0: $this->addContactForm('no_autoreply', 'no_autoreply', 'bar@example.com', '', FALSE); Chris@0: Chris@0: // Log the current user out in order to test the name and email fields. Chris@0: $this->drupalLogout(); Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']); Chris@0: Chris@0: // Test the auto-reply for form 'foo'. Chris@0: $email = $this->randomMachineName(32) . '@example.com'; Chris@0: $subject = $this->randomMachineName(64); Chris@0: $this->submitContact($this->randomMachineName(16), $email, $subject, 'foo', $this->randomString(128)); Chris@0: Chris@0: // We are testing the auto-reply, so there should be one email going to the sender. Chris@0: $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]); Chris@0: $this->assertEqual(count($captured_emails), 1); Chris@0: $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($foo_autoreply))); Chris@0: Chris@0: // Test the auto-reply for form 'bar'. Chris@0: $email = $this->randomMachineName(32) . '@example.com'; Chris@0: $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'bar', $this->randomString(128)); Chris@0: Chris@0: // Auto-reply for form 'bar' should result in one auto-reply email to the sender. Chris@0: $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]); Chris@0: $this->assertEqual(count($captured_emails), 1); Chris@0: $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($bar_autoreply))); Chris@0: Chris@0: // Verify that no auto-reply is sent when the auto-reply field is left blank. Chris@0: $email = $this->randomMachineName(32) . '@example.com'; Chris@0: $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'no_autoreply', $this->randomString(128)); Chris@0: $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]); Chris@0: $this->assertEqual(count($captured_emails), 0); Chris@0: Chris@0: // Verify that the current error message doesn't show, that the auto-reply Chris@0: // doesn't get sent and the correct silent error gets logged. Chris@0: $email = ''; Chris@0: entity_get_form_display('contact_message', 'foo', 'default') Chris@0: ->removeComponent('mail') Chris@0: ->save(); Chris@0: $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'foo', $this->randomString(128)); Chris@0: $this->assertNoText('Unable to send email. Contact the site administrator if the problem persists.'); Chris@0: $captured_emails = $this->getMails(['id' => 'contact_page_autoreply', 'to' => $email]); Chris@0: $this->assertEqual(count($captured_emails), 0); Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->drupalGet('admin/reports/dblog'); Chris@0: $this->assertRaw('Error sending auto-reply, missing sender e-mail address in foo'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a form. Chris@0: * Chris@0: * @param string $id Chris@0: * The form machine name. Chris@0: * @param string $label Chris@0: * The form label. Chris@0: * @param string $recipients Chris@0: * The list of recipient email addresses. Chris@0: * @param string $reply Chris@0: * The auto-reply text that is sent to a user upon completing the contact Chris@0: * form. Chris@0: * @param bool $selected Chris@0: * A Boolean indicating whether the form should be selected by default. Chris@0: * @param string $message Chris@0: * The message that will be displayed to a user upon completing the contact Chris@0: * form. Chris@0: * @param array $third_party_settings Chris@0: * Array of third party settings to be added to the posted form data. Chris@0: */ Chris@0: public function addContactForm($id, $label, $recipients, $reply, $selected, $message = 'Your message has been sent.', $third_party_settings = []) { Chris@0: $edit = []; Chris@0: $edit['label'] = $label; Chris@0: $edit['id'] = $id; Chris@0: $edit['message'] = $message; Chris@0: $edit['recipients'] = $recipients; Chris@0: $edit['reply'] = $reply; Chris@0: $edit['selected'] = ($selected ? TRUE : FALSE); Chris@0: $edit += $third_party_settings; Chris@0: $this->drupalPostForm('admin/structure/contact/add', $edit, t('Save')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Updates a form. Chris@0: * Chris@0: * @param string $id Chris@0: * The form machine name. Chris@0: * @param string $label Chris@0: * The form label. Chris@0: * @param string $recipients Chris@0: * The list of recipient email addresses. Chris@0: * @param string $reply Chris@0: * The auto-reply text that is sent to a user upon completing the contact Chris@0: * form. Chris@0: * @param bool $selected Chris@0: * A Boolean indicating whether the form should be selected by default. Chris@0: * @param string $message Chris@0: * The message that will be displayed to a user upon completing the contact Chris@0: * form. Chris@0: * @param string $redirect Chris@0: * The path where user will be redirect after this form has been submitted.. Chris@0: */ Chris@0: public function updateContactForm($id, $label, $recipients, $reply, $selected, $message = 'Your message has been sent.', $redirect = '/') { Chris@0: $edit = []; Chris@0: $edit['label'] = $label; Chris@0: $edit['recipients'] = $recipients; Chris@0: $edit['reply'] = $reply; Chris@0: $edit['selected'] = ($selected ? TRUE : FALSE); Chris@0: $edit['message'] = $message; Chris@0: $edit['redirect'] = $redirect; Chris@0: $this->drupalPostForm("admin/structure/contact/manage/$id", $edit, t('Save')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Submits the contact form. Chris@0: * Chris@0: * @param string $name Chris@0: * The name of the sender. Chris@0: * @param string $mail Chris@0: * The email address of the sender. Chris@0: * @param string $subject Chris@0: * The subject of the message. Chris@0: * @param string $id Chris@0: * The form ID of the message. Chris@0: * @param string $message Chris@0: * The message body. Chris@0: */ Chris@0: public function submitContact($name, $mail, $subject, $id, $message) { Chris@0: $edit = []; Chris@0: $edit['name'] = $name; Chris@0: $edit['mail'] = $mail; Chris@0: $edit['subject[0][value]'] = $subject; Chris@0: $edit['message[0][value]'] = $message; Chris@0: if ($id == $this->config('contact.settings')->get('default_form')) { Chris@0: $this->drupalPostForm('contact', $edit, t('Send message')); Chris@0: } Chris@0: else { Chris@0: $this->drupalPostForm('contact/' . $id, $edit, t('Send message')); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes all forms. Chris@0: */ Chris@0: public function deleteContactForms() { Chris@18: $contact_forms = ContactForm::loadMultiple(); Chris@0: foreach ($contact_forms as $id => $contact_form) { Chris@0: if ($id == 'personal') { Chris@0: // Personal form could not be deleted. Chris@0: $this->drupalGet("admin/structure/contact/manage/$id/delete"); Chris@0: $this->assertResponse(403); Chris@0: } Chris@0: else { Chris@0: $this->drupalPostForm("admin/structure/contact/manage/$id/delete", [], t('Delete')); Chris@0: $this->assertRaw(t('The contact form %label has been deleted.', ['%label' => $contact_form->label()])); Chris@0: $this->assertFalse(ContactForm::load($id), format_string('Form %contact_form not found', ['%contact_form' => $contact_form->label()])); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }