Skip to content
Snippets Groups Projects
Commit 1b512372 authored by Aaron Bauman's avatar Aaron Bauman
Browse files

Merge branch 'addl_tests_3169218' into 8.x-4.x

parents 3a59a9a6 52343c63
No related branches found
No related tags found
No related merge requests found
langcode: en
status: true
dependencies:
config:
- field.storage.node.field_salesforce_test_tax_ref
- node.type.salesforce_mapping_test_content
- taxonomy.vocabulary.salesforce_test_vocabulary
id: node.salesforce_mapping_test_content.field_salesforce_test_tax_ref
field_name: field_salesforce_test_tax_ref
entity_type: node
bundle: salesforce_mapping_test_content
label: 'Salesforce Test Taxonomy Ref'
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
handler: 'default:taxonomy_term'
handler_settings:
target_bundles:
salesforce_test_vocabulary: salesforce_test_vocabulary
sort:
field: name
direction: asc
auto_create: false
auto_create_bundle: ''
field_type: entity_reference
langcode: en
status: true
dependencies:
module:
- node
- taxonomy
id: node.field_salesforce_test_tax_ref
field_name: field_salesforce_test_tax_ref
entity_type: node
type: entity_reference
settings:
target_type: taxonomy_term
module: core
locked: false
cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
langcode: en
status: true
dependencies: { }
name: 'Salesforce Test Vocabulary'
vid: salesforce_test_vocabulary
description: ''
weight: 0
......@@ -9,3 +9,4 @@ dependencies:
- node
- salesforce_mapping
- salesforce_test_rest_client
- taxonomy
......@@ -2,8 +2,8 @@
namespace Drupal\salesforce_mapping_ui\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Component\Utility\NestedArray;
use \Drupal\Core\Render\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\salesforce_mapping\SalesforceMappingFieldPluginInterface as FieldPluginInterface;
......@@ -69,9 +69,7 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
$form['field_mappings_wrapper'] = [
'#title' => $this->t('Mapped Fields'),
'#type' => 'details',
'#id' => 'edit-field-mappings-wrapper',
'#open' => TRUE,
'#type' => 'fieldset',
];
$field_mappings_wrapper = &$form['field_mappings_wrapper'];
......@@ -81,7 +79,8 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
'#tree' => TRUE,
'#type' => 'container',
// @TODO there's probably a better way to tie ajax callbacks to this element than by hard-coding an HTML DOM ID here.
'#id' => 'edit-field-mappings',
'#prefix' => '<div id="edit-field-mappings">',
'#suffix' => '</div>',
'#attributes' => ['class' => ['container-striped']],
];
$rows = &$field_mappings_wrapper['field_mappings'];
......@@ -95,7 +94,10 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
$add_field_text = !empty($field_mappings) ? $this->t('Add another field mapping') : $this->t('Add a field mapping to get started');
$form['buttons'] = ['#type' => 'container'];
$form['buttons'] = [
'#type' => 'container',
'#tree' => true
];
$form['buttons']['field_type'] = [
'#title' => $this->t('Field Type'),
'#type' => 'select',
......@@ -106,11 +108,11 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
$form['buttons']['add'] = [
'#value' => $add_field_text,
'#type' => 'submit',
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => [],
'#limit_validation_errors' => [['buttons']],
'#submit' => ['::addField'],
'#ajax' => [
'callback' => [$this, 'fieldAddCallback'],
'wrapper' => 'edit-field-mappings-wrapper',
'wrapper' => 'edit-field-mappings',
],
'#states' => [
'disabled' => [
......@@ -119,46 +121,17 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
],
];
$row_template = [
'#type' => 'container',
'#attributes' => ['class' => ['field_mapping_field', 'row']],
];
// Add a row for each saved mapping.
$zebra = 0;
foreach ($this->entity->getFieldMappings() as $field_plugin) {
$row = $row_template;
$row['#attributes']['class']['zebra'] = ($zebra % 2) ? 'odd' : 'even';
$rows[] = $row + $this->getRow($field_plugin, $form, $form_state);
$zebra++;
$rows[] = $this->getRow($form, $form_state, $field_plugin);
}
// Apply any changes from form_state to existing fields.
$input = $form_state->getUserInput();
if (!empty($input['field_mappings'])) {
for ($i = count($this->entity->getFieldMappings()); $i < count($input['field_mappings']); $i++) {
$row = $row_template;
$row['#attributes']['class']['zebra'] = ($zebra % 2) ? 'odd' : 'even';
$field_plugin = $this->entity->getFieldMapping($input['field_mappings'][$i]);
$rows[] = $row + $this->getRow($field_plugin, $form, $form_state);
$zebra++;
}
}
// @TODO input does not contain the clicked button, have to go to values for
// that. This may change?
// Add button was clicked. See if we have a field_type value -- it's
// required. If not, take no action. #states is already used to prevent
// users from adding without selecting field_type. If they've worked
// around that, they're going to have problems.
if (!empty($form_state->getValues())
&& $form_state->getValue('add') == $form_state->getValue('op')
&& !empty($input['field_type'])
&& $form_state->getTriggeringElement()['#name'] != 'context_drupal_field_value') {
$row = $row_template;
$row['#attributes']['class']['zebra'] = ($zebra % 2) ? 'odd' : 'even';
$rows[] = $row + $this->getRow(NULL, $form, $form_state);
$zebra++;
// Add a new row in case it was just added.
$values = &$form_state->getValues();
$new_field = NestedArray::getValue($values, ['buttons', 'new_field']);
if (!empty($new_field)) {
$rows[] = $this->getRow($form, $form_state);
NestedArray::unsetValue($values, ['buttons', 'new_field']);
}
// Retrieve and add the form actions array.
......@@ -193,23 +166,31 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
/**
* Helper function to return an empty row for the field mapping form.
*/
private function getRow(FieldPluginInterface $field_plugin = NULL, $form, FormStateInterface $form_state) {
static $i = 0;
$input = $form_state->getUserInput();
private function getRow($form, FormStateInterface $form_state, FieldPluginInterface $field_plugin = NULL) {
$values = &$form_state->getValues();
if ($field_plugin == NULL) {
$field_type = $input['field_type'];
$field_type = NestedArray::getValue($values, ['buttons', 'new_field']);
$field_plugin_definition = $this->getFieldPlugin($field_type);
$configuration = [
'mapping' => $this->entity,
'id' => count(Element::children($form['field_mappings_wrapper']['field_mappings'])),
'drupal_field_type' => $field_type,
];
/** @var \Drupal\salesforce_mapping\SalesforceMappingFieldPluginInterface $field_plugin */
$field_plugin = $this->mappingFieldPluginManager->createInstance(
$field_plugin_definition['id'],
['mapping' => $this->entity, 'id' => $i++]
$field_plugin_definition['id'], $configuration
);
}
else {
$i++;
$field_mapping_plugins = $this->entity->getFieldMappings();
$config = [];
foreach ($field_mapping_plugins as $plugin) {
$config[] = $plugin->getConfiguration();
}
$config[] = $field_plugin->getConfiguration();
$this->entity->set('field_mappings', $config);
}
$row['config'] = $field_plugin->buildConfigurationForm($form, $form_state);
$row['config']['id'] = ['#type' => 'value', '#value' => $i];
$row['config']['id'] = ['#type' => 'value', '#value' => $field_plugin->config('id')];
// @TODO implement "lock/unlock" logic here:
// @TODO convert these to AJAX operations
$operations = [
......@@ -227,6 +208,10 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
'#type' => 'hidden',
'#value' => $field_plugin->getPluginId(),
];
$row['#type'] = 'container';
$row['#attributes'] = [
'class' => ['field_mapping_field', 'row', $field_plugin->config('id') % 2 ? 'odd' : 'even']
];
return $row;
}
......@@ -281,7 +266,7 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
// Need to transform the schema slightly to remove the "config" dereference.
// Also trigger submit handlers on plugins.
$form_state->unsetValue(['field_type', 'ops']);
$form_state->unsetValue(['buttons', 'field_type', 'ops']);
$values = &$form_state->getValues();
foreach ($values['field_mappings'] as $i => &$value) {
......@@ -300,11 +285,20 @@ class SalesforceMappingFieldsForm extends SalesforceMappingFormBase {
/**
* Ajax callback for adding a new field.
*/
public function fieldAddCallback($form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// Requires updating itself and the field map.
$response->addCommand(new ReplaceCommand('#edit-field-mappings-wrapper', render($form['field_mappings_wrapper'])));
return $response;
public function fieldAddCallback(&$form, FormStateInterface $form_state) {
return $form['field_mappings_wrapper']['field_mappings'];
}
public function addField(&$form, FormStateInterface $form_state) {
$trigger = $form_state->getTriggeringElement();
$values = &$form_state->getValues();
$new_field = NestedArray::getValue($values, ['buttons', 'field_type']);
if (in_array('add', $trigger['#array_parents'])
&& !empty($new_field)
&& $trigger['#name'] != 'context_drupal_field_value') {
NestedArray::setValue($values, ['buttons', 'new_field'], $new_field);
}
$form_state->setRebuild(TRUE);
}
/**
......
......@@ -31,9 +31,11 @@ class SalesforceMappingCrudFormTest extends BrowserTestBase {
'salesforce_test_rest_client',
'salesforce_mapping',
'salesforce_mapping_ui',
'salesforce_mapping_test',
'user',
'link',
'dynamic_entity_reference',
'taxonomy',
];
/**
......@@ -66,16 +68,13 @@ class SalesforceMappingCrudFormTest extends BrowserTestBase {
$post = [
'id' => $mapping_name,
'label' => $mapping_name,
'drupal_entity_type' => 'user',
'drupal_bundle' => 'user',
'drupal_entity_type' => 'node',
'drupal_bundle' => 'salesforce_mapping_test_content',
'salesforce_object_type' => 'Contact',
];
$this->drupalPostForm('admin/structure/salesforce/mappings/add', $post, $this->t('Save'));
$newurl = parse_url($this->getUrl());
$this->assertSession()->pageTextContainsOnce($this->t('The mapping has been successfully saved.'));
// Make sure the redirect was correct (and therefore form was submitted
// successfully).
$this->assertEqual($newurl['path'], $base_path . 'admin/structure/salesforce/mappings/manage/' . $mapping_name . '/fields');
$mapping = $mappingStorage->load($mapping_name);
// Make sure mapping was saved correctly.
$this->assertEqual($mapping->id(), $mapping_name);
......@@ -86,8 +85,8 @@ class SalesforceMappingCrudFormTest extends BrowserTestBase {
drupal_flush_all_caches();
$post = [
'label' => $this->randomMachineName(),
'drupal_entity_type' => 'user',
'drupal_bundle' => 'user',
'drupal_entity_type' => 'node',
'drupal_bundle' => 'salesforce_mapping_test_content',
'salesforce_object_type' => 'Contact',
];
$this->drupalPostForm('admin/structure/salesforce/mappings/manage/' . $mapping_name, $post, $this->t('Save'));
......@@ -98,15 +97,44 @@ class SalesforceMappingCrudFormTest extends BrowserTestBase {
// cause fatal errors.
$mappingFieldsPluginManager = \Drupal::service('plugin.manager.salesforce_mapping_field');
$field_plugins = $mappingFieldsPluginManager->getDefinitions();
$post = [];
$i = 0;
$this->drupalGet('admin/structure/salesforce/mappings/manage/' . $mapping_name . '/fields');
foreach ($field_plugins as $definition) {
if (call_user_func([$definition['class'], 'isAllowed'], $mapping)) {
$post = [
'field_type' => $definition['id'],
];
$this->drupalPostForm('admin/structure/salesforce/mappings/manage/' . $mapping_name . '/fields', $post, $this->t('Add a field mapping to get started'));
// Add a new field:
$post['buttons[field_type]'] = $definition['id'];
$this->drupalPostForm(NULL, $post, $this->t('Add a field mapping to get started'));
// Confirm that the new field shows up:
$this->assertText($definition['label']);
// @TODO need an interface for field plugins that will tell us which config values are applicable.
// Add all components of this field plugin to our post array to build up the mapping.
$this->assertElementPresent("[name='field_mappings[$i][config][drupal_field_value]'], [name='field_mappings[$i][config][drupal_field_value][setting]']");
$this->assertElementPresent("[name='field_mappings[$i][config][salesforce_field]'], [name='field_mappings[$i][config][drupal_constant]']");
$this->assertField("field_mappings[$i][config][description]");
$this->assertField("field_mappings[$i][config][direction]");
$this->assertSession()->hiddenFieldExists("field_mappings[$i][drupal_field_type]");
if ($this->getSession()->getPage()->find('css', "select[name='field_mappings[$i][config][salesforce_field]'] option[value='LastName']")) {
$post["field_mappings[$i][config][salesforce_field]"] = 'LastName';
}
if ($this->getSession()->getPage()->find('css', "select[name='field_mappings[$i][config][drupal_field_value]'] option[value='title']")) {
$post["field_mappings[$i][config][drupal_field_value]"] = 'title';
}
$i++;
}
}
// Confirm that form saves correctly.
$this->drupalPostForm(NULL, $post, $this->t('Save'));
$this->assertSession()->pageTextContainsOnce($this->t('The mapping has been successfully saved.'));
// Confirm that the changes are stored properly by reloading and counting the fields.
$this->drupalGet('admin/structure/salesforce/mappings/manage/' . $mapping_name . '/fields');
for ($j = 0; $j < $i; $j++) {
$this->assertSession()->elementExists('css', "#edit-field-mappings-$j");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment