View source
<?php
namespace Drupal\Tests\contact_ajax\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class ContactAjaxTest extends WebDriverTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'node',
'text',
'block',
'contact',
'contact_test',
'field_ui',
'contact_ajax',
];
public function testContactAjax() {
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('page_title_block');
$admin_user = $this
->drupalCreateUser([
'access site-wide contact form',
'administer contact forms',
'administer users',
'administer account settings',
'administer contact_message fields',
'administer contact_message form display',
'administer contact_message display',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/structure/contact/add');
$this
->assertSession()
->pageTextContains(t('Ajax Form'));
$this
->configureContactAjax();
$this
->sendContactAjax();
}
public function configureContactAjax() {
$common = [
'label' => 'test_label',
'recipients' => 'simpletest@example.com',
'reply' => '',
'selected' => TRUE,
'contact_ajax_enabled' => TRUE,
];
$edit = [];
$edit['id'] = 'test_id';
$edit['contact_ajax_confirmation_type'] = CONTACT_AJAX_LOAD_DEFAULT_MESSAGE;
$this
->createContactAjaxForm($common, $edit);
$edit = [];
$edit['id'] = 'test_custom_message_id';
$edit['contact_ajax_confirmation_type'] = CONTACT_AJAX_LOAD_FROM_MESSAGE;
$edit['contact_ajax_load_from_message[value]'] = '<div><b>test ajax message</b></div>';
$this
->createContactAjaxForm($common, $edit);
$edit = [];
$edit['id'] = 'test_node_content';
$edit['contact_ajax_confirmation_type'] = CONTACT_AJAX_LOAD_FROM_URI;
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$node = $this
->drupalCreateNode([
'title' => 'test ajax title',
'type' => 'article',
]);
$edit['contact_ajax_load_from_uri'] = 'test ajax title (' . $node
->id() . ')';
$this
->createContactAjaxForm($common, $edit);
$edit = [];
$edit['id'] = 'test_load_other_element';
$edit['contact_ajax_confirmation_type'] = CONTACT_AJAX_LOAD_DEFAULT_MESSAGE;
$edit['contact_ajax_prefix_id'] = 'ajax-contact-prefix';
$edit['contact_ajax_render_selector'] = '#ajax-contact-prefix';
$this
->createContactAjaxForm($common, $edit);
$edit = [];
$edit['id'] = 'test_load_with_validation_errors';
$edit['contact_ajax_confirmation_type'] = CONTACT_AJAX_LOAD_DEFAULT_MESSAGE;
$this
->createContactAjaxForm($common, $edit);
user_role_grant_permissions(ANONYMOUS_ID, [
'access site-wide contact form',
]);
$this
->drupalLogout();
}
public function createContactAjaxForm($common, $edit) {
$assert = $this
->assertSession();
$message = 'Your message has been sent.';
$this
->drupalGet('admin/structure/contact/add');
if ($this
->xpath($this
->constructFieldXpath('name', 'message'))) {
$edit['message'] = $message;
}
$this
->drupalPostForm(NULL, $common, t('Save'));
$this
->click('#edit-advanced summary');
$assert
->waitForElementVisible('css', '[name="contact_ajax_prefix_id"]');
$assert
->waitForElementVisible('css', '[name="id"]');
$this
->drupalPostForm(NULL, $common + $edit, t('Save'));
$assert
->pageTextContains(t('Contact form test_label has been added.'));
}
public function sendContactAjax() {
$assert = $this
->assertSession();
$edit = [];
$edit['name'] = 'Test name';
$edit['mail'] = 'simpletest@example.com';
$edit['subject[0][value]'] = 'test subject';
$edit['message[0][value]'] = 'test message';
$form_id = 'test_id';
$this
->drupalGet('contact/' . $form_id);
$this
->drupalPostForm(NULL, $edit, t('Send message'));
$assert
->assertWaitOnAjaxRequest();
$assert
->pageTextContains('Your message has been sent.', '[OK] Your message has been sent.');
$form_id = 'test_custom_message_id';
$this
->drupalGet('contact/' . $form_id);
$this
->drupalPostForm(NULL, $edit, t('Send message'));
$assert
->assertWaitOnAjaxRequest();
$assert
->pageTextContains('test ajax message', '[OK] test ajax message.');
$form_id = 'test_node_content';
$this
->drupalGet('contact/' . $form_id);
$this
->drupalPostForm(NULL, $edit, t('Send message'));
$assert
->assertWaitOnAjaxRequest();
$assert
->pageTextContains('test ajax title', '[OK] test ajax title');
$form_id = 'test_load_other_element';
$this
->drupalGet('contact/' . $form_id);
$assert
->responseContains('id="ajax-contact-prefix"', '[OK] ajax prefix wrapper added');
$this
->drupalPostForm(NULL, $edit, t('Send message'));
$assert
->assertWaitOnAjaxRequest();
$assert
->pageTextContains('Your message has been sent.', '[OK] Your message has been sent.');
$assert
->responseNotContains('<form', '[OK] Form has been removed.');
unset($edit['name']);
$form_id = 'test_load_with_validation_errors';
$this
->drupalGet('contact/' . $form_id);
$this
->drupalPostForm(NULL, $edit, t('Send message'));
$assert
->assertWaitOnAjaxRequest();
$assert
->pageTextContains('Your name field is required.', '[OK] test render with form errors.');
}
}