function TMGMTUiTest::testCart

Test the cart functionality.

File

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

Class

TMGMTUiTest
Verifies basic functionality of the user interface

Namespace

Drupal\Tests\tmgmt\Functional

Code

function testCart() {
  $this
    ->addLanguage('fr');
  $job_items = array();

  // Create a few job items and add them to the cart.
  for ($i = 1; $i < 6; $i++) {
    $job_item = tmgmt_job_item_create('test_source', 'test', $i);
    $job_item
      ->save();
    $job_items[$i] = $job_item;
  }
  $this
    ->loginAsTranslator();
  foreach ($job_items as $job_item) {
    $this
      ->drupalGet('tmgmt-add-to-cart/' . $job_item
      ->id());
  }

  // Check if the items are displayed in the cart.
  $this
    ->drupalGet('admin/tmgmt/cart');
  foreach ($job_items as $job_item) {
    $this
      ->assertSession()
      ->pageTextContains($job_item
      ->label());
  }

  // Test the remove items from cart functionality.
  $this
    ->submitForm([
    'items[1]' => TRUE,
    'items[2]' => FALSE,
    'items[3]' => FALSE,
    'items[4]' => TRUE,
    'items[5]' => FALSE,
  ], 'Remove selected item');
  $this
    ->assertSession()
    ->pageTextContains($job_items[2]
    ->label());
  $this
    ->assertSession()
    ->pageTextContains($job_items[3]
    ->label());
  $this
    ->assertSession()
    ->pageTextContains($job_items[5]
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[1]
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[4]
    ->label());
  $this
    ->assertSession()
    ->pageTextContains('Job items were removed from the cart.');

  // Test that removed job items from cart were deleted as well.
  $existing_items = JobItem::loadMultiple();
  $this
    ->assertTrue(!isset($existing_items[$job_items[1]
    ->id()]));
  $this
    ->assertTrue(!isset($existing_items[$job_items[4]
    ->id()]));
  $this
    ->submitForm([], 'Empty cart');
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[2]
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[3]
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[5]
    ->label());
  $this
    ->assertSession()
    ->pageTextContains('All job items were removed from the cart.');

  // No remaining job items.
  $existing_items = JobItem::loadMultiple();
  $this
    ->assertTrue(empty($existing_items));
  $language_sequence = array(
    'en',
    'en',
    'fr',
    'fr',
    'de',
    'de',
  );
  for ($i = 1; $i < 7; $i++) {
    $job_item = tmgmt_job_item_create('test_source', 'test', $i);
    $job_item
      ->save();
    $job_items[$i] = $job_item;
    $languages[$job_items[$i]
      ->id()] = $language_sequence[$i - 1];
  }
  \Drupal::state()
    ->set('tmgmt.test_source_languages', $languages);
  foreach ($job_items as $job_item) {
    $this
      ->drupalGet('tmgmt-add-to-cart/' . $job_item
      ->id());
  }
  $this
    ->drupalGet('admin/tmgmt/cart');
  $this
    ->submitForm([
    'items[' . $job_items[1]
      ->id() . ']' => TRUE,
    'items[' . $job_items[2]
      ->id() . ']' => TRUE,
    'items[' . $job_items[3]
      ->id() . ']' => TRUE,
    'items[' . $job_items[4]
      ->id() . ']' => TRUE,
    'items[' . $job_items[5]
      ->id() . ']' => TRUE,
    'items[' . $job_items[6]
      ->id() . ']' => FALSE,
    'target_language[]' => [
      'en',
      'de',
    ],
  ], 'Request translation');
  $this
    ->assertSession()
    ->pageTextContains(t('@count jobs need to be checked out.', array(
    '@count' => 4,
  )));

  // We should have four jobs with following language combinations:
  // [fr, fr] => [en]
  // [de] => [en]
  // [en, en] => [de]
  // [fr, fr] => [de]
  $storage = \Drupal::entityTypeManager()
    ->getStorage('tmgmt_job');
  $jobs = $storage
    ->loadByProperties([
    'source_language' => 'fr',
    'target_language' => 'en',
  ]);
  $job = reset($jobs);
  $this
    ->assertCount(2, $job
    ->getItems());
  $jobs = $storage
    ->loadByProperties([
    'source_language' => 'de',
    'target_language' => 'en',
  ]);
  $job = reset($jobs);
  $this
    ->assertCount(1, $job
    ->getItems());
  $jobs = $storage
    ->loadByProperties([
    'source_language' => 'en',
    'target_language' => 'de',
  ]);
  $job = reset($jobs);
  $this
    ->assertCount(2, $job
    ->getItems());
  $jobs = $storage
    ->loadByProperties([
    'source_language' => 'fr',
    'target_language' => 'de',
  ]);
  $job = reset($jobs);
  $this
    ->assertCount(2, $job
    ->getItems());
  $this
    ->drupalGet('admin/tmgmt/cart');

  // Both fr and one de items must be gone.
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[1]
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[2]
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[3]
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[4]
    ->label());
  $this
    ->assertSession()
    ->pageTextNotContains($job_items[5]
    ->label());

  // One de item is in the cart as it was not selected for checkout.
  $this
    ->assertSession()
    ->pageTextContains($job_items[6]
    ->label());

  // Check to see if no items are selected and the error message pops up.
  $this
    ->drupalGet('admin/tmgmt/cart');
  $this
    ->submitForm([
    'items[' . $job_items[6]
      ->id() . ']' => FALSE,
  ], 'Request translation');
  $this
    ->assertSession()
    ->pageTextContainsOnce("You didn't select any source items.");
}