<?php
namespace Drupal\Tests\tmgmt_content\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\Node;
use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
use Drupal\Tests\tmgmt\Functional\TmgmtEntityTestTrait;
use Drupal\Tests\tmgmt\Functional\TMGMTTestBase;
use Drupal\tmgmt_composite_test\Entity\EntityTestComposite;
class ContentEntitySourceTranslatableEntityTest extends TMGMTTestBase {
use TmgmtEntityTestTrait;
use EntityReferenceFieldCreationTrait;
protected static $modules = array(
'node',
'field',
'tmgmt_composite_test',
'tmgmt_content',
);
function setUp() : void {
parent::setUp();
$this
->addLanguage('de');
$this
->loginAsAdmin([
'administer tmgmt',
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('entity_test_composite', 'entity_test_composite', TRUE);
$content_translation_manager
->setEnabled('node', 'article', TRUE);
}
public function testTranslatableEntityReferences() {
$this
->drupalGet('/admin/tmgmt/settings');
$xpath = '//*[@id="edit-content"]';
$embedded_entity = '<label for="edit-always-embedded">Always embedded</label>';
$embedded_node = '<span class="fieldset-legend">Content</span>';
$this
->assertSession()
->pageTextNotContains('Authored by (User)');
$this
->assertStringNotContainsString($embedded_entity, $this
->xpath($xpath)[0]
->getOuterHtml());
$this
->assertStringNotContainsString($embedded_node, $this
->xpath($xpath)[0]
->getOuterHtml());
$this
->createEntityReferenceField('node', 'article', 'entity_test_composite', 'entity_test_composite', 'entity_test_composite');
FieldConfig::loadByName('node', 'article', 'entity_test_composite')
->setTranslatable(FALSE)
->save();
$this
->drupalGet('/admin/tmgmt/settings');
$this
->assertSession()
->pageTextContains('Content: entity_test_composite');
$this
->assertStringContainsString($embedded_entity, $this
->xpath($xpath)[0]
->getOuterHtml());
$composite = EntityTestComposite::create(array(
'name' => 'composite name',
));
$composite
->save();
$node = $this
->createNode(array(
'title' => 'node title',
'type' => 'article',
'entity_test_composite' => $composite,
));
$job = $this
->createJob();
$job
->save();
$job_item = tmgmt_job_item_create('content', $node
->getEntityTypeId(), $node
->id(), [
'tjid' => $job
->id(),
]);
$job_item
->save();
$data = $job_item
->getData();
$this
->assertTrue(isset($data['entity_test_composite']));
$this
->assertEquals('entity_test_composite', $data['entity_test_composite']['#label']);
$this
->assertFalse(isset($data['entity_test_composite'][0]['#label']));
$this
->assertEquals('Name', $data['entity_test_composite'][0]['entity']['name']['#label']);
$this
->assertEquals('composite name', $data['entity_test_composite'][0]['entity']['name'][0]['value']['#text']);
$this
->drupalGet('/admin/tmgmt/sources');
$this
->assertSession()
->optionExists('edit-source', 'content:node');
$this
->assertSession()
->optionNotExists('edit-source', 'content:entity_test_composite');
$job->translator = $this->default_translator
->id();
$job
->requestTranslation();
$items = $job
->getItems();
$item = reset($items);
$item
->acceptTranslation();
$node = Node::load($node
->id());
$translation = $node
->getTranslation('de');
$this
->assertEquals($node
->get('entity_test_composite')->target_id, $translation
->get('entity_test_composite')->target_id);
$composite = EntityTestComposite::load($translation
->get('entity_test_composite')->target_id);
$composite = $composite
->getTranslation('de');
$this
->assertEquals('de(de-ch): composite name', $composite
->label());
}
public function testTranslatableEntityReferencesUntranslatableTarget() {
\Drupal::service('content_translation.manager')
->setEnabled('entity_test_composite', 'entity_test_composite', FALSE);
$this
->createEntityReferenceField('node', 'article', 'entity_test_t_composite', 'entity_test_t_composite', 'entity_test_composite');
$this
->drupalGet('/admin/tmgmt/settings');
$this
->assertSession()
->fieldExists('embedded_fields[node][entity_test_t_composite]');
$this
->assertSession()
->pageTextContains('Note: This is a translatable field to an untranslatable target. A copy of the target will be created when translating.');
}
}