You are here

function CrudTest::testJobItemsCounters in Translation Management Tool 8

Test the calculations of the counters.

File

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

Class

CrudTest
Basic crud operations for jobs and translators

Namespace

Drupal\Tests\tmgmt\Kernel

Code

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

  // Some test data items.
  $data1 = array(
    '#text' => 'The text to be translated.',
  );
  $data2 = array(
    '#text' => 'The text to be translated.',
    '#translation' => '',
  );
  $data3 = array(
    '#text' => 'The text to be translated.',
    '#translation' => 'The translated data. Set by the translator plugin.',
  );
  $data4 = array(
    '#text' => 'Another, longer text to be translated.',
    '#translation' => 'The translated data. Set by the translator plugin.',
    '#status' => TMGMT_DATA_ITEM_STATE_REVIEWED,
  );
  $data5 = array(
    '#label' => 'label',
    'data1' => $data1,
    'data4' => $data4,
  );
  $data6 = array(
    '#text' => '<p>Test the HTML tags count.</p>',
  );

  // No data items.
  $this
    ->assertEqual(0, $job
    ->getCountPending());
  $this
    ->assertEqual(0, $job
    ->getCountTranslated());
  $this
    ->assertEqual(0, $job
    ->getCountReviewed());
  $this
    ->assertEqual(0, $job
    ->getCountAccepted());
  $this
    ->assertEqual(0, $job
    ->getWordCount());

  // Add a test items.
  $job_item1 = tmgmt_job_item_create('plugin', 'type', 4, array(
    'tjid' => $job
      ->id(),
  ));
  $job_item1
    ->save();

  // No pending, translated and confirmed data items.
  $job = Job::load($job
    ->id());
  $job_item1 = JobItem::load($job_item1
    ->id());
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(0, $job_item1
    ->getCountPending());
  $this
    ->assertEqual(0, $job_item1
    ->getCountTranslated());
  $this
    ->assertEqual(0, $job_item1
    ->getCountReviewed());
  $this
    ->assertEqual(0, $job_item1
    ->getCountAccepted());
  $this
    ->assertEqual(0, $job
    ->getCountPending());
  $this
    ->assertEqual(0, $job
    ->getCountTranslated());
  $this
    ->assertEqual(0, $job
    ->getCountReviewed());
  $this
    ->assertEqual(0, $job
    ->getCountAccepted());

  // Add an untranslated data item.
  $job_item1
    ->updateData('data_item1', $data1);
  $job_item1
    ->save();

  // One pending data items.
  $job = Job::load($job
    ->id());
  $job_item1 = JobItem::load($job_item1
    ->id());
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(1, $job_item1
    ->getCountPending());
  $this
    ->assertEqual(0, $job_item1
    ->getCountTranslated());
  $this
    ->assertEqual(0, $job_item1
    ->getCountReviewed());
  $this
    ->assertEqual(5, $job_item1
    ->getWordCount());
  $this
    ->assertEqual(1, $job
    ->getCountPending());
  $this
    ->assertEqual(0, $job
    ->getCountReviewed());
  $this
    ->assertEqual(0, $job
    ->getCountTranslated());
  $this
    ->assertEqual(5, $job
    ->getWordCount());

  // Add another untranslated data item.
  // Test with an empty translation set.
  $job_item1
    ->updateData('data_item1', $data2, TRUE);
  $job_item1
    ->save();

  // One pending data items.
  $job = Job::load($job
    ->id());
  $job_item1 = JobItem::load($job_item1
    ->id());
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(1, $job_item1
    ->getCountPending());
  $this
    ->assertEqual(0, $job_item1
    ->getCountTranslated());
  $this
    ->assertEqual(0, $job_item1
    ->getCountReviewed());
  $this
    ->assertEqual(5, $job_item1
    ->getWordCount());
  $this
    ->assertEqual(1, $job
    ->getCountPending());
  $this
    ->assertEqual(0, $job
    ->getCountTranslated());
  $this
    ->assertEqual(0, $job
    ->getCountReviewed());
  $this
    ->assertEqual(5, $job
    ->getWordCount());

  // Add a translated data item.
  $job_item1
    ->updateData('data_item1', $data3, TRUE);
  $job_item1
    ->save();

  // One translated data items.
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(0, $job_item1
    ->getCountPending());
  $this
    ->assertEqual(1, $job_item1
    ->getCountTranslated());
  $this
    ->assertEqual(0, $job_item1
    ->getCountReviewed());
  $this
    ->assertEqual(0, $job
    ->getCountPending());
  $this
    ->assertEqual(0, $job
    ->getCountReviewed());
  $this
    ->assertEqual(1, $job
    ->getCountTranslated());

  // Add a confirmed data item.
  $job_item1
    ->updateData('data_item1', $data4, TRUE);
  $job_item1
    ->save();

  // One reviewed data item.
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(1, $job_item1
    ->getCountReviewed());
  $this
    ->assertEqual(1, $job
    ->getCountReviewed());

  // Add a translated and an untranslated and a confirmed data item
  $job = Job::load($job
    ->id());
  $job_item1 = JobItem::load($job_item1
    ->id());
  $job_item1
    ->updateData('data_item1', $data1, TRUE);
  $job_item1
    ->updateData('data_item2', $data3, TRUE);
  $job_item1
    ->updateData('data_item3', $data4, TRUE);
  $job_item1
    ->save();

  // One pending and translated data items each.
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(1, $job
    ->getCountPending());
  $this
    ->assertEqual(1, $job
    ->getCountTranslated());
  $this
    ->assertEqual(1, $job
    ->getCountReviewed());
  $this
    ->assertEqual(16, $job
    ->getWordCount());

  // Add nested data items.
  $job_item1
    ->updateData('data_item1', $data5, TRUE);
  $job_item1
    ->save();

  // One pending data items.
  $job = Job::load($job
    ->id());
  $job_item1 = JobItem::load($job_item1
    ->id());
  $this
    ->assertEqual('label', $job_item1
    ->getData()['data_item1']['#label']);
  $this
    ->assertEqual(3, count($job_item1
    ->getData()['data_item1']));

  // Add a greater number of data items
  for ($index = 1; $index <= 3; $index++) {
    $job_item1
      ->updateData('data_item' . $index, $data1, TRUE);
  }
  for ($index = 4; $index <= 10; $index++) {
    $job_item1
      ->updateData('data_item' . $index, $data3, TRUE);
  }
  for ($index = 11; $index <= 15; $index++) {
    $job_item1
      ->updateData('data_item' . $index, $data4, TRUE);
  }
  $job_item1
    ->save();

  // 3 pending and 7 translated data items each.
  $job = Job::load($job
    ->id());
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(3, $job
    ->getCountPending());
  $this
    ->assertEqual(7, $job
    ->getCountTranslated());
  $this
    ->assertEqual(5, $job
    ->getCountReviewed());

  // Check for HTML tags count.
  $job_item1
    ->updateData('data_item1', $data6);
  $job_item1
    ->save();
  $this
    ->assertEqual(2, $job_item1
    ->getTagsCount());

  // Add several job items
  $job_item2 = tmgmt_job_item_create('plugin', 'type', 5, array(
    'tjid' => $job
      ->id(),
  ));
  for ($index = 1; $index <= 4; $index++) {
    $job_item2
      ->updateData('data_item' . $index, $data1, TRUE);
  }
  for ($index = 5; $index <= 12; $index++) {
    $job_item2
      ->updateData('data_item' . $index, $data3, TRUE);
  }
  for ($index = 13; $index <= 16; $index++) {
    $job_item2
      ->updateData('data_item' . $index, $data4, TRUE);
  }
  $job_item2
    ->save();

  // 3 pending and 7 translated data items each.
  $job = Job::load($job
    ->id());
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(7, $job
    ->getCountPending());
  $this
    ->assertEqual(15, $job
    ->getCountTranslated());
  $this
    ->assertEqual(9, $job
    ->getCountReviewed());

  // Accept the job items.
  foreach ($job
    ->getItems() as $item) {

    // Set the state directly to avoid triggering translator and source
    // controllers that do not exist.
    $item
      ->setState(JobItem::STATE_ACCEPTED);
    $item
      ->save();
  }
  drupal_static_reset('tmgmt_job_statistics_load');
  $this
    ->assertEqual(0, $job
    ->getCountPending());
  $this
    ->assertEqual(0, $job
    ->getCountTranslated());
  $this
    ->assertEqual(0, $job
    ->getCountReviewed());
  $this
    ->assertEqual(31, $job
    ->getCountAccepted());
}