You are here

public function UniqueFieldAjaxLanguageTest::testUniqueTitlePerLang in Unique field ajax 2.x

Tests unique title per language.

Throws

\Drupal\Core\Entity\EntityStorageException

\Behat\Mink\Exception\ResponseTextException

File

tests/src/Functional/UniqueFieldAjaxLanguageTest.php, line 99

Class

UniqueFieldAjaxLanguageTest
Test the field permissions report page.

Namespace

Drupal\Tests\unique_field_ajax\Functional

Code

public function testUniqueTitlePerLang() {

  // Enable all the languages.
  foreach ($this->translationOptions as $lang_id => $name) {
    ConfigurableLanguage::createFromLangcode($lang_id)
      ->save();
  }

  // Grab the translation options and shuffle, for fun.
  $translation_options = $this->translationOptions;
  $this
    ->shuffleAssoc($translation_options);

  // Enable language settings.
  $edit = [
    'language_configuration[language_alterable]' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/structure/types/manage/' . $this->contentTypeName, $edit, t('Save content type'));
  $this
    ->updateThirdPartyEntitySetting('unique', TRUE);
  $this
    ->updateThirdPartyEntitySetting('per_lang', FALSE);
  $current_lang = 0;
  $edit = NULL;
  foreach ($translation_options as $lang_id => $name) {
    if (!$edit) {
      $edit = $this
        ->createBasicUpdateData();
    }
    $edit['langcode[0][value]'] = $lang_id;

    // First language should save, the rest will fail.
    if ($current_lang === 0) {
      $this
        ->itCanSaveField($edit);
    }
    else {
      $this
        ->itCannotSaveField($edit, NULL, NULL, 'title');
    }
    ++$current_lang;
  }
  $this
    ->updateThirdPartyEntitySetting('unique', TRUE);
  $this
    ->updateThirdPartyEntitySetting('per_lang', TRUE);
  $current_lang = 0;
  $edit = NULL;

  // Confirms across languages can contain the same information.
  foreach ($translation_options as $lang_id => $name) {
    if (!$edit) {
      $edit = $this
        ->createBasicUpdateData();
    }
    $edit['langcode[0][value]'] = $lang_id;

    // Confirm we can save one but not another.
    // Then on second loop if "pre_lang" enabled, it can still be saved.
    $this
      ->itCanSaveField($edit);
    $this
      ->itCannotSaveField($edit, NULL, NULL, 'title');
    ++$current_lang;
  }
}