function CrudTest::testJobItems

Test crud operations of job items.

File

tests/src/Kernel/CrudTest.php, line 209

Class

CrudTest
Basic crud operations for jobs and translators

Namespace

Drupal\Tests\tmgmt\Kernel

Code

function testJobItems() {
  $job = $this
    ->createJob();

  // Add some test items.
  $item1 = $job
    ->addItem('test_source', 'type', 5);
  $item2 = $job
    ->addItem('test_source', 'test_with_long_label', 4);

  // Test single load callback.
  $item = JobItem::load($item1
    ->id());
  $this
    ->assertEquals($item1
    ->getPlugin(), $item
    ->getPlugin());
  $this
    ->assertEquals($item1
    ->getItemType(), $item
    ->getItemType());
  $this
    ->assertEquals($item1
    ->getItemId(), $item
    ->getItemId());

  // Test multiple load callback.
  $items = JobItem::loadMultiple(array(
    $item1
      ->id(),
    $item2
      ->id(),
  ));
  $this
    ->assertCount(2, $items);
  $this
    ->assertEquals($item1
    ->getPlugin(), $items[$item1
    ->id()]
    ->getPlugin());
  $this
    ->assertEquals($item1
    ->getItemType(), $items[$item1
    ->id()]
    ->getItemType());
  $this
    ->assertEquals($item1
    ->getItemId(), $items[$item1
    ->id()]
    ->getItemId());
  $this
    ->assertEquals($item2
    ->getPlugin(), $items[$item2
    ->id()]
    ->getPlugin());
  $this
    ->assertEquals($item2
    ->getItemType(), $items[$item2
    ->id()]
    ->getItemType());
  $this
    ->assertEquals($item2
    ->getItemId(), $items[$item2
    ->id()]
    ->getItemId());

  // Test the second item label length - it must not exceed the
  // TMGMT_JOB_LABEL_MAX_LENGTH.
  $this
    ->assertTrue(Job::LABEL_MAX_LENGTH >= strlen($items[$item2
    ->id()]
    ->label()));
  $translator = Translator::load('test_translator');
  $translator
    ->setAutoAccept(TRUE)
    ->save();
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('test_source', 'test_type', 1, [
    'tjid' => $job
      ->id(),
  ]);
  $job_item
    ->save();

  // Add translated data to the job item.
  $translation['dummy']['deep_nesting']['#text'] = 'Invalid translation that will cause an exception';
  $job_item
    ->addTranslatedData($translation);

  // If it was set to Auto Accept but there was an error, the Job Item should
  // be set as Needs Review.
  $this
    ->assertEquals(JobItemInterface::STATE_REVIEW, $job_item
    ->getState());

  // There should be a message if auto accept has failed.
  $messages = $job
    ->getMessages();
  $last_message = end($messages);
  $this
    ->assertEquals('Failed to automatically accept translation, error: The translation cannot be saved.', $last_message
    ->getMessage());
}