You are here

function TMGMTLocalTestCase::testBasicWorkflow in Translation Management Tool 7

Test the basic translation workflow

File

translators/tmgmt_local/tmgmt_local.test, line 146
Test cases for the local translator module.

Class

TMGMTLocalTestCase
Basic tests for the local translator.

Code

function testBasicWorkflow() {
  $translator = tmgmt_translator_load('local');

  // Create a job and request a local translation.
  $this
    ->loginAsTranslator();
  $job = $this
    ->createJob();
  $job->translator = $translator->name;
  $job->settings['job_comment'] = $job_comment = 'Dummy job comment';
  $job
    ->addItem('test_source', 'test', '1');
  $job
    ->addItem('test_source', 'test', '2');
  $job
    ->save();
  $uri = $job
    ->uri();

  // Make sure that the checkout page works as expected when there are no
  // roles.
  $this
    ->drupalGet($uri['path']);
  $this
    ->assertText(t('@translator can not translate from @source to @target.', array(
    '@translator' => 'Local Translator (auto created)',
    '@source' => 'English',
    '@target' => 'German',
  )));
  $this->local_translator = $this
    ->drupalCreateUser($this->local_translator_permissions);

  // The same when there is a single role.
  $this
    ->drupalGet($uri['path']);
  $this
    ->assertText(t('@translator can not translate from @source to @target.', array(
    '@translator' => 'Local Translator (auto created)',
    '@source' => 'English',
    '@target' => 'German',
  )));

  // Create another local translator with the required capabilities.
  $other_translator_same = $this
    ->drupalCreateUser($this->local_translator_permissions);

  // And test again with two roles but still no capabilities.
  $this
    ->drupalGet($uri['path']);
  $this
    ->assertText(t('@translator can not translate from @source to @target.', array(
    '@translator' => 'Local Translator (auto created)',
    '@source' => 'English',
    '@target' => 'German',
  )));
  $this
    ->drupalLogin($other_translator_same);

  // Configure language capabilities.
  $edit = array(
    'tmgmt_translation_skills[und][0][language_from]' => 'en',
    'tmgmt_translation_skills[und][0][language_to]' => 'de',
  );
  $this
    ->drupalPost('user/' . $other_translator_same->uid . '/edit', $edit, t('Save'));

  // Check that the user is not listed in the translator selection form.
  $this
    ->loginAsAdmin();
  $this
    ->drupalGet($uri['path']);
  $this
    ->assertText(t('Select translator for this job'));
  $this
    ->assertText($other_translator_same->name);
  $this
    ->assertNoText($this->local_translator->name);
  $this
    ->drupalLogin($this->local_translator);

  // Configure language capabilities.
  $edit = array(
    'tmgmt_translation_skills[und][0][language_from]' => 'en',
    'tmgmt_translation_skills[und][0][language_to]' => 'de',
  );
  $this
    ->drupalPost('user/' . $this->local_translator->uid . '/edit', $edit, t('Save'));

  // Check that the translator is now listed.
  $this
    ->loginAsAdmin();
  $this
    ->drupalGet($uri['path']);
  $this
    ->assertText($this->local_translator->name);
  $job
    ->requestTranslation();

  // Test for job comment in the job checkout info pane.
  $uri = $job
    ->uri();
  $this
    ->drupalGet($uri['path']);
  $this
    ->assertText($job_comment);
  $this
    ->drupalLogin($this->local_translator);

  // Create a second local translator with different language capabilities,
  // make sure that he does not see the task.
  $other_translator = $this
    ->drupalCreateUser($this->local_translator_permissions);
  $this
    ->drupalLogin($other_translator);

  // Configure language capabilities.
  $edit = array(
    'tmgmt_translation_skills[und][0][language_from]' => 'de',
    'tmgmt_translation_skills[und][0][language_to]' => 'en',
  );
  $this
    ->drupalPost('user/' . $other_translator->uid . '/edit', $edit, t('Save'));
  $this
    ->drupalGet('translate');
  $this
    ->assertNoText(t('Task for @job', array(
    '@job' => $job
      ->label(),
  )));
  $this
    ->drupalLogin($this->local_translator);

  // Check the translate overview.
  $this
    ->drupalGet('translate');
  $this
    ->assertText(t('Task for @job', array(
    '@job' => $job
      ->label(),
  )));

  // @todo: Fails, encoding problem?

  //$this->assertText(t('@from => @to', array('@from' => 'en', '@to' => 'de')));
  $edit = array(
    'views_bulk_operations[0]' => $job->tjid,
  );
  $this
    ->drupalPost(NULL, $edit, t('Assign to me'));
  $this
    ->assertText(t('Performed Assign to me on 1 item.'));

  // Unassign again.
  $edit = array(
    'views_bulk_operations[0]' => $job->tjid,
  );
  $this
    ->drupalPost(NULL, $edit, t('Unassign'));
  $this
    ->assertText(t('Performed Unassign on 1 item.'));

  // Now test the assign link.
  $this
    ->clickLink(t('assign'));

  // Log in with the translator with the same capabilities, make sure that he
  // does not see the assigned task.
  $this
    ->drupalLogin($other_translator_same);
  $this
    ->drupalGet('translate');
  $this
    ->assertNoText(t('Task for @job', array(
    '@job' => $job
      ->label(),
  )));
  $this
    ->drupalLogin($this->local_translator);

  // Translate the task.
  $this
    ->drupalGet('translate');
  $this
    ->clickLink(t('view'));

  // Assert created local task and task items.
  $this
    ->assertTrue(preg_match('|translate/(\\d+)|', $this
    ->getUrl(), $matches), 'Task found');
  $task = tmgmt_local_task_load($matches[1]);
  $this
    ->assertTrue($task
    ->isPending());
  $this
    ->assertEqual($task
    ->getCountCompleted(), 0);
  $this
    ->assertEqual($task
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($task
    ->getCountUntranslated(), 2);
  list($first_task_item, $second_task_item) = array_values($task
    ->getItems());
  $this
    ->assertTrue($first_task_item
    ->isPending());
  $this
    ->assertEqual($first_task_item
    ->getCountCompleted(), 0);
  $this
    ->assertEqual($first_task_item
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($first_task_item
    ->getCountUntranslated(), 1);
  $this
    ->assertText('test_source:test:1');
  $this
    ->assertText('test_source:test:2');
  $this
    ->assertText(t('Untranslated'));

  // Translate the first item.
  $this
    ->clickLink(t('translate'));
  $this
    ->assertText(t('Dummy'));

  // Job comment is present in the translate tool as well.
  $this
    ->assertText($job_comment);
  $this
    ->assertText('test_source:test:1');

  // Try to complete a translation when translations are missing.
  $this
    ->drupalPost(NULL, array(), t('Save as completed'));
  $this
    ->assertText(t('Missing translation.'));
  $edit = array(
    'dummy|deep_nesting[translation]' => $translation1 = 'German translation of source 1',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save as completed'));

  // Review and accept the first item.
  entity_get_controller('tmgmt_job_item')
    ->resetCache(array(
    1,
  ));
  entity_get_controller('tmgmt_local_task')
    ->resetCache();
  entity_get_controller('tmgmt_local_task_item')
    ->resetCache();
  drupal_static_reset('tmgmt_local_task_statistics_load');
  $item1 = tmgmt_job_item_load(1);
  $item1
    ->acceptTranslation();

  // The first item should be accepted now, the second still in progress.
  $this
    ->drupalGet('translate/1');
  $this
    ->assertText(t('Completed'));
  $this
    ->assertText(t('Untranslated'));
  $task = tmgmt_local_task_load($task->tltid);
  $this
    ->assertTrue($task
    ->isPending());
  $this
    ->assertEqual($task
    ->getCountCompleted(), 1);
  $this
    ->assertEqual($task
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($task
    ->getCountUntranslated(), 1);
  list($first_task_item, $second_task_item) = array_values($task
    ->getItems());
  $this
    ->assertTrue($first_task_item
    ->isClosed());
  $this
    ->assertEqual($first_task_item
    ->getCountCompleted(), 1);
  $this
    ->assertEqual($first_task_item
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($first_task_item
    ->getCountUntranslated(), 0);
  $this
    ->assertTrue($second_task_item
    ->isPending());
  $this
    ->assertEqual($second_task_item
    ->getCountCompleted(), 0);
  $this
    ->assertEqual($second_task_item
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($second_task_item
    ->getCountUntranslated(), 1);

  // Translate the second item but do not mark as translated it yet.
  $this
    ->clickLink(t('translate'));
  $edit = array(
    'dummy|deep_nesting[translation]' => $translation2 = 'German translation of source 2',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // The first item is still completed, the second still untranslated.
  $this
    ->assertText(t('Completed'));
  $this
    ->assertText(t('Untranslated'));
  entity_get_controller('tmgmt_local_task')
    ->resetCache();
  entity_get_controller('tmgmt_local_task_item')
    ->resetCache();
  drupal_static_reset('tmgmt_local_task_statistics_load');
  $task = tmgmt_local_task_load($task->tltid);
  $this
    ->assertTrue($task
    ->isPending());
  $this
    ->assertEqual($task
    ->getCountCompleted(), 1);
  $this
    ->assertEqual($task
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($task
    ->getCountUntranslated(), 1);
  list($first_task_item, $second_task_item) = array_values($task
    ->getItems());
  $this
    ->assertTrue($first_task_item
    ->isClosed());
  $this
    ->assertEqual($first_task_item
    ->getCountCompleted(), 1);
  $this
    ->assertEqual($first_task_item
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($first_task_item
    ->getCountUntranslated(), 0);
  $this
    ->assertTrue($second_task_item
    ->isPending());
  $this
    ->assertEqual($second_task_item
    ->getCountCompleted(), 0);
  $this
    ->assertEqual($second_task_item
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($second_task_item
    ->getCountUntranslated(), 1);

  // Mark the data item as translated but don't save the task item as
  // completed.
  $this
    ->clickLink(t('translate'));
  $this
    ->drupalPost(NULL, array(), t('✓'));
  entity_get_controller('tmgmt_local_task')
    ->resetCache();
  entity_get_controller('tmgmt_local_task_item')
    ->resetCache();
  drupal_static_reset('tmgmt_local_task_statistics_load');
  $task = tmgmt_local_task_load($task->tltid);
  $this
    ->assertTrue($task
    ->isPending());
  $this
    ->assertEqual($task
    ->getCountCompleted(), 1);
  $this
    ->assertEqual($task
    ->getCountTranslated(), 1);
  $this
    ->assertEqual($task
    ->getCountUntranslated(), 0);
  list($first_task_item, $second_task_item) = array_values($task
    ->getItems());
  $this
    ->assertTrue($first_task_item
    ->isClosed());
  $this
    ->assertEqual($first_task_item
    ->getCountCompleted(), 1);
  $this
    ->assertEqual($first_task_item
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($first_task_item
    ->getCountUntranslated(), 0);
  $this
    ->assertTrue($second_task_item
    ->isPending());
  $this
    ->assertEqual($second_task_item
    ->getCountCompleted(), 0);
  $this
    ->assertEqual($second_task_item
    ->getCountTranslated(), 1);
  $this
    ->assertEqual($second_task_item
    ->getCountUntranslated(), 0);

  // Check the job data.
  entity_get_controller('tmgmt_job')
    ->resetCache(array(
    $job->tjid,
  ));
  entity_get_controller('tmgmt_job_item')
    ->resetCache();
  $job = tmgmt_job_load($job->tjid);
  list($item1, $item2) = array_values($job
    ->getItems());

  // The text in the first item should be available for review, the
  // translation of the second item not.
  $this
    ->assertEqual($item1
    ->getData(array(
    'dummy',
    'deep_nesting',
    '#translation',
    '#text',
  )), $translation1);
  $this
    ->assertEqual($item2
    ->getData(array(
    'dummy',
    'deep_nesting',
    '#translation',
    '#text',
  )), '');

  // Check the overview page, the task should still show in progress.
  $this
    ->drupalGet('translate');
  $this
    ->assertText(t('Pending'));

  // Mark the second item as completed now.
  $this
    ->clickLink(t('view'));
  $this
    ->clickLink(t('translate'));
  $this
    ->drupalPost(NULL, array(), t('Save as completed'));

  // Review and accept the second item.
  entity_get_controller('tmgmt_job_item')
    ->resetCache(array(
    2,
  ));
  entity_get_controller('tmgmt_local_task')
    ->resetCache();
  entity_get_controller('tmgmt_local_task_item')
    ->resetCache();
  drupal_static_reset('tmgmt_local_task_statistics_load');
  $item1 = tmgmt_job_item_load(2);
  $item1
    ->acceptTranslation();

  // Refresh the page.
  $this
    ->drupalGet($this->url);
  $task = tmgmt_local_task_load($task->tltid);
  $this
    ->assertTrue($task
    ->isClosed());
  $this
    ->assertEqual($task
    ->getCountCompleted(), 2);
  $this
    ->assertEqual($task
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($task
    ->getCountUntranslated(), 0);
  list($first_task_item, $second_task_item) = array_values($task
    ->getItems());
  $this
    ->assertTrue($first_task_item
    ->isClosed());
  $this
    ->assertEqual($first_task_item
    ->getCountCompleted(), 1);
  $this
    ->assertEqual($first_task_item
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($first_task_item
    ->getCountUntranslated(), 0);
  $this
    ->assertTrue($second_task_item
    ->isClosed());
  $this
    ->assertEqual($second_task_item
    ->getCountCompleted(), 1);
  $this
    ->assertEqual($second_task_item
    ->getCountTranslated(), 0);
  $this
    ->assertEqual($second_task_item
    ->getCountUntranslated(), 0);

  // We should have been redirect back to the overview, the task should be
  // completed now.
  $this
    ->assertNoText($task
    ->getJob()
    ->label());
  $this
    ->clickLink(t('Closed'));
  $this
    ->assertText($task
    ->getJob()
    ->label());
  $this
    ->assertText(t('Completed'));
  entity_get_controller('tmgmt_job')
    ->resetCache(array(
    $job->tjid,
  ));
  entity_get_controller('tmgmt_job_item')
    ->resetCache();
  $job = tmgmt_job_load($job->tjid);
  list($item1, $item2) = array_values($job
    ->getItems());

  // Job was accepted and finished automatically due to the default approve
  // setting.
  $this
    ->assertTrue($job
    ->isFinished());
  $this
    ->assertEqual($item1
    ->getData(array(
    'dummy',
    'deep_nesting',
    '#translation',
    '#text',
  )), $translation1);
  $this
    ->assertEqual($item2
    ->getData(array(
    'dummy',
    'deep_nesting',
    '#translation',
    '#text',
  )), $translation2);

  // Delete the job, make sure that the corresponding task and task items were
  // deleted.
  $job
    ->delete();
  $this
    ->assertFalse(tmgmt_local_task_item_load($task->tltid));
  $this
    ->assertFalse($task
    ->getItems());
}