/** * Tests that field overrides work as expected. */ public function testFieldOverrides() { if (!NodeType::load('ponies')) { NodeType::create(['name' => 'Ponies', 'type' => 'ponies'])->save(); } $override = BaseFieldOverride::loadByName('node', 'ponies', 'uid'); if ($override) { $override->delete(); } $uid_field = \Drupal::entityManager()->getBaseFieldDefinitions('node')['uid']; $config = $uid_field->getConfig('ponies'); $config->save(); $this->assertEqual($config->get('default_value_callback'), 'Drupal\\node\\Entity\\Node::getCurrentUserId'); /** @var \Drupal\node\NodeInterface $node */ $node = Node::create(['type' => 'ponies']); $owner = $node->getOwner(); $this->assertTrue($owner instanceof UserInterface); $this->assertEqual($owner->id(), $this->user->id()); }
/** * 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]); } }
/** * {@inheritdoc} */ public function getConfig($bundle) { $override = BaseFieldOverride::loadByName($this->getTargetEntityTypeId(), $bundle, $this->getName()); if ($override) { return $override; } return BaseFieldOverride::createFromBaseFieldDefinition($this, $bundle); }