public function TMGMTUiTest::testProgress

Tests of the job item review process.

File

tests/src/Functional/TMGMTUiTest.php, line 858

Class

TMGMTUiTest
Verifies basic functionality of the user interface

Namespace

Drupal\Tests\tmgmt\Functional

Code

public function testProgress() {

  // Test that there are no jobs at the beginning.
  $this
    ->drupalGet('admin/tmgmt/jobs');
  $this
    ->assertSession()
    ->pageTextContains('No jobs available.');
  $this
    ->assertSession()
    ->optionExists('edit-state', 'Items - In progress');
  $this
    ->assertSession()
    ->optionExists('edit-state', 'Items - Needs review');
  $this
    ->assertSession()
    ->optionExists('edit-state', 'Items - Translation is requested from the elders of the Internet');

  // Make sure the legend label is displayed for the test translator state.
  $this
    ->assertSession()
    ->pageTextContains('Translation is requested from the elders of the Internet');
  $this
    ->drupalGet('admin/tmgmt/sources');

  // Create Jobs.
  $job1 = $this
    ->createJob();
  $job1
    ->save();
  $job1
    ->setState(Job::STATE_UNPROCESSED);
  $job2 = $this
    ->createJob();
  $job2
    ->save();
  $job2
    ->setState(Job::STATE_ACTIVE);
  $job3 = $this
    ->createJob();
  $job3
    ->save();
  $job3
    ->setState(Job::STATE_REJECTED);
  $job4 = $this
    ->createJob();
  $job4
    ->save();
  $job4
    ->setState(Job::STATE_ABORTED);
  $job5 = $this
    ->createJob();
  $job5
    ->save();
  $job5
    ->setState(Job::STATE_FINISHED);

  // Test their icons.
  $this
    ->drupalGet('admin/tmgmt/jobs', array(
    'query' => array(
      'state' => 'All',
    ),
  ));
  $this
    ->assertCount(5, $this
    ->xpath('//tbody/tr'));
  $this
    ->assertJobStateIcon(1, 'Unprocessed');
  $this
    ->assertJobStateIcon(2, 'In progress');
  $this
    ->assertJobStateIcon(3, 'Rejected');
  $this
    ->assertJobStateIcon(4, 'Aborted');
  $this
    ->assertJobStateIcon(5, 'Finished');

  // Test the row amount for each state selected.
  $this
    ->drupalGet('admin/tmgmt/jobs', [
    'query' => [
      'state' => 'open_jobs',
    ],
  ]);
  $this
    ->assertCount(3, $this
    ->xpath('//tbody/tr'));
  $this
    ->drupalGet('admin/tmgmt/jobs', [
    'query' => [
      'state' => JobInterface::STATE_UNPROCESSED,
    ],
  ]);
  $this
    ->assertCount(1, $this
    ->xpath('//tbody/tr'));
  $this
    ->drupalGet('admin/tmgmt/jobs', [
    'query' => [
      'state' => JobInterface::STATE_REJECTED,
    ],
  ]);
  $this
    ->assertCount(1, $this
    ->xpath('//tbody/tr'));
  $this
    ->drupalGet('admin/tmgmt/jobs', array(
    'query' => array(
      'state' => JobInterface::STATE_ABORTED,
    ),
  ));
  $this
    ->assertCount(1, $this
    ->xpath('//tbody/tr'));
  $this
    ->drupalGet('admin/tmgmt/jobs', array(
    'query' => array(
      'state' => JobInterface::STATE_FINISHED,
    ),
  ));
  $this
    ->assertCount(1, $this
    ->xpath('//tbody/tr'));
  \Drupal::state()
    ->set('tmgmt.test_source_data', array(
    'title' => array(
      'deep_nesting' => array(
        '#text' => '<p><em><strong>Six dummy HTML tags in the title.</strong></em></p>',
        '#label' => 'Title',
      ),
    ),
    'body' => array(
      'deep_nesting' => array(
        '#text' => '<p>Two dummy HTML tags in the body.</p>',
        '#label' => 'Body',
      ),
    ),
  ));

  // Add 2 items to job1 and submit it to provider.
  $item1 = $job1
    ->addItem('test_source', 'test', 1);
  $job1
    ->addItem('test_source', 'test', 2);
  $this
    ->drupalGet('admin/tmgmt/job_items', array(
    'query' => array(
      'state' => 'All',
    ),
  ));
  $this
    ->assertCount(2, $this
    ->xpath('//tbody/tr'));
  $this
    ->assertJobItemOverviewStateIcon(1, 'Inactive');
  $this
    ->assertSession()
    ->linkExists($job1
    ->label());
  $this
    ->drupalGet($job1
    ->toUrl());
  $edit = array(
    'target_language' => 'de',
    'settings[action]' => 'submit',
  );
  $this
    ->submitForm($edit, 'Submit to provider');

  // Translate body of one item.
  $this
    ->drupalGet('admin/tmgmt/items/' . $item1
    ->id());
  $this
    ->submitForm([
    'body|deep_nesting[translation]' => 'translation',
  ], 'Save');

  // Check job item state is still in progress.
  $this
    ->assertJobItemStateIcon(1, 'In progress');
  $this
    ->drupalGet('admin/tmgmt/job_items', array(
    'query' => array(
      'state' => JobItemInterface::STATE_ACTIVE,
    ),
  ));
  $this
    ->assertCount(2, $this
    ->xpath('//tbody/tr'));
  $this
    ->assertJobItemOverviewStateIcon(1, 'In progress');
  $this
    ->drupalGet('admin/tmgmt/jobs', [
    'query' => [
      'state' => 'job_item_' . JobItemInterface::STATE_ACTIVE,
    ],
  ]);

  // Check progress bar and icon.
  $this
    ->assertJobProgress(1, 3, 1, 0, 0);
  $this
    ->assertJobStateIcon(1, 'In progress');

  // Set the translator status to tmgmt_test_generating.
  \Drupal::entityTypeManager()
    ->getStorage('tmgmt_job_item')
    ->resetCache();
  $item1 = JobItem::load($item1
    ->id());
  $item1
    ->setTranslatorState('tmgmt_test_generating');
  $item1
    ->save();
  $this
    ->drupalGet('admin/tmgmt/job_items', array(
    'query' => array(
      'state' => 'tmgmt_test_generating',
    ),
  ));
  $this
    ->assertCount(1, $this
    ->xpath('//tbody/tr'));
  $this
    ->assertJobItemOverviewStateIcon(1, 'Translation is requested from the elders of the Internet');
  $this
    ->assertSession()
    ->responseContains('earth.svg"');
  $this
    ->drupalGet('admin/tmgmt/jobs', [
    'query' => [
      'state' => 'job_item_' . JobItemInterface::STATE_ACTIVE,
    ],
  ]);
  $this
    ->assertJobProgress(1, 3, 1, 0, 0);
  $this
    ->assertJobStateIcon(1, 'Translation is requested from the elders of the Internet');
  $this
    ->assertSession()
    ->responseContains('earth.svg"');

  // Also check the translator state.
  $this
    ->drupalGet('admin/tmgmt/jobs', [
    'query' => [
      'state' => 'job_item_tmgmt_test_generating',
    ],
  ]);
  $this
    ->assertJobProgress(1, 3, 1, 0, 0);
  $this
    ->assertJobStateIcon(1, 'Translation is requested from the elders of the Internet');
  $this
    ->assertSession()
    ->responseContains('earth.svg"');

  // Translate title of one item.
  $this
    ->drupalGet('admin/tmgmt/items/' . $item1
    ->id());
  $this
    ->submitForm([
    'title|deep_nesting[translation]' => 'translation',
  ], 'Save');

  // Check job item state changed to needs review.
  $this
    ->assertJobItemStateIcon(1, 'Needs review');
  $this
    ->drupalGet('admin/tmgmt/job_items', array(
    'query' => array(
      'state' => JobItemInterface::STATE_REVIEW,
    ),
  ));
  $this
    ->assertCount(1, $this
    ->xpath('//tbody/tr'));
  $this
    ->assertJobItemOverviewStateIcon(1, 'Needs review');

  // Check exposed filter for needs review.
  $this
    ->drupalGet('admin/tmgmt/jobs', [
    'query' => [
      'state' => 'job_item_' . JobItemInterface::STATE_REVIEW,
    ],
  ]);
  $this
    ->assertCount(1, $this
    ->xpath('//tbody/tr'));

  // Check progress bar and icon.
  $this
    ->assertJobProgress(1, 2, 2, 0, 0);
  $this
    ->assertJobStateIcon(1, 'Needs review');

  // Review the translation one by one.
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->drupalGet('admin/tmgmt/items/' . $item1
    ->id());
  $page
    ->pressButton('reviewed-body|deep_nesting');
  $this
    ->drupalGet('admin/tmgmt/jobs/' . $job1
    ->id());

  // Check the icon of the job item.
  $this
    ->assertJobItemStateIcon(1, 'Needs review');
  $this
    ->drupalGet('admin/tmgmt/items/' . $item1
    ->id());
  $page
    ->pressButton('reviewed-title|deep_nesting');
  $this
    ->drupalGet('admin/tmgmt/jobs/' . $job1
    ->id());

  // Check the icon of the job item.
  $this
    ->assertJobItemStateIcon(1, 'Needs review');
  $this
    ->drupalGet('admin/tmgmt/jobs', array(
    'query' => array(
      'state' => 'open_jobs',
    ),
  ));

  // Check progress bar and icon.
  $this
    ->assertJobProgress(1, 2, 0, 2, 0);
  $this
    ->assertJobStateIcon(1, 'Needs review');

  // Save one job item as completed.
  $this
    ->drupalGet($item1
    ->toUrl());
  $this
    ->submitForm([], 'Save as completed');

  // Check job item state changed to accepted.
  $this
    ->assertJobItemStateIcon(1, 'Accepted');
  $this
    ->drupalGet('admin/tmgmt/job_items', array(
    'query' => array(
      'state' => JobItemInterface::STATE_ACCEPTED,
    ),
  ));
  $this
    ->assertCount(1, $this
    ->xpath('//tbody/tr'));
  $this
    ->assertJobItemOverviewStateIcon(1, 'Accepted');
  $this
    ->drupalGet('admin/tmgmt/jobs', array(
    'query' => array(
      'state' => 'open_jobs',
    ),
  ));

  // Check progress bar and icon.
  $this
    ->assertJobProgress(1, 2, 0, 0, 2);
  $this
    ->assertJobStateIcon(1, 'In progress');

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