You are here

public function LocalTranslatorTest::testLocalProgress in Translation Management Tool 8

Tests of the task progress.

File

translators/tmgmt_local/tests/src/Functional/LocalTranslatorTest.php, line 795

Class

LocalTranslatorTest
Basic tests for the local translator.

Namespace

Drupal\Tests\tmgmt_local\Functional

Code

public function testLocalProgress() {

  // Load the local translator.
  $translator = Translator::load('local');

  // Create assignee with the skills.
  $assignee1 = $this
    ->drupalCreateUser($this->localManagerPermissions);
  $this
    ->drupalLogin($assignee1);
  $edit = array(
    'tmgmt_translation_skills[0][language_from]' => 'en',
    'tmgmt_translation_skills[0][language_to]' => 'de',
  );
  $this
    ->drupalPostForm('user/' . $assignee1
    ->id() . '/edit', $edit, t('Save'));

  // Login as translator.
  $this
    ->loginAsTranslator();

  // Create the basic html format.
  $basic_html_format = FilterFormat::create(array(
    'format' => 'basic_html',
    'name' => 'Basic HTML',
  ));
  $basic_html_format
    ->save();

  // Create a Job.
  $job = $this
    ->createJob();
  $job->translator = $translator
    ->id();
  $item1 = $job
    ->addItem('test_source', 'test', '1');
  \Drupal::state()
    ->set('tmgmt.test_source_data', array(
    'title' => array(
      'deep_nesting' => array(
        '#text' => 'Example text',
        '#label' => 'Label for job item with type test and id 2.',
        '#translate' => TRUE,
      ),
    ),
    'text' => array(
      'deep_nesting' => array(
        '#text' => 'Example text',
        '#label' => 'Label for job item with type test and id 2.',
        '#translate' => TRUE,
      ),
    ),
  ));
  $job
    ->addItem('test_source', 'test', '2');
  $job
    ->save();
  $this
    ->drupalGet($job
    ->toUrl());
  $this
    ->drupalPostForm(NULL, NULL, t('Submit to provider'));

  // Login as assignee.
  $this
    ->drupalLogin($assignee1);

  // Check the task unassigned icon.
  $this
    ->drupalGet('/manage-translate');
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'unassigned', 'Unassigned');

  // Assign the task and check its icon.
  $edit = array(
    'action' => 'tmgmt_local_task_assign_to_me',
    'tmgmt_local_task_bulk_form[0]' => 1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Apply to selected items'));
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'assigned', 'Needs action');

  // Unassign it back.
  $edit = array(
    'action' => 'tmgmt_local_task_unassign_multiple',
    'tmgmt_local_task_bulk_form[0]' => 1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Apply to selected items'));

  // Check its unassigned icon.
  $this
    ->drupalGet('/manage-translate');
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'unassigned', 'Unassigned');
  $this
    ->drupalGet('/translate');

  // Check the unassigned status.
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'unassigned', 'Unassigned');
  $edit = array(
    'tmgmt_local_task_bulk_form[0]' => 1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Apply to selected items'));

  // Check the needs action status.
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'my-tasks', 'Needs action');

  // Check if the task is displayed on pending overview.
  $this
    ->drupalGet('/translate/pending');
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'pending', 'Needs action');
  $this
    ->drupalGet('/manage-translate/pending');
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'pending', 'Needs action');

  // Check the icons of the task items.
  $this
    ->drupalGet('/translate/1');
  $this
    ->assertTaskItemStatusIcon('test_source:test:1', 'Untranslated');
  $this
    ->assertTaskItemStatusIcon('test_source:test:2', 'Untranslated');
  $this
    ->assertTaskItemProgress('test_source:test:1', 1, 0, 0);
  $this
    ->assertTaskItemProgress('test_source:test:2', 2, 0, 0);

  // Check the progress bar and status of the task.
  $this
    ->drupalGet('/translate');
  $this
    ->assertTaskProgress(1, 'my-tasks', 3, 0, 0);
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'my-tasks', 'Needs action');
  $this
    ->drupalGet('/manage-translate');
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'assigned', 'Needs action');

  // Set two items as translated.
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->drupalGet('translate/items/1');
  $page
    ->pressButton('finish-dummy|deep_nesting');
  $this
    ->drupalGet('translate/items/2');
  $page
    ->pressButton('finish-title|deep_nesting');

  // Check the task items icons and progress.
  $this
    ->drupalGet('/translate/1');
  $this
    ->assertTaskItemStatusIcon('test_source:test:1', 'Untranslated');
  $this
    ->assertTaskItemStatusIcon('test_source:test:2', 'Untranslated');
  $this
    ->assertTaskItemProgress('test_source:test:1', 0, 1, 0);
  $this
    ->assertTaskItemProgress('test_source:test:2', 1, 1, 0);

  // Check the progress bar and status of the task.
  $this
    ->drupalGet('/translate');
  $this
    ->assertTaskProgress(1, 'my-tasks', 1, 2, 0);
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'my-tasks', 'Needs action');
  $this
    ->drupalGet('/manage-translate');
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'assigned', 'Needs action');

  // Save the first item as completed and check item icons and progress.
  $edit = [
    'dummy|deep_nesting[translation]' => 'German translation',
  ];
  $this
    ->drupalPostForm('/translate/items/1', $edit, t('Save as completed'));
  $this
    ->assertTaskItemStatusIcon('test_source:test:1', 'Translated');
  $this
    ->assertTaskItemStatusIcon('test_source:test:2', 'Untranslated');
  $this
    ->assertTaskItemProgress('test_source:test:1', 0, 0, 1);
  $this
    ->assertTaskItemProgress('test_source:test:2', 1, 1, 0);

  // Check the progress bar and status of the task.
  $this
    ->drupalGet('/translate');
  $this
    ->assertTaskProgress(1, 'my-tasks', 1, 1, 1);
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'my-tasks', 'Needs action');
  $this
    ->drupalGet('/manage-translate');
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'assigned', 'Needs action');

  // Save the second item as completed.
  $edit = [
    'title|deep_nesting[translation]' => 'German translation of title',
    'text|deep_nesting[translation]' => 'German translation of text',
  ];
  $this
    ->drupalPostForm('/translate/items/2', $edit, t('Save as completed'));

  // Check the icon a progress bar of the task.
  $this
    ->assertTaskProgress(1, 'my-tasks', 0, 0, 3);
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'my-tasks', 'In review');

  // Check the task items icons.
  $this
    ->drupalGet('/translate/1');
  $this
    ->assertTaskItemStatusIcon('test_source:test:1', 'Translated');
  $this
    ->assertTaskItemStatusIcon('test_source:test:2', 'Translated');
  $this
    ->assertTaskItemProgress('test_source:test:1', 0, 0, 1);
  $this
    ->assertTaskItemProgress('test_source:test:2', 0, 0, 2);

  // Check if the task is displayed on the completed overview.
  $this
    ->drupalGet('/translate/completed');
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'completed', 'In review');
  $this
    ->drupalGet('/manage-translate/completed');
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'completed', 'In review');

  // Accept translation of the job items.

  /** @var \Drupal\tmgmt\Entity\Job $job1 */
  $job1 = Job::load($job
    ->id());

  /** @var \Drupal\tmgmt\Entity\JobItem $item */
  foreach ($job1
    ->getItems() as $item) {
    $item
      ->acceptTranslation();
  }

  // Check if the task is displayed on the closed overview.
  $this
    ->drupalGet('/translate/closed');
  $this
    ->assertTaskStatusIcon(1, 'task-overview', 'closed', 'Closed');
  $this
    ->drupalGet('/manage-translate/closed');
  $this
    ->assertTaskStatusIcon(1, 'manage-translate-task', 'closed', 'Closed');

  // Assert the legend.
  $this
    ->drupalGet('/translate/items/' . $item1
    ->id());
  $this
    ->assertRaw('class="tmgmt-color-legend');
}