<?php
namespace Drupal\Tests\tmgmt_content\Kernel;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\tmgmt\ContinuousTranslatorInterface;
use Drupal\tmgmt\Entity\Translator;
use Drupal\tmgmt\JobItemInterface;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt_test\EventSubscriber\TestContinuousEventSubscriber;
class ContentEntitySourceUnitTest extends ContentEntityTestBase {
protected static $modules = [
'node',
'file',
'image',
];
protected $image_label;
public function setUp() : void {
parent::setUp();
$filter = FilterFormat::create([
'format' => 'unallowed_format',
'name' => 'Unallowed Format',
]);
$filter
->save();
$this
->config('tmgmt.settings')
->set('allowed_formats', [
'text_plain',
])
->save();
$this
->installSchema('node', [
'node_access',
]);
FieldStorageConfig::create([
'entity_type' => $this->entityTypeId,
'field_name' => 'field_test_text',
'type' => 'text',
'cardinality' => 1,
])
->save();
FieldConfig::create([
'entity_type' => $this->entityTypeId,
'field_name' => 'field_test_text',
'bundle' => $this->entityTypeId,
'label' => 'Test text-field',
'translatable' => FALSE,
])
->save();
$field_storage = FieldStorageConfig::loadByName($this->entityTypeId, 'field_test_text');
$field_storage
->setCardinality(4);
$field_storage
->save();
$field = FieldConfig::loadByName($this->entityTypeId, $this->entityTypeId, 'field_test_text');
$field
->setTranslatable(TRUE);
$field
->save();
$this
->installEntitySchema('file');
$this
->installSchema('file', array(
'file_usage',
));
$this
->installConfig(array(
'node',
));
$field_storage = FieldStorageConfig::create(array(
'field_name' => 'image_test',
'entity_type' => $this->entityTypeId,
'type' => 'image',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'translatable' => TRUE,
));
$field_storage
->save();
FieldConfig::create(array(
'entity_type' => $this->entityTypeId,
'field_storage' => $field_storage,
'bundle' => $this->entityTypeId,
'label' => $this->image_label = $this
->randomMachineName(),
))
->save();
\Drupal::service('file_system')
->copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = File::create([
'uri' => 'public://example.jpg',
]);
$this->image
->save();
$field_storage = FieldStorageConfig::create(array(
'field_name' => 'ignored_field',
'entity_type' => $this->entityTypeId,
'type' => 'text',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'translatable' => TRUE,
));
$field_storage
->save();
FieldConfig::create(array(
'entity_type' => $this->entityTypeId,
'field_storage' => $field_storage,
'bundle' => $this->entityTypeId,
'label' => $this
->randomMachineName(),
))
->setThirdPartySetting('tmgmt_content', 'excluded', TRUE)
->save();
$field_storage = FieldStorageConfig::create(array(
'field_name' => 'later_addition',
'entity_type' => $this->entityTypeId,
'type' => 'text',
'cardinality' => 1,
'translatable' => TRUE,
));
$field_storage
->save();
FieldConfig::create(array(
'entity_type' => $this->entityTypeId,
'field_storage' => $field_storage,
'bundle' => $this->entityTypeId,
'label' => $this->later_addition_label = $this
->randomMachineName(),
))
->setTranslatable(TRUE)
->save();
}
public function testEntityTest() {
$values = array(
'langcode' => 'en',
'user_id' => 1,
);
$entity_test = EntityTestMul::create($values);
$entity_test->name->value = $this
->randomMachineName();
$entity_test->field_test_text
->appendItem([
'value' => $this
->randomMachineName(),
'format' => 'text_plain',
]);
$entity_test->field_test_text
->appendItem([
'value' => $this
->randomMachineName(),
'format' => 'text_plain',
]);
$entity_test->field_test_text
->appendItem([
'value' => $this
->randomMachineName(),
'format' => 'unallowed_format',
]);
$entity_test->field_test_text
->appendItem([
'value' => $this
->randomMachineName(),
'format' => 'text_plain',
]);
$values = array(
'target_id' => $this->image
->id(),
'alt' => $this
->randomMachineName(),
'title' => $this
->randomMachineName(),
);
$entity_test->image_test
->appendItem($values);
$entity_test->ignored_field
->appendItem([
'value' => 'This field should not be translated.',
'format' => 'text_plain',
]);
$entity_test
->save();
$job = tmgmt_job_create('en', 'de');
$job->translator = 'test_translator';
$job
->save();
$job_item = tmgmt_job_item_create('content', $this->entityTypeId, $entity_test
->id(), array(
'tjid' => $job
->id(),
));
$job_item
->save();
$source_plugin = $this->container
->get('plugin.manager.tmgmt.source')
->createInstance('content');
$data = $source_plugin
->getData($job_item);
$this
->assertEquals('Name', $data['name']['#label']);
$this
->assertFalse(isset($data['name'][0]['#label']));
$this
->assertFalse(isset($data['name'][0]['value']['#label']));
$this
->assertEquals($entity_test->name->value, $data['name'][0]['value']['#text']);
$this
->assertTrue($data['name'][0]['value']['#translate']);
$this
->assertEquals('Test text-field', $data['field_test_text']['#label']);
$this
->assertEquals('Delta #0', $data['field_test_text'][0]['#label']);
$this
->assertFalse(isset($data['field_test_text'][0]['value']['#label']));
$this
->assertEquals($entity_test->field_test_text->value, $data['field_test_text'][0]['value']['#text']);
$this
->assertTrue($data['field_test_text'][0]['value']['#translate']);
$this
->assertFalse(isset($data['field_test_text'][0]['format']['#label']));
$this
->assertEquals('text_plain', $data['field_test_text'][0]['value']['#format']);
$this
->assertEquals($entity_test->field_test_text->format, $data['field_test_text'][0]['format']['#text']);
$this
->assertFalse($data['field_test_text'][0]['format']['#translate']);
$this
->assertFalse(isset($data['field_test_text'][0]['processed']));
$this
->assertEquals('Delta #1', $data['field_test_text'][1]['#label']);
$this
->assertFalse(isset($data['field_test_text'][1]['value']['#label']));
$this
->assertEquals($entity_test->field_test_text[1]->value, $data['field_test_text'][1]['value']['#text']);
$this
->assertTrue($data['field_test_text'][1]['value']['#translate']);
$this
->assertFalse(isset($data['field_test_text'][1]['format']['#label']));
$this
->assertEquals('text_plain', $data['field_test_text'][1]['value']['#format']);
$this
->assertEquals($entity_test->field_test_text[1]->format, $data['field_test_text'][1]['format']['#text']);
$this
->assertFalse($data['field_test_text'][1]['format']['#translate']);
$this
->assertFalse(isset($data['field_test_text'][1]['processed']));
$this
->assertEquals('Delta #2', $data['field_test_text'][2]['#label']);
$this
->assertFalse(isset($data['field_test_text'][2]['value']['#label']));
$this
->assertEquals($entity_test->field_test_text[2]->value, $data['field_test_text'][2]['value']['#text']);
$this
->assertFalse($data['field_test_text'][2]['value']['#translate']);
$this
->assertFalse(isset($data['field_test_text'][2]['format']['#label']));
$this
->assertEquals($entity_test->field_test_text[2]->format, $data['field_test_text'][2]['format']['#text']);
$this
->assertFalse($data['field_test_text'][2]['format']['#translate']);
$this
->assertFalse(isset($data['field_test_text'][2]['processed']));
$this
->assertEquals('Delta #3', $data['field_test_text'][3]['#label']);
$this
->assertFalse(isset($data['field_test_text'][3]['value']['#label']));
$this
->assertEquals('text_plain', $data['field_test_text'][3]['value']['#format']);
$this
->assertEquals($entity_test->field_test_text[3]->value, $data['field_test_text'][3]['value']['#text']);
$this
->assertTrue($data['field_test_text'][3]['value']['#translate']);
$this
->assertFalse(isset($data['field_test_text'][3]['format']['#label']));
$this
->assertEquals($entity_test->field_test_text[3]->format, $data['field_test_text'][3]['format']['#text']);
$this
->assertFalse($data['field_test_text'][3]['format']['#translate']);
$this
->assertFalse(isset($data['field_test_text'][3]['processed']));
$image_item = $data['image_test'][0];
$this
->assertEquals($this->image_label, $data['image_test']['#label']);
$this
->assertFalse(isset($image_item['#label']));
$this
->assertFalse($image_item['target_id']['#translate']);
$this
->assertFalse($image_item['width']['#translate']);
$this
->assertFalse($image_item['height']['#translate']);
$this
->assertTrue($image_item['alt']['#translate']);
$this
->assertEquals('Alternative text', $image_item['alt']['#label']);
$this
->assertEquals($entity_test->image_test->alt, $image_item['alt']['#text']);
$this
->assertTrue($image_item['title']['#translate']);
$this
->assertEquals('Title', $image_item['title']['#label']);
$this
->assertEquals($entity_test->image_test->title, $image_item['title']['#text']);
$this
->assertFalse(isset($data['ignored_field']));
$job
->requestTranslation();
$entity_test
->get('field_test_text')
->offsetUnset(3);
$entity_test
->save();
$items = $job
->getItems();
$item = reset($items);
$item
->acceptTranslation();
$data = $item
->getData();
$entity_test = \Drupal::entityTypeManager()
->getStorage($this->entityTypeId)
->load($entity_test
->id());
$translation = $entity_test
->getTranslation('de');
$this
->assertEquals($data['name'][0]['value']['#translation']['#text'], $translation->name->value);
$this
->assertEquals($data['field_test_text'][0]['value']['#translation']['#text'], $translation->field_test_text[0]->value);
$this
->assertEquals($data['field_test_text'][1]['value']['#translation']['#text'], $translation->field_test_text[1]->value);
$this
->assertEquals($data['field_test_text'][2]['value']['#text'], $translation->field_test_text[2]->value);
$this
->assertEquals($data['field_test_text'][3]['value']['#translation']['#text'], $translation->field_test_text[3]->value);
$source = $entity_test
->getTranslation('en');
$source->later_addition
->appendItem([
'value' => 'This field data is added after the translation exists.',
'format' => 'text_plain',
]);
$entity_test
->save();
$job_item
->resetData();
$job_item
->save();
$source_plugin = $this->container
->get('plugin.manager.tmgmt.source')
->createInstance('content');
$data = $source_plugin
->getData($job_item);
$this
->assertEquals($this->later_addition_label, $data['later_addition']['#label']);
$this
->assertFalse(isset($data['later_addition'][0]['value']['#label']));
$this
->assertEquals($entity_test->later_addition->value, $data['later_addition'][0]['value']['#text']);
$this
->assertTrue($data['later_addition'][0]['value']['#translate']);
$this
->assertFalse(isset($data['later_addition'][0]['format']['#label']));
$this
->assertEquals('text_plain', $data['later_addition'][0]['value']['#format']);
$this
->assertEquals($entity_test->later_addition->format, $data['later_addition'][0]['format']['#text']);
$this
->assertFalse($data['later_addition'][0]['format']['#translate']);
$this
->assertFalse(isset($data['later_addition'][0]['processed']));
$job
->requestTranslation();
$items = $job
->getItems();
$item = reset($items);
$item
->acceptTranslation();
$data = $item
->getData();
$entity_test = \Drupal::entityTypeManager()
->getStorage($this->entityTypeId)
->load($entity_test
->id());
$translation = $entity_test
->getTranslation('de');
$this
->assertEquals($data['later_addition'][0]['value']['#translation']['#text'], $translation->later_addition[0]->value);
}
public function testNode() {
$account = $this
->createUser();
$type = $this
->drupalCreateContentType();
$field = FieldStorageConfig::loadByName('node', 'body');
$field
->setTranslatable(TRUE);
$field
->setCardinality(2);
$field
->save();
$node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'en',
]);
$value = [
'value' => $this
->randomMachineName(),
'summary' => $this
->randomMachineName(),
'format' => 'text_plain',
];
$node->body
->appendItem($value);
$node->body
->appendItem($value);
$node
->save();
$job = tmgmt_job_create('en', 'de');
$job
->save();
$job_item = tmgmt_job_item_create('content', 'node', $node
->id(), array(
'tjid' => $job
->id(),
));
$job_item
->save();
$source_plugin = $this->container
->get('plugin.manager.tmgmt.source')
->createInstance('content');
$data = $source_plugin
->getData($job_item);
$this
->assertEquals('Title', $data['title']['#label']);
$this
->assertFalse(isset($data['title'][0]['#label']));
$this
->assertFalse(isset($data['title'][0]['value']['#label']));
$this
->assertEquals($node
->getTitle(), $data['title'][0]['value']['#text']);
$this
->assertTrue($data['title'][0]['value']['#translate']);
$this
->assertEquals('Body', $data['body']['#label']);
$this
->assertEquals('Delta #0', $data['body'][0]['#label']);
$this
->assertEquals('Text', (string) $data['body'][0]['value']['#label']);
$this
->assertEquals($node->body->value, $data['body'][0]['value']['#text']);
$this
->assertTrue($data['body'][0]['value']['#translate']);
$this
->assertEquals('text_plain', $data['body'][0]['value']['#format']);
$this
->assertEquals('Summary', (string) $data['body'][0]['summary']['#label']);
$this
->assertEquals($node->body->summary, $data['body'][0]['summary']['#text']);
$this
->assertTrue($data['body'][0]['summary']['#translate']);
$this
->assertEquals('Text format', (string) $data['body'][0]['format']['#label']);
$this
->assertEquals($node->body->format, $data['body'][0]['format']['#text']);
$this
->assertFalse($data['body'][0]['format']['#translate']);
$this
->assertFalse(isset($data['body'][0]['processed']));
$this
->assertEquals('Delta #1', $data['body'][1]['#label']);
$this
->assertEquals('Text', (string) $data['body'][1]['value']['#label']);
$this
->assertEquals($node->body[1]->value, $data['body'][1]['value']['#text']);
$this
->assertTrue($data['body'][1]['value']['#translate']);
$this
->assertEquals('Summary', (string) $data['body'][1]['summary']['#label']);
$this
->assertEquals($node->body[1]->summary, $data['body'][1]['summary']['#text']);
$this
->assertTrue($data['body'][1]['summary']['#translate']);
$this
->assertEquals('text_plain', $data['body'][0]['summary']['#format']);
$this
->assertEquals('Text format', (string) $data['body'][1]['format']['#label']);
$this
->assertEquals($node->body[1]->format, $data['body'][1]['format']['#text']);
$this
->assertFalse($data['body'][1]['format']['#translate']);
$this
->assertFalse(isset($data['body'][1]['processed']));
$job->translator = 'test_translator';
$job
->requestTranslation();
$job
->acceptTranslation();
$tracked_translation_insertions = tmgmt_test_tracked_translation_inserts();
$this
->assertTrue($tracked_translation_insertions['node'][$node
->id()]['de']);
$node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$node
->save();
$job = tmgmt_job_create(LanguageInterface::LANGCODE_NOT_SPECIFIED, 'de');
$job
->save();
$job_item = tmgmt_job_item_create('content', 'node', $node
->id(), array(
'tjid' => $job
->id(),
));
$this
->expectException(EntityStorageException::class);
$this
->expectExceptionMessage('Entity <em class="placeholder">und</em> could not be translated because the language <em class="placeholder">Not specified</em> is not applicable');
$job_item
->save();
}
public function testAcceptTranslation() {
$account = $this
->createUser();
$type = $this
->drupalCreateContentType();
$this->container
->get('content_translation.manager')
->setEnabled('node', $type
->id(), TRUE);
$translator = Translator::load('test_translator');
$translator
->setAutoAccept(TRUE)
->save();
$node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'en',
]);
$node
->save();
$job = tmgmt_job_create('en', 'de');
$job->translator = 'test_translator';
$job
->save();
$job_item = tmgmt_job_item_create('content', $node
->getEntityTypeId(), $node
->id(), array(
'tjid' => $job
->id(),
));
$job_item
->save();
$job
->requestTranslation();
$items = $job
->getItems();
$item = reset($items);
$this
->assertEquals(JobItemInterface::STATE_ACCEPTED, $item
->getState());
$node = Node::load($node
->id());
$manager = $this->container
->get('content_translation.manager');
$this
->assertEquals('en', $manager
->getTranslationMetadata($node
->getTranslation('de'))
->getSource(), 'Source language is correct.');
}
public function testRequestDataForSpecificLanguage() {
$account = $this
->createUser();
$type = $this
->drupalCreateContentType();
$field = FieldStorageConfig::loadByName('node', 'body');
$field
->setTranslatable(TRUE);
$field
->setCardinality(2);
$field
->save();
$node = Node::create(array(
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'cs',
));
$node = $node
->addTranslation('en');
$node
->get('title')
->appendItem(array(
'value' => $this
->randomMachineName(),
));
$value = array(
'value' => $this
->randomMachineName(),
'summary' => $this
->randomMachineName(),
'format' => 'text_plain',
);
$node
->get('body')
->appendItem($value);
$node
->get('body')
->appendItem($value);
$node
->save();
$job = tmgmt_job_create('en', 'de');
$job
->save();
$job_item = tmgmt_job_item_create('content', 'node', $node
->id(), array(
'tjid' => $job
->id(),
));
$job_item
->save();
$source_plugin = $this->container
->get('plugin.manager.tmgmt.source')
->createInstance('content');
$data = $source_plugin
->getData($job_item);
$this
->assertEquals($value['value'], $data['body'][0]['value']['#text']);
}
protected function drupalCreateContentType($settings = array()) {
$name = strtolower($this
->randomMachineName(8));
$values = array(
'type' => $name,
'name' => $name,
'base' => 'node_content',
'title_label' => 'Title',
'body_label' => 'Body',
'has_title' => 1,
'has_body' => 1,
);
$type = NodeType::create($values);
$saved = $type
->save();
node_add_body_field($type);
$this
->assertEquals(SAVED_NEW, $saved);
return $type;
}
public function testEmbeddedReferences() {
$field1 = FieldStorageConfig::create(array(
'field_name' => 'field1',
'entity_type' => $this->entityTypeId,
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => $this->entityTypeId,
),
));
$field1
->save();
$field2 = FieldStorageConfig::create(array(
'field_name' => 'field2',
'entity_type' => $this->entityTypeId,
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => array(
'target_type' => $this->entityTypeId,
),
));
$field2
->save();
FieldConfig::create(array(
'field_storage' => $field1,
'bundle' => $this->entityTypeId,
'label' => 'Field 1',
'settings' => array(),
))
->save();
FieldConfig::create(array(
'field_storage' => $field2,
'bundle' => $this->entityTypeId,
'label' => 'Field 2',
'translatable' => FALSE,
'settings' => array(),
))
->save();
$this
->config('tmgmt_content.settings')
->set('embedded_fields.' . $this->entityTypeId . '.field1', TRUE)
->save();
$referenced_entities = [];
for ($i = 0; $i < 7; $i++) {
$referenced_values = [
'langcode' => $i < 5 ? 'en' : 'cs',
'user_id' => 1,
'name' => 'Referenced entity #' . $i,
];
$referenced_entities[$i] = EntityTestMul::create($referenced_values);
$referenced_entities[$i]
->save();
}
$referenced_entities[5]
->addTranslation('en', [
'name' => 'EN entity #5',
]);
$referenced_entities[5]
->save();
$values = array(
'langcode' => 'en',
'user_id' => 1,
'name' => $this
->randomString(),
'field1' => [
$referenced_entities[0],
$referenced_entities[1],
$referenced_entities[2],
$referenced_entities[5],
$referenced_entities[6],
],
'field2' => $referenced_entities[4],
);
$entity_test = EntityTestMul::create($values);
$entity_test
->save();
$job = tmgmt_job_create('en', 'de');
$job->translator = 'test_translator';
$job
->save();
$job_item = tmgmt_job_item_create('content', $this->entityTypeId, $entity_test
->id(), array(
'tjid' => $job
->id(),
));
$job_item
->save();
$source_plugin = $this->container
->get('plugin.manager.tmgmt.source')
->createInstance('content');
$data = $source_plugin
->getData($job_item);
$this
->assertFalse(isset($data['field2']));
$this
->assertEquals('Field 1', $data['field1']['#label']);
$this
->assertEquals('Delta #0', $data['field1'][0]['#label']);
$this
->assertEquals('Name', $data['field1'][0]['entity']['name']['#label'], 'Name');
$this
->assertEquals($data['field1'][0]['entity']['name'][0]['value']['#text'], 'Referenced entity #0');
$this
->assertEquals($data['field1'][1]['entity']['name'][0]['value']['#text'], 'Referenced entity #1');
$this
->assertEquals($data['field1'][2]['entity']['name'][0]['value']['#text'], 'Referenced entity #2');
$this
->assertEquals($data['field1'][3]['entity']['name'][0]['value']['#text'], 'EN entity #5');
$this
->assertEquals($data['field1'][4]['entity']['name'][0]['value']['#text'], 'Referenced entity #6');
$job
->requestTranslation();
$entity_test
->set('field1', [
$referenced_entities[2],
$referenced_entities[0],
$referenced_entities[5],
$referenced_entities[6],
]);
$entity_test
->save();
$items = $job
->getItems();
$item = reset($items);
$item
->acceptTranslation();
\Drupal::entityTypeManager()
->getStorage('entity_test_mul')
->resetCache();
$referenced_translation = EntityTestMul::load($referenced_entities[0]
->id())
->getTranslation('de');
$this
->assertEquals('de(de-ch): Referenced entity #0', $referenced_translation
->get('name')->value);
$referenced_translation = EntityTestMul::load($referenced_entities[2]
->id())
->getTranslation('de');
$this
->assertEquals('de(de-ch): Referenced entity #2', $referenced_translation
->get('name')->value);
$referenced_translation = EntityTestMul::load($referenced_entities[5]
->id())
->getTranslation('de');
$this
->assertEquals('de(de-ch): EN entity #5', $referenced_translation
->get('name')->value);
$referenced_translation = EntityTestMul::load($referenced_entities[6]
->id())
->getTranslation('de');
$this
->assertEquals('de(de-ch): Referenced entity #6', $referenced_translation
->get('name')->value);
$this
->assertFalse(EntityTestMul::load($referenced_entities[1]
->id())
->hasTranslation('de'));
$this
->assertFalse(EntityTestMul::load($referenced_entities[3]
->id())
->hasTranslation('de'));
$this
->assertFalse(EntityTestMul::load($referenced_entities[4]
->id())
->hasTranslation('de'));
}
public function testContinuousJobItems() {
$account = $this
->createUser();
$type = $this
->drupalCreateContentType();
$second_type = $this
->drupalCreateContentType();
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('node', $type
->label(), TRUE);
$content_translation_manager
->setEnabled('node', $second_type
->label(), TRUE);
$translator = Translator::load('test_translator');
$continuous_settings = [
'content' => [
'node' => [
'enabled' => 1,
'bundles' => [
$type
->id() => 1,
$second_type
->id() => 0,
],
],
],
];
$continuous_job = tmgmt_job_create('en', 'de', $account
->id(), [
'job_type' => Job::TYPE_CONTINUOUS,
'translator' => $translator,
'continuous_settings' => $continuous_settings,
]);
$this
->assertEquals(SAVED_NEW, $continuous_job
->save());
$prevented_node = Node::create([
'title' => TestContinuousEventSubscriber::DISALLOWED_LABEL,
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'en',
]);
$prevented_node
->save();
$this
->assertEquals(0, count($continuous_job
->getItems()));
$node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'en',
]);
$node
->save();
$continuous_job_items = $continuous_job
->getItems();
$continuous_job_item = reset($continuous_job_items);
$this
->assertEquals($node
->label(), $continuous_job_item
->label(), 'Continuous job item is automatically created for an english node.');
$this
->assertEquals($continuous_job_item
->getState(), JobItemInterface::STATE_REVIEW, 'Translation for an english node is in state review.');
$node
->set('title', $this
->randomMachineName());
$node
->save();
$this
->assertEquals(count($continuous_job
->getItems()), 1, 'There are no new job items for an english node.');
$continuous_job_item
->acceptTranslation();
$node = Node::load($node
->id());
$translation = $node
->getTranslation('de');
$data = $continuous_job_item
->getData();
$this
->assertEquals($translation
->label(), $data['title'][0]['value']['#translation']['#text'], 'Translation for an english node has been saved correctly.');
$this
->assertEquals($continuous_job_item
->getState(), JobItemInterface::STATE_ACCEPTED, 'Translation for an english node has been accepted.');
$german_node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'de',
]);
$german_node
->save();
$this
->assertEquals(count($continuous_job
->getItems()), 1, 'Continuous job item is not created for a german node.');
$second_node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $second_type
->id(),
'langcode' => 'en',
]);
$second_node
->save();
$this
->assertEquals(count($continuous_job
->getItems()), 1, 'Continuous job item is not created for a second english node.');
$node
->set('title', $this
->randomMachineName());
$node
->save();
$this
->assertEquals(count($continuous_job
->getItems()), 1, 'Continuous job item is not created for an updated english node.');
$translation = $node
->getTranslation('de');
$translation->content_translation_outdated->value = 1;
$translation
->save();
$this
->assertCount(2, $continuous_job
->getItems(), 'Continuous job item is automatically created for an updated english node.');
$continuous_job_item_recent = $continuous_job
->getMostRecentItem('content', $node
->getEntityTypeId(), $node
->id());
$continuous_job_item_recent
->setState(JobItemInterface::STATE_ABORTED, NULL, array(), 'status');
$node
->set('title', $this
->randomMachineName());
$node
->save();
$this
->assertEquals(count($continuous_job
->getItems()), 3, 'Continuous job item is automatically created for an updated english node.');
}
public function testSubmitContinuousOnCron() {
$account = $this
->createUser();
$type = $this
->drupalCreateContentType();
$second_type = $this
->drupalCreateContentType();
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('node', $type
->id(), TRUE);
$content_translation_manager
->setEnabled('node', $second_type
->id(), TRUE);
$translator = Translator::load('test_translator');
$continuous_settings = [
'content' => [
'node' => [
'enabled' => 1,
'bundles' => [
$type
->id() => 1,
$second_type
->id() => 0,
],
],
],
];
$this
->config('tmgmt.settings')
->set('submit_job_item_on_cron', TRUE)
->set('job_items_cron_limit', 3)
->save();
$first_job = tmgmt_job_create('en', 'de', $account
->id(), [
'job_type' => Job::TYPE_CONTINUOUS,
'translator' => $translator,
'continuous_settings' => $continuous_settings,
]);
$first_job
->save();
$first_node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'en',
]);
$first_node
->save();
$first_items = array_values($first_job
->getItems());
foreach ($first_items as $job_item) {
$this
->assertEquals(JobItemInterface::STATE_INACTIVE, $job_item
->getState(), 'Job item is inactive before cron run');
}
$this
->assertCount(1, $first_job
->getItems(), 'There is one job item for an english node.');
$first_node
->set('title', $this
->randomMachineName());
$first_node
->save();
$this
->assertCount(1, $first_job
->getItems(), 'There are no new job items for updated english node.');
$first_job_items = $first_job
->getItems();
$first_job_item = reset($first_job_items);
$data = $first_job_item
->getData();
$this
->assertEquals($first_node
->label(), $data['title'][0]['value']['#text'], 'Data in job item has been updated properly.');
$second_job = tmgmt_job_create('de', 'en', $account
->id(), [
'job_type' => Job::TYPE_CONTINUOUS,
'translator' => $translator,
'continuous_settings' => $continuous_settings,
]);
$second_job
->save();
$second_node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'de',
]);
$second_node
->save();
$third_node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'de',
]);
$third_node
->save();
$second_items = array_values($second_job
->getItems());
foreach ($second_items as $job_item) {
$this
->assertEquals(JobItemInterface::STATE_INACTIVE, $job_item
->getState(), 'Job item is inactive before cron run');
}
$third_job = tmgmt_job_create('cs', 'en', $account
->id(), [
'job_type' => Job::TYPE_CONTINUOUS,
'translator' => $translator,
'continuous_settings' => $continuous_settings,
]);
$third_job
->save();
for ($i = 0; $i < 3; $i++) {
$node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'cs',
]);
$node
->save();
}
tmgmt_cron();
$expected_groups = [
[
[
'item_id' => $first_items[0]
->id(),
'job_id' => $first_items[0]
->getJobId(),
],
],
[
[
'item_id' => $second_items[0]
->id(),
'job_id' => $second_items[0]
->getJobId(),
],
[
'item_id' => $second_items[1]
->id(),
'job_id' => $second_items[1]
->getJobId(),
],
],
];
$this
->assertEquals($expected_groups, \Drupal::state()
->get('job_item_groups'), 'Job items groups are equal');
foreach ($first_job
->getItems() as $job_item) {
$this
->assertEquals(JobItemInterface::STATE_REVIEW, $job_item
->getState(), 'Job item is active after cron run');
}
foreach ($second_job
->getItems() as $job_item) {
$this
->assertEquals(JobItemInterface::STATE_REVIEW, $job_item
->getState(), 'Job item is active after cron run');
}
tmgmt_cron();
$third_items = array_values($third_job
->getItems());
$expected_groups[] = [
[
'item_id' => $third_items[0]
->id(),
'job_id' => $third_items[0]
->getJobId(),
],
[
'item_id' => $third_items[1]
->id(),
'job_id' => $third_items[1]
->getJobId(),
],
[
'item_id' => $third_items[2]
->id(),
'job_id' => $third_items[2]
->getJobId(),
],
];
$this
->assertEquals($expected_groups, \Drupal::state()
->get('job_item_groups'), 'Job items groups are equal');
foreach ($third_job
->getItems() as $job_item) {
$this
->assertEquals(JobItemInterface::STATE_REVIEW, $job_item
->getState(), 'Job item is active after cron run');
}
}
public function testContinuousTranslatorsAbortion() {
\Drupal::service('router.builder')
->rebuild();
$translator = Translator::load('test_translator');
$this
->assertTrue($translator
->getPlugin() instanceof ContinuousTranslatorInterface);
$type = NodeType::create([
'type' => $this
->randomMachineName(),
]);
$type
->save();
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('node', $type
->id(), TRUE);
$continuous_job = tmgmt_job_create('en', 'de', 0, [
'job_type' => Job::TYPE_CONTINUOUS,
'continuous_settings' => [
'content' => [
'node' => [
'enabled' => TRUE,
'bundles' => [
$type
->id() => TRUE,
],
],
],
],
]);
$continuous_job->translator = $translator;
$continuous_job
->save();
$continuous_job
->aborted();
$node = Node::create(array(
'title' => $this
->randomMachineName(),
'type' => $type
->id(),
'language' => 'en',
'body' => $this
->randomMachineName(),
));
$node
->save();
$updated_continuous_job = Job::load($continuous_job
->id());
$this
->assertEquals([], $updated_continuous_job
->getItems());
$this
->assertEquals(Job::STATE_ABORTED, $updated_continuous_job
->getState());
}
}