View source
<?php
namespace Drupal\salesforce_mapping_ui\Tests;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
class SalesforceMappingCrudFormTest extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'salesforce',
'salesforce_test_rest_client',
'salesforce_mapping',
'salesforce_mapping_ui',
'salesforce_mapping_test',
'user',
'link',
'dynamic_entity_reference',
'taxonomy',
];
protected $adminSalesforceUser;
protected function setUp() : void {
parent::setUp();
$this->adminSalesforceUser = $this
->drupalCreateUser([
'administer salesforce mapping',
]);
}
public function testMappingCrudForm() {
global $base_path;
$mappingStorage = \Drupal::entityTypeManager()
->getStorage('salesforce_mapping');
$this
->drupalLogin($this->adminSalesforceUser);
$this
->drupalGet('admin/structure/salesforce/mappings/add');
$mapping_name = 'mapping' . rand(100, 10000);
$post = [
'id' => $mapping_name,
'label' => $mapping_name,
'drupal_entity_type' => 'node',
'drupal_bundle' => 'salesforce_mapping_test_content',
'salesforce_object_type' => 'Contact',
];
$this
->submitForm($post, $this
->t('Save'));
$this
->assertSession()
->pageTextContainsOnce($this
->t('The mapping has been successfully saved.'));
$mapping = $mappingStorage
->load($mapping_name);
$this
->assertEquals($mapping
->id(), $mapping_name);
$this
->assertEquals($mapping
->label(), $mapping_name);
drupal_flush_all_caches();
$this
->drupalGet('admin/structure/salesforce/mappings/manage/' . $mapping_name);
$post = [
'label' => $this
->randomMachineName(),
'drupal_entity_type' => 'node',
'drupal_bundle' => 'salesforce_mapping_test_content',
'salesforce_object_type' => 'Contact',
];
$this
->submitForm($post, $this
->t('Save'));
$this
->assertSession()
->fieldValueEquals('label', $post['label']);
$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['buttons[field_type]'] = $definition['id'];
$this
->submitForm($post, $this
->t('Add a field mapping to get started'));
$this
->assertSession()
->pageTextContains($definition['label']);
$this
->assertSession()
->elementExists('css', "[name='field_mappings[{$i}][config][drupal_field_value]'], [name='field_mappings[{$i}][config][drupal_field_value][setting]']");
$this
->assertSession()
->elementExists('css', "[name='field_mappings[{$i}][config][salesforce_field]'], [name='field_mappings[{$i}][config][drupal_constant]']");
$this
->assertSession()
->fieldExists("field_mappings[{$i}][config][description]");
$this
->assertSession()
->fieldExists("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++;
}
}
$this
->submitForm($post, $this
->t('Save'));
$this
->assertSession()
->pageTextContainsOnce($this
->t('The mapping has been successfully saved.'));
$this
->drupalGet('admin/structure/salesforce/mappings/manage/' . $mapping_name . '/fields');
for ($j = 0; $j < $i; $j++) {
$this
->assertSession()
->elementExists('css', "#edit-field-mappings-{$j}");
}
}
}