<?php
namespace Drupal\Tests\tmgmt_content\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\tmgmt\Kernel\TMGMTKernelTestBase;
class ContentEntitySuggestionsTest extends TMGMTKernelTestBase {
protected static $modules = array(
'menu_link_content',
'link',
'tmgmt_content',
'tmgmt_test',
'content_translation',
'node',
'filter',
);
public function setUp() : void {
parent::setUp();
$this
->installEntitySchema('menu_link_content');
$this
->installEntitySchema('node');
$this
->installConfig([
'node',
]);
}
protected function prepareTranslationSuggestions() {
$untranslatable_type = NodeType::create([
'type' => $this
->randomMachineName(),
]);
$untranslatable_type
->save();
$type = NodeType::create([
'type' => $this
->randomMachineName(),
]);
$type
->save();
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('node', $type
->id(), TRUE);
$field1 = FieldStorageConfig::create(array(
'field_name' => 'field1',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => 'node',
),
));
$field1
->save();
$field2 = FieldStorageConfig::create(array(
'field_name' => 'field2',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => 'node',
),
));
$field2
->save();
$embedded_field = FieldStorageConfig::create(array(
'field_name' => 'embedded_field',
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => 'node',
),
));
$embedded_field
->save();
$this
->config('tmgmt_content.settings')
->set('embedded_fields.node.embedded_field', TRUE)
->save();
FieldConfig::create(array(
'field_storage' => $field1,
'bundle' => $type
->id(),
'label' => 'Field 1',
'translatable' => FALSE,
'settings' => array(),
))
->save();
FieldConfig::create(array(
'field_storage' => $field2,
'bundle' => $type
->id(),
'label' => 'Field 2',
'translatable' => TRUE,
'settings' => array(),
))
->save();
FieldConfig::create(array(
'field_storage' => $embedded_field,
'bundle' => $type
->id(),
'label' => 'Field 2',
'translatable' => TRUE,
'settings' => array(),
))
->save();
node_add_body_field($type);
$field = FieldConfig::loadByName('node', $type
->id(), 'body');
$field
->setTranslatable(TRUE);
$field
->save();
$references = array();
for ($i = 0; $i < 4; $i++) {
$references[$i] = Node::create(array(
'title' => $this
->randomMachineName(),
'body' => $this
->randomMachineName(),
'type' => $type
->id(),
));
$references[$i]
->save();
}
$untranslatable_node = Node::create([
'title' => $this
->randomMachineName(),
'type' => $untranslatable_type
->id(),
]);
$untranslatable_node
->save();
$different_language_node = Node::create([
'title' => $this
->randomMachineName(),
'type' => $type
->id(),
'langcode' => 'de',
]);
$different_language_node
->save();
$node = Node::create([
'title' => $this
->randomMachineName(),
'type' => $type
->id(),
'language' => 'en',
'body' => $this
->randomMachineName(),
$field1
->getName() => [
[
'target_id' => $references[0]
->id(),
],
[
'target_id' => $references[1]
->id(),
],
],
$field2
->getName() => [
[
'target_id' => $references[2]
->id(),
],
[
'target_id' => $untranslatable_node
->id(),
],
[
'target_id' => $different_language_node
->id(),
],
],
$embedded_field
->getName() => [
[
'target_id' => $references[3]
->id(),
],
],
]);
$node
->save();
$link = MenuLinkContent::create([
'link' => [
[
'uri' => 'entity:node/' . $node
->id(),
],
],
'title' => 'Node menu link',
'menu_name' => 'main',
]);
$link
->save();
$node->link = $link;
$second_link = MenuLinkContent::create([
'link' => [
[
'uri' => 'entity:node/' . $node
->id(),
],
],
'title' => 'German Node menu link',
'menu_name' => 'main',
'langcode' => 'de',
]);
$second_link
->save();
return $node;
}
public function testSuggestions() {
$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());
$suggestions = $job
->getSuggestions();
$job
->cleanSuggestionsList($suggestions);
$this
->assertEquals(4, count($suggestions));
foreach ($suggestions as $suggestion) {
switch ($suggestion['job_item']
->getItemType()) {
case 'node':
$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':
$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']);
}
$this
->assertTrue(empty($expected_nodes), 'Found unexpected node suggestions.');
$suggestions = $job
->getSuggestions();
$job
->cleanSuggestionsList($suggestions);
$this
->assertCount(0, $suggestions, 'Found no more suggestions.');
}
}