public function ContentEntitySuggestionsTest::testSuggestions

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
          ->assertEquals(2, $suggestion['job_item']
          ->getWordCount(), 'Two translatable words in the suggestion.');
        $this
          ->assertEquals('node', $suggestion['job_item']
          ->getItemType(), '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
          ->assertEquals(3, $suggestion['job_item']
          ->getWordCount(), 'Three translatable words in the suggestion.');
        $this
          ->assertEquals('menu_link_content', $suggestion['job_item']
          ->getItemType(), 'Got a menu link in the suggestion.');
        $this
          ->assertEquals($node->link
          ->id(), $suggestion['job_item']
          ->getItemId(), 'Menu link id match between menu link and suggestion.');
        break;
      default:
        $this
          ->fail('Found an invalid suggestion.');
        break;
    }
    $this
      ->assertEquals('content', $suggestion['job_item']
      ->getPlugin(), 'Got a content entity as plugin in the suggestion.');
    $this
      ->assertEquals($item
      ->id(), $suggestion['from_item']);
    $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
    ->assertCount(0, $suggestions, 'Found no more suggestions.');
}