Ejemplo n.º 1
0
 /**
  * Tests email field.
  */
 function testEmailField()
 {
     // Create a field with settings to validate.
     $field_name = drupal_strtolower($this->randomMachineName());
     $this->fieldStorage = entity_create('field_storage_config', array('name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'email'));
     $this->fieldStorage->save();
     $this->instance = entity_create('field_instance_config', array('field_storage' => $this->fieldStorage, 'bundle' => 'entity_test'));
     $this->instance->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('user_id' => 1, 'name' => $this->randomMachineName(), "{$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 = entity_load('entity_test', $id);
     $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
     $content = $display->build($entity);
     $this->drupalSetContent(drupal_render($content));
     $this->assertLinkByHref('mailto:test@example.com');
 }
Ejemplo n.º 2
0
 /**
  * Tests boolean field.
  */
 function testBooleanField()
 {
     $on = $this->randomName();
     $off = $this->randomName();
     $label = $this->randomName();
     // Create a field with settings to validate.
     $field_name = drupal_strtolower($this->randomName());
     $this->field = FieldStorageConfig::create(array('name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'boolean', 'settings' => array('on_label' => $on, 'off_label' => $off)));
     $this->field->save();
     $this->instance = FieldInstanceConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'label' => $label, 'required' => TRUE));
     $this->instance->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('user_id' => 1, 'name' => $this->randomName(), "{$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->instance->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-settings-on-label', $on);
     $this->assertFieldById('edit-field-settings-off-label', $off);
 }
Ejemplo n.º 3
0
 /**
  * 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');
 }
 /**
  * {@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();
 }
Ejemplo n.º 5
0
 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);
 }
Ejemplo n.º 6
0
 function setUp()
 {
     parent::setUp();
     $web_user = $this->drupalCreateUser(array('access content', 'view test entity', 'administer entity_test content', 'administer content types', 'administer node fields'));
     $this->drupalLogin($web_user);
     // Create a field with settings to validate.
     $this->fieldStorage = entity_create('field_storage_config', array('name' => drupal_strtolower($this->randomMachineName()), 'entity_type' => 'entity_test', 'type' => 'datetime', 'settings' => array('datetime_type' => 'date')));
     $this->fieldStorage->save();
     $this->instance = entity_create('field_instance_config', array('field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'required' => TRUE));
     $this->instance->save();
     entity_get_form_display($this->instance->entity_type, $this->instance->bundle, 'default')->setComponent($this->fieldStorage->name, array('type' => 'datetime_default'))->save();
     $this->display_options = array('type' => 'datetime_default', 'label' => 'hidden', 'settings' => array('format_type' => 'medium'));
     entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')->setComponent($this->fieldStorage->name, $this->display_options)->save();
 }
Ejemplo n.º 7
0
 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);
     }
 }
 /**
  * Creates a text_with_summary field and field instance.
  *
  * @param string $entity_type
  *   Entity type for which the field should be created.
  */
 protected function createField($entity_type)
 {
     // Create a field .
     $this->fieldStorage = entity_create('field_storage_config', array('name' => 'summary_field', 'entity_type' => $entity_type, 'type' => 'text_with_summary', 'settings' => array('max_length' => 10)));
     $this->fieldStorage->save();
     $this->instance = entity_create('field_instance_config', array('field_storage' => $this->fieldStorage, 'bundle' => $entity_type, 'settings' => array('text_processing' => 0)));
     $this->instance->save();
 }
Ejemplo n.º 9
0
 /**
  * Test insert with empty or NULL fields, with default value.
  */
 function testFieldAttachSaveEmptyDataDefaultValue()
 {
     $entity_type = 'entity_test_rev';
     $this->createFieldWithInstance('', $entity_type);
     // Add a default value function.
     $this->instance->default_value_function = 'field_test_default_value';
     $this->instance->save();
     // Verify that fields are populated with default values.
     $entity_init = entity_create($entity_type, array('id' => 1, 'revision_id' => 1));
     $default = field_test_default_value($entity_init, $this->instance);
     $this->assertEqual($entity_init->{$this->field_name}->getValue(), $default, 'Default field value correctly populated.');
     // Insert: Field is NULL.
     $entity = clone $entity_init;
     $entity->{$this->field_name} = NULL;
     $entity->enforceIsNew();
     $entity = $this->entitySaveReload($entity);
     $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'Insert: NULL field results in no value saved');
     // Verify that prepopulated field values are not overwritten by defaults.
     $value = array(array('value' => $default[0]['value'] - mt_rand(1, 127)));
     $entity = entity_create($entity_type, array('type' => $entity_init->bundle(), $this->field_name => $value));
     $this->assertEqual($entity->{$this->field_name}->getValue(), $value, 'Prepopulated field value correctly maintained.');
 }
Ejemplo n.º 10
0
 /**
  * Tests the link title settings of a link field.
  */
 function testLinkTitle()
 {
     $field_name = drupal_strtolower($this->randomMachineName());
     // Create a field with settings to validate.
     $this->fieldStorage = entity_create('field_storage_config', array('name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'link'));
     $this->fieldStorage->save();
     $this->instance = entity_create('field_instance_config', array('field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'label' => 'Read more about this entity', 'settings' => array('title' => DRUPAL_OPTIONAL, 'link_type' => LinkItemInterface::LINK_GENERIC)));
     $this->instance->save();
     entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => 'link_default', 'settings' => array('placeholder_url' => 'http://example.com', 'placeholder_title' => 'Enter the text for this link')))->save();
     entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name, array('type' => 'link', 'label' => 'hidden'))->save();
     // Verify that the link text field works according to the field setting.
     foreach (array(DRUPAL_DISABLED, DRUPAL_REQUIRED, DRUPAL_OPTIONAL) as $title_setting) {
         // Update the link title field setting.
         $this->instance->settings['title'] = $title_setting;
         $this->instance->save();
         // Display creation form.
         $this->drupalGet('entity_test/add');
         // Assert label is shown.
         $this->assertText('Read more about this entity');
         $this->assertFieldByName("{$field_name}[0][url]", '', 'URL field found.');
         $this->assertRaw('placeholder="http://example.com"');
         if ($title_setting === DRUPAL_DISABLED) {
             $this->assertNoFieldByName("{$field_name}[0][title]", '', 'Link text field not found.');
             $this->assertNoRaw('placeholder="Enter the text for this link"');
         } else {
             $this->assertRaw('placeholder="Enter the text for this link"');
             $this->assertFieldByName("{$field_name}[0][title]", '', 'Link text field found.');
             if ($title_setting === DRUPAL_REQUIRED) {
                 // Verify that the link text is required, if the URL is non-empty.
                 $edit = array("{$field_name}[0][url]" => 'http://www.example.com');
                 $this->drupalPostForm(NULL, $edit, t('Save'));
                 $this->assertText(t('!name field is required.', array('!name' => t('Link text'))));
                 // Verify that the link text is not required, if the URL is empty.
                 $edit = array("{$field_name}[0][url]" => '');
                 $this->drupalPostForm(NULL, $edit, t('Save'));
                 $this->assertNoText(t('!name field is required.', array('!name' => t('Link text'))));
                 // Verify that a URL and link text meets requirements.
                 $this->drupalGet('entity_test/add');
                 $edit = array("{$field_name}[0][url]" => 'http://www.example.com', "{$field_name}[0][title]" => 'Example');
                 $this->drupalPostForm(NULL, $edit, t('Save'));
                 $this->assertNoText(t('!name field is required.', array('!name' => t('Link text'))));
             }
         }
     }
     // Verify that a link without link text is rendered using the URL as text.
     $value = 'http://www.example.com/';
     $edit = array('user_id' => 1, 'name' => $this->randomMachineName(), "{$field_name}[0][url]" => $value, "{$field_name}[0][title]" => '');
     $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->renderTestEntity($id);
     $expected_link = l($value, $value);
     $this->assertRaw($expected_link);
     // Verify that a link with text is rendered using the link text.
     $title = $this->randomMachineName();
     $edit = array('user_id' => 1, 'name' => $this->randomMachineName(), "{$field_name}[0][title]" => $title);
     $this->drupalPostForm("entity_test/manage/{$id}", $edit, t('Save'));
     $this->assertText(t('entity_test @id has been updated.', array('@id' => $id)));
     $this->renderTestEntity($id);
     $expected_link = l($title, $value);
     $this->assertRaw($expected_link);
 }