You are here

public function MergeTermsTargetTest::newOrExistingTermMustBeSelected in Term Merge 8

Tests validation of the target term.

@test @dataProvider selectedTargetsProvider

File

tests/src/Kernel/Form/MergeTermsTargetTest.php, line 97

Class

MergeTermsTargetTest
Tests the merge terms target terms form.

Namespace

Drupal\Tests\term_merge\Kernel\Form

Code

public function newOrExistingTermMustBeSelected($selectedTerms) {
  $sut = new MergeTermsTarget($this->entityTypeManager, $this->privateTempStoreFactory);
  $knownTermIds = array_keys($this->terms);
  $selectedTermIds = array_slice($knownTermIds, 0, 2);
  $this->privateTempStoreFactory
    ->get('term_merge')
    ->set('terms', $selectedTermIds);
  $formState = new FormState();
  $build = $sut
    ->buildForm([], $formState, $this->vocabulary);
  self::assertEmpty($formState
    ->getErrors());
  if ($selectedTerms == 'both') {
    $formState
      ->setValue('new', 'New term');
    $formState
      ->setValue('existing', end($knownTermIds));
  }
  $sut
    ->validateForm($build, $formState);
  $expectedError = new TranslatableMarkup('You must either select an existing term or enter a new term.');
  self::assertEquals([
    'new' => $expectedError,
  ], $formState
    ->getErrors());
}