/**
  * Tests Drupal 6 node type to Drupal 8 migration.
  */
 public function testNodeType()
 {
     $migration = entity_load('migration', 'd6_node_type');
     // Test the test_page content type.
     $node_type_page = NodeType::load('test_page');
     $this->assertIdentical('test_page', $node_type_page->id(), 'Node type test_page loaded');
     $this->assertIdentical(TRUE, $node_type_page->displaySubmitted());
     $this->assertIdentical(FALSE, $node_type_page->isNewRevision());
     $this->assertIdentical(DRUPAL_OPTIONAL, $node_type_page->getPreviewMode());
     $this->assertIdentical($migration->getIdMap()->lookupDestinationID(array('test_page')), array('test_page'));
     // Test we have a body field.
     $field = FieldConfig::loadByName('node', 'test_page', 'body');
     $this->assertIdentical('This is the body field label', $field->getLabel(), 'Body field was found.');
     // Test the test_story content type.
     $node_type_story = NodeType::load('test_story');
     $this->assertIdentical('test_story', $node_type_story->id(), 'Node type test_story loaded');
     $this->assertIdentical(TRUE, $node_type_story->displaySubmitted());
     $this->assertIdentical(FALSE, $node_type_story->isNewRevision());
     $this->assertIdentical(DRUPAL_OPTIONAL, $node_type_story->getPreviewMode());
     $this->assertIdentical($migration->getIdMap()->lookupDestinationID(array('test_story')), array('test_story'));
     // Test we don't have a body field.
     $field = FieldConfig::loadByName('node', 'test_story', 'body');
     $this->assertIdentical(NULL, $field, 'No body field found');
     // Test the test_event content type.
     $node_type_event = NodeType::load('test_event');
     $this->assertIdentical('test_event', $node_type_event->id(), 'Node type test_event loaded');
     $this->assertIdentical(TRUE, $node_type_event->displaySubmitted());
     $this->assertIdentical(TRUE, $node_type_event->isNewRevision());
     $this->assertIdentical(DRUPAL_OPTIONAL, $node_type_event->getPreviewMode());
     $this->assertIdentical($migration->getIdMap()->lookupDestinationID(array('test_event')), array('test_event'));
     // Test we have a body field.
     $field = FieldConfig::loadByName('node', 'test_event', 'body');
     $this->assertIdentical('Body', $field->getLabel(), 'Body field was found.');
 }
 /**
  * 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;
 }
 /**
  * Tests the required property on file fields.
  */
 function testRequired()
 {
     $type_name = 'article';
     $field_name = strtolower($this->randomMachineName());
     $storage = $this->createFileField($field_name, 'node', $type_name, array(), array('required' => '1'));
     $field = FieldConfig::loadByName('node', $type_name, $field_name);
     $test_file = $this->getTestFile('text');
     // Try to post a new node without uploading a file.
     $edit = array();
     $edit['title[0][value]'] = $this->randomMachineName();
     $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save and publish'));
     $this->assertRaw(t('!title field is required.', array('!title' => $field->getLabel())), 'Node save failed when required file field was empty.');
     // Create a new node with the uploaded file.
     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
     $this->assertTrue($nid !== FALSE, format_string('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', array('@test_file' => $test_file->getFileUri(), '@field_name' => $field_name, '@type_name' => $type_name)));
     $node = node_load($nid, TRUE);
     $node_file = file_load($node->{$field_name}->target_id);
     $this->assertFileExists($node_file, 'File exists after uploading to the required field.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
     // Try again with a multiple value field.
     $storage->delete();
     $this->createFileField($field_name, 'node', $type_name, array('cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED), array('required' => '1'));
     // Try to post a new node without uploading a file in the multivalue field.
     $edit = array();
     $edit['title[0][value]'] = $this->randomMachineName();
     $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save and publish'));
     $this->assertRaw(t('!title field is required.', array('!title' => $field->getLabel())), 'Node save failed when required multiple value file field was empty.');
     // Create a new node with the uploaded file into the multivalue field.
     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
     $node = node_load($nid, TRUE);
     $node_file = file_load($node->{$field_name}->target_id);
     $this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
 }
 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'));
 }
 /**
  * Tests editing a node type using the UI.
  */
 function testNodeTypeEditing()
 {
     $web_user = $this->drupalCreateUser(array('bypass node access', 'administer content types', 'administer node fields'));
     $this->drupalLogin($web_user);
     $field = FieldConfig::loadByName('node', 'page', 'body');
     $this->assertEqual($field->getLabel(), 'Body', 'Body field was found.');
     // Verify that title and body fields are displayed.
     $this->drupalGet('node/add/page');
     $this->assertRaw('Title', 'Title field was found.');
     $this->assertRaw('Body', 'Body field was found.');
     // Rename the title field.
     $edit = array('title_label' => 'Foo');
     $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
     $this->drupalGet('node/add/page');
     $this->assertRaw('Foo', 'New title label was displayed.');
     $this->assertNoRaw('Title', 'Old title label was not displayed.');
     // Change the name, machine name and description.
     $edit = array('name' => 'Bar', 'type' => 'bar', 'description' => 'Lorem ipsum.');
     $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
     $this->drupalGet('node/add');
     $this->assertRaw('Bar', 'New name was displayed.');
     $this->assertRaw('Lorem ipsum', 'New description was displayed.');
     $this->clickLink('Bar');
     $this->assertUrl(\Drupal::url('node.add', ['node_type' => 'bar'], ['absolute' => TRUE]), [], 'New machine name was used in URL.');
     $this->assertRaw('Foo', 'Title field was found.');
     $this->assertRaw('Body', 'Body field was found.');
     // Remove the body field.
     $this->drupalPostForm('admin/structure/types/manage/bar/fields/node.bar.body/delete', array(), t('Delete'));
     // Resave the settings for this type.
     $this->drupalPostForm('admin/structure/types/manage/bar', array(), t('Save content type'));
     // Check that the body field doesn't exist.
     $this->drupalGet('node/add/bar');
     $this->assertNoRaw('Body', 'Body field was not found.');
 }
 /**
  * Tests that importing list_float fields works.
  */
 public function testImport()
 {
     $field_name = 'field_options_float';
     $type = 'options_install_test';
     // Test the results on installing options_config_install_test. All the
     // necessary configuration for this test is created by installing that
     // module.
     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
     $this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
     $admin_path = 'admin/structure/types/manage/' . $type . '/fields/node.' . $type . '.' . $field_name . '/storage';
     // Export active config to sync.
     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
     // Set the active to not use dots in the allowed values key names.
     $edit = array('settings[allowed_values]' => "0|Zero\n1|One");
     $this->drupalPostForm($admin_path, $edit, t('Save field settings'));
     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
     $this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '1' => 'One'));
     // Import configuration with dots in the allowed values key names. This
     // tests \Drupal\Core\Config\Entity\ConfigEntityStorage::importUpdate().
     $this->drupalGet('admin/config/development/configuration');
     $this->drupalPostForm(NULL, array(), t('Import all'));
     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
     $this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
     // Delete field to test creation. This tests
     // \Drupal\Core\Config\Entity\ConfigEntityStorage::importCreate().
     FieldConfig::loadByName('node', $type, $field_name)->delete();
     $this->drupalGet('admin/config/development/configuration');
     $this->drupalPostForm(NULL, array(), t('Import all'));
     $field_storage = FieldStorageConfig::loadByName('node', $field_name);
     $this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
 }
 /**
  * Tests user selection by roles.
  */
 function testUserSelectionByRole()
 {
     $field_definition = FieldConfig::loadByName('user', 'user', 'user_reference');
     $handler_settings = $field_definition->getSetting('handler_settings');
     $handler_settings['filter']['role'] = array($this->role1->id() => $this->role1->id(), $this->role2->id() => 0);
     $handler_settings['filter']['type'] = 'role';
     $field_definition->setSetting('handler_settings', $handler_settings);
     $field_definition->save();
     $user1 = $this->createUser(array('name' => 'aabb'));
     $user1->addRole($this->role1->id());
     $user1->save();
     $user2 = $this->createUser(array('name' => 'aabbb'));
     $user2->addRole($this->role1->id());
     $user2->save();
     $user3 = $this->createUser(array('name' => 'aabbbb'));
     $user3->addRole($this->role2->id());
     $user3->save();
     /** @var \Drupal\Core\Entity\EntityAutocompleteMatcher $autocomplete */
     $autocomplete = \Drupal::service('entity.autocomplete_matcher');
     $matches = $autocomplete->getMatches('user', 'default', $field_definition->getSetting('handler_settings'), 'aabb');
     $this->assertEqual(count($matches), 2);
     $users = array();
     foreach ($matches as $match) {
         $users[] = $match['label'];
     }
     $this->assertTrue(in_array($user1->label(), $users));
     $this->assertTrue(in_array($user2->label(), $users));
     $this->assertFalse(in_array($user3->label(), $users));
     $matches = $autocomplete->getMatches('user', 'default', $field_definition->getSetting('handler_settings'), 'aabbbb');
     $this->assertEqual(count($matches), 0, '');
 }
 /**
  * Tests that the default 'comment_body' field is correctly added.
  */
 function testCommentDefaultFields()
 {
     // Do not make assumptions on default node types created by the test
     // installation profile, and create our own.
     $this->drupalCreateContentType(array('type' => 'test_node_type'));
     $this->container->get('comment.manager')->addDefaultField('node', 'test_node_type');
     // Check that the 'comment_body' field is present on the comment bundle.
     $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
     $this->assertTrue(!empty($field), 'The comment_body field is added when a comment bundle is created');
     $field->delete();
     // Check that the 'comment_body' field is deleted.
     $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
     $this->assertTrue(empty($field_storage), 'The comment_body field was deleted');
     // Create a new content type.
     $type_name = 'test_node_type_2';
     $this->drupalCreateContentType(array('type' => $type_name));
     $this->container->get('comment.manager')->addDefaultField('node', $type_name);
     // Check that the 'comment_body' field exists and has an instance on the
     // new comment bundle.
     $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
     $this->assertTrue($field_storage, 'The comment_body field exists');
     $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
     $this->assertTrue(isset($field), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
     // Test adding a field that defaults to CommentItemInterface::CLOSED.
     $this->container->get('comment.manager')->addDefaultField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
     $field = entity_load('field_config', 'node.test_node_type.who_likes_ponies');
     $this->assertEqual($field->default_value[0]['status'], CommentItemInterface::CLOSED);
 }
示例#9
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     // Load two modules: the captcha module itself and the comment
     // module for testing anonymous comments.
     parent::setUp();
     module_load_include('inc', 'captcha');
     $this->drupalCreateContentType(array('type' => 'page'));
     // Create a normal user.
     $permissions = array('access comments', 'post comments', 'skip comment approval', 'access content', 'create page content', 'edit own page content');
     $this->normalUser = $this->drupalCreateUser($permissions);
     // Create an admin user.
     $permissions[] = 'administer CAPTCHA settings';
     $permissions[] = 'skip CAPTCHA';
     $permissions[] = 'administer permissions';
     $permissions[] = 'administer content types';
     $this->adminUser = $this->drupalCreateUser($permissions);
     // Open comment for page content type.
     $this->addDefaultCommentField('node', 'page');
     // Put comments on page nodes on a separate page.
     $comment_field = FieldConfig::loadByName('node', 'page', 'comment');
     $comment_field->setSetting('form_location', CommentItemInterface::FORM_SEPARATE_PAGE);
     $comment_field->save();
     /* @var \Drupal\captcha\Entity\CaptchaPoint $captcha_point */
     $captcha_point = \Drupal::entityManager()->getStorage('captcha_point')->load('user_login_form');
     $captcha_point->enable()->save();
     $this->config('captcha.settings')->set('default_challenge', 'captcha/test')->save();
 }
 /**
  * Posts a comment.
  *
  * @param \Drupal\Core\Entity\EntityInterface|null $entity
  *   Entity to post comment on or NULL to post to the previously loaded page.
  * @param string $comment
  *   Comment body.
  * @param string $subject
  *   Comment subject.
  * @param mixed $contact
  *   Set to NULL for no contact info, TRUE to ignore success checking, and
  *   array of values to set contact info.
  *
  * @return \Drupal\comment\CommentInterface
  *   The new comment entity.
  */
 function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL)
 {
     $edit = array();
     $edit['comment_body[0][value]'] = $comment;
     $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment');
     $preview_mode = $field->getSetting('preview');
     // Must get the page before we test for fields.
     if ($entity !== NULL) {
         $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment');
     }
     // Determine the visibility of subject form field.
     if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
         // Subject input allowed.
         $edit['subject[0][value]'] = $subject;
     } else {
         $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
     }
     if ($contact !== NULL && is_array($contact)) {
         $edit += $contact;
     }
     switch ($preview_mode) {
         case DRUPAL_REQUIRED:
             // Preview required so no save button should be found.
             $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
             $this->drupalPostForm(NULL, $edit, t('Preview'));
             // Don't break here so that we can test post-preview field presence and
             // function below.
         // Don't break here so that we can test post-preview field presence and
         // function below.
         case DRUPAL_OPTIONAL:
             $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
             $this->assertFieldByName('op', t('Save'), 'Save button found.');
             $this->drupalPostForm(NULL, $edit, t('Save'));
             break;
         case DRUPAL_DISABLED:
             $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
             $this->assertFieldByName('op', t('Save'), 'Save button found.');
             $this->drupalPostForm(NULL, $edit, t('Save'));
             break;
     }
     $match = array();
     // Get comment ID
     preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
     // Get comment.
     if ($contact !== TRUE) {
         // If true then attempting to find error message.
         if ($subject) {
             $this->assertText($subject, 'Comment subject posted.');
         }
         $this->assertText($comment, 'Comment body posted.');
         $this->assertTrue(!empty($match) && !empty($match[1]), 'Comment ID found.');
     }
     if (isset($match[1])) {
         return Comment::load($match[1]);
     }
 }
示例#11
0
 /**
  * Creates a field of an entity reference field storage on the specified bundle.
  *
  * @param string $entity_type
  *   The type of entity the field will be attached to.
  * @param string $bundle
  *   The bundle name of the entity the field will be attached to.
  * @param string $field_name
  *   The name of the field; if it already exists, a new instance of the existing
  *   field will be created.
  * @param string $field_label
  *   The label of the field.
  * @param string $target_entity_type
  *   The type of the referenced entity.
  * @param string $selection_handler
  *   The selection handler used by this field.
  * @param array $selection_handler_settings
  *   An array of settings supported by the selection handler specified above.
  *   (e.g. 'target_bundles', 'sort', 'auto_create', etc).
  * @param int $cardinality
  *   The cardinality of the field.
  *
  * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
  */
 protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array(), $cardinality = 1)
 {
     // Look for or add the specified field to the requested entity bundle.
     if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
         FieldStorageConfig::create(array('field_name' => $field_name, 'type' => 'entity_reference', 'entity_type' => $entity_type, 'cardinality' => $cardinality, 'settings' => array('target_type' => $target_entity_type)))->save();
     }
     if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
         FieldConfig::create(array('field_name' => $field_name, 'entity_type' => $entity_type, 'bundle' => $bundle, 'label' => $field_label, 'settings' => array('handler' => $selection_handler, 'handler_settings' => $selection_handler_settings)))->save();
     }
 }
示例#12
0
 protected function setUp()
 {
     parent::setUp();
     $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
     $this->vocabulary = $this->createVocabulary();
     $field_name = 'taxonomy_' . $this->vocabulary->id();
     $handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE);
     $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
     $this->field = FieldConfig::loadByName('node', 'article', $field_name);
     entity_get_form_display('node', 'article', 'default')->setComponent($field_name, array('type' => 'options_select'))->save();
     entity_get_display('node', 'article', 'default')->setComponent($field_name, array('type' => 'entity_reference_label'))->save();
 }
 /**
  * Tests node body field storage persistence even if there are no instances.
  */
 public function testFieldOverrides()
 {
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertTrue($field_storage, 'Node body field storage exists.');
     $type = NodeType::create(['name' => 'Ponies', 'type' => 'ponies']);
     $type->save();
     node_add_body_field($type);
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertTrue(count($field_storage->getBundles()) == 1, 'Node body field storage is being used on the new node type.');
     $field = FieldConfig::loadByName('node', 'ponies', 'body');
     $field->delete();
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertTrue(count($field_storage->getBundles()) == 0, 'Node body field storage exists after deleting the only instance of a field.');
     \Drupal::service('module_installer')->uninstall(array('node'));
     $field_storage = FieldStorageConfig::loadByName('node', 'body');
     $this->assertFalse($field_storage, 'Node body field storage does not exist after uninstalling the Node module.');
 }
示例#14
0
 /**
  * Test that a field set to an empty array is different than an absent field.
  */
 public function testMarkFieldForDeletion()
 {
     // Add a default value for a field.
     $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
     $field->setDefaultValue(array(array('value' => 'Llama')));
     $field->save();
     // Denormalize data that contains no entry for the field, and check that
     // the default value is present in the resulting entity.
     $data = array('_links' => array('type' => array('href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString())));
     $entity = $this->serializer->denormalize($data, $this->entityClass, $this->format);
     $this->assertEqual($entity->field_test_text->count(), 1);
     $this->assertEqual($entity->field_test_text->value, 'Llama');
     // Denormalize data that contains an empty entry for the field, and check
     // that the field is empty in the resulting entity.
     $data = array('_links' => array('type' => array('href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString())), 'field_test_text' => array());
     $entity = $this->serializer->denormalize($data, get_class($entity), $this->format, ['target_instance' => $entity]);
     $this->assertEqual($entity->field_test_text->count(), 0);
 }
示例#15
0
 /**
  * Tests creating a content type during config import.
  */
 public function testImportCreate()
 {
     $node_type_id = 'import';
     $node_type_config_name = "node.type.{$node_type_id}";
     // Simulate config data to import.
     $active = $this->container->get('config.storage');
     $sync = $this->container->get('config.storage.sync');
     $this->copyConfig($active, $sync);
     // Manually add new node type.
     $src_dir = __DIR__ . '/../../../modules/node_test_config/sync';
     $target_dir = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
     $this->assertTrue(file_unmanaged_copy("{$src_dir}/{$node_type_config_name}.yml", "{$target_dir}/{$node_type_config_name}.yml"));
     // Import the content of the sync directory.
     $this->configImporter()->import();
     // Check that the content type was created.
     $node_type = NodeType::load($node_type_id);
     $this->assertTrue($node_type, 'Import node type from sync was created.');
     $this->assertFalse(FieldConfig::loadByName('node', $node_type_id, 'body'));
 }
示例#16
0
 /**
  * Creates node type with several text fields with different cardinality.
  *
  * Internally it calls TMGMTEntityTestCaseUtility::attachFields() to create
  * and attach fields to newly created bundle. You can than use
  * $this->field_names['node']['YOUR_BUNDLE_NAME'] to access them.
  *
  * @param string $machine_name
  *   Machine name of the node type.
  * @param string $human_name
  *   Human readable name of the node type.
  * @param bool $translation
  *   TRUE if translation for this enitty type should be enabled.
  * pparam bool $attach_fields
  *   (optional) If fields with the same translatability should automatically
  *   be attached to the node type.
  */
 function createNodeType($machine_name, $human_name, $translation = FALSE, $attach_fields = TRUE)
 {
     $type = $this->drupalCreateContentType(array('type' => $machine_name, 'name' => $human_name));
     // Push in also the body field.
     $this->field_names['node'][$machine_name][] = 'body';
     if (\Drupal::hasService('content_translation.manager') && $translation) {
         $content_translation_manager = \Drupal::service('content_translation.manager');
         $content_translation_manager->setEnabled('node', $machine_name, TRUE);
     }
     $this->applySchemaUpdates();
     if ($attach_fields) {
         $this->attachFields('node', $machine_name, $translation);
     } else {
         // Change body field to be translatable.
         $body = FieldConfig::loadByName('node', $machine_name, 'body');
         $body->setTranslatable(TRUE);
         $body->save();
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function createEntity()
 {
     // Create a "bar" bundle for the "entity_test" entity type and create.
     $bundle = 'bar';
     entity_test_create_bundle($bundle, NULL, 'entity_test');
     // Create a comment field on this bundle.
     \Drupal::service('comment.manager')->addDefaultField('entity_test', 'bar', 'comment');
     // Display comments in a flat list; threaded comments are not render cached.
     $field = FieldConfig::loadByName('entity_test', 'bar', 'comment');
     $field->settings['default_mode'] = CommentManagerInterface::COMMENT_MODE_FLAT;
     $field->save();
     // Create a "Camelids" test entity.
     $entity_test = entity_create('entity_test', array('name' => 'Camelids', 'type' => 'bar'));
     $entity_test->save();
     // Create a "Llama" comment.
     $comment = entity_create('comment', array('subject' => 'Llama', 'comment_body' => array('value' => 'The name "llama" was adopted by European settlers from native Peruvians.', 'format' => 'plain_text'), 'entity_id' => $entity_test->id(), 'entity_type' => 'entity_test', 'field_name' => 'comment', 'status' => \Drupal\comment\CommentInterface::PUBLISHED));
     $comment->save();
     return $comment;
 }
 /**
  * Tests creating a content type during config import.
  */
 public function testImportCreate()
 {
     $node_type_id = 'import';
     $node_type_config_name = "node.type.{$node_type_id}";
     // Simulate config data to import.
     $active = $this->container->get('config.storage');
     $staging = $this->container->get('config.storage.staging');
     $this->copyConfig($active, $staging);
     // Manually add new node type.
     $src_dir = drupal_get_path('module', 'node_test_config') . '/staging';
     $target_dir = $this->configDirectories[CONFIG_STAGING_DIRECTORY];
     $this->assertTrue(file_unmanaged_copy("{$src_dir}/{$node_type_config_name}.yml", "{$target_dir}/{$node_type_config_name}.yml"));
     // Import the content of the staging directory.
     $this->configImporter()->import();
     // Check that the content type was created.
     $node_type = NodeType::load($node_type_id);
     $this->assertTrue($node_type, 'Import node type from staging was created.');
     $this->assertFalse(FieldConfig::loadByName('node', $node_type_id, 'body'));
 }
示例#19
0
 /**
  * Tests editing a node type using the UI.
  */
 function testNodeTypeEditing()
 {
     $web_user = $this->drupalCreateUser(array('bypass node access', 'administer content types', 'administer node fields'));
     $this->drupalLogin($web_user);
     $field = FieldConfig::loadByName('node', 'page', 'body');
     $this->assertEqual($field->getLabel(), 'Body', 'Body field was found.');
     // Verify that title and body fields are displayed.
     $this->drupalGet('node/add/page');
     $this->assertRaw('Title', 'Title field was found.');
     $this->assertRaw('Body', 'Body field was found.');
     // Rename the title field.
     $edit = array('title_label' => 'Foo');
     $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
     $this->drupalGet('node/add/page');
     $this->assertRaw('Foo', 'New title label was displayed.');
     $this->assertNoRaw('Title', 'Old title label was not displayed.');
     // Change the name and the description.
     $edit = array('name' => 'Bar', 'description' => 'Lorem ipsum.');
     $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
     $this->drupalGet('node/add');
     $this->assertRaw('Bar', 'New name was displayed.');
     $this->assertRaw('Lorem ipsum', 'New description was displayed.');
     $this->clickLink('Bar');
     $this->assertRaw('Foo', 'Title field was found.');
     $this->assertRaw('Body', 'Body field was found.');
     // Change the name through the API
     /** @var \Drupal\node\NodeTypeInterface $node_type */
     $node_type = NodeType::load('page');
     $node_type->set('name', 'NewBar');
     $node_type->save();
     /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
     $bundle_info = \Drupal::service('entity_type.bundle.info');
     $node_bundles = $bundle_info->getBundleInfo('node');
     $this->assertEqual($node_bundles['page']['label'], 'NewBar', 'Node type bundle cache is updated');
     // Remove the body field.
     $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.body/delete', array(), t('Delete'));
     // Resave the settings for this type.
     $this->drupalPostForm('admin/structure/types/manage/page', array(), t('Save content type'));
     // Check that the body field doesn't exist.
     $this->drupalGet('node/add/page');
     $this->assertNoRaw('Body', 'Body field was not found.');
 }
 public function testNodePathWithViewSelection()
 {
     // Change the term entity reference field to use a view as selection plugin.
     \Drupal::service('module_installer')->install(['entity_reference_test']);
     $field_name = 'field_' . $this->vocabulary->id();
     $field = FieldConfig::loadByName('node', 'article', $field_name);
     $field->setSetting('handler', 'views');
     $field->setSetting('handler_settings', ['view' => ['view_name' => 'test_entity_reference', 'display_name' => 'entity_reference_1']]);
     $field->save();
     $view = Views::getView('taxonomy_default_argument_test');
     $request = Request::create($this->nodes[0]->url());
     $request->server->set('SCRIPT_NAME', $GLOBALS['base_path'] . 'index.php');
     $request->server->set('SCRIPT_FILENAME', 'index.php');
     $response = $this->container->get('http_kernel')->handle($request, HttpKernelInterface::SUB_REQUEST);
     $view->setRequest($request);
     $view->setResponse($response);
     $view->initHandlers();
     $expected = implode(',', array($this->term1->id(), $this->term2->id()));
     $this->assertEqual($expected, $view->argument['tid']->getDefaultArgument());
 }
 /**
  * Tests editing a comment type using the UI.
  */
 public function testCommentTypeEditing()
 {
     $this->drupalLogin($this->adminUser);
     $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
     $this->assertEqual($field->getLabel(), 'Comment', 'Comment body field was found.');
     // Change the comment type name.
     $this->drupalGet('admin/structure/comment');
     $edit = array('label' => 'Bar');
     $this->drupalPostForm('admin/structure/comment/manage/comment', $edit, t('Save'));
     $this->drupalGet('admin/structure/comment');
     $this->assertRaw('Bar', 'New name was displayed.');
     $this->clickLink('Manage fields');
     $this->assertUrl(\Drupal::url('field_ui.overview_comment', array('comment_type' => 'comment'), array('absolute' => TRUE)), [], 'Original machine name was used in URL.');
     // Remove the body field.
     $this->drupalPostForm('admin/structure/comment/manage/comment/fields/comment.comment.comment_body/delete', array(), t('Delete'));
     // Resave the settings for this type.
     $this->drupalPostForm('admin/structure/comment/manage/comment', array(), t('Save'));
     // Check that the body field doesn't exist.
     $this->drupalGet('admin/structure/comment/manage/comment/fields');
     $this->assertNoRaw('comment_body', 'Body field was not found.');
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('entity_test_no_label');
     /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
     $storage = $this->container->get('entity.manager')->getStorage('entity_test_no_label');
     // Create a new node-type.
     NodeType::create(['type' => $node_type = Unicode::strtolower($this->randomMachineName()), 'name' => $this->randomString()])->save();
     // Create an entity reference field targeting 'entity_test_no_label'
     // entities.
     $field_name = Unicode::strtolower($this->randomMachineName());
     $this->createEntityReferenceField('node', $node_type, $field_name, $this->randomString(), 'entity_test_no_label');
     $field_config = FieldConfig::loadByName('node', $node_type, $field_name);
     $this->selectionHandler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config);
     // Generate a bundle name to be used with 'entity_test_no_label'.
     $this->bundle = Unicode::strtolower($this->randomMachineName());
     // Create 6 entities to be referenced by the field.
     foreach (static::$labels as $name) {
         $storage->create(['id' => Unicode::strtolower($this->randomMachineName()), 'name' => $name, 'type' => $this->bundle])->save();
     }
 }
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     // Add the languages.
     $this->installConfig(['language']);
     ConfigurableLanguage::createFromLangcode('de')->save();
     ConfigurableLanguage::createFromLangcode('cs')->save();
     $this->installEntitySchema('tmgmt_job');
     $this->installEntitySchema('tmgmt_job_item');
     $this->installEntitySchema('tmgmt_remote');
     $this->installEntitySchema('tmgmt_message');
     $this->installEntitySchema('entity_test_rev');
     $this->installEntitySchema('entity_test_mulrev');
     $this->installEntitySchema('entity_test_mul');
     $this->container->get('content_translation.manager')->setEnabled('entity_test_mul', 'entity_test_mul', TRUE);
     $this->installSchema('system', array('router'));
     $this->installSchema('node', array('node_access'));
     \Drupal::moduleHandler()->loadInclude('entity_test', 'install');
     entity_test_install();
     // Make the test field translatable.
     $field_storage = FieldStorageConfig::loadByName('entity_test_mul', 'field_test_text');
     $field_storage->setCardinality(2);
     $field_storage->save();
     $field = FieldConfig::loadByName('entity_test_mul', 'entity_test_mul', 'field_test_text');
     $field->setTranslatable(TRUE);
     $field->save();
     // Add an image field and make it translatable.
     $this->installEntitySchema('file');
     $this->installSchema('file', array('file_usage'));
     $this->installConfig(array('node'));
     \Drupal::service('router.builder')->rebuild();
     $field_storage = FieldStorageConfig::create(array('field_name' => 'image_test', 'entity_type' => $this->entityTypeId, 'type' => 'image', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'translatable' => TRUE));
     $field_storage->save();
     FieldConfig::create(array('entity_type' => $this->entityTypeId, 'field_storage' => $field_storage, 'bundle' => $this->entityTypeId, 'label' => $this->image_label = $this->randomMachineName()))->save();
     file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg');
     $this->image = entity_create('file', array('uri' => 'public://example.jpg'));
     $this->image->save();
     tmgmt_translator_auto_create(\Drupal::service('plugin.manager.tmgmt.translator')->getDefinition('test_translator'));
 }
 /**
  * Tests the required property on file fields.
  */
 function testRequired()
 {
     $node_storage = $this->container->get('entity.manager')->getStorage('node');
     $type_name = 'article';
     $field_name = strtolower($this->randomMachineName());
     $storage = $this->createFileField($field_name, 'node', $type_name, array(), array('required' => '1'));
     $field = FieldConfig::loadByName('node', $type_name, $field_name);
     $test_file = $this->getTestFile('text');
     // Try to post a new node without uploading a file.
     $edit = array();
     $edit['title[0][value]'] = $this->randomMachineName();
     $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save and publish'));
     $this->assertText('1 error has been found:   ' . $field->label(), 'Node save failed when required file field was empty.');
     $this->assertIdentical(1, count($this->xpath('//div[contains(concat(" ", normalize-space(@class), " "), :class)]//a', [':class' => ' messages--error '])), 'There is one link in the error message.');
     // Create a new node with the uploaded file.
     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
     $this->assertTrue($nid !== FALSE, format_string('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', array('@test_file' => $test_file->getFileUri(), '@field_name' => $field_name, '@type_name' => $type_name)));
     $node_storage->resetCache(array($nid));
     $node = $node_storage->load($nid);
     $node_file = File::load($node->{$field_name}->target_id);
     $this->assertFileExists($node_file, 'File exists after uploading to the required field.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
     // Try again with a multiple value field.
     $storage->delete();
     $this->createFileField($field_name, 'node', $type_name, array('cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED), array('required' => '1'));
     // Try to post a new node without uploading a file in the multivalue field.
     $edit = array();
     $edit['title[0][value]'] = $this->randomMachineName();
     $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save and publish'));
     $this->assertText('1 error has been found:   ' . $field->label(), 'Node save failed when required multiple value file field was empty.');
     $this->assertIdentical(1, count($this->xpath('//div[contains(concat(" ", normalize-space(@class), " "), :class)]//a', [':class' => ' messages--error '])), 'There is one link in the error message.');
     // Create a new node with the uploaded file into the multivalue field.
     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
     $node_storage->resetCache(array($nid));
     $node = $node_storage->load($nid);
     $node_file = File::load($node->{$field_name}->target_id);
     $this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
     $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
 }
 /**
  * Set up configuration for multiple languages.
  */
 function setUpLanguages()
 {
     // Add languages.
     $this->defaultLanguage = 'en';
     $this->secondaryLanguage = 'es';
     $this->addLanguage($this->secondaryLanguage);
     // Display the language widget.
     $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'simplenews_issue');
     $config->setLanguageAlterable(TRUE);
     $config->save();
     // Make Simplenews issue translatable.
     \Drupal::service('content_translation.manager')->setEnabled('node', 'simplenews_issue', TRUE);
     drupal_static_reset();
     \Drupal::entityManager()->clearCachedDefinitions();
     \Drupal::service('router.builder')->rebuild();
     \Drupal::service('entity.definition_update_manager')->applyUpdates();
     // Make Simplenews issue body translatable.
     $field = FieldConfig::loadByName('node', 'simplenews_issue', 'body');
     $field->setTranslatable(TRUE);
     $field->save();
     $this->rebuildContainer();
 }
 /**
  * Tests the cross deletion behavior between field storages and fields.
  */
 function testDeleteFieldCrossDeletion()
 {
     $field_definition_2 = $this->fieldDefinition;
     $field_definition_2['bundle'] .= '_another_bundle';
     entity_test_create_bundle($field_definition_2['bundle']);
     // Check that deletion of a field storage deletes its fields.
     $field_storage = $this->fieldStorage;
     FieldConfig::create($this->fieldDefinition)->save();
     FieldConfig::create($field_definition_2)->save();
     $field_storage->delete();
     $this->assertFalse(FieldConfig::loadByName('entity_test', $this->fieldDefinition['bundle'], $field_storage->getName()));
     $this->assertFalse(FieldConfig::loadByName('entity_test', $field_definition_2['bundle'], $field_storage->getName()));
     // Check that deletion of the last field deletes the storage.
     $field_storage = FieldStorageConfig::create($this->fieldStorageDefinition);
     $field_storage->save();
     $field = FieldConfig::create($this->fieldDefinition);
     $field->save();
     $field_2 = FieldConfig::create($field_definition_2);
     $field_2->save();
     $field->delete();
     $this->assertTrue(FieldStorageConfig::loadByName('entity_test', $field_storage->getName()));
     $field_2->delete();
     $this->assertFalse(FieldStorageConfig::loadByName('entity_test', $field_storage->getName()));
     // Check that deletion of all fields using a storage simultaneously deletes
     // the storage.
     $field_storage = FieldStorageConfig::create($this->fieldStorageDefinition);
     $field_storage->save();
     $field = FieldConfig::create($this->fieldDefinition);
     $field->save();
     $field_2 = FieldConfig::create($field_definition_2);
     $field_2->save();
     $this->container->get('entity.manager')->getStorage('field_config')->delete(array($field, $field_2));
     $this->assertFalse(FieldStorageConfig::loadByName('entity_test', $field_storage->getName()));
 }
 /**
  * Tests the entity reference field with all its supported field widgets.
  */
 public function testSupportedEntityTypesAndWidgets()
 {
     foreach ($this->getTestEntities() as $key => $referenced_entities) {
         $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId();
         // Create an Entity reference field.
         $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2);
         // Test the default 'entity_reference_autocomplete' widget.
         entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
         $entity_name = $this->randomMachineName();
         $edit = array('name[0][value]' => $entity_name, $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')', $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label());
         $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
         $this->assertFieldValues($entity_name, $referenced_entities);
         // Try to post the form again with no modification and check if the field
         // values remain the same.
         $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
         $this->drupalGet($this->entityType . '/manage/' . $entity->id() . '/edit');
         $this->assertFieldByName($this->fieldName . '[0][target_id]', $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')');
         $this->assertFieldByName($this->fieldName . '[1][target_id]', $referenced_entities[1]->label() . ' (' . $referenced_entities[1]->id() . ')');
         $this->drupalPostForm(NULL, array(), t('Save'));
         $this->assertFieldValues($entity_name, $referenced_entities);
         // Test the 'entity_reference_autocomplete_tags' widget.
         entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_autocomplete_tags'))->save();
         $entity_name = $this->randomMachineName();
         $target_id = $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')';
         // Test an input of the entity label without a ' (entity_id)' suffix.
         $target_id .= ', ' . $referenced_entities[1]->label();
         $edit = array('name[0][value]' => $entity_name, $this->fieldName . '[target_id]' => $target_id);
         $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
         $this->assertFieldValues($entity_name, $referenced_entities);
         // Try to post the form again with no modification and check if the field
         // values remain the same.
         $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
         $this->drupalGet($this->entityType . '/manage/' . $entity->id() . '/edit');
         $this->assertFieldByName($this->fieldName . '[target_id]', $target_id . ' (' . $referenced_entities[1]->id() . ')');
         $this->drupalPostForm(NULL, array(), t('Save'));
         $this->assertFieldValues($entity_name, $referenced_entities);
         // Test all the other widgets supported by the entity reference field.
         // Since we don't know the form structure for these widgets, just test
         // that editing and saving an already created entity works.
         $exclude = array('entity_reference_autocomplete', 'entity_reference_autocomplete_tags');
         $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
         $supported_widgets = \Drupal::service('plugin.manager.field.widget')->getOptions('entity_reference');
         $supported_widget_types = array_diff(array_keys($supported_widgets), $exclude);
         foreach ($supported_widget_types as $widget_type) {
             entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array('type' => $widget_type))->save();
             $this->drupalPostForm($this->entityType . '/manage/' . $entity->id() . '/edit', array(), t('Save'));
             $this->assertFieldValues($entity_name, $referenced_entities);
         }
         // Reset to the default 'entity_reference_autocomplete' widget.
         entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
         // Set first entity as the default_value.
         $field_edit = array('default_value_input[' . $this->fieldName . '][0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')');
         if ($key == 'content') {
             $field_edit['settings[handler_settings][target_bundles][' . $referenced_entities[0]->getEntityTypeId() . ']'] = TRUE;
         }
         $this->drupalPostForm($this->entityType . '/structure/' . $this->bundle . '/fields/' . $this->entityType . '.' . $this->bundle . '.' . $this->fieldName, $field_edit, t('Save settings'));
         // Ensure the configuration has the expected dependency on the entity that
         // is being used a default value.
         $field = FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName);
         $this->assertTrue(in_array($referenced_entities[0]->getConfigDependencyName(), $field->getDependencies()[$key]), SafeMarkup::format('Expected @type dependency @name found', ['@type' => $key, '@name' => $referenced_entities[0]->getConfigDependencyName()]));
         // Ensure that the field can be imported without change even after the
         // default value deleted.
         $referenced_entities[0]->delete();
         // Reload the field since deleting the default value can change the field.
         \Drupal::entityManager()->getStorage($field->getEntityTypeId())->resetCache([$field->id()]);
         $field = FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName);
         $this->assertConfigEntityImport($field);
         // Once the default value has been removed after saving the dependency
         // should be removed.
         $field = FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName);
         $field->save();
         $dependencies = $field->getDependencies();
         $this->assertFalse(isset($dependencies[$key]) && in_array($referenced_entities[0]->getConfigDependencyName(), $dependencies[$key]), SafeMarkup::format('@type dependency @name does not exist.', ['@type' => $key, '@name' => $referenced_entities[0]->getConfigDependencyName()]));
     }
 }
 /**
  * Tests that the settings UI works as expected.
  */
 function testSettingsUI()
 {
     // Check for the content_translation_menu_links_discovered_alter() changes.
     $this->drupalGet('admin/config');
     $this->assertLink('Content language and translation');
     $this->assertText('Configure language and translation support for content.');
     // Test that the translation settings are ignored if the bundle is marked
     // translatable but the entity type is not.
     $edit = array('settings[comment][comment_article][translatable]' => TRUE);
     $this->assertSettings('comment', NULL, FALSE, $edit);
     // Test that the translation settings are ignored if only a field is marked
     // as translatable and not the related entity type and bundle.
     $edit = array('settings[comment][comment_article][fields][comment_body]' => TRUE);
     $this->assertSettings('comment', NULL, FALSE, $edit);
     // Test that the translation settings are not stored if an entity type and
     // bundle are marked as translatable but no field is.
     $edit = array('entity_types[comment]' => TRUE, 'settings[comment][comment_article][translatable]' => TRUE, 'settings[comment][comment_article][fields][changed]' => FALSE, 'settings[comment][comment_article][fields][created]' => FALSE, 'settings[comment][comment_article][fields][homepage]' => FALSE, 'settings[comment][comment_article][fields][hostname]' => FALSE, 'settings[comment][comment_article][fields][mail]' => FALSE, 'settings[comment][comment_article][fields][name]' => FALSE, 'settings[comment][comment_article][fields][status]' => FALSE, 'settings[comment][comment_article][fields][subject]' => FALSE, 'settings[comment][comment_article][fields][uid]' => FALSE);
     $this->assertSettings('comment', 'comment_article', FALSE, $edit);
     $xpath_err = '//div[contains(@class, "error")]';
     $this->assertTrue($this->xpath($xpath_err), 'Enabling translation only for entity bundles generates a form error.');
     // Test that the translation settings are not stored if a non-configurable
     // language is set as default and the language selector is hidden.
     $edit = array('entity_types[comment]' => TRUE, 'settings[comment][comment_article][settings][language][langcode]' => Language::LANGCODE_NOT_SPECIFIED, 'settings[comment][comment_article][settings][language][language_alterable]' => FALSE, 'settings[comment][comment_article][translatable]' => TRUE, 'settings[comment][comment_article][fields][comment_body]' => TRUE);
     $this->assertSettings('comment', 'comment_article', FALSE, $edit);
     $this->assertTrue($this->xpath($xpath_err), 'Enabling translation with a fixed non-configurable language generates a form error.');
     // Test that a field shared among different bundles can be enabled without
     // needing to make all the related bundles translatable.
     $edit = array('entity_types[comment]' => TRUE, 'settings[comment][comment_article][settings][language][langcode]' => 'current_interface', 'settings[comment][comment_article][settings][language][language_alterable]' => TRUE, 'settings[comment][comment_article][translatable]' => TRUE, 'settings[comment][comment_article][fields][comment_body]' => TRUE, 'settings[comment][comment_article][fields][subject]' => FALSE, 'settings[comment][comment][fields][subject]' => FALSE);
     $this->assertSettings('comment', 'comment_article', TRUE, $edit);
     $definition = $this->entityManager()->getFieldDefinitions('comment', 'comment_article')['comment_body'];
     $this->assertTrue($definition->isTranslatable(), 'Article comment body is translatable.');
     $definition = $this->entityManager()->getFieldDefinitions('comment', 'comment_article')['subject'];
     $this->assertFalse($definition->isTranslatable(), 'Article comment subject is not translatable.');
     $definition = $this->entityManager()->getFieldDefinitions('comment', 'comment')['comment_body'];
     $this->assertFalse($definition->isTranslatable(), 'Page comment body is not translatable.');
     $definition = $this->entityManager()->getFieldDefinitions('comment', 'comment')['subject'];
     $this->assertFalse($definition->isTranslatable(), 'Page comment subject is not translatable.');
     // Test that translation can be enabled for base fields.
     $edit = array('entity_types[entity_test_mul]' => TRUE, 'settings[entity_test_mul][entity_test_mul][translatable]' => TRUE, 'settings[entity_test_mul][entity_test_mul][fields][name]' => TRUE, 'settings[entity_test_mul][entity_test_mul][fields][user_id]' => FALSE);
     $this->assertSettings('entity_test_mul', 'entity_test_mul', TRUE, $edit);
     $field_override = BaseFieldOverride::loadByName('entity_test_mul', 'entity_test_mul', 'name');
     $this->assertTrue($field_override->isTranslatable(), 'Base fields can be overridden with a base field bundle override entity.');
     $definitions = $this->entityManager()->getFieldDefinitions('entity_test_mul', 'entity_test_mul');
     $this->assertTrue($definitions['name']->isTranslatable() && !$definitions['user_id']->isTranslatable(), 'Base field bundle overrides were correctly altered.');
     // Test that language settings are correctly stored.
     $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_article');
     $this->assertEqual($language_configuration->getDefaultLangcode(), 'current_interface', 'The default language for article comments is set to the interface text language selected for page.');
     $this->assertTrue($language_configuration->isLanguageAlterable(), 'The language selector for article comments is shown.');
     // Verify language widget appears on comment type form.
     $this->drupalGet('admin/structure/comment/manage/comment_article');
     $this->assertField('language_configuration[content_translation]');
     $this->assertFieldChecked('edit-language-configuration-content-translation');
     // Verify that translation may be enabled for the article content type.
     $edit = array('language_configuration[content_translation]' => TRUE);
     // Make sure the checkbox is available and not checked by default.
     $this->drupalGet('admin/structure/types/manage/article');
     $this->assertField('language_configuration[content_translation]');
     $this->assertNoFieldChecked('edit-language-configuration-content-translation');
     $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
     $this->drupalGet('admin/structure/types/manage/article');
     $this->assertFieldChecked('edit-language-configuration-content-translation');
     // Test that the title field of nodes is available in the settings form.
     $edit = array('entity_types[node]' => TRUE, 'settings[node][article][settings][language][langcode]' => 'current_interface', 'settings[node][article][settings][language][language_alterable]' => TRUE, 'settings[node][article][translatable]' => TRUE, 'settings[node][article][fields][title]' => TRUE);
     $this->assertSettings('node', NULL, TRUE, $edit);
     foreach (array(TRUE, FALSE) as $translatable) {
         // Test that configurable field translatability is correctly switched.
         $edit = array('settings[node][article][fields][body]' => $translatable);
         $this->assertSettings('node', 'article', TRUE, $edit);
         $field = FieldConfig::loadByName('node', 'article', 'body');
         $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'article');
         $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.');
         $this->assertEqual($field->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.');
         // Test that also the Field UI form behaves correctly.
         $translatable = !$translatable;
         $edit = array('translatable' => $translatable);
         $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
         \Drupal::entityManager()->clearCachedFieldDefinitions();
         $field = FieldConfig::loadByName('node', 'article', 'body');
         $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'article');
         $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.');
         $this->assertEqual($field->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.');
     }
     // Test that the order of the language list is similar to other language
     // lists, such as in Views UI.
     $this->drupalGet('admin/config/regional/content-language');
     $expected_elements = array('site_default', 'current_interface', 'authors_default', 'en', 'und', 'zxx');
     $elements = $this->xpath('//select[@id="edit-settings-node-article-settings-language-langcode"]/option');
     // Compare values inside the option elements with expected values.
     for ($i = 0; $i < count($elements); $i++) {
         $this->assertEqual($elements[$i]->attributes()->{'value'}, $expected_elements[$i]);
     }
 }
示例#29
0
 /**
  * Verify access rules for comment indexing with different permissions.
  */
 function testSearchResultsCommentAccess()
 {
     $comment_body = 'Test comment body';
     $this->commentSubject = 'Test comment subject';
     $roles = $this->adminUser->getRoles(TRUE);
     $this->adminRole = $roles[0];
     // Create a node.
     // Make preview optional.
     $field = FieldConfig::loadByName('node', 'article', 'comment');
     $field->setSetting('preview', DRUPAL_OPTIONAL);
     $field->save();
     $this->node = $this->drupalCreateNode(array('type' => 'article'));
     // Post a comment using 'Full HTML' text format.
     $edit_comment = array();
     $edit_comment['subject[0][value]'] = $this->commentSubject;
     $edit_comment['comment_body[0][value]'] = '<h1>' . $comment_body . '</h1>';
     $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit_comment, t('Save'));
     $this->drupalLogout();
     $this->setRolePermissions(RoleInterface::ANONYMOUS_ID);
     $this->assertCommentAccess(FALSE, 'Anon user has search permission but no access comments permission, comments should not be indexed');
     $this->setRolePermissions(RoleInterface::ANONYMOUS_ID, TRUE);
     $this->assertCommentAccess(TRUE, 'Anon user has search permission and access comments permission, comments should be indexed');
     $this->drupalLogin($this->adminUser);
     $this->drupalGet('admin/people/permissions');
     // Disable search access for authenticated user to test admin user.
     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, FALSE, FALSE);
     $this->setRolePermissions($this->adminRole);
     $this->assertCommentAccess(FALSE, 'Admin user has search permission but no access comments permission, comments should not be indexed');
     $this->drupalGet('node/' . $this->node->id());
     $this->setRolePermissions($this->adminRole, TRUE);
     $this->assertCommentAccess(TRUE, 'Admin user has search permission and access comments permission, comments should be indexed');
     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID);
     $this->assertCommentAccess(FALSE, 'Authenticated user has search permission but no access comments permission, comments should not be indexed');
     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE);
     $this->assertCommentAccess(TRUE, 'Authenticated user has search permission and access comments permission, comments should be indexed');
     // Verify that access comments permission is inherited from the
     // authenticated role.
     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE, FALSE);
     $this->setRolePermissions($this->adminRole);
     $this->assertCommentAccess(TRUE, 'Admin user has search permission and no access comments permission, but comments should be indexed because admin user inherits authenticated user\'s permission to access comments');
     // Verify that search content permission is inherited from the authenticated
     // role.
     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE, TRUE);
     $this->setRolePermissions($this->adminRole, TRUE, FALSE);
     $this->assertCommentAccess(TRUE, 'Admin user has access comments permission and no search permission, but comments should be indexed because admin user inherits authenticated user\'s permission to search');
 }
示例#30
0
 /**
  * Associate field associated_components with new bundle
  *
  * @param $bundle
  *   bundle name
  *
  * @return
  *   TODO
  */
 public function updateAssociatedComponents($bundle)
 {
     $field_associated_components = FieldConfig::loadByName('paragraph', 'associated_component', 'component');
     $handler_settings = $field_associated_components->getSetting('handler_settings');
     $handler_settings['target_bundles'][$bundle] = $bundle;
     $field_associated_components->setSetting('handler_settings', $handler_settings);
     $field_associated_components->save();
     \Drupal::logger('clutch:workflow')->notice('Add new target bundle @bundle for component reference field on Paragraph Type Associated Component.', array('@bundle' => $bundle));
 }