function TMGMTUiTest::testAbortJob

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);
  $this
    ->drupalGet($job
    ->toUrl());
  $edit = array(
    'target_language' => 'es',
    'settings[action]' => 'translate',
  );
  $this
    ->submitForm($edit, 'Submit to provider');

  // Abort job.
  $this
    ->drupalGet($job
    ->toUrl());
  $this
    ->submitForm([], 'Abort job');
  $this
    ->submitForm([], 'Confirm');
  $this
    ->assertSession()
    ->pageTextContains('The user ordered aborting the Job through the UI.');
  $this
    ->assertSession()
    ->addressEquals('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
    ->submitForm([], 'Resubmit');
  $this
    ->submitForm([], 'Confirm');

  // Test for the log message.
  $this
    ->assertSession()
    ->responseContains(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
    ->assertEquals($job
    ->getTranslator()
    ->id(), $resubmitted_job
    ->getTranslator()
    ->id());
  $this
    ->assertEquals($job
    ->getSourceLangcode(), $resubmitted_job
    ->getSourceLangcode());
  $this
    ->assertEquals($job
    ->getTargetLangcode(), $resubmitted_job
    ->getTargetLangcode());
  $this
    ->assertEquals($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
      ->assertNotEquals($item
      ->getJobId(), $_item
      ->getJobId());
    $this
      ->assertEquals($item
      ->getPlugin(), $_item
      ->getPlugin());
    $this
      ->assertEquals($item
      ->getItemId(), $_item
      ->getItemId());
    $this
      ->assertEquals($item
      ->getItemType(), $_item
      ->getItemType());

    // Make sure counts have been recalculated.
    $this
      ->assertTrue($_item
      ->getWordCount() > 0);
    $this
      ->assertTrue($_item
      ->getCountPending() > 0);
    $this
      ->assertEquals(0, $_item
      ->getCountTranslated());
    $this
      ->assertEquals(0, $_item
      ->getCountAccepted());
    $this
      ->assertEquals(0, $_item
      ->getCountReviewed());
  }
  $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
    ->assertEquals('Aborted', $element
    ->getText());
  $this
    ->assertSession()
    ->responseContains(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
    ->submitForm([], 'Delete');
  $this
    ->submitForm([], 'Delete');
  $this
    ->assertSession()
    ->pageTextContains('The translation job ' . $resubmitted_job
    ->label() . ' has been deleted.');
  $this
    ->drupalGet('admin/tmgmt/jobs/2/delete');
  $this
    ->submitForm([], 'Delete');
  $this
    ->drupalGet('admin/tmgmt/jobs/');
  $this
    ->assertSession()
    ->pageTextContains('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('Manage');
  $this
    ->submitForm([], 'Abort job');
  $this
    ->submitForm([], '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());
}