/** * 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 the views data generation. */ public function testViewsData() { // Test that the field is not exposed to views, since contact_message // entities have no storage. $table_name = 'contact_message__' . $this->fieldStorage->getName(); $data = $this->container->get('views.views_data')->get($table_name); $this->assertFalse($data, 'The field is not exposed to Views.'); }
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'); }
/** * Check if the field translation attached to the entity revision identified * by the passed arguments were correctly stored. */ private function checkTranslationRevisions($id, $revision_id, $available_langcodes) { $field_name = $this->fieldStorage->getName(); $entity = entity_revision_load($this->entityTypeId, $revision_id); foreach ($available_langcodes as $langcode => $value) { $passed = $entity->getTranslation($langcode)->{$field_name}->value == $value + 1; $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', array('@language' => $langcode, '@revision' => $entity->getRevisionId()))); } }
/** * Tests configurable field validation. * * @see field_test_entity_bundle_field_info_alter() */ protected function doFieldValidationTests() { $entity = EntityTest::create(); $entity->set($this->fieldStorage->getName(), 1); $violations = $entity->validate(); $this->assertEqual(count($violations), 0, 'No violations found when in-range value passed.'); $entity->set($this->fieldStorage->getName(), 33); $violations = $entity->validate(); $this->assertEqual(count($violations), 1, 'Violations found when using value outside the range.'); $this->assertEqual($violations[0]->getPropertyPath(), $this->fieldStorage->getName() . '.0.value'); $this->assertEqual($violations[0]->getMessage(), t('This value should be %limit or less.', ['%limit' => 32])); }
/** * Tests the translation of the range separator. */ public function testSeparatorTranslation() { // Create an entity. $entity = EntityTest::create(['name' => $this->randomString(), $this->fieldStorage->getName() => ['value' => '2016-09-20', 'end_value' => '2016-09-21']]); // Verify the untranslated separator. $display = EntityViewDisplay::collectRenderDisplay($entity, 'default'); $build = $display->build($entity); $output = $this->container->get('renderer')->renderRoot($build); $this->verbose($output); $this->assertContains('UNTRANSLATED', (string) $output); // Translate the separator. ConfigurableLanguage::createFromLangcode('nl')->save(); /** @var \Drupal\language\ConfigurableLanguageManagerInterface $language_manager */ $language_manager = $this->container->get('language_manager'); $language_manager->getLanguageConfigOverride('nl', 'core.entity_view_display.entity_test.entity_test.default')->set('content.' . $this->fieldStorage->getName() . '.settings.separator', 'NL_TRANSLATED!')->save(); $this->container->get('language.config_factory_override')->setLanguage(new Language(['id' => 'nl'])); $this->container->get('cache_tags.invalidator')->invalidateTags($entity->getCacheTags()); $display = EntityViewDisplay::collectRenderDisplay($entity, 'default'); $build = $display->build($entity); $output = $this->container->get('renderer')->renderRoot($build); $this->verbose($output); $this->assertContains('NL_TRANSLATED!', (string) $output); }
/** * 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 reacting to a bundle being renamed. */ function testFieldSqlStorageBundleRename() { $entity_type = $bundle = 'entity_test_rev'; $field_name = $this->fieldStorage->getName(); // Create an entity. $value = mt_rand(1, 127); $entity = entity_create($entity_type, array('type' => $bundle, $field_name => $value)); $entity->save(); // Rename the bundle. $bundle_new = $bundle . '_renamed'; entity_test_rename_bundle($bundle, $bundle_new, $entity_type); // Check that the 'bundle' column has been updated in storage. $row = db_select($this->table, 't')->fields('t', array('bundle', $field_name . '_value'))->condition('entity_id', $entity->id())->execute()->fetch(); $this->assertEqual($row->bundle, $bundle_new); $this->assertEqual($row->{$field_name . '_value'}, $value); }
/** * Tests the 'options_select' and 'options_button' widget for empty value. */ function testEmptyValue() { // Create an instance of the 'single value' field. $field = entity_create('field_config', ['field_storage' => $this->card1, 'bundle' => 'entity_test']); $field->save(); // Change it to the check boxes/radio buttons widget. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->card1->getName(), ['type' => 'options_buttons'])->save(); // Create an entity. $entity = entity_create('entity_test', ['user_id' => 1, 'name' => $this->randomMachineName()]); $entity->save(); // Display form: check that _none options are present and has label. $this->drupalGet('entity_test/manage/' . $entity->id()); $this->assertTrue($this->xpath('//div[@id=:id]//input[@value=:value]', array(':id' => 'edit-card-1', ':value' => '_none')), 'A test radio button has a "None" choice.'); $this->assertTrue($this->xpath('//div[@id=:id]//label[@for=:for and text()=:label]', array(':id' => 'edit-card-1', ':for' => 'edit-card-1-none', ':label' => 'N/A')), 'A test radio button has a "N/A" choice.'); // Change it to the select widget. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->card1->getName(), array('type' => 'options_select'))->save(); // Display form: check that _none options are present and has label. $this->drupalGet('entity_test/manage/' . $entity->id()); // A required field without any value has a "none" option. $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1', ':label' => t('- None -'))), 'A test select has a "None" choice.'); }
/** * 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->card_2, 'bundle' => 'entity_test')); $field->save(); entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->card_2->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->required = 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->card_2->settings['allowed_values'] = array(); $this->card_2->settings['allowed_values_function'] = 'options_test_allowed_values_callback'; $this->card_2->save(); $field->required = 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()); }
/** * XXXXX. * @param \Drupal\field\Entity\FieldStorageConfig $field_storage. * @return array * xxx */ protected function buildRow(FieldStorageConfig $field_storage) { $row = []; if ($field_storage->isLocked()) { $row[0]['class'] = array('menu-disabled'); $row[0]['data']['id'] = $this->t('@field_name (Locked)', array('@field_name' => $field_storage->getName())); } else { $row[0]['data'] = $field_storage->getName(); } $row[1]['data'] = $field_storage->getType(); $row[2]['data'] = $field_storage->getTargetEntityTypeId(); $row[3]['data'] = implode(",", $field_storage->getBundles()); $default_type = $this->fieldPermissions->fieldGetPermissionType($field_storage); if ($default_type == FIELD_PERMISSIONS_PUBLIC) { $row[4]['data'] = t("Public field (author and administrators can edit, everyone can view)"); $row[4]['colspan'] = 5; } elseif ($default_type == FIELD_PERMISSIONS_PRIVATE) { $row[4]['data'] = t("Private field (only author and administrators can edit and view)"); $row[4]['colspan'] = 5; } elseif ($default_type == FIELD_PERMISSIONS_CUSTOM) { $row[4]['data'] = t("Custom field Permission ()"); $row[4]['colspan'] = 5; } return $row; }