/** * Tests boolean field. */ function testBooleanField() { $on = $this->randomMachineName(); $off = $this->randomMachineName(); $label = $this->randomMachineName(); // Create a field with settings to validate. $field_name = drupal_strtolower($this->randomMachineName()); $this->field_storage = FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'boolean', 'settings' => array('on_label' => $on, 'off_label' => $off))); $this->field_storage->save(); $this->field = FieldConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'label' => $label, 'required' => TRUE)); $this->field->save(); // Create a form display for the default form mode. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => 'boolean_checkbox'))->save(); // Create a display for the full view mode. entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name, array('type' => 'boolean'))->save(); // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.'); $this->assertRaw($on); // Submit and ensure it is accepted. $edit = array("{$field_name}[value]" => 1); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); // Verify that boolean value is displayed. $entity = entity_load('entity_test', $id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->drupalSetContent(drupal_render($content)); $this->assertRaw('<div class="field-item">' . $on . '</div>'); // Test the display_label option. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => 'boolean_checkbox', 'settings' => array('display_label' => TRUE)))->save(); $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.'); $this->assertNoRaw($on); $this->assertText($this->field->label()); // Go to the form display page and check if the default settings works as // expected. $fieldEditUrl = 'entity_test/structure/entity_test/form-display'; $this->drupalGet($fieldEditUrl); // Click on the widget settings button to open the widget settings form. $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit"); $this->assertText('Use field label instead of the "On label" as label', t('Display setting checkbox available.')); // Enable setting. $edit = array('fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1); $this->drupalPostAjaxForm(NULL, $edit, $field_name . "_plugin_settings_update"); $this->drupalPostForm(NULL, NULL, 'Save'); // Go again to the form display page and check if the setting // is stored and has the expected effect. $this->drupalGet($fieldEditUrl); $this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.'); $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit"); $this->assertText('Use field label instead of the "On label" as label', t('Display setting checkbox is available')); $this->assertFieldByXPath('*//input[@id="edit-fields-' . $field_name . '-settings-edit-form-settings-display-label" and @value="1"]', TRUE, t('Display label changes label of the checkbox')); // Test the boolean field settings. $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name . '/storage'); $this->assertFieldById('edit-field-storage-settings-on-label', $on); $this->assertFieldById('edit-field-storage-settings-off-label', $off); }
/** * Checks block edit functionality. */ public function testBlockFields() { $this->drupalLogin($this->adminUser); $this->blockType = $this->createBlockContentType('link'); // Create a field with settings to validate. $this->fieldStorage = entity_create('field_storage_config', array('name' => drupal_strtolower($this->randomName()), 'entity_type' => 'block_content', 'type' => 'link', 'cardinality' => 2)); $this->fieldStorage->save(); $this->instance = entity_create('field_instance_config', array('field_storage' => $this->fieldStorage, 'bundle' => 'link', 'settings' => array('title' => DRUPAL_OPTIONAL))); $this->instance->save(); entity_get_form_display('block_content', 'link', 'default')->setComponent($this->fieldStorage->getName(), array('type' => 'link_default'))->save(); entity_get_display('block_content', 'link', 'default')->setComponent($this->fieldStorage->getName(), array('type' => 'link', 'label' => 'hidden'))->save(); // Create a block. $this->drupalGet('block/add/link'); $edit = array('info[0][value]' => $this->randomName(8), $this->fieldStorage->getName() . '[0][url]' => 'http://example.com', $this->fieldStorage->getName() . '[0][title]' => 'Example.com'); $this->drupalPostForm(NULL, $edit, t('Save')); $block = entity_load('block_content', 1); $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . \Drupal::config('system.theme')->get('default'); // Place the block. $instance = array('id' => drupal_strtolower($edit['info[0][value]']), 'settings[label]' => $edit['info[0][value]'], 'region' => 'sidebar_first'); $this->drupalPostForm($url, $instance, t('Save block')); // Navigate to home page. $this->drupalGet('<front>'); $this->assertLinkByHref('http://example.com'); $this->assertText('Example.com'); }
/** * Tests email field. */ function testEmailField() { // Create a field with settings to validate. $field_name = Unicode::strtolower($this->randomMachineName()); $this->fieldStorage = FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'email')); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'entity_test']); $this->field->save(); // Create a form display for the default form mode. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => 'email_default', 'settings' => array('placeholder' => '*****@*****.**')))->save(); // Create a display for the full view mode. entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name, array('type' => 'email_mailto'))->save(); // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget found.'); $this->assertRaw('placeholder="*****@*****.**"'); // Submit a valid email address and ensure it is accepted. $value = '*****@*****.**'; $edit = array("{$field_name}[0][value]" => $value); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertRaw($value); // Verify that a mailto link is displayed. $entity = EntityTest::load($id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->setRawContent(\Drupal::service('renderer')->renderRoot($content)); $this->assertLinkByHref('mailto:test@example.com'); }
function setUp() { parent::setUp(); $this->fieldStorageDefinition = array('field_name' => Unicode::strtolower($this->randomMachineName()), 'entity_type' => 'entity_test', 'type' => 'test_field'); $this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition); $this->fieldStorage->save(); $this->fieldDefinition = array('field_name' => $this->fieldStorage->getName(), 'entity_type' => 'entity_test', 'bundle' => 'entity_test'); }
function setUp() { parent::setUp(); $this->fieldStorageDefinition = array('name' => drupal_strtolower($this->randomMachineName()), 'entity_type' => 'entity_test', 'type' => 'test_field'); $this->fieldStorage = entity_create('field_storage_config', $this->fieldStorageDefinition); $this->fieldStorage->save(); $this->instanceDefinition = array('field_name' => $this->fieldStorage->getName(), 'entity_type' => 'entity_test', 'bundle' => 'entity_test'); }
protected function setUp() { parent::setUp(); $this->fieldStorage = FieldStorageConfig::create(array('field_name' => strtolower($this->randomMachineName()), 'entity_type' => 'contact_message', 'type' => 'text')); $this->fieldStorage->save(); ContactForm::create(['id' => 'contact_message', 'label' => 'Test contact form'])->save(); FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'contact_message'])->save(); $this->container->get('views.views_data')->clear(); }
protected function setUp() { parent::setUp(); $this->field_storage = entity_create('field_storage_config', array('name' => strtolower($this->randomMachineName()), 'entity_type' => 'contact_message', 'type' => 'text')); $this->field_storage->save(); entity_create('contact_category', array('id' => 'contact_message', 'label' => 'Test contact category'))->save(); entity_create('field_instance_config', array('field_storage' => $this->field_storage, 'bundle' => 'contact_message'))->save(); $this->container->get('views.views_data')->clear(); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->installSchema('system', array('router')); $this->fieldStorageDefinition = array('name' => $this->fieldName, 'entity_type' => 'entity_test', 'type' => 'list_integer', 'cardinality' => 1, 'settings' => array('allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'))); $this->fieldStorage = entity_create('field_storage_config', $this->fieldStorageDefinition); $this->fieldStorage->save(); $this->instance = entity_create('field_instance_config', array('field_storage' => $this->fieldStorage, 'bundle' => 'entity_test')); $this->instance->save(); entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->fieldName, array('type' => 'options_buttons'))->save(); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->container->get('router.builder')->rebuild(); $this->fieldStorageDefinition = array('field_name' => $this->fieldName, 'entity_type' => 'entity_test', 'type' => 'list_integer', 'cardinality' => 1, 'settings' => array('allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'))); $this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'entity_test']); $this->field->save(); entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->fieldName, array('type' => 'options_buttons'))->save(); }
/** * Tests URI field. */ public function testUriField() { $label = $this->randomMachineName(); // Create a field with settings to validate. $field_name = Unicode::strtolower($this->randomMachineName()); $this->fieldStorage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'uri']); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'label' => $label, 'required' => TRUE, 'settings' => ['size' => 123, 'placeholder' => '']]); $this->field->save(); // Create a form display for the default form mode. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, ['type' => 'uri'])->save(); }
/** * Creates a date test field. */ protected function createField() { $field_name = Unicode::strtolower($this->randomMachineName()); $type = $this->getTestFieldType(); $widget_type = $formatter_type = $type . '_default'; $this->fieldStorage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => $type, 'settings' => ['datetime_type' => DateRangeItem::DATETIME_TYPE_DATE]]); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'required' => TRUE]); $this->field->save(); EntityFormDisplay::load('entity_test.entity_test.default')->setComponent($field_name, ['type' => $widget_type])->save(); $this->displayOptions = ['type' => $formatter_type, 'label' => 'hidden', 'settings' => ['format_type' => 'medium'] + $this->defaultSettings]; EntityViewDisplay::create(['targetEntityType' => $this->field->getTargetEntityTypeId(), 'bundle' => $this->field->getTargetBundle(), 'mode' => 'full', 'status' => TRUE])->setComponent($field_name, $this->displayOptions)->save(); }
function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_rev'); $entity_type = 'entity_test_rev'; $this->field_name = strtolower($this->randomName()); $this->field_cardinality = 4; $this->fieldStorage = entity_create('field_storage_config', array('name' => $this->field_name, 'entity_type' => $entity_type, 'type' => 'test_field', 'cardinality' => $this->field_cardinality)); $this->fieldStorage->save(); $this->instance = entity_create('field_instance_config', array('field_storage' => $this->fieldStorage, 'bundle' => $entity_type)); $this->instance->save(); $this->table = ContentEntityDatabaseStorage::_fieldTableName($this->fieldStorage); $this->revision_table = ContentEntityDatabaseStorage::_fieldRevisionTableName($this->fieldStorage); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); // Create content type with unlimited text field. $this->nodeType = $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); // Create the unlimited text field. $this->fieldName = 'field_views_testing_group_rows'; $this->fieldStorage = FieldStorageConfig::create(['field_name' => $this->fieldName, 'entity_type' => 'node', 'type' => 'text', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED]); $this->fieldStorage->save(); // Create an instance of the text field on the content type. $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => $this->nodeType->id()]); $this->field->save(); $edit = ['title' => $this->randomMachineName(), $this->fieldName => ['a', 'b', 'c']]; $this->drupalCreateNode($edit); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test'); $this->installEntitySchema('user'); $this->installConfig(['system']); $this->installSchema('system', ['sequences', 'key_value']); // Add a datetime range field. $this->fieldStorage = FieldStorageConfig::create(['field_name' => Unicode::strtolower($this->randomMachineName()), 'entity_type' => 'entity_test', 'type' => 'daterange', 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE]]); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'required' => TRUE]); $this->field->save(); $display_options = ['type' => 'daterange_default', 'label' => 'hidden', 'settings' => ['format_type' => 'fallback', 'separator' => 'UNTRANSLATED']]; EntityViewDisplay::create(['targetEntityType' => $this->field->getTargetEntityTypeId(), 'bundle' => $this->field->getTargetBundle(), 'mode' => 'default', 'status' => TRUE])->setComponent($this->fieldStorage->getName(), $display_options)->save(); }
protected function setUp() { parent::setUp(); $this->installConfig(array('language')); $this->field_name = drupal_strtolower($this->randomMachineName()); $this->entity_type = 'entity_test'; $this->field_storage_definition = array('field_name' => $this->field_name, 'entity_type' => $this->entity_type, 'type' => 'test_field', 'cardinality' => 4, 'translatable' => TRUE); $this->fieldStorage = entity_create('field_storage_config', $this->field_storage_definition); $this->fieldStorage->save(); $this->field_definition = array('field_storage' => $this->fieldStorage, 'bundle' => 'entity_test'); $this->field = entity_create('field_config', $this->field_definition); $this->field->save(); for ($i = 0; $i < 3; ++$i) { ConfigurableLanguage::create(array('id' => 'l' . $i, 'label' => $this->randomString()))->save(); } }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installConfig(array('language')); $this->fieldName = Unicode::strtolower($this->randomMachineName()); $this->entityType = 'entity_test'; $this->fieldStorageDefinition = array('field_name' => $this->fieldName, 'entity_type' => $this->entityType, 'type' => 'test_field', 'cardinality' => 4); $this->fieldStorage = FieldStorageConfig::create($this->fieldStorageDefinition); $this->fieldStorage->save(); $this->fieldDefinition = array('field_storage' => $this->fieldStorage, 'bundle' => 'entity_test'); $this->field = FieldConfig::create($this->fieldDefinition); $this->field->save(); for ($i = 0; $i < 3; ++$i) { ConfigurableLanguage::create(array('id' => 'l' . $i, 'label' => $this->randomString()))->save(); } }
function setUp() { parent::setUp(); $this->installConfig(array('language')); $this->field_name = drupal_strtolower($this->randomName()); $this->entity_type = 'entity_test'; $this->field_definition = array('name' => $this->field_name, 'entity_type' => $this->entity_type, 'type' => 'test_field', 'cardinality' => 4, 'translatable' => TRUE); $this->fieldStorage = entity_create('field_storage_config', $this->field_definition); $this->fieldStorage->save(); $this->instance_definition = array('field_storage' => $this->fieldStorage, 'bundle' => 'entity_test'); $this->instance = entity_create('field_instance_config', $this->instance_definition); $this->instance->save(); for ($i = 0; $i < 3; ++$i) { $language = new Language(array('id' => 'l' . $i, 'name' => $this->randomString())); language_save($language); } }
protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_rev'); $entity_type = 'entity_test_rev'; $this->fieldName = strtolower($this->randomMachineName()); $this->fieldCardinality = 4; $this->fieldStorage = FieldStorageConfig::create(array('field_name' => $this->fieldName, 'entity_type' => $entity_type, 'type' => 'test_field', 'cardinality' => $this->fieldCardinality)); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => $entity_type]); $this->field->save(); /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = \Drupal::entityManager()->getStorage($entity_type)->getTableMapping(); $this->tableMapping = $table_mapping; $this->table = $table_mapping->getDedicatedDataTableName($this->fieldStorage); $this->revisionTable = $table_mapping->getDedicatedRevisionTableName($this->fieldStorage); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->drupalLogin($this->drupalCreateUser(array('administer views'))); // Create the text field. $this->fieldName = 'field_test_entity_ref_display'; $this->fieldStorage = FieldStorageConfig::create(['field_name' => $this->fieldName, 'entity_type' => 'entity_test', 'type' => 'text']); $this->fieldStorage->save(); // Create an instance of the text field on the content type. $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'entity_test']); $this->field->save(); // Create some entities to search. Add a common string to the name and // the text field in two entities so we can test that we can search in both. for ($i = 0; $i < 5; $i++) { EntityTest::create(['bundle' => 'entity_test', 'name' => 'name' . $i, $this->fieldName => 'text'])->save(); EntityTest::create(['bundle' => 'entity_test', 'name' => 'name', $this->fieldName => 'text' . $i])->save(); } }
/** * Test that invalid values are caught and marked as invalid. */ function testInvalidField() { // Change the field to a datetime field. $this->fieldStorage->setSetting('datetime_type', 'datetime'); $this->fieldStorage->save(); $field_name = $this->fieldStorage->getName(); // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.'); $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.'); // Submit invalid dates and ensure they is not accepted. $date_value = ''; $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '12:00:00'); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', 'Empty date value has been caught.'); $date_value = 'aaaa-12-01'; $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00'); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', array('%date' => $date_value))); $date_value = '2012-75-01'; $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00'); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', array('%date' => $date_value))); $date_value = '2012-12-99'; $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00'); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', array('%date' => $date_value))); $date_value = '2012-12-01'; $time_value = ''; $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', 'Empty time value has been caught.'); $date_value = '2012-12-01'; $time_value = '49:00:00'; $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', array('%time' => $time_value))); $date_value = '2012-12-01'; $time_value = '12:99:00'; $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', array('%time' => $time_value))); $date_value = '2012-12-01'; $time_value = '12:15:99'; $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', array('%time' => $time_value))); }
/** * Tests the 'options_select' widget (multiple select). */ function testSelectListMultiple() { // Create an instance of the 'multiple values' field. $field = entity_create('field_config', array('field_storage' => $this->card2, 'bundle' => 'entity_test')); $field->save(); entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->card2->getName(), array('type' => 'options_select'))->save(); // Create an entity. $entity = entity_create('entity_test', array('user_id' => 1, 'name' => $this->randomMachineName())); $entity->save(); $entity_init = clone $entity; // Display form: with no field data, nothing is selected. $this->drupalGet('entity_test/manage/' . $entity->id()); $this->assertOptionSelected("edit-card-2", '_none'); $this->assertNoOptionSelected('edit-card-2', 0); $this->assertNoOptionSelected('edit-card-2', 1); $this->assertNoOptionSelected('edit-card-2', 2); $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.'); // Submit form: select first and third options. $edit = array('card_2[]' => array(0 => 0, 2 => 2)); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertFieldValues($entity_init, 'card_2', array(0, 2)); // Display form: check that the right options are selected. $this->drupalGet('entity_test/manage/' . $entity->id()); $this->assertOptionSelected('edit-card-2', 0); $this->assertNoOptionSelected('edit-card-2', 1); $this->assertOptionSelected('edit-card-2', 2); // Submit form: select only first option. $edit = array('card_2[]' => array(0 => 0)); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertFieldValues($entity_init, 'card_2', array(0)); // Display form: check that the right options are selected. $this->drupalGet('entity_test/manage/' . $entity->id()); $this->assertOptionSelected('edit-card-2', 0); $this->assertNoOptionSelected('edit-card-2', 1); $this->assertNoOptionSelected('edit-card-2', 2); // Submit form: select the three options while the field accepts only 2. $edit = array('card_2[]' => array(0 => 0, 1 => 1, 2 => 2)); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.'); // Submit form: uncheck all options. $edit = array('card_2[]' => array()); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertFieldValues($entity_init, 'card_2', array()); // Test the 'None' option. // Check that the 'none' option has no effect if actual options are selected // as well. $edit = array('card_2[]' => array('_none' => '_none', 0 => 0)); $this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save')); $this->assertFieldValues($entity_init, 'card_2', array(0)); // Check that selecting the 'none' option empties the field. $edit = array('card_2[]' => array('_none' => '_none')); $this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save')); $this->assertFieldValues($entity_init, 'card_2', array()); // A required select list does not have an empty key. $field->setRequired(TRUE); $field->save(); $this->drupalGet('entity_test/manage/' . $entity->id()); $this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2')), 'A required select list does not have an empty key.'); // We do not have to test that a required select list with one option is // auto-selected because the browser does it for us. // Test optgroups. // Use a callback function defining optgroups. $this->card2->setSetting('allowed_values', []); $this->card2->setSetting('allowed_values_function', 'options_test_allowed_values_callback'); $this->card2->save(); $field->setRequired(FALSE); $field->save(); // Display form: with no field data, nothing is selected. $this->drupalGet('entity_test/manage/' . $entity->id()); $this->assertNoOptionSelected('edit-card-2', 0); $this->assertNoOptionSelected('edit-card-2', 1); $this->assertNoOptionSelected('edit-card-2', 2); $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.'); $this->assertRaw('More <script>dangerous</script> markup', 'Option group text was properly filtered.'); $this->assertRaw('Group 1', 'Option groups are displayed.'); // Submit form: select first option. $edit = array('card_2[]' => array(0 => 0)); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertFieldValues($entity_init, 'card_2', array(0)); // Display form: check that the right options are selected. $this->drupalGet('entity_test/manage/' . $entity->id()); $this->assertOptionSelected('edit-card-2', 0); $this->assertNoOptionSelected('edit-card-2', 1); $this->assertNoOptionSelected('edit-card-2', 2); // Submit form: Unselect the option. $edit = array('card_2[]' => array('_none' => '_none')); $this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save')); $this->assertFieldValues($entity_init, 'card_2', array()); }
/** * Tests the 'link_separate' formatter. * * This test is mostly the same as testLinkFormatter(), but they cannot be * merged, since they involve different configuration and output. */ function testLinkSeparateFormatter() { $field_name = Unicode::strtolower($this->randomMachineName()); // Create a field with settings to validate. $this->fieldStorage = entity_create('field_storage_config', array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'link', 'cardinality' => 2)); $this->fieldStorage->save(); entity_create('field_config', array('field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'settings' => array('title' => DRUPAL_OPTIONAL, 'link_type' => LinkItemInterface::LINK_GENERIC)))->save(); $display_options = array('type' => 'link_separate', 'label' => 'hidden'); entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => 'link_default'))->save(); entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name, $display_options)->save(); // Create an entity with two link field values: // - The first field item uses a URL only. // - The second field item uses a URL and link text. // For consistency in assertion code below, the URL is assigned to the title // variable for the first field. $this->drupalGet('entity_test/add'); $url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com'; $url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org'; // Intentionally contains an ampersand that needs sanitization on output. $title2 = 'A very long & strange example title that could break the nice layout of the site'; $edit = array("{$field_name}[0][uri]" => $url1, "{$field_name}[1][uri]" => $url2, "{$field_name}[1][title]" => $title2); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); // Verify that the link is output according to the formatter settings. $options = array('trim_length' => array(NULL, 6), 'rel' => array(NULL, 'nofollow'), 'target' => array(NULL, '_blank')); foreach ($options as $setting => $values) { foreach ($values as $new_value) { // Update the field formatter settings. $display_options['settings'] = array($setting => $new_value); entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name, $display_options)->save(); $this->renderTestEntity($id); switch ($setting) { case 'trim_length': $url = $url1; $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url; $expected = '<div class="link-item">'; $expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>'; $expected .= '</div>'; $this->assertRaw($expected); $url = $url2; $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url; $title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2; $expected = '<div class="link-item">'; $expected .= '<div class="link-title">' . Html::escape($title) . '</div>'; $expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>'; $expected .= '</div>'; $this->assertRaw($expected); break; case 'rel': $rel = isset($new_value) ? ' rel="' . $new_value . '"' : ''; $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($url1) . '</a></div>'); $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($url2) . '</a></div>'); break; case 'target': $target = isset($new_value) ? ' target="' . $new_value . '"' : ''; $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($url1) . '</a></div>'); $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($url2) . '</a></div>'); break; } } } }
/** * Saves storage settings. */ protected function saveFieldStorageSettings(array $settings) { $this->fieldStorage->setSettings(NestedArray::mergeDeep($this->fieldStorage->getSettings(), $settings)); $this->fieldStorage->save(); }