Chris@0: drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@0: Chris@0: // Setup users. Chris@0: $admin_user = $this->drupalCreateUser(['administer languages', 'administer content types', 'access administration pages', 'create page content', 'edit own page content']); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: // Add a new language. Chris@0: ConfigurableLanguage::createFromLangcode('it')->save(); Chris@0: Chris@0: // Enable URL language detection and selection. Chris@0: $edit = ['language_interface[enabled][language-url]' => '1']; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Set "Basic page" content type to use multilingual support. Chris@0: $edit = [ Chris@0: 'language_configuration[language_alterable]' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); Chris@0: $this->assertRaw(t('The content type %type has been updated.', ['%type' => 'Basic page']), 'Basic page content type has been updated.'); Chris@0: Chris@0: // Make node body translatable. Chris@0: $field_storage = FieldStorageConfig::loadByName('node', 'body'); Chris@0: $field_storage->setTranslatable(TRUE); Chris@0: $field_storage->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests whether field languages are correctly set through the node form. Chris@0: */ Chris@0: public function testMultilingualNodeForm() { Chris@0: // Create "Basic page" content. Chris@0: $langcode = language_get_default_langcode('node', 'page'); Chris@0: $title_key = 'title[0][value]'; Chris@0: $title_value = $this->randomMachineName(8); Chris@0: $body_key = 'body[0][value]'; Chris@0: $body_value = $this->randomMachineName(16); Chris@0: Chris@0: // Create node to edit. Chris@0: $edit = []; Chris@0: $edit[$title_key] = $title_value; Chris@0: $edit[$body_key] = $body_value; Chris@0: $this->drupalPostForm('node/add/page', $edit, t('Save')); Chris@0: Chris@0: // Check that the node exists in the database. Chris@0: $node = $this->drupalGetNodeByTitle($edit[$title_key]); Chris@0: $this->assertTrue($node, 'Node found in database.'); Chris@0: $this->assertTrue($node->language()->getId() == $langcode && $node->body->value == $body_value, 'Field language correctly set.'); Chris@0: Chris@0: // Change node language. Chris@0: $langcode = 'it'; Chris@0: $this->drupalGet("node/{$node->id()}/edit"); Chris@0: $edit = [ Chris@0: $title_key => $this->randomMachineName(8), Chris@0: 'langcode[0][value]' => $langcode, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $node = $this->drupalGetNodeByTitle($edit[$title_key], TRUE); Chris@0: $this->assertTrue($node, 'Node found in database.'); Chris@0: $this->assertTrue($node->language()->getId() == $langcode && $node->body->value == $body_value, 'Field language correctly changed.'); Chris@0: Chris@0: // Enable content language URL detection. Chris@0: $this->container->get('language_negotiator')->saveConfiguration(LanguageInterface::TYPE_CONTENT, [LanguageNegotiationUrl::METHOD_ID => 0]); Chris@0: Chris@0: // Test multilingual field language fallback logic. Chris@0: $this->drupalGet("it/node/{$node->id()}"); Chris@0: $this->assertRaw($body_value, 'Body correctly displayed using Italian as requested language'); Chris@0: Chris@0: $this->drupalGet("node/{$node->id()}"); Chris@0: $this->assertRaw($body_value, 'Body correctly displayed using English as requested language'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests multilingual field display settings. Chris@0: */ Chris@0: public function testMultilingualDisplaySettings() { Chris@0: // Create "Basic page" content. Chris@0: $title_key = 'title[0][value]'; Chris@0: $title_value = $this->randomMachineName(8); Chris@0: $body_key = 'body[0][value]'; Chris@0: $body_value = $this->randomMachineName(16); Chris@0: Chris@0: // Create node to edit. Chris@0: $edit = []; Chris@0: $edit[$title_key] = $title_value; Chris@0: $edit[$body_key] = $body_value; Chris@0: $this->drupalPostForm('node/add/page', $edit, t('Save')); Chris@0: Chris@0: // Check that the node exists in the database. Chris@0: $node = $this->drupalGetNodeByTitle($edit[$title_key]); Chris@0: $this->assertTrue($node, 'Node found in database.'); Chris@0: Chris@0: // Check if node body is showed. Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: $body = $this->xpath('//article[contains(concat(" ", normalize-space(@class), " "), :node-class)]//div[contains(concat(" ", normalize-space(@class), " "), :content-class)]/descendant::p', [ Chris@0: ':node-class' => ' node ', Chris@0: ':content-class' => 'node__content', Chris@0: ]); Chris@0: $this->assertEqual($body[0]->getText(), $node->body->value, 'Node body found.'); Chris@0: } Chris@0: Chris@0: }