public function ContentEntitySourceUnitTest::testSubmitContinuousOnCron

Test submit continuous job items on cron.

File

sources/content/tests/src/Kernel/ContentEntitySourceUnitTest.php, line 779

Class

ContentEntitySourceUnitTest
Content entity Source unit tests.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testSubmitContinuousOnCron() {
  $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
    ->id(), TRUE);
  $content_translation_manager
    ->setEnabled('node', $second_type
    ->id(), 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,
        ],
      ],
    ],
  ];
  $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();

  // Create an english node.
  $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');
  }

  // Test that there is one job item for an english node.
  $this
    ->assertCount(1, $first_job
    ->getItems(), 'There is one job item for an english node.');

  // Update english node.
  $first_node
    ->set('title', $this
    ->randomMachineName());
  $first_node
    ->save();

  // Test that there is no new job item for updated english node.
  $this
    ->assertCount(1, $first_job
    ->getItems(), 'There are no new job items for updated english node.');

  // Test that job item's data is updated properly.
  $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();

  // Create a german node.
  $second_node = Node::create([
    'title' => $this
      ->randomMachineName(),
    'uid' => $account
      ->id(),
    'type' => $type
      ->id(),
    'langcode' => 'de',
  ]);
  $second_node
    ->save();

  // Create a german node.
  $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();

  // Create 3 sample nodes.
  for ($i = 0; $i < 3; $i++) {
    $node = Node::create([
      'title' => $this
        ->randomMachineName(),
      'uid' => $account
        ->id(),
      'type' => $type
        ->id(),
      'langcode' => 'cs',
    ]);
    $node
      ->save();
  }
  tmgmt_cron();

  // Assert that the translator was called twice, once with the item of the
  // first job and once with the 2 items of the second job.
  $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(),
      ],
    ],
  ];

  // Check job items is properly grouped and we have exactly 2 groups.
  $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');
  }

  // Run cron again to process 3 remaining job items.
  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(),
    ],
  ];

  // Assert there are 3 new job items appeared from the third job.
  $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');
  }
}