View source
<?php
namespace Drupal\Tests\conditional_fields\FunctionalJavascript\Entity;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\conditional_fields\FunctionalJavascript\ConditionalFieldTestBase;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
class ConditionalFieldsUserTest extends ConditionalFieldTestBase {
protected $dependee = 'field_dependee';
protected $dependent = 'field_dependent';
public static $modules = [
'conditional_fields',
'field_ui',
];
protected $screenshotPath = 'sites/simpletest/conditional_fields/user/';
public function setUp() {
parent::setUp();
$this
->addField($this->dependee, 'boolean', 'boolean_checkbox');
$this
->addField($this->dependent, 'text', 'text_textfield');
}
protected function addField($field_name, $type, $widget) {
$fieldStorageDefinition = [
'field_name' => $field_name,
'entity_type' => 'user',
'type' => $type,
'cardinality' => -1,
];
$fieldStorage = FieldStorageConfig::create($fieldStorageDefinition);
$fieldStorage
->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'user',
]);
$field
->save();
$entity_form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('user.user.default');
if (!$entity_form_display) {
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
'status' => TRUE,
]);
}
if ($entity_form_display instanceof EntityDisplayInterface) {
$entity_form_display
->setComponent($field_name, [
'type' => $widget,
])
->save();
}
}
public function testUserEntity() {
$this
->baseTestSteps();
$this
->createCondition($this->dependent, $this->dependee, 'visible', 'checked');
$this
->drupalGet('admin/structure/conditional_fields/user/user');
$this
->createScreenshot($this->screenshotPath . '01-config-was-added.png');
$this
->assertSession()
->pageTextContains($this->dependent . ' ' . $this->dependee . ' visible checked');
$this
->drupalGet('admin/people/create');
$this
->createScreenshot($this->screenshotPath . '02-mail-not-visible.png');
$this
->waitUntilHidden('.field--name-field-dependent', 50, 'Dependent field is not visible');
$this
->changeSelect('#edit-field-dependee-value', TRUE);
$this
->createScreenshot($this->screenshotPath . '03-mail-visible.png');
$this
->waitUntilVisible('.field--name-field-dependent', 50, 'Dependent field is visible');
}
protected function baseTestSteps() {
$user = $this
->drupalCreateUser([
'administer users',
'administer account settings',
'view conditional fields',
'edit conditional fields',
'delete conditional fields',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/structure/conditional_fields');
$this
->assertSession()
->pageTextContains('User');
$this
->drupalGet('admin/structure/conditional_fields/user');
$this
->drupalGet('admin/structure/conditional_fields/user/user');
}
}