View source
<?php
namespace Drupal\Tests\lingotek\FunctionalJavascript;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
abstract class LingotekFunctionalJavascriptTestBase extends WebDriverTestBase {
protected $minkDefaultDriverClass = DrupalSelenium2Driver::class;
protected $defaultTheme = 'classy';
public static $modules = [
'lingotek',
'lingotek_test',
];
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->rootUser);
$this
->connectToLingotek();
}
protected function connectToLingotek() {
$this
->drupalGet('admin/lingotek/setup/account');
$this
->clickLink('Connect Lingotek Account');
$this
->drupalPostForm(NULL, [
'community' => 'test_community',
], 'Next');
$this
->drupalPostForm(NULL, [
'project' => 'test_project',
'vault' => 'test_vault',
], 'Save configuration');
}
public function createTextField($name, $type_name, $entity_type_id = 'node', array $storage_settings = [], array $field_settings = [], array $widget_settings = []) {
$fieldStorage = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->create([
'field_name' => $name,
'entity_type' => $entity_type_id,
'type' => 'text',
'settings' => $storage_settings,
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
]);
$fieldStorage
->save();
$field_config = \Drupal::entityTypeManager()
->getStorage('field_config')
->create([
'field_name' => $name,
'label' => $name,
'entity_type' => $entity_type_id,
'bundle' => $type_name,
'required' => !empty($field_settings['required']),
'settings' => $field_settings,
]);
$field_config
->save();
$entity_form_display = EntityFormDisplay::load($entity_type_id . '.' . $type_name . '.' . 'default');
if (!$entity_form_display) {
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => $entity_type_id,
'bundle' => $type_name,
'mode' => 'default',
'status' => TRUE,
]);
}
$entity_form_display
->setComponent($name, [
'type' => 'text_textfield',
'settings' => $widget_settings,
])
->save();
$display = EntityViewDisplay::load($entity_type_id . '.' . $type_name . '.' . 'default');
if (!$display) {
$display = EntityViewDisplay::create([
'targetEntityType' => $entity_type_id,
'bundle' => $type_name,
'mode' => 'default',
'status' => TRUE,
]);
}
$display
->setComponent($name)
->save();
return $field_config;
}
public function createImageField($name, $type_name, $entity_type_id = 'node', array $storage_settings = [], array $field_settings = [], array $widget_settings = []) {
$fieldStorage = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->create([
'field_name' => $name,
'entity_type' => $entity_type_id,
'type' => 'image',
'settings' => $storage_settings,
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
]);
$fieldStorage
->save();
$field_config = \Drupal::entityTypeManager()
->getStorage('field_config')
->create([
'field_name' => $name,
'label' => $name,
'entity_type' => $entity_type_id,
'bundle' => $type_name,
'required' => !empty($field_settings['required']),
'settings' => $field_settings,
]);
$field_config
->save();
$entity_form_display = EntityFormDisplay::load($entity_type_id . '.' . $type_name . '.' . 'default');
if (!$entity_form_display) {
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => $entity_type_id,
'bundle' => $type_name,
'mode' => 'default',
'status' => TRUE,
]);
}
$entity_form_display
->setComponent($name, [
'type' => 'image_image',
'settings' => $widget_settings,
])
->save();
$display = EntityViewDisplay::load($entity_type_id . '.' . $type_name . '.' . 'default');
if (!$display) {
$display = EntityViewDisplay::create([
'targetEntityType' => $entity_type_id,
'bundle' => $type_name,
'mode' => 'default',
'status' => TRUE,
]);
}
$display
->setComponent($name)
->save();
return $field_config;
}
protected function saveLingotekContentTranslationSettings($settings) {
$this
->drupalGet('/admin/lingotek/settings');
$page = $this
->getSession()
->getPage();
$contentTabDetails = $page
->find('css', '#edit-parent-details');
$contentTabDetails
->click();
foreach ($settings as $entity_type => $entity_type_settings) {
$entityTabDetails = $page
->find('css', '#edit-entity-' . str_replace('_', '-', $entity_type));
$entityTabDetails
->click();
foreach ($entity_type_settings as $bundle_id => $bundle_settings) {
$field_definitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $bundle_id);
$fieldEnabled = $page
->find('css', '#edit-' . str_replace('_', '-', $entity_type) . '-' . str_replace('_', '-', $bundle_id) . '-enabled');
$fieldEnabled
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest();
if (isset($bundle_settings['profiles']) && $bundle_settings['profiles'] !== NULL) {
$page
->selectFieldOption('edit-' . str_replace('_', '-', $entity_type) . '-' . str_replace('_', '-', $bundle_id) . '-profiles', $bundle_settings['profiles']);
}
$fieldsCheckboxes = $page
->findAll('css', '#container-' . str_replace('_', '-', $entity_type) . '-' . str_replace('_', '-', $bundle_id) . ' input[type="checkbox"]');
$propertiesCheckboxes = $page
->findAll('css', 'input[type="checkbox"] .field-property-checkbox');
foreach ($fieldsCheckboxes as $fieldCheckbox) {
if ($fieldCheckbox
->isChecked()) {
$fieldCheckbox
->click();
}
}
foreach ($propertiesCheckboxes as $propertyCheckbox) {
if ($propertyCheckbox
->isChecked()) {
$propertyCheckbox
->click();
}
}
foreach ($bundle_settings['fields'] as $field_id => $field_properties) {
$field_definition = $field_definitions[$field_id];
$field_type_definition = \Drupal::service('plugin.manager.field.field_type')
->getDefinition($field_definition
->getType());
$fieldElementId = 'edit-' . str_replace('_', '-', $entity_type) . '-' . str_replace('_', '-', $bundle_id) . '-fields-' . str_replace('_', '-', $field_id);
$fieldCheckbox = $page
->find('css', '#' . $fieldElementId);
$fieldCheckbox
->click();
if (is_array($field_properties)) {
$column_groups = $field_type_definition['column_groups'];
$properties = [];
foreach ($column_groups as $property_id => $property) {
if (isset($property['translatable']) && $property['translatable']) {
$property_definitions = $field_type_definition['class']::propertyDefinitions($field_definition
->getFieldStorageDefinition());
if (isset($property_definitions[$property_id])) {
$properties[$property_id] = $property_id;
}
}
}
foreach ($properties as $field_property) {
$propertyElementId = 'edit-' . str_replace('_', '-', $entity_type) . '-' . str_replace('_', '-', $bundle_id) . '-fields-' . str_replace('_', '-', $field_id) . 'properties-' . str_replace('_', '-', $field_property);
$propertyCheckbox = $page
->find('css', '#' . $propertyElementId);
if ($propertyCheckbox
->isChecked()) {
$propertyCheckbox
->click();
}
}
foreach ($field_properties as $field_property) {
$propertyElementId = 'edit-' . str_replace('_', '-', $entity_type) . '-' . str_replace('_', '-', $bundle_id) . '-fields-' . str_replace('_', '-', $field_id) . 'properties-' . str_replace('_', '-', $field_property);
$propertyCheckbox = $page
->find('css', '#' . $propertyElementId);
if (!$propertyCheckbox
->isChecked()) {
$propertyCheckbox
->click();
}
}
}
}
if (isset($bundle_settings['moderation']) && $bundle_settings['moderation'] !== NULL) {
$page
->selectFieldOption('edit-' . str_replace('_', '-', $entity_type) . '-' . str_replace('_', '-', $bundle_id) . '-moderation-upload-status', $bundle_settings['moderation']['upload_status']);
$page
->selectFieldOption('edit-' . str_replace('_', '-', $entity_type) . '-' . str_replace('_', '-', $bundle_id) . '-moderation-download-transition', $bundle_settings['moderation']['download_transition']);
}
}
}
$this
->drupalPostForm(NULL, [], 'Save', [], 'lingoteksettings-tab-content-form');
}
protected function saveLingotekContentTranslationSettingsForNodeTypes($node_types = [
'article',
], $profile = 'automatic') {
$settings = [];
foreach ($node_types as $node_type) {
$settings['node'][$node_type] = [
'profiles' => $profile,
'fields' => [
'title' => 1,
'body' => 1,
],
];
}
$this
->saveLingotekContentTranslationSettings($settings);
}
protected function stop() {
$this
->assertSession()
->waitForElementVisible('css', '.test-wait', 1.0E+26);
}
}