/**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('user');
     $this->installEntitySchema('entity_test');
     ViewTestData::createTestViews(get_class($this), array('entity_reference_test_views'));
     $field_storage = FieldStorageConfig::create(array('entity_type' => 'entity_test', 'field_name' => 'field_test', 'type' => 'entity_reference', 'settings' => array('target_type' => 'entity_test'), 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED));
     $field_storage->save();
     $field = FieldConfig::create(array('entity_type' => 'entity_test', 'field_name' => 'field_test', 'bundle' => 'entity_test', 'settings' => array('handler' => 'default', 'handler_settings' => array())));
     $field->save();
     // Create some test entities which link each other.
     $entity_storage = \Drupal::entityManager()->getStorage('entity_test');
     $referenced_entity = $entity_storage->create(array());
     $referenced_entity->save();
     $this->entities[$referenced_entity->id()] = $referenced_entity;
     $entity = $entity_storage->create(array());
     $entity->field_test->target_id = $referenced_entity->id();
     $entity->save();
     $this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id());
     $this->entities[$entity->id()] = $entity;
     $entity = $entity_storage->create(array());
     $entity->field_test->target_id = $referenced_entity->id();
     $entity->save();
     $this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id());
     $this->entities[$entity->id()] = $entity;
     Views::viewsData()->clear();
 }
 protected function setUp()
 {
     parent::setUp();
     // Let there be T-rex.
     \Drupal::state()->set('editor_test_give_me_a_trex_thanks', TRUE);
     \Drupal::service('plugin.manager.editor')->clearCachedDefinitions();
     // Add text formats.
     $filtered_html_format = entity_create('filter_format', array('format' => 'filtered_html', 'name' => 'Filtered HTML', 'weight' => 0, 'filters' => array()));
     $filtered_html_format->save();
     $full_html_format = entity_create('filter_format', array('format' => 'full_html', 'name' => 'Full HTML', 'weight' => 1, 'filters' => array()));
     $full_html_format->save();
     // Create article node type.
     $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
     // Create page node type, but remove the body.
     $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Page'));
     $body = FieldConfig::loadByName('node', 'page', 'body');
     $body->delete();
     // Create a formatted text field, which uses an <input type="text">.
     FieldStorageConfig::create(array('field_name' => 'field_text', 'entity_type' => 'node', 'type' => 'text'))->save();
     FieldConfig::create(array('field_name' => 'field_text', 'entity_type' => 'node', 'label' => 'Textfield', 'bundle' => 'page'))->save();
     entity_get_form_display('node', 'page', 'default')->setComponent('field_text')->save();
     // Create 3 users, each with access to different text formats.
     $this->untrustedUser = $this->drupalCreateUser(array('create article content', 'edit any article content'));
     $this->normalUser = $this->drupalCreateUser(array('create article content', 'edit any article content', 'use text format filtered_html'));
     $this->privilegedUser = $this->drupalCreateUser(array('create article content', 'edit any article content', 'create page content', 'edit any page content', 'use text format filtered_html', 'use text format full_html'));
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->installConfig(['system']);
     $this->installConfig(['field']);
     $this->installConfig(['text']);
     $this->installConfig(['address']);
     $this->installEntitySchema('entity_test');
     ConfigurableLanguage::createFromLangcode('zh-hant')->save();
     // The address module is never installed, so the importer doesn't run
     // automatically. Instead, we manually import the address formats we need.
     $country_codes = ['AD', 'SV', 'TW', 'US', 'ZZ'];
     $importer = \Drupal::service('address.address_format_importer');
     $importer->importEntities($country_codes);
     $importer->importTranslations(['zh-hant']);
     $this->entityType = 'entity_test';
     $this->bundle = $this->entityType;
     $this->fieldName = Unicode::strtolower($this->randomMachineName());
     $field_storage = FieldStorageConfig::create(['field_name' => $this->fieldName, 'entity_type' => $this->entityType, 'type' => 'address']);
     $field_storage->save();
     $field = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $this->bundle, 'label' => $this->randomMachineName()]);
     $field->save();
     $values = ['targetEntityType' => $this->entityType, 'bundle' => $this->bundle, 'mode' => 'default', 'status' => TRUE];
     $this->display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->create($values);
     $this->display->setComponent($this->fieldName, ['type' => 'address_default', 'settings' => []]);
     $this->display->save();
 }
示例#4
0
 protected function setUp()
 {
     parent::setUp();
     // Create a telephone field storage and field for validation.
     FieldStorageConfig::create(array('field_name' => 'field_test', 'entity_type' => 'entity_test', 'type' => 'telephone'))->save();
     FieldConfig::create(['entity_type' => 'entity_test', 'field_name' => 'field_test', 'bundle' => 'entity_test'])->save();
 }
 public function testDefaultValueCallbackForm()
 {
     // Create a field and storage for checking.
     /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
     FieldStorageConfig::create(['field_name' => $this->fieldName, 'entity_type' => 'node', 'type' => 'text'])->save();
     /** @var \Drupal\field\Entity\FieldConfig $field_config */
     $field_config = FieldConfig::create(['entity_type' => 'node', 'field_name' => $this->fieldName, 'bundle' => 'article']);
     $field_config->save();
     $this->drupalLogin($this->rootUser);
     // Check that the default field form is visible when no callback is set.
     $this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test');
     $this->assertFieldByName('default_value_input[field_test][0][value]', NULL, 'The default field form is visible.');
     // Set a different field value, it should be on the field.
     $default_value = $this->randomString();
     $field_config->setDefaultValue([['value' => $default_value]])->save();
     $this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test');
     $this->assertFieldByName('default_value_input[field_test][0][value]', $default_value, 'The default field form is visible.');
     // Set a different field value to the field directly, instead of an array.
     $default_value = $this->randomString();
     $field_config->setDefaultValue($default_value)->save();
     $this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test');
     $this->assertFieldByName('default_value_input[field_test][0][value]', $default_value, 'The default field form is visible.');
     // Set a default value callback instead, and the default field form should
     // not be visible.
     $field_config->setDefaultValueCallback('\\Drupal\\field\\Tests\\FieldDefaultValueCallbackProvider::calculateDefaultValue')->save();
     $this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test');
     $this->assertNoFieldByName('default_value_input[field_test][0][value]', 'Calculated default value', 'The default field form is not visible when a callback is defined.');
 }
示例#6
0
 /**
  * Creates a name field with default settings.
  *
  * @param $field_name
  * @param $entity_type
  * @param $bundle
  *
  * @return \Drupal\Core\Field\FieldDefinitionInterface
  */
 public function createNameField($field_name, $entity_type, $bundle)
 {
     FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => $entity_type, 'type' => 'name'))->save();
     $field_config = FieldConfig::create(array('field_name' => $field_name, 'entity_type' => $entity_type, 'type' => 'name', 'bundle' => $bundle));
     $field_config->save();
     return $field_config;
 }
 /**
  * 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);
 }
 protected function setUp()
 {
     parent::setUp();
     $this->entityStorage = $this->entityManager->getStorage('entity_test');
     $this->factory = $this->container->get('entity.query');
     // Add some fieldapi fields to be used in the test.
     for ($i = 1; $i <= 2; $i++) {
         $field_name = 'field_test_' . $i;
         FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'integer', 'cardinality' => 2))->save();
         FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test'])->save();
     }
     $entity = $this->entityStorage->create(array('id' => 1, 'user_id' => 1, 'field_test_1' => 1, 'field_test_2' => 2));
     $entity->enforceIsNew();
     $entity->save();
     $entity = $this->entityStorage->create(array('id' => 2, 'user_id' => 2, 'field_test_1' => 1, 'field_test_2' => 7));
     $entity->enforceIsNew();
     $entity->save();
     $entity = $this->entityStorage->create(array('id' => 3, 'user_id' => 2, 'field_test_1' => 2, 'field_test_2' => 1));
     $entity->enforceIsNew();
     $entity->save();
     $entity = $this->entityStorage->create(array('id' => 4, 'user_id' => 2, 'field_test_1' => 2, 'field_test_2' => 8));
     $entity->enforceIsNew();
     $entity->save();
     $entity = $this->entityStorage->create(array('id' => 5, 'user_id' => 3, 'field_test_1' => 2, 'field_test_2' => 2));
     $entity->enforceIsNew();
     $entity->save();
     $entity = $this->entityStorage->create(array('id' => 6, 'user_id' => 3, 'field_test_1' => 3, 'field_test_2' => 8));
     $entity->enforceIsNew();
     $entity->save();
 }
示例#9
0
 public function createField($id, $field)
 {
     $field_storage = FieldStorageConfig::create(['field_name' => $field['field_name'], 'entity_type' => 'contact_message', 'type' => $field['field_type'], 'cardinality' => 1, 'custom_storage' => FALSE]);
     $field_storage->save();
     $field_instance = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $id, 'label' => $field['field_name']]);
     $field_instance->save();
 }
示例#10
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     FilterFormat::create(array('format' => 'my_text_format', 'name' => 'My text format', 'filters' => array('filter_autop' => array('module' => 'filter', 'status' => TRUE))))->save();
     FieldStorageConfig::create(array('field_name' => 'formatted_text', 'entity_type' => $this->entityType, 'type' => 'text', 'settings' => array()))->save();
     FieldConfig::create(['entity_type' => $this->entityType, 'bundle' => $this->bundle, 'field_name' => 'formatted_text', 'label' => 'Filtered text'])->save();
 }
示例#11
0
 /**
  * Tests that a form's action is retained after an Ajax submission.
  *
  * The 'action' attribute of a form should not change after an Ajax submission
  * followed by a non-Ajax submission, which triggers a validation error.
  */
 function testPreserveFormActionAfterAJAX()
 {
     // Create a multi-valued field for 'page' nodes to use for Ajax testing.
     $field_name = 'field_ajax_test';
     FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'node', 'type' => 'text', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED))->save();
     FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'node', 'bundle' => 'page'])->save();
     entity_get_form_display('node', 'page', 'default')->setComponent($field_name, array('type' => 'text_textfield'))->save();
     // Log in a user who can create 'page' nodes.
     $this->webUser = $this->drupalCreateUser(array('create page content'));
     $this->drupalLogin($this->webUser);
     // Get the form for adding a 'page' node. Submit an "add another item" Ajax
     // submission and verify it worked by ensuring the updated page has two text
     // field items in the field for which we just added an item.
     $this->drupalGet('node/add/page');
     $this->drupalPostAjaxForm(NULL, array(), array('field_ajax_test_add_more' => t('Add another item')), NULL, array(), array(), 'node-page-form');
     $this->assert(count($this->xpath('//div[contains(@class, "field--name-field-ajax-test")]//input[@type="text"]')) == 2, 'AJAX submission succeeded.');
     // Submit the form with the non-Ajax "Save" button, leaving the title field
     // blank to trigger a validation error, and ensure that a validation error
     // occurred, because this test is for testing what happens when a form is
     // re-rendered without being re-built, which is what happens when there's
     // a validation error.
     $this->drupalPostForm(NULL, array(), t('Save'));
     $this->assertText('Title field is required.', 'Non-AJAX submission correctly triggered a validation error.');
     // Ensure that the form contains two items in the multi-valued field, so we
     // know we're testing a form that was correctly retrieved from cache.
     $this->assert(count($this->xpath('//form[contains(@id, "node-page-form")]//div[contains(@class, "js-form-item-field-ajax-test")]//input[@type="text"]')) == 2, 'Form retained its state from cache.');
     // Ensure that the form's action is correct.
     $forms = $this->xpath('//form[contains(@class, "node-page-form")]');
     $this->assertEqual(1, count($forms));
     // Strip query params off the action before asserting.
     $url = parse_url($forms[0]['action'])['path'];
     $this->assertEqual(Url::fromRoute('node.add', ['node_type' => 'page'])->toString(), $url);
 }
示例#12
0
 function setUpFields($bundle = 'page')
 {
     foreach ($this->fieldStorages as $key => $field_storage) {
         $this->fields[$key] = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $bundle]);
         $this->fields[$key]->save();
     }
 }
示例#13
0
 protected function setUp()
 {
     parent::setUp();
     $this->addDefaultCommentField('node', 'page');
     $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
     $this->drupalLogin($web_user);
     // Add a vocabulary so we can test different view modes.
     $vocabulary = entity_create('taxonomy_vocabulary', array('name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => $this->randomMachineName(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'help' => ''));
     $vocabulary->save();
     $this->vocabulary = $vocabulary;
     // Add a term to the vocabulary.
     $term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $term->save();
     $this->term = $term;
     // Create an image field.
     FieldStorageConfig::create(['field_name' => 'field_image', 'entity_type' => 'node', 'type' => 'image', 'settings' => [], 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED])->save();
     $field_config = FieldConfig::create(['field_name' => 'field_image', 'label' => 'Images', 'entity_type' => 'node', 'bundle' => 'page', 'required' => FALSE, 'settings' => []]);
     $field_config->save();
     // Create a field.
     $this->fieldName = Unicode::strtolower($this->randomMachineName());
     $handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE);
     $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
     entity_get_form_display('node', 'page', 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_autocomplete_tags'))->save();
     // Show on default display and teaser.
     entity_get_display('node', 'page', 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_label'))->save();
     entity_get_display('node', 'page', 'teaser')->setComponent($this->fieldName, array('type' => 'entity_reference_label'))->save();
     entity_get_form_display('node', 'page', 'default')->setComponent('field_image', array('type' => 'image_image', 'settings' => []))->save();
     entity_get_display('node', 'page', 'default')->setComponent('field_image')->save();
 }
 protected function setUp()
 {
     parent::setUp();
     // Use Classy theme for testing markup output.
     \Drupal::service('theme_handler')->install(['classy']);
     \Drupal::service('theme_handler')->setDefault('classy');
     // Grant the 'view test entity' permission.
     $this->installConfig(array('user'));
     Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('view test entity')->save();
     // The label formatter rendering generates links, so build the router.
     $this->container->get('router.builder')->rebuild();
     $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, 'default', array(), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
     // Set up a field, so that the entity that'll be referenced bubbles up a
     // cache tag when rendering it entirely.
     FieldStorageConfig::create(array('field_name' => 'body', 'entity_type' => $this->entityType, 'type' => 'text', 'settings' => array()))->save();
     FieldConfig::create(['entity_type' => $this->entityType, 'bundle' => $this->bundle, 'field_name' => 'body', 'label' => 'Body'])->save();
     entity_get_display($this->entityType, $this->bundle, 'default')->setComponent('body', array('type' => 'text_default', 'settings' => array()))->save();
     FilterFormat::create(array('format' => 'full_html', 'name' => 'Full HTML'))->save();
     // Create the entity to be referenced.
     $this->referencedEntity = $this->container->get('entity_type.manager')->getStorage($this->entityType)->create(array('name' => $this->randomMachineName()));
     $this->referencedEntity->body = array('value' => '<p>Hello, world!</p>', 'format' => 'full_html');
     $this->referencedEntity->save();
     // Create another entity to be referenced but do not save it.
     $this->unsavedReferencedEntity = $this->container->get('entity_type.manager')->getStorage($this->entityType)->create(array('name' => $this->randomMachineName()));
     $this->unsavedReferencedEntity->body = array('value' => '<p>Hello, unsaved world!</p>', 'format' => 'full_html');
 }
 /**
  * Creates the test image field.
  */
 protected function setupTestFields()
 {
     $this->fieldName = 'field_test_et_ui_image';
     $this->cardinality = 3;
     FieldStorageConfig::create(array('field_name' => $this->fieldName, 'entity_type' => $this->entityTypeId, 'type' => 'image', 'cardinality' => $this->cardinality))->save();
     FieldConfig::create(['entity_type' => $this->entityTypeId, 'field_name' => $this->fieldName, 'bundle' => $this->entityTypeId, 'label' => 'Test translatable image field', 'third_party_settings' => array('content_translation' => array('translation_sync' => array('file' => FALSE, 'alt' => 'alt', 'title' => 'title')))])->save();
 }
示例#16
0
 protected function setUp()
 {
     parent::setUp();
     // Create a 'test_field' field and storage for validation.
     FieldStorageConfig::create(array('field_name' => $this->fieldName, 'entity_type' => 'entity_test', 'type' => 'test_field'))->save();
     FieldConfig::create(['entity_type' => 'entity_test', 'field_name' => $this->fieldName, 'bundle' => 'entity_test'])->save();
 }
示例#17
0
 public function testUserHooks()
 {
     FieldStorageConfig::create(array('field_name' => 'field_text', 'type' => 'string', 'entity_type' => 'user'))->save();
     FieldConfig::create(array('field_name' => 'field_text', 'type' => 'string', 'entity_type' => 'user', 'bundle' => 'user'))->save();
     $this->assertIdentical('', \Drupal::config('name.settings')->get('user_preferred'));
     FieldStorageConfig::create(array('field_name' => 'field_name_test', 'type' => 'name', 'entity_type' => 'user'))->save();
     FieldStorageConfig::create(array('field_name' => 'field_name_test2', 'type' => 'name', 'entity_type' => 'user'))->save();
     $field = FieldConfig::create(array('field_name' => 'field_name_test', 'type' => 'name', 'entity_type' => 'user', 'bundle' => 'user'));
     $field->save();
     $field2 = FieldConfig::create(array('field_name' => 'field_name_test2', 'type' => 'name', 'entity_type' => 'user', 'bundle' => 'user'));
     $field2->save();
     $this->assertEqual($field->getName(), \Drupal::config('name.settings')->get('user_preferred'));
     \Drupal::configFactory()->getEditable('name.settings')->set('user_preferred', $field2->getName())->save();
     $field2->delete();
     $this->assertEqual('', \Drupal::config('name.settings')->get('user_preferred'));
     \Drupal::configFactory()->getEditable('name.settings')->set('user_preferred', $field->getName())->save();
     $account = User::create(array('name' => 'test'));
     $account->field_name_test[0] = array('given' => 'Max', 'family' => 'Mustermann');
     $account->save();
     $account = User::load($account->id());
     $this->assertEqual('Max Mustermann', $account->realname);
     $this->assertEqual('Max Mustermann', user_format_name($account));
     $this->assertEqual('test', $account->getUsername());
     $this->assertEqual('Max Mustermann', $account->getDisplayName());
 }
示例#18
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->drupalPlaceBlock('local_tasks_block');
     $this->drupalPlaceBlock('local_actions_block');
     $this->drupalPlaceBlock('page_title_block');
     $this->type = $this->createProfileType('test', 'Test profile', TRUE);
     $id = $this->type->id();
     $field_storage = FieldStorageConfig::create(['field_name' => 'profile_fullname', 'entity_type' => 'profile', 'type' => 'text']);
     $field_storage->save();
     $this->field = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $this->type->id(), 'label' => 'Full name']);
     $this->field->save();
     // Configure the default display.
     $this->display = EntityViewDisplay::load("profile.{$this->type->id()}.default");
     if (!$this->display) {
         $this->display = EntityViewDisplay::create(['targetEntityType' => 'profile', 'bundle' => $this->type->id(), 'mode' => 'default', 'status' => TRUE]);
         $this->display->save();
     }
     $this->display->setComponent($this->field->getName(), ['type' => 'string'])->save();
     // Configure rhe default form.
     $this->form = EntityFormDisplay::load("profile.{$this->type->id()}.default");
     if (!$this->form) {
         $this->form = EntityFormDisplay::create(['targetEntityType' => 'profile', 'bundle' => $this->type->id(), 'mode' => 'default', 'status' => TRUE]);
         $this->form->save();
     }
     $this->form->setComponent($this->field->getName(), ['type' => 'string_textfield'])->save();
     $this->checkPermissions(['administer profile types', "view own {$id} profile", "view any {$id} profile", "add own {$id} profile", "add any {$id} profile", "edit own {$id} profile", "edit any {$id} profile", "delete own {$id} profile", "delete any {$id} profile"]);
     user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['access user profiles']);
     $this->adminUser = $this->drupalCreateUser(['administer profile types', "view any {$id} profile", "add any {$id} profile", "edit any {$id} profile", "delete any {$id} profile"]);
 }
 /**
  * Assert sorting by field and property.
  */
 public function testSort()
 {
     // Add text field to entity, to sort by.
     FieldStorageConfig::create(array('field_name' => 'field_text', 'entity_type' => 'node', 'type' => 'text', 'entity_types' => array('node')))->save();
     FieldConfig::create(['label' => 'Text Field', 'field_name' => 'field_text', 'entity_type' => 'node', 'bundle' => 'article', 'settings' => array(), 'required' => FALSE])->save();
     // Build a set of test data.
     $node_values = array('published1' => array('type' => 'article', 'status' => 1, 'title' => 'Node published1 (<&>)', 'uid' => 1, 'field_text' => array(array('value' => 1))), 'published2' => array('type' => 'article', 'status' => 1, 'title' => 'Node published2 (<&>)', 'uid' => 1, 'field_text' => array(array('value' => 2))));
     $nodes = array();
     $node_labels = array();
     foreach ($node_values as $key => $values) {
         $node = Node::create($values);
         $node->save();
         $nodes[$key] = $node;
         $node_labels[$key] = Html::escape($node->label());
     }
     $selection_options = array('target_type' => 'node', 'handler' => 'default', 'handler_settings' => array('target_bundles' => NULL, 'sort' => array('field' => 'field_text.value', 'direction' => 'DESC')));
     $handler = $this->container->get('plugin.manager.entity_reference_selection')->getInstance($selection_options);
     // Not only assert the result, but make sure the keys are sorted as
     // expected.
     $result = $handler->getReferenceableEntities();
     $expected_result = array($nodes['published2']->id() => $node_labels['published2'], $nodes['published1']->id() => $node_labels['published1']);
     $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values.');
     // Assert sort by base field.
     $selection_options['handler_settings']['sort'] = array('field' => 'nid', 'direction' => 'ASC');
     $handler = $this->container->get('plugin.manager.entity_reference_selection')->getInstance($selection_options);
     $result = $handler->getReferenceableEntities();
     $expected_result = array($nodes['published1']->id() => $node_labels['published1'], $nodes['published2']->id() => $node_labels['published2']);
     $this->assertIdentical($result['article'], $expected_result, 'Query sorted by property returned expected values.');
 }
 function setUp()
 {
     parent::setUp();
     // Add two new languages.
     ConfigurableLanguage::createFromLangcode('fr')->save();
     ConfigurableLanguage::createFromLangcode('es')->save();
     // Set up term names.
     $this->termNames = array('en' => 'Food in Paris', 'es' => 'Comida en Paris', 'fr' => 'Nouriture en Paris');
     // Create a vocabulary.
     $this->vocabulary = Vocabulary::create(['name' => 'Views testing tags', 'vid' => 'views_testing_tags']);
     $this->vocabulary->save();
     // Add a translatable field to the vocabulary.
     $field = FieldStorageConfig::create(array('field_name' => 'field_foo', 'entity_type' => 'taxonomy_term', 'type' => 'text'));
     $field->save();
     FieldConfig::create(['field_name' => 'field_foo', 'entity_type' => 'taxonomy_term', 'label' => 'Foo', 'bundle' => 'views_testing_tags'])->save();
     // Create term with translations.
     $taxonomy = $this->createTermWithProperties(array('name' => $this->termNames['en'], 'langcode' => 'en', 'description' => $this->termNames['en'], 'field_foo' => $this->termNames['en']));
     foreach (array('es', 'fr') as $langcode) {
         $translation = $taxonomy->addTranslation($langcode, array('name' => $this->termNames[$langcode]));
         $translation->description->value = $this->termNames[$langcode];
         $translation->field_foo->value = $this->termNames[$langcode];
     }
     $taxonomy->save();
     Views::viewsData()->clear();
     ViewTestData::createTestViews(get_class($this), array('taxonomy_test_views'));
     $this->container->get('router.builder')->rebuild();
 }
 /**
  * Prepare a node to get suggestions from.
  *
  * Creates a node with two file fields. The first one is not translatable,
  * the second one is. Both fields got two files attached, where one has
  * translatable content (title and atl-text) and the other one not.
  *
  * @return object
  *   The node which is prepared with all needed fields for the suggestions.
  */
 protected function prepareTranslationSuggestions()
 {
     // Create a content type with fields.
     // Only the first field is a translatable reference.
     $type = NodeType::create(['type' => $this->randomMachineName()]);
     $type->save();
     $content_translation_manager = \Drupal::service('content_translation.manager');
     $content_translation_manager->setEnabled('node', $type->id(), TRUE);
     $field1 = FieldStorageConfig::create(array('field_name' => 'field1', 'entity_type' => 'node', 'type' => 'entity_reference', 'cardinality' => -1, 'settings' => array('target_type' => 'node')));
     $field1->save();
     $field2 = FieldStorageConfig::create(array('field_name' => 'field2', 'entity_type' => 'node', 'type' => 'entity_reference', 'cardinality' => -1, 'settings' => array('target_type' => 'node')));
     $field2->save();
     // Create field instances on the content type.
     FieldConfig::create(array('field_storage' => $field1, 'bundle' => $type->id(), 'label' => 'Field 1', 'translatable' => FALSE, 'settings' => array()))->save();
     FieldConfig::create(array('field_storage' => $field2, 'bundle' => $type->id(), 'label' => 'Field 2', 'translatable' => TRUE, 'settings' => array()))->save();
     // Create a translatable body field.
     node_add_body_field($type);
     $field = FieldConfig::loadByName('node', $type->id(), 'body');
     $field->setTranslatable(TRUE);
     $field->save();
     // Create 4 nodes to be referenced.
     $references = array();
     for ($i = 0; $i < 4; $i++) {
         $references[$i] = Node::create(array('title' => $this->randomMachineName(), 'body' => $this->randomMachineName(), 'type' => $type->id()));
         $references[$i]->save();
     }
     // Create a node with two translatable and two non-translatable references.
     $node = Node::create(array('title' => $this->randomMachineName(), 'type' => $type->id(), 'language' => 'en', 'body' => $this->randomMachineName(), $field1->getName() => array(array('target_id' => $references[0]->id()), array('target_id' => $references[1]->id())), $field2->getName() => array(array('target_id' => $references[2]->id()), array('target_id' => $references[3]->id()))));
     $node->save();
     $link = MenuLinkContent::create(['link' => [['uri' => 'entity:node/' . $node->id()]], 'title' => 'Node menu link', 'menu_name' => 'main']);
     $link->save();
     $node->link = $link;
     return $node;
 }
示例#22
0
 function setUp()
 {
     parent::setUp();
     // Create test user.
     $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'bypass node access'));
     $this->drupalLogin($admin_user);
     // Create content type, with underscores.
     $type_name = strtolower($this->randomMachineName(8)) . '_test';
     $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
     $this->type = $type->id();
     $display = entity_get_display('node', $type_name, 'default');
     // Create a node.
     $node_values = array('type' => $type_name);
     // Create test fields.
     $test_fields = array('field_test', 'field_test_2', 'field_no_access');
     foreach ($test_fields as $field_name) {
         $field_storage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'node', 'type' => 'test_field']);
         $field_storage->save();
         $instance = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $type_name, 'label' => $this->randomMachineName()]);
         $instance->save();
         // Assign a test value for the field.
         $node_values[$field_name][0]['value'] = mt_rand(1, 127);
         // Set the field visible on the display object.
         $display_options = array('label' => 'above', 'type' => 'field_test_default', 'settings' => array('test_formatter_setting' => $this->randomMachineName()));
         $display->setComponent($field_name, $display_options);
     }
     // Save display + create node.
     $display->save();
     $this->node = $this->drupalCreateNode($node_values);
 }
示例#23
0
 /**
  * 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');
 }
示例#24
0
 /**
  * Helper function for testTextfieldWidgets().
  */
 function _testTextfieldWidgets($field_type, $widget_type)
 {
     // Create a field.
     $field_name = Unicode::strtolower($this->randomMachineName());
     $field_storage = FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => $field_type));
     $field_storage->save();
     FieldConfig::create(['field_storage' => $field_storage, 'bundle' => 'entity_test', 'label' => $this->randomMachineName() . '_label'])->save();
     entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => $widget_type, 'settings' => array('placeholder' => 'A placeholder on ' . $widget_type)))->save();
     entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name)->save();
     // Display creation form.
     $this->drupalGet('entity_test/add');
     $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
     $this->assertNoFieldByName("{$field_name}[0][format]", '1', 'Format selector is not displayed');
     $this->assertRaw(format_string('placeholder="A placeholder on @widget_type"', array('@widget_type' => $widget_type)));
     // Submit with some value.
     $value = $this->randomMachineName();
     $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)), 'Entity was created');
     // Display the entity.
     $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->assertText($value, 'Filtered tags are not displayed');
 }
 protected function setUp()
 {
     parent::setUp();
     // Create the user profile field and instance.
     FieldStorageConfig::create(array('entity_type' => 'user', 'field_name' => 'user_picture', 'type' => 'image', 'translatable' => '0'))->save();
     FieldConfig::create(['label' => 'User Picture', 'description' => '', 'field_name' => 'user_picture', 'entity_type' => 'user', 'bundle' => 'user', 'required' => 0])->save();
     ViewTestData::createTestViews(get_class($this), array('image_test_views'));
 }
 protected function setUp()
 {
     parent::setUp();
     // Auto-create a field for testing.
     FieldStorageConfig::create(array('entity_type' => 'user', 'field_name' => 'test_multiple', 'type' => 'text', 'cardinality' => -1, 'translatable' => FALSE))->save();
     FieldConfig::create(['entity_type' => 'user', 'field_name' => 'test_multiple', 'bundle' => 'user', 'label' => 'Test a multiple valued field'])->save();
     entity_get_form_display('user', 'user', 'register')->setComponent('test_multiple', array('type' => 'text_textfield', 'weight' => 0))->save();
 }
示例#27
0
 /**
  * {@inheritdoc}
  */
 protected function setUp($import_test_views = TRUE)
 {
     parent::setUp();
     $field_name = 'test_options';
     $this->fieldStorage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'list_string', 'cardinality' => 1, 'settings' => ['allowed_values_function' => 'options_test_dynamic_values_callback']]);
     $this->fieldStorage->save();
     $this->field = FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'required' => TRUE])->save();
 }
示例#28
0
 /**
  * Test that allowed values can be updated.
  */
 function testUpdateAllowedValues()
 {
     // All three options appear.
     $entity = EntityTest::create();
     $form = \Drupal::service('entity.form_builder')->getForm($entity);
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists');
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists');
     // Use one of the values in an actual entity, and check that this value
     // cannot be removed from the list.
     $entity = EntityTest::create();
     $entity->{$this->fieldName}->value = 1;
     $entity->save();
     $this->fieldStorage->setSetting('allowed_values', [2 => 'Two']);
     try {
         $this->fieldStorage->save();
         $this->fail(t('Cannot update a list field storage to not include keys with existing data.'));
     } catch (FieldStorageDefinitionUpdateForbiddenException $e) {
         $this->pass(t('Cannot update a list field storage to not include keys with existing data.'));
     }
     // Empty the value, so that we can actually remove the option.
     unset($entity->{$this->fieldName});
     $entity->save();
     // Removed options do not appear.
     $this->fieldStorage->setSetting('allowed_values', [2 => 'Two']);
     $this->fieldStorage->save();
     $entity = EntityTest::create();
     $form = \Drupal::service('entity.form_builder')->getForm($entity);
     $this->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist');
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
     $this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist');
     // Completely new options appear.
     $this->fieldStorage->setSetting('allowed_values', [10 => 'Update', 20 => 'Twenty']);
     $this->fieldStorage->save();
     // The entity holds an outdated field object with the old allowed values
     // setting, so we need to reinitialize the entity object.
     $entity = EntityTest::create();
     $form = \Drupal::service('entity.form_builder')->getForm($entity);
     $this->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist');
     $this->assertTrue(empty($form[$this->fieldName]['widget'][2]), 'Option 2 does not exist');
     $this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist');
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][10]), 'Option 10 exists');
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][20]), 'Option 20 exists');
     // Options are reset when a new field with the same name is created.
     $this->fieldStorage->delete();
     FieldStorageConfig::create($this->fieldStorageDefinition)->save();
     FieldConfig::create(['field_name' => $this->fieldName, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'required' => TRUE])->save();
     entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->fieldName, array('type' => 'options_buttons'))->save();
     $entity = EntityTest::create();
     $form = \Drupal::service('entity.form_builder')->getForm($entity);
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists');
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
     $this->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists');
     // Test the generateSampleValue() method.
     $entity = EntityTest::create();
     $entity->{$this->fieldName}->generateSampleItems();
     $this->entityValidateAndSave($entity);
 }
示例#29
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Create a boolean field and storage for validation.
     FieldStorageConfig::create(array('field_name' => 'field_boolean', 'entity_type' => 'entity_test', 'type' => 'boolean'))->save();
     FieldConfig::create(['entity_type' => 'entity_test', 'field_name' => 'field_boolean', 'bundle' => 'entity_test'])->save();
     // Create a form display for the default form mode.
     entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent('field_boolean', array('type' => 'boolean_checkbox'))->save();
 }
 /**
  * Tests deleting field storages and fields as part of config import.
  */
 public function testImportDeleteUninstall()
 {
     // Create a telephone field.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'field_tel', 'entity_type' => 'entity_test', 'type' => 'telephone'));
     $field_storage->save();
     FieldConfig::create(['field_storage' => $field_storage, 'bundle' => 'entity_test'])->save();
     // Create a text field.
     $date_field_storage = FieldStorageConfig::create(array('field_name' => 'field_date', 'entity_type' => 'entity_test', 'type' => 'datetime'));
     $date_field_storage->save();
     FieldConfig::create(['field_storage' => $date_field_storage, 'bundle' => 'entity_test'])->save();
     // Create an entity which has values for the telephone and text field.
     $entity = EntityTest::create();
     $value = '+0123456789';
     $entity->field_tel = $value;
     $entity->field_date = time();
     $entity->name->value = $this->randomMachineName();
     $entity->save();
     // Delete the text field before exporting configuration so that we can test
     // that deleted fields that are provided by modules that will be uninstalled
     // are also purged and that the UI message includes such fields.
     $date_field_storage->delete();
     // Verify entity has been created properly.
     $id = $entity->id();
     $entity = entity_load('entity_test', $id);
     $this->assertEqual($entity->field_tel->value, $value);
     $this->assertEqual($entity->field_tel[0]->value, $value);
     $active = $this->container->get('config.storage');
     $sync = $this->container->get('config.storage.sync');
     $this->copyConfig($active, $sync);
     // Stage uninstall of the Telephone module.
     $core_extension = $this->config('core.extension')->get();
     unset($core_extension['module']['telephone']);
     $sync->write('core.extension', $core_extension);
     // Stage the field deletion
     $sync->delete('field.storage.entity_test.field_tel');
     $sync->delete('field.field.entity_test.entity_test.field_tel');
     $this->drupalGet('admin/config/development/configuration');
     // Test that the message for one field being purged during a configuration
     // synchronization is correct.
     $this->assertText('This synchronization will delete data from the field entity_test.field_tel.');
     // Stage an uninstall of the datetime module to test the message for
     // multiple fields.
     unset($core_extension['module']['datetime']);
     $sync->write('core.extension', $core_extension);
     $this->drupalGet('admin/config/development/configuration');
     $this->assertText('This synchronization will delete data from the fields: entity_test.field_tel, entity_test.field_date.');
     // This will purge all the data, delete the field and uninstall the
     // Telephone and Text modules.
     $this->drupalPostForm(NULL, array(), t('Import all'));
     $this->assertNoText('Field data will be deleted by this synchronization.');
     $this->rebuildContainer();
     $this->assertFalse(\Drupal::moduleHandler()->moduleExists('telephone'));
     $this->assertFalse(\Drupal::entityManager()->loadEntityByUuid('field_storage_config', $field_storage->uuid()), 'The telephone field has been deleted by the configuration synchronization');
     $deleted_storages = \Drupal::state()->get('field.storage.deleted') ?: array();
     $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Telephone field has been completed removed from the system.');
     $this->assertFalse(isset($deleted_storages[$field_storage->uuid()]), 'Text field has been completed removed from the system.');
 }