Chris@0: installEntitySchema('user'); Chris@0: Chris@0: // Activate Spanish language, so there are two languages activated. Chris@0: $language = $this->container->get('entity.manager')->getStorage('configurable_language')->create([ Chris@0: 'id' => 'es', Chris@0: ]); Chris@0: $language->save(); Chris@0: Chris@0: // Create a new content type which has Undefined language by default. Chris@0: $this->createContentType('ctund', LanguageInterface::LANGCODE_NOT_SPECIFIED); Chris@0: // Create a new content type which has Spanish language by default. Chris@0: $this->createContentType('ctes', 'es'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that default language code is properly set for new nodes. Chris@0: */ Chris@0: public function testEntityTranslationDefaultLanguageViaCode() { Chris@0: // With language module activated, and a content type that is configured to Chris@0: // have no language by default, a new node of this content type will have Chris@0: // "und" language code when language is not specified. Chris@0: $node = $this->createNode('ctund'); Chris@0: $this->assertEqual($node->langcode->value, LanguageInterface::LANGCODE_NOT_SPECIFIED); Chris@0: // With language module activated, and a content type that is configured to Chris@0: // have no language by default, a new node of this content type will have Chris@0: // "es" language code when language is specified as "es". Chris@0: $node = $this->createNode('ctund', 'es'); Chris@0: $this->assertEqual($node->langcode->value, 'es'); Chris@0: Chris@0: // With language module activated, and a content type that is configured to Chris@0: // have language "es" by default, a new node of this content type will have Chris@0: // "es" language code when language is not specified. Chris@0: $node = $this->createNode('ctes'); Chris@0: $this->assertEqual($node->langcode->value, 'es'); Chris@0: // With language module activated, and a content type that is configured to Chris@0: // have language "es" by default, a new node of this content type will have Chris@0: // "en" language code when language "en" is specified. Chris@0: $node = $this->createNode('ctes', 'en'); Chris@0: $this->assertEqual($node->langcode->value, 'en'); Chris@0: Chris@0: // Disable language module. Chris@0: $this->disableModules(['language']); Chris@0: Chris@0: // With language module disabled, and a content type that is configured to Chris@0: // have no language specified by default, a new node of this content type Chris@0: // will have site's default language code when language is not specified. Chris@0: $node = $this->createNode('ctund'); Chris@0: $this->assertEqual($node->langcode->value, 'en'); Chris@0: // With language module disabled, and a content type that is configured to Chris@0: // have no language specified by default, a new node of this type will have Chris@0: // "es" language code when language "es" is specified. Chris@0: $node = $this->createNode('ctund', 'es'); Chris@0: $this->assertEqual($node->langcode->value, 'es'); Chris@0: Chris@0: // With language module disabled, and a content type that is configured to Chris@0: // have language "es" by default, a new node of this type will have site's Chris@0: // default language code when language is not specified. Chris@0: $node = $this->createNode('ctes'); Chris@0: $this->assertEqual($node->langcode->value, 'en'); Chris@0: // With language module disabled, and a content type that is configured to Chris@0: // have language "es" by default, a new node of this type will have "en" Chris@0: // language code when language "en" is specified. Chris@0: $node = $this->createNode('ctes', 'en'); Chris@0: $this->assertEqual($node->langcode->value, 'en'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a new node content type. Chris@0: * Chris@0: * @param string $name Chris@0: * The content type name. Chris@0: * @param string $langcode Chris@0: * Default language code of the nodes of this type. Chris@0: */ Chris@0: protected function createContentType($name, $langcode) { Chris@0: $content_type = $this->container->get('entity.manager')->getStorage('node_type')->create([ Chris@0: 'name' => 'Test ' . $name, Chris@0: 'title_label' => 'Title', Chris@0: 'type' => $name, Chris@0: 'create_body' => FALSE, Chris@0: ]); Chris@0: $content_type->save(); Chris@0: ContentLanguageSettings::loadByEntityTypeBundle('node', $name) Chris@0: ->setLanguageAlterable(FALSE) Chris@0: ->setDefaultLangcode($langcode) Chris@0: ->save(); Chris@0: Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a new node of given type and language using Entity API. Chris@0: * Chris@0: * @param string $type Chris@0: * The node content type. Chris@0: * @param string $langcode Chris@0: * (optional) Language code to pass to entity create. Chris@0: * Chris@0: * @return \Drupal\node\NodeInterface Chris@0: * The node created. Chris@0: */ Chris@0: protected function createNode($type, $langcode = NULL) { Chris@0: $values = [ Chris@0: 'type' => $type, Chris@0: 'title' => $this->randomString(), Chris@0: ]; Chris@0: if (!empty($langcode)) { Chris@0: $values['langcode'] = $langcode; Chris@0: } Chris@0: $node = $this->container->get('entity.manager')->getStorage('node')->create($values); Chris@0: return $node; Chris@0: } Chris@0: Chris@0: }