function TMGMTUiTest::testCart in Translation Management Tool 8
Test the cart functionality.
File
- tests/
src/ Functional/ TMGMTUiTest.php, line 578
Class
- TMGMTUiTest
- Verifies basic functionality of the user interface
Namespace
Drupal\Tests\tmgmt\FunctionalCode
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
->assertText($job_item
->label());
}
// Test the remove items from cart functionality.
$this
->drupalPostForm(NULL, [
'items[1]' => TRUE,
'items[2]' => FALSE,
'items[3]' => FALSE,
'items[4]' => TRUE,
'items[5]' => FALSE,
], t('Remove selected item'));
$this
->assertText($job_items[2]
->label());
$this
->assertText($job_items[3]
->label());
$this
->assertText($job_items[5]
->label());
$this
->assertNoText($job_items[1]
->label());
$this
->assertNoText($job_items[4]
->label());
$this
->assertText(t('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
->drupalPostForm(NULL, array(), t('Empty cart'));
$this
->assertNoText($job_items[2]
->label());
$this
->assertNoText($job_items[3]
->label());
$this
->assertNoText($job_items[5]
->label());
$this
->assertText(t('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
->drupalPostForm('admin/tmgmt/cart', array(
'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[]' => array(
'en',
'de',
),
), t('Request translation'));
$this
->assertText(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
->assertEquals(2, count($job
->getItems()));
$jobs = $storage
->loadByProperties([
'source_language' => 'de',
'target_language' => 'en',
]);
$job = reset($jobs);
$this
->assertEquals(1, count($job
->getItems()));
$jobs = $storage
->loadByProperties([
'source_language' => 'en',
'target_language' => 'de',
]);
$job = reset($jobs);
$this
->assertEquals(2, count($job
->getItems()));
$jobs = $storage
->loadByProperties([
'source_language' => 'fr',
'target_language' => 'de',
]);
$job = reset($jobs);
$this
->assertEquals(2, count($job
->getItems()));
$this
->drupalGet('admin/tmgmt/cart');
// Both fr and one de items must be gone.
$this
->assertNoText($job_items[1]
->label());
$this
->assertNoText($job_items[2]
->label());
$this
->assertNoText($job_items[3]
->label());
$this
->assertNoText($job_items[4]
->label());
$this
->assertNoText($job_items[5]
->label());
// One de item is in the cart as it was not selected for checkout.
$this
->assertText($job_items[6]
->label());
// Check to see if no items are selected and the error message pops up.
$this
->drupalPostForm('admin/tmgmt/cart', [
'items[' . $job_items[6]
->id() . ']' => FALSE,
], t('Request translation'));
$this
->assertUniqueText(t("You didn't select any source items."));
}