function LocaleContentTest::testContentTypeLanguageConfiguration

Test if a content type can be set to multilingual and language is present.

File

drupal/core/modules/locale/lib/Drupal/locale/Tests/LocaleContentTest.php, line 63
Definition of Drupal\locale\Tests\LocaleContentTest.

Class

LocaleContentTest
Functional tests for multilingual support on nodes.

Namespace

Drupal\locale\Tests

Code

function testContentTypeLanguageConfiguration() {
  global $base_url;
  $type1 = $this
    ->drupalCreateContentType();
  $type2 = $this
    ->drupalCreateContentType();

  // User to add and remove language.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer languages',
    'administer content types',
    'access administration pages',
  ));

  // User to create a node.
  $web_user = $this
    ->drupalCreateUser(array(
    "create {$type1->type} content",
    "create {$type2->type} content",
    "edit any {$type2->type} content",
  ));

  // Add custom language.
  $this
    ->drupalLogin($admin_user);

  // Code for the language.
  $langcode = 'xx';

  // The English name for the language.
  $name = $this
    ->randomName(16);
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'name' => $name,
    'direction' => '0',
  );
  $this
    ->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));

  // Set the content type to use multilingual support.
  $this
    ->drupalGet("admin/structure/types/manage/{$type2->type}");
  $this
    ->assertText(t('Language settings'), 'Multilingual support widget present on content type configuration form.');
  $edit = array(
    'language_configuration[language_hidden]' => FALSE,
  );
  $this
    ->drupalPost("admin/structure/types/manage/{$type2->type}", $edit, t('Save content type'));
  $this
    ->assertRaw(t('The content type %type has been updated.', array(
    '%type' => $type2->name,
  )));
  $this
    ->drupalLogout();

  // Verify language selection is not present on the node add form.
  $this
    ->drupalLogin($web_user);
  $this
    ->drupalGet("node/add/{$type1->type}");

  // Verify language select list is not present.
  $this
    ->assertNoFieldByName('language', NULL, 'Language select not present on the node add form.');

  // Verify language selection appears on the node add form.
  $this
    ->drupalGet("node/add/{$type2->type}");

  // Verify language select list is present.
  $this
    ->assertFieldByName('langcode', NULL, 'Language select present on the node add form.');

  // Ensure language appears.
  $this
    ->assertText($name, 'Language present.');

  // Create a node.
  $node_title = $this
    ->randomName();
  $node_body = $this
    ->randomName();
  $edit = array(
    'type' => $type2->type,
    'title' => $node_title,
    'body' => array(
      $langcode => array(
        array(
          'value' => $node_body,
        ),
      ),
    ),
    'langcode' => $langcode,
  );
  $node = $this
    ->drupalCreateNode($edit);

  // Edit the content and ensure correct language is selected.
  $path = 'node/' . $node->nid . '/edit';
  $this
    ->drupalGet($path);
  $this
    ->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', t('Correct language selected.'));

  // Ensure we can change the node language.
  $edit = array(
    'langcode' => 'en',
  );
  $this
    ->drupalPost($path, $edit, t('Save'));
  $this
    ->assertRaw(t('%title has been updated.', array(
    '%title' => $node_title,
  )));
  $this
    ->drupalLogout();
}