View source
<?php
namespace Drupal\contact_emails;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\UserInterface;
class ContactEmailsTestBase extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected $adminUser = FALSE;
public function setUp() {
parent::setUp();
$this
->createUserAndLogin();
$this
->createBaseContactForm();
}
protected function createUserAndLogin() {
$this->adminUser = $this
->createUser([], NULL, TRUE);
$this
->drupalLogin($this->adminUser);
}
protected function createBaseContactForm() {
$params = [
'label' => 'Contact Emails Test Form',
'id' => 'contact_emails_test_form',
'message' => 'Your message has been sent.',
'recipients' => 'test@example.com',
'contact_storage_submit_text' => 'Send message',
];
$this
->drupalPostForm('admin/structure/contact/add', $params, t('Save'));
}
protected function setSiteMail() {
$settings['config']['system.site']['mail'] = (object) [
'value' => 'site-default-mail@test.com',
'required' => TRUE,
];
$this
->writeSettings($settings);
}
protected function addEmailFieldToContactForm() {
$params = [
'new_storage_type' => 'email',
'label' => 'Email address',
'field_name' => 'email_address',
];
$this
->drupalPostForm('admin/structure/contact/manage/contact_emails_test_form/fields/add-field', $params, t('Save and continue'));
$this
->drupalPostForm(NULL, [], t('Save field settings'));
$this
->drupalPostForm(NULL, [], t('Save settings'));
$this
->assertText('field_email_address', 'Field email address exists.');
}
protected function addContactFormWithEmailFieldForReferencing() {
$params = [
'label' => 'Contact Reference Test Form',
'id' => 'contact_reference_test_form',
'message' => 'Your message has been sent.',
'recipients' => 'test@example.com',
'contact_storage_submit_text' => 'Send message',
];
$this
->drupalPostForm('admin/structure/contact/add', $params, t('Save'));
$params = [
'new_storage_type' => 'email',
'label' => 'Email reference',
'field_name' => 'email_reference',
];
$this
->drupalPostForm('admin/structure/contact/manage/contact_reference_test_form/fields/add-field', $params, t('Save and continue'));
$this
->drupalPostForm(NULL, [], t('Save field settings'));
$this
->drupalPostForm(NULL, [], t('Save settings'));
$this
->assertText('field_email_reference', 'Field email address exists.');
$params = [
'new_storage_type' => 'entity_reference',
'label' => 'Reference',
'field_name' => 'reference',
];
$this
->drupalPostForm('admin/structure/contact/manage/contact_emails_test_form/fields/add-field', $params, t('Save and continue'));
$params = [
'settings[target_type]' => 'contact_message',
];
$this
->drupalPostForm(NULL, $params, t('Save field settings'));
$params = [
'settings[handler_settings][target_bundles][contact_reference_test_form]' => 'contact_reference_test_form',
];
$this
->drupalPostForm(NULL, $params, t('Save settings'));
$this
->assertText('field_reference', 'Field reference exists.');
$params = [
'fields[field_reference][type]' => 'options_select',
];
$this
->drupalPostForm('admin/structure/contact/manage/contact_emails_test_form/form-display', $params, t('Save'));
$params = [
'subject[0][value]' => 'Submission Test Form Subject',
'message[0][value]' => 'Submission Test Form Body',
'field_email_reference[0][value]' => 'email-via-reference@test.com',
];
$this
->drupalPostForm('contact/contact_reference_test_form', $params, t('Send message'));
$this
->assertText('Your message has been sent.', 'Message sent successfully.');
}
}