You are here

function TMGMTUiTest::testAbortJob in Translation Management Tool 8

Test the process of aborting and resubmitting the job.

File

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

Class

TMGMTUiTest
Verifies basic functionality of the user interface

Namespace

Drupal\Tests\tmgmt\Functional

Code

function testAbortJob() {
  $job = $this
    ->createJob();
  $job
    ->addItem('test_source', 'test', 1);
  $job
    ->addItem('test_source', 'test', 2);
  $job
    ->addItem('test_source', 'test', 3);
  $edit = array(
    'target_language' => 'es',
    'settings[action]' => 'translate',
  );
  $this
    ->drupalPostForm('admin/tmgmt/jobs/' . $job
    ->id(), $edit, t('Submit to provider'));

  // Abort job.
  $this
    ->drupalPostForm('admin/tmgmt/jobs/' . $job
    ->id(), array(), t('Abort job'));
  $this
    ->drupalPostForm(NULL, array(), t('Confirm'));
  $this
    ->assertText('The user ordered aborting the Job through the UI.');
  $this
    ->assertUrl('admin/tmgmt/jobs/' . $job
    ->id());

  // Reload job and check its state.
  \Drupal::entityTypeManager()
    ->getStorage('tmgmt_job')
    ->resetCache();
  $job = Job::load($job
    ->id());
  $this
    ->assertTrue($job
    ->isAborted());
  foreach ($job
    ->getItems() as $item) {
    $this
      ->assertTrue($item
      ->isAborted());
  }

  // Resubmit the job.
  $this
    ->drupalPostForm('admin/tmgmt/jobs/' . $job
    ->id(), array(), t('Resubmit'));
  $this
    ->drupalPostForm(NULL, array(), t('Confirm'));

  // Test for the log message.
  $this
    ->assertRaw(t('This job is a duplicate of the previously aborted job <a href=":url">#@id</a>', array(
    ':url' => $job
      ->toUrl()
      ->toString(),
    '@id' => $job
      ->id(),
  )));

  // Load the resubmitted job and check for its status and values.
  $url_parts = explode('/', $this
    ->getUrl());
  $resubmitted_job = Job::load(array_pop($url_parts));
  $this
    ->assertTrue($resubmitted_job
    ->isUnprocessed());
  $this
    ->assertEqual($job
    ->getTranslator()
    ->id(), $resubmitted_job
    ->getTranslator()
    ->id());
  $this
    ->assertEqual($job
    ->getSourceLangcode(), $resubmitted_job
    ->getSourceLangcode());
  $this
    ->assertEqual($job
    ->getTargetLangcode(), $resubmitted_job
    ->getTargetLangcode());
  $this
    ->assertEqual($job
    ->get('settings')
    ->getValue(), $resubmitted_job
    ->get('settings')
    ->getValue());

  // Test if job items were duplicated correctly.
  foreach ($job
    ->getItems() as $item) {

    // We match job items based on "id #" string. This is not that straight
    // forward, but it works as the test source text is generated as follows:
    // Text for job item with type #type and id #id.
    $_items = $resubmitted_job
      ->getItems(array(
      'data' => array(
        'value' => 'id ' . $item
          ->getItemId(),
        'operator' => 'CONTAINS',
      ),
    ));
    $_item = reset($_items);
    $this
      ->assertNotEqual($_item
      ->getJobId(), $item
      ->getJobId());
    $this
      ->assertEqual($_item
      ->getPlugin(), $item
      ->getPlugin());
    $this
      ->assertEqual($_item
      ->getItemId(), $item
      ->getItemId());
    $this
      ->assertEqual($_item
      ->getItemType(), $item
      ->getItemType());

    // Make sure counts have been recalculated.
    $this
      ->assertTrue($_item
      ->getWordCount() > 0);
    $this
      ->assertTrue($_item
      ->getCountPending() > 0);
    $this
      ->assertEqual($_item
      ->getCountTranslated(), 0);
    $this
      ->assertEqual($_item
      ->getCountAccepted(), 0);
    $this
      ->assertEqual($_item
      ->getCountReviewed(), 0);
  }
  $this
    ->loginAsAdmin();

  // Navigate back to the aborted job and check for the log message.
  $this
    ->drupalGet('admin/tmgmt/jobs/' . $job
    ->id());

  // Assert that the progress is N/A since the job was aborted.
  $element = $this
    ->xpath('//div[@class="view-content"]/table[@class="views-table views-view-table cols-8"]/tbody//tr[1]/td[4]')[0];
  $this
    ->assertEqual($element
    ->getText(), t('Aborted'));
  $this
    ->assertRaw(t('Job has been duplicated as a new job <a href=":url">#@id</a>.', array(
    ':url' => $resubmitted_job
      ->toUrl()
      ->toString(),
    '@id' => $resubmitted_job
      ->id(),
  )));
  $this
    ->drupalPostForm(NULL, array(), t('Delete'));
  $this
    ->drupalPostForm(NULL, array(), t('Delete'));
  $this
    ->assertText('The translation job ' . $resubmitted_job
    ->label() . ' has been deleted.');
  $this
    ->drupalGet('admin/tmgmt/jobs/2/delete');
  $this
    ->drupalPostForm(NULL, array(), t('Delete'));
  $this
    ->drupalGet('admin/tmgmt/jobs/');
  $this
    ->assertText('No jobs available.');

  // Create a translator.
  $translator = $this
    ->createTranslator();

  // Create a job and attach to the translator.
  $job = $this
    ->createJob();
  $job->translator = $translator;
  $job
    ->save();
  $job
    ->setState(Job::STATE_ACTIVE);

  // Add item to the job.
  $job
    ->addItem('test_source', 'test', 1);
  $this
    ->drupalGet('admin/tmgmt/jobs');

  // Try to abort the job and save.
  $this
    ->clickLink(t('Manage'));
  $this
    ->drupalPostForm(NULL, [], t('Abort job'));
  $this
    ->drupalPostForm(NULL, [], t('Confirm'));

  // Go back to the job page.
  $this
    ->drupalGet('admin/tmgmt/jobs', array(
    'query' => array(
      'state' => JobInterface::STATE_ABORTED,
    ),
  ));

  // Check that job is aborted now.
  $this
    ->assertJobStateIcon(1, 'Aborted');

  // Ensure that a job can still be viewed when the target language was
  // deleted.
  ConfigurableLanguage::load('de')
    ->delete();
  $this
    ->drupalGet('admin/tmgmt/jobs/' . $job
    ->id());
}