You are here

RequestBulkTranslationTest.php in TMGMT Extension Suite 8

File

src/Tests/RequestBulkTranslationTest.php
View source
<?php

namespace Drupal\tmgmt_extension_suit\Tests;

use Drupal\Core\Language\Language;
use Drupal\node\Entity\Node;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Form\SourceOverviewForm;

/**
 * Class RequestBulkTranslationTest
 *
 * @group tmgmt_extension_suit
 */
class RequestBulkTranslationTest extends TmgmtExtensionSuitTestBase {

  /**
   * Check target language preselection.
   */
  function testBulkSubmitTranslationPreselectTargetLanguage() {
    $settings = [
      'body' => [
        [
          'value' => $this
            ->randomMachineName(32),
          'format' => filter_default_format(),
        ],
      ],
      'title' => $this
        ->randomMachineName(8),
      'type' => 'translatable_node',
      'uid' => \Drupal::currentUser()
        ->id(),
      'langcode' => 'fr',
    ];
    $node = Node::create($settings);
    $node
      ->save();

    // Preselect target languages: later.
    $this
      ->drupalPostForm('admin/tmgmt/sources', [
      'items[1]' => 'item[1]',
      'target_language' => Language::LANGCODE_NOT_SPECIFIED,
    ], t('Request translation in a batch'));
    $this
      ->assertFalse($this
      ->isFieldChecked('edit-target-language-de'));
    $this
      ->assertFalse($this
      ->isFieldChecked('edit-target-language-fr'));
    $this
      ->assertNoField('edit-target-language-en');

    // Preselect target languages: multiple.
    $this
      ->drupalPostForm('admin/tmgmt/sources', [
      'items[1]' => 'item[1]',
      'target_language' => SourceOverviewForm::MULTIPLE,
      'target_languages[en]' => 'en',
      'target_languages[fr]' => 'fr',
    ], t('Request translation in a batch'));
    $this
      ->assertFalse($this
      ->isFieldChecked('edit-target-language-de'));
    $this
      ->assertTrue($this
      ->isFieldChecked('edit-target-language-fr'));
    $this
      ->assertNoField('edit-target-language-en');

    // Preselect target languages: all.
    $this
      ->drupalPostForm('admin/tmgmt/sources', [
      'items[1]' => 'item[1]',
      'target_language' => SourceOverviewForm::ALL,
    ], t('Request translation in a batch'));
    $this
      ->assertTrue($this
      ->isFieldChecked('edit-target-language-de'));
    $this
      ->assertTrue($this
      ->isFieldChecked('edit-target-language-fr'));
    $this
      ->assertNoField('edit-target-language-en');

    // Preselect target languages: single.
    $this
      ->drupalPostForm('admin/tmgmt/sources', [
      'items[1]' => 'item[1]',
      'target_language' => 'de',
    ], t('Request translation in a batch'));
    $this
      ->assertTrue($this
      ->isFieldChecked('edit-target-language-de'));
    $this
      ->assertFalse($this
      ->isFieldChecked('edit-target-language-fr'));
    $this
      ->assertNoField('edit-target-language-en');
  }

  /**
   * Test validation for bulk translation.
   */
  function testRequestBulkTranslationValidation() {
    $settings = [
      'body' => [
        [
          'value' => $this
            ->randomMachineName(32),
          'format' => filter_default_format(),
        ],
      ],
      'title' => $this
        ->randomMachineName(8),
      'type' => 'translatable_node',
      'uid' => \Drupal::currentUser()
        ->id(),
      'langcode' => 'fr',
    ];
    $node = Node::create($settings);
    $node
      ->save();

    // Didn't select any nodes.
    $this
      ->requestBulkTranslation([], []);
    $this
      ->assertText("You didn't select any source items.");

    // Select nodes with different source languages.
    $this
      ->requestBulkTranslation([
      3,
      $node
        ->id(),
    ], []);
    $this
      ->assertText("You can't request translation in a batch for items in different source languages.");

    // Selected source language != entity source language.
    $this
      ->drupalPostForm('admin/tmgmt/sources', [
      'items[1]' => 'item[1]',
      'source_language' => 'de',
    ], t('Request translation in a batch'));
    $this
      ->assertText('From the selection you made it was not possible to create any translation job.');

    // Select nodes with same source languages.
    $this
      ->requestBulkTranslation([
      2,
      3,
    ], []);
    $this
      ->assertTitle("Request translation | Drupal");
  }

  /**
   * Test request bulk translation.
   */
  function testRequestBulkTranslation() {
    $this
      ->requestBulkTranslation([
      2,
      3,
    ], [
      'fr',
      'de',
    ], 'local', [
      'settings[translator]' => 1,
      'settings[job_comment]' => 'Comment',
    ]);
    $this
      ->assertText('Translation request queued for 4 jobs. Return to "Sources" page.');

    // In upload queue should be 4 jobs: 2 nodes in 2 languages.
    $this
      ->assertEqual($this
      ->getCountOfItemsInQueue('tmgmt_extension_suit_upload'), 4);
    $this
      ->assertEqual($this
      ->isItemAddedToQueue('tmgmt_extension_suit_upload', 1), 1);
    $this
      ->assertEqual($this
      ->isItemAddedToQueue('tmgmt_extension_suit_upload', 2), 1);
    $this
      ->assertEqual($this
      ->isItemAddedToQueue('tmgmt_extension_suit_upload', 3), 1);
    $this
      ->assertEqual($this
      ->isItemAddedToQueue('tmgmt_extension_suit_upload', 4), 1);
    $jobs = Job::loadMultiple([
      1,
      2,
      3,
      4,
    ]);

    // Check source language.
    $this
      ->assertEqual($jobs[1]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[2]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[3]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[4]
      ->getSourceLangcode(), 'en');

    // Check target language.
    $this
      ->assertEqual($jobs[1]
      ->getTargetLangcode(), 'de');
    $this
      ->assertEqual($jobs[2]
      ->getTargetLangcode(), 'fr');
    $this
      ->assertEqual($jobs[3]
      ->getTargetLangcode(), 'de');
    $this
      ->assertEqual($jobs[4]
      ->getTargetLangcode(), 'fr');

    // Check job settings.
    $this
      ->assertEqual($jobs[1]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
    $this
      ->assertEqual($jobs[2]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
    $this
      ->assertEqual($jobs[3]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
    $this
      ->assertEqual($jobs[4]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
  }

  /**
   * Test request bulk translation with conflicting item.
   */
  public function testRequestBulkTranslationSkippingNodeInOneLanguage() {

    // Let's get a conflicting job item.
    $this
      ->requestTranslation([
      1,
    ], 'fr', 1);

    // Request bulk translation including conflicting job item.
    $this
      ->requestBulkTranslation([
      1,
      2,
      3,
    ], [
      'fr',
    ], 'local', [
      'settings[translator]' => 1,
      'settings[job_comment]' => 'Comment',
    ]);
    $this
      ->assertText('1 item conflicted with pending item and was skipped. Existing job item:');
    $this
      ->assertText('Job item with source language = "en" and target language = "fr" for entity "node" with id = "1".');
    $this
      ->assertText('Translation request queued for 2 jobs. Return to "Sources" page.');

    // In upload queue should be 2 jobs: 2 nodes in fr.
    $this
      ->assertEqual($this
      ->getCountOfItemsInQueue('tmgmt_extension_suit_upload'), 2);
    $jobs = Job::loadMultiple([
      1,
      2,
      3,
    ]);

    // Check source language.
    $this
      ->assertEqual($jobs[1]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[2]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[3]
      ->getSourceLangcode(), 'en');

    // Check target language.
    $this
      ->assertEqual($jobs[1]
      ->getTargetLangcode(), 'fr');
    $this
      ->assertEqual($jobs[2]
      ->getTargetLangcode(), 'fr');
    $this
      ->assertEqual($jobs[3]
      ->getTargetLangcode(), 'fr');

    // Check job settings.
    $this
      ->assertEqual($jobs[1]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => '',
        'job_comment' => '',
      ],
    ]);
    $this
      ->assertEqual($jobs[2]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
    $this
      ->assertEqual($jobs[3]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
  }

  /**
   * Test request bulk translation with conflicting items.
   */
  public function testRequestBulkTranslationSkippingNodeInTwoLanguages() {

    // Let's get a conflicting job items.
    $this
      ->requestTranslation([
      1,
    ], 'fr', 1);
    $this
      ->requestTranslation([
      1,
    ], 'de', 2);

    // Request bulk translation including conflicting job items.
    $this
      ->requestBulkTranslation([
      1,
      2,
      3,
    ], [
      'fr',
      'de',
    ], 'local', [
      'settings[translator]' => 1,
      'settings[job_comment]' => 'Comment',
    ]);
    $this
      ->assertText('2 items conflicted with pending items and were skipped. Existing job items:');
    $this
      ->assertText('Job item with source language = "en" and target language = "fr" for entity "node" with id = "1".');
    $this
      ->assertText('Job item with source language = "en" and target language = "de" for entity "node" with id = "1".');
    $this
      ->assertText('Translation request queued for 4 jobs. Return to "Sources" page.');

    // In upload queue should be 4 jobs: 2 nodes in fr and de.
    $this
      ->assertEqual($this
      ->getCountOfItemsInQueue('tmgmt_extension_suit_upload'), 4);
    $jobs = Job::loadMultiple([
      1,
      2,
      3,
      4,
      5,
      6,
    ]);

    // Check source language.
    $this
      ->assertEqual($jobs[1]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[2]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[3]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[4]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[5]
      ->getSourceLangcode(), 'en');
    $this
      ->assertEqual($jobs[6]
      ->getSourceLangcode(), 'en');

    // Check target language.
    $this
      ->assertEqual($jobs[1]
      ->getTargetLangcode(), 'fr');
    $this
      ->assertEqual($jobs[2]
      ->getTargetLangcode(), 'de');
    $this
      ->assertEqual($jobs[3]
      ->getTargetLangcode(), 'de');
    $this
      ->assertEqual($jobs[4]
      ->getTargetLangcode(), 'fr');
    $this
      ->assertEqual($jobs[5]
      ->getTargetLangcode(), 'de');
    $this
      ->assertEqual($jobs[6]
      ->getTargetLangcode(), 'fr');

    // Check job settings.
    $this
      ->assertEqual($jobs[1]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => '',
        'job_comment' => '',
      ],
    ]);
    $this
      ->assertEqual($jobs[2]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => '',
        'job_comment' => '',
      ],
    ]);
    $this
      ->assertEqual($jobs[3]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
    $this
      ->assertEqual($jobs[4]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
    $this
      ->assertEqual($jobs[5]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
    $this
      ->assertEqual($jobs[6]
      ->get('settings')
      ->getValue(), [
      [
        'translator' => 1,
        'job_comment' => 'Comment',
      ],
    ]);
  }

}

Classes

Namesort descending Description
RequestBulkTranslationTest Class RequestBulkTranslationTest