You are here

public function ContentEntitySuggestionsTest::testSuggestions in Translation Management Tool 8

Test suggested entities from a translation job.

File

sources/content/tests/src/Kernel/ContentEntitySuggestionsTest.php, line 188

Class

ContentEntitySuggestionsTest
Basic Source-Suggestions tests.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testSuggestions() {

  // Prepare a job and a node for testing.
  $job = $this
    ->createJob();
  $node = $this
    ->prepareTranslationSuggestions();
  $expected_nodes = array(
    $node->field1[0]->target_id => $node->field1[0]->target_id,
    $node->field1[1]->target_id => $node->field1[1]->target_id,
    $node->field2[0]->target_id => $node->field2[0]->target_id,
  );
  $item = $job
    ->addItem('content', 'node', $node
    ->id());

  // Get all suggestions and clean the list.
  $suggestions = $job
    ->getSuggestions();
  $job
    ->cleanSuggestionsList($suggestions);

  // There should be 4 suggestions, 3 translatable nodes and the menu link.
  $this
    ->assertEquals(4, count($suggestions));
  foreach ($suggestions as $suggestion) {
    switch ($suggestion['job_item']
      ->getItemType()) {
      case 'node':

        // Check for valid attributes on the node suggestions.
        $this
          ->assertEqual($suggestion['job_item']
          ->getWordCount(), 2, 'Two translatable words in the suggestion.');
        $this
          ->assertEqual($suggestion['job_item']
          ->getItemType(), 'node', 'Got a node in the suggestion.');
        $this
          ->assertTrue(in_array($suggestion['job_item']
          ->getItemId(), $expected_nodes), 'Node id match between node and suggestion.');
        unset($expected_nodes[$suggestion['job_item']
          ->getItemId()]);
        break;
      case 'menu_link_content':

        // Check for valid attributes on the menu link suggestions.
        $this
          ->assertEqual($suggestion['job_item']
          ->getWordCount(), 3, 'Three translatable words in the suggestion.');
        $this
          ->assertEqual($suggestion['job_item']
          ->getItemType(), 'menu_link_content', 'Got a menu link in the suggestion.');
        $this
          ->assertEqual($suggestion['job_item']
          ->getItemId(), $node->link
          ->id(), 'Menu link id match between menu link and suggestion.');
        break;
      default:
        $this
          ->fail('Found an invalid suggestion.');
        break;
    }
    $this
      ->assertEqual($suggestion['job_item']
      ->getPlugin(), 'content', 'Got a content entity as plugin in the suggestion.');
    $this
      ->assertEqual($suggestion['from_item'], $item
      ->id());
    $job
      ->addExistingItem($suggestion['job_item']);
  }

  // Check that we tested all expected nodes.
  $this
    ->assertTrue(empty($expected_nodes), 'Found unexpected node suggestions.');

  // Add the suggestion to the job and re-get all suggestions.
  $suggestions = $job
    ->getSuggestions();
  $job
    ->cleanSuggestionsList($suggestions);

  // Check for no more suggestions.
  $this
    ->assertEqual(count($suggestions), 0, 'Found no more suggestions.');
}