Test creation of continuous job items.
public function testContinuousJobItems() {
$account = $this
->createUser();
$type = $this
->drupalCreateContentType();
$second_type = $this
->drupalCreateContentType();
// Enable entity translations for nodes.
$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);
// Create test translator for continuous job.
$translator = Translator::load('test_translator');
// Continuous settings configuration.
$continuous_settings = [
'content' => [
'node' => [
'enabled' => 1,
'bundles' => [
$type
->id() => 1,
$second_type
->id() => 0,
],
],
],
];
// Create continuous job with source language set to english.
$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());
// Create an english node.
$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()));
// Create an english node.
$node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'en',
]);
$node
->save();
// Test hook_entity_insert() for english node.
$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.');
// Test that continuous job item is in state review.
$this
->assertEquals($continuous_job_item
->getState(), JobItemInterface::STATE_REVIEW, 'Translation for an english node is in state review.');
// Update english node.
$node
->set('title', $this
->randomMachineName());
$node
->save();
// Test that there is no new job item.
$this
->assertEquals(count($continuous_job
->getItems()), 1, 'There are no new job items for an english node.');
// Accept translation for an english node.
$continuous_job_item
->acceptTranslation();
// Test that the translation for an english node is created and saved.
$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.');
// Create a german node.
$german_node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $type
->id(),
'langcode' => 'de',
]);
$german_node
->save();
// Test that there is no new item for german node.
$this
->assertEquals(count($continuous_job
->getItems()), 1, 'Continuous job item is not created for a german node.');
// Create new english node with different type.
$second_node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => $account
->id(),
'type' => $second_type
->id(),
'langcode' => 'en',
]);
$second_node
->save();
// Test that there is no new item for second english node.
$this
->assertEquals(count($continuous_job
->getItems()), 1, 'Continuous job item is not created for a second english node.');
// Update english node.
$node
->set('title', $this
->randomMachineName());
$node
->save();
// Test that there are no new job items for english node because it's
// translation is not outdated.
$this
->assertEquals(count($continuous_job
->getItems()), 1, 'Continuous job item is not created for an updated english node.');
// Set the outdated flag to true.
$translation = $node
->getTranslation('de');
$translation->content_translation_outdated->value = 1;
$translation
->save();
// Test that there are now two items for english node.
$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());
// Set job item state to aborted.
$continuous_job_item_recent
->setState(JobItemInterface::STATE_ABORTED, NULL, array(), 'status');
// Update english node.
$node
->set('title', $this
->randomMachineName());
$node
->save();
// Test that there are now three items for english node.
$this
->assertEquals(count($continuous_job
->getItems()), 3, 'Continuous job item is automatically created for an updated english node.');
}