View source
<?php
namespace Drupal\content_translation\Tests;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Field\Entity\BaseFieldOverride;
use Drupal\Core\Language\Language;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\simpletest\WebTestBase;
class ContentTranslationSettingsTest extends WebTestBase {
use CommentTestTrait;
public static $modules = array(
'language',
'content_translation',
'node',
'comment',
'field_ui',
'entity_test',
);
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'article',
));
$this
->drupalCreateContentType(array(
'type' => 'page',
));
$this
->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
$this
->addDefaultCommentField('node', 'page', 'comment_page');
$admin_user = $this
->drupalCreateUser(array(
'access administration pages',
'administer languages',
'administer content translation',
'administer content types',
'administer node fields',
'administer comment fields',
'administer comments',
'administer comment types',
'administer account settings',
));
$this
->drupalLogin($admin_user);
}
function testSettingsUI() {
$this
->drupalGet('admin/config');
$this
->assertLink('Content language and translation');
$this
->assertText('Configure language and translation support for content.');
$edit = array(
'settings[comment][comment_article][translatable]' => TRUE,
);
$this
->assertSettings('comment', NULL, FALSE, $edit);
$edit = array(
'settings[comment][comment_article][fields][comment_body]' => TRUE,
);
$this
->assertSettings('comment', NULL, FALSE, $edit);
$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.');
$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.');
$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.');
$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.');
$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.');
$this
->drupalGet('admin/structure/comment/manage/comment_article');
$this
->assertField('language_configuration[content_translation]');
$this
->assertFieldChecked('edit-language-configuration-content-translation');
$edit = array(
'language_configuration[content_translation]' => TRUE,
);
$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');
$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) {
$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.');
$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.');
}
$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');
for ($i = 0; $i < count($elements); $i++) {
$this
->assertEqual($elements[$i]
->attributes()->{'value'}, $expected_elements[$i]);
}
}
function testAccountLanguageSettingsUI() {
$this
->drupalGet('admin/config/people/accounts');
$this
->assertField('language[content_translation]');
$this
->assertNoFieldChecked('edit-language-content-translation');
$edit = array(
'language[content_translation]' => TRUE,
);
$this
->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
$this
->drupalGet('admin/config/people/accounts');
$this
->assertFieldChecked('edit-language-content-translation');
$this
->drupalPostForm('admin/config/people/accounts', array(
'anonymous' => 'Save me please!',
), 'Save configuration');
$this
->assertFieldByName('anonymous', 'Save me please!', 'Anonymous name has been changed.');
$this
->assertText('The configuration options have been saved.');
}
protected function assertSettings($entity_type, $bundle, $enabled, $edit) {
$this
->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
$args = array(
'@entity_type' => $entity_type,
'@bundle' => $bundle,
'@enabled' => $enabled ? 'enabled' : 'disabled',
);
$message = format_string('Translation for entity @entity_type (@bundle) is @enabled.', $args);
\Drupal::entityManager()
->clearCachedDefinitions();
return $this
->assertEqual(\Drupal::service('content_translation.manager')
->isEnabled($entity_type, $bundle), $enabled, $message);
}
function testFieldTranslatableSettingsUI() {
$edit = array(
'new_storage_type' => 'text',
'label' => 'Test',
'field_name' => 'article_text',
);
$this
->drupalPostForm('admin/structure/types/manage/article/fields/add-field', $edit, 'Save and continue');
$path = 'admin/structure/types/manage/article/fields/node.article.field_article_text';
$this
->drupalGet($path);
$this
->assertFieldByXPath('//input[@id="edit-translatable" and @disabled="disabled"]');
$this
->assertText('To configure translation for this field, enable language support for this type.', 'No translatable setting for field.');
$edit = array(
'entity_types[node]' => TRUE,
'settings[node][article][settings][language][language_alterable]' => TRUE,
'settings[node][article][translatable]' => TRUE,
'settings[node][article][fields][field_article_text]' => TRUE,
);
$this
->assertSettings('node', 'article', TRUE, $edit);
$this
->drupalGet($path);
$this
->assertFieldByXPath('//input[@id="edit-translatable" and not(@disabled) and @checked="checked"]');
$this
->assertNoText('To enable translation of this field, enable language support for this type.', 'Translatable setting for field available.');
}
function testNonTranslatableTranslationSettingsUI() {
$this
->drupalGet('admin/config/regional/content-language');
$this
->assertNoField('settings[entity_test][entity_test][translatable]');
}
protected function entityManager() {
return $this->container
->get('entity.manager');
}
}