Commit ca90e672 authored by larowlan's avatar larowlan Committed by larowlan

Issue #2615112 by tim.plunkett, EclipseGc, naveenvalecha, larowlan: Add a view...

Issue #2615112 by tim.plunkett, EclipseGc, naveenvalecha, larowlan: Add a view builder for contact_form
parent b12255da
......@@ -7,6 +7,7 @@
namespace Drupal\contact_storage;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityHandlerInterface;
......@@ -41,11 +42,11 @@ class ContactFormViewBuilder implements EntityViewBuilderInterface, EntityHandle
protected $renderer;
/**
* The configuration factory.
* The contact settings config object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
* @var \Drupal\Core\Config\Config
*/
protected $configFactory;
protected $config;
/**
* The contact message storage.
......@@ -61,15 +62,15 @@ class ContactFormViewBuilder implements EntityViewBuilderInterface, EntityHandle
* The entity form builder service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory service.
* @param \Drupal\Core\Config\Config $config
* The contact settings config object.
* @param \Drupal\Core\Entity\EntityStorageInterface $contact_message_storage
* The contact message storage.
*/
public function __construct(EntityFormBuilderInterface $entity_form_builder, RendererInterface $renderer, ConfigFactoryInterface $config_factory, EntityStorageInterface $contact_message_storage) {
public function __construct(EntityFormBuilderInterface $entity_form_builder, RendererInterface $renderer, Config $config, EntityStorageInterface $contact_message_storage) {
$this->entityFormBuilder = $entity_form_builder;
$this->renderer = $renderer;
$this->configFactory = $config_factory;
$this->config = $config;
$this->contactMessageStorage = $contact_message_storage;
}
......@@ -80,7 +81,7 @@ class ContactFormViewBuilder implements EntityViewBuilderInterface, EntityHandle
return new static(
$container->get('entity.form_builder'),
$container->get('renderer'),
$container->get('config.factory'),
$container->get('config.factory')->get('contact.settings'),
$container->get('entity_type.manager')->getStorage('contact_message')
);
}
......@@ -97,8 +98,7 @@ class ContactFormViewBuilder implements EntityViewBuilderInterface, EntityHandle
$form['#title'] = $entity->label();
$form['#cache']['contexts'][] = 'user.permissions';
$config = $this->configFactory->get('contact.settings');
$this->renderer->addCacheableDependency($form, $config);
$this->renderer->addCacheableDependency($form, $this->config);
return $form;
}
......
......@@ -7,14 +7,12 @@
namespace Drupal\contact_storage\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests storing contact messages and viewing them through UI.
*
* @group contact_storage
*/
class ContactStorageTest extends WebTestBase {
class ContactStorageTest extends ContactStorageTestBase {
/**
* Modules to enable.
......@@ -97,59 +95,4 @@ class ContactStorageTest extends WebTestBase {
$this->assertEqual($this->url, $admin_user->urlInfo()->setAbsolute()->toString());
}
/**
* Adds a form.
*
* @param string $id
* The form machine name.
* @param string $label
* The form label.
* @param string $recipients
* The list of recipient email addresses.
* @param string $reply
* The auto-reply text that is sent to a user upon completing the contact
* form.
* @param bool $selected
* A Boolean indicating whether the form should be selected by default.
* @param array $third_party_settings
* Array of third party settings to be added to the posted form data.
*/
function addContactForm($id, $label, $recipients, $reply, $selected, $third_party_settings = []) {
$edit = array();
$edit['label'] = $label;
$edit['id'] = $id;
$edit['recipients'] = $recipients;
$edit['reply'] = $reply;
$edit['selected'] = ($selected ? TRUE : FALSE);
$edit += $third_party_settings;
$this->drupalPostForm('admin/structure/contact/add', $edit, t('Save'));
}
/**
* Submits the contact form.
*
* @param string $name
* The name of the sender.
* @param string $mail
* The email address of the sender.
* @param string $subject
* The subject of the message.
* @param string $id
* The form ID of the message.
* @param string $message
* The message body.
*/
function submitContact($name, $mail, $subject, $id, $message) {
$edit = array();
$edit['name'] = $name;
$edit['mail'] = $mail;
$edit['subject[0][value]'] = $subject;
$edit['message[0][value]'] = $message;
if ($id == $this->config('contact.settings')->get('default_form')) {
$this->drupalPostForm('contact', $edit, t('Send message'));
}
else {
$this->drupalPostForm('contact/' . $id, $edit, t('Send message'));
}
}
}
<?php
/**
* @file
* Contains ContactStorageTestBase.php
* Contains \Drupal\contact_storage\Tests\ContactStorageTestBase.
*/
namespace Drupal\contact_storage\Tests;
use Drupal\simpletest\WebTestBase;
class ContactStorageTestBase {
/**
* Defines a base-class for contact-storage tests.
*/
abstract class ContactStorageTestBase extends WebTestBase {
/**
* Adds a form.
*
* @param string $id
* The form machine name.
* @param string $label
* The form label.
* @param string $recipients
* The list of recipient email addresses.
* @param string $reply
* The auto-reply text that is sent to a user upon completing the contact
* form.
* @param bool $selected
* A Boolean indicating whether the form should be selected by default.
* @param array $third_party_settings
* Array of third party settings to be added to the posted form data.
*/
public function addContactForm($id, $label, $recipients, $reply, $selected, $third_party_settings = []) {
$edit = [];
$edit['label'] = $label;
$edit['id'] = $id;
$edit['recipients'] = $recipients;
$edit['reply'] = $reply;
$edit['selected'] = ($selected ? TRUE : FALSE);
$edit += $third_party_settings;
$this->drupalPostForm('admin/structure/contact/add', $edit, t('Save'));
}
/**
* Submits the contact form.
*
* @param string $name
* The name of the sender.
* @param string $mail
* The email address of the sender.
* @param string $subject
* The subject of the message.
* @param string $id
* The form ID of the message.
* @param string $message
* The message body.
*/
public function submitContact($name, $mail, $subject, $id, $message) {
$edit = [];
$edit['name'] = $name;
$edit['mail'] = $mail;
$edit['subject[0][value]'] = $subject;
$edit['message[0][value]'] = $message;
if ($id == $this->config('contact.settings')->get('default_form')) {
$this->drupalPostForm('contact', $edit, t('Send message'));
}
else {
$this->drupalPostForm('contact/' . $id, $edit, t('Send message'));
}
}
}
......@@ -7,14 +7,12 @@
namespace Drupal\contact_storage\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests adding contact form as entity reference and viewing them through UI.
*
* @group contact_storage
*/
class ContactViewBuilderTest extends WebTestBase {
class ContactViewBuilderTest extends ContactStorageTestBase {
/**
* Modules to enable.
......@@ -26,7 +24,6 @@ class ContactViewBuilderTest extends WebTestBase {
'node',
'contact',
'field_ui',
'contact_storage_test',
'contact_test',
'contact_storage',
];
......@@ -45,24 +42,26 @@ class ContactViewBuilderTest extends WebTestBase {
parent::setUp();
// Create Article node type.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article', 'display_submitted' => FALSE));
}
$this->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
'display_submitted' => FALSE,
]);
}
/**
* Helper function for testContactViewBuilder().
* Tests contact view builder functionality.
*/
public function testContactViewBuilder() {
// Create test admin user.
$this->adminUser = $this->drupalCreateUser(array(
$this->adminUser = $this->drupalCreateUser([
'administer content types',
'access site-wide contact form',
'administer contact forms',
'administer users',
'administer account settings',
'administer contact_message fields',
));
]);
// Login as admin user.
$this->drupalLogin($this->adminUser);
......@@ -79,41 +78,41 @@ class ContactViewBuilderTest extends WebTestBase {
// Add a Entity Reference Contact Field to Article content type.
$field_storage = \Drupal::entityManager()
->getStorage('field_storage_config')
->create(array(
->create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => 'entity_reference',
'settings' => array('target_type' => 'contact_form'),
));
'settings' => ['target_type' => 'contact_form'],
]);
$field_storage->save();
$field = \Drupal::entityManager()
->getStorage('field_config')
->create(array(
'field_storage' => $field_storage,
'bundle' => $bundle_name,
'settings' => array(
'handler' => 'default',
),
));
->create([
'field_storage' => $field_storage,
'bundle' => $bundle_name,
'settings' => [
'handler' => 'default',
],
]);
$field->save();
// configure the contact reference field form Entity form display.
// Configure the contact reference field form Entity form display.
entity_get_form_display($entity_type, $bundle_name, 'default')
->setComponent($field_name, array(
->setComponent($field_name, [
'type' => 'options_select',
'settings' => array(
'settings' => [
'weight' => 20,
),
))
],
])
->save();
// configure the contact reference field form Entity view display.
// Configure the contact reference field form Entity view display.
entity_get_display('node', 'article', 'default')
->setComponent($field_name, array(
->setComponent($field_name, [
'label' => 'above',
'type' => 'entity_reference_entity_view',
'weight' => 20,
))
])
->save();
// Display Article creation form.
......@@ -122,7 +121,7 @@ class ContactViewBuilderTest extends WebTestBase {
$body_key = 'body[0][value]';
$contact_key = 'contact';
// Create article node.
$edit = array();
$edit = [];
$edit[$title_key] = $this->randomMachineName(8);
$edit[$body_key] = $this->randomMachineName(16);
$edit[$contact_key] = 'test_id';
......@@ -134,34 +133,8 @@ class ContactViewBuilderTest extends WebTestBase {
$this->assertText(t('Your email address'));
$this->assertText(t('Subject'));
$this->assertText(t('Message'));
}
/**
* Adds a form.
*
* @param string $id
* The form machine name.
* @param string $label
* The form label.
* @param string $recipients
* The list of recipient email addresses.
* @param string $reply
* The auto-reply text that is sent to a user upon completing the contact
* form.
* @param bool $selected
* A Boolean indicating whether the form should be selected by default.
* @param array $third_party_settings
* Array of third party settings to be added to the posted form data.
*/
function addContactForm($id, $label, $recipients, $reply, $selected, $third_party_settings = []) {
$edit = array();
$edit['label'] = $label;
$edit['id'] = $id;
$edit['recipients'] = $recipients;
$edit['reply'] = $reply;
$edit['selected'] = ($selected ? TRUE : FALSE);
$edit += $third_party_settings;
$this->drupalPostForm('admin/structure/contact/add', $edit, t('Save'));
$this->assertFieldByName('subject[0][value]');
$this->assertFieldByName('message[0][value]');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment