You are here

function TMGMTCRUDTestCase::testJobItemsCounters in Translation Management Tool 7

Test the calculations of the counters.

File

tests/tmgmt.crud.test, line 346

Class

TMGMTCRUDTestCase
Basic CRUD tests.

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,
  );

  // 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->tjid,
  ));
  $job_item1
    ->save();

  // No pending, translated and confirmed data items.
  $job = entity_load_single('tmgmt_job', $job->tjid);
  $job_item1 = entity_load_single('tmgmt_job_item', $job_item1->tjiid);
  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->data['data_item1'] = $data1;
  $job_item1
    ->save();

  // One pending data items.
  $job = entity_load_single('tmgmt_job', $job->tjid);
  $job_item1 = entity_load_single('tmgmt_job_item', $job_item1->tjiid);
  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->data['data_item1'] = $data2;
  $job_item1
    ->save();

  // One pending data items.
  $job = entity_load_single('tmgmt_job', $job->tjid);
  $job_item1 = entity_load_single('tmgmt_job_item', $job_item1->tjiid);
  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->data['data_item1'] = $data3;
  $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->data['data_item1'] = $data4;
  $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 = entity_load_single('tmgmt_job', $job->tjid);
  $job_item1 = entity_load_single('tmgmt_job_item', $job_item1->tjiid);
  $job_item1->data['data_item1'] = $data1;
  $job_item1->data['data_item2'] = $data3;
  $job_item1->data['data_item3'] = $data4;
  $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->data['data_item1'] = $data5;
  $job_item1
    ->save();

  // One pending data items.
  $job = entity_load_single('tmgmt_job', $job->tjid);
  $job_item1 = entity_load_single('tmgmt_job_item', $job_item1->tjiid);
  $this
    ->assertEqual('label', $job_item1->data['data_item1']['#label']);
  $this
    ->assertEqual(3, count($job_item1->data['data_item1']));

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

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

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

  // 3 pending and 7 translated data items each.
  $job = entity_load_single('tmgmt_job', $job->tjid);
  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(TMGMT_JOB_ITEM_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());
}