View source
<?php
namespace Drupal\Tests\tmgmt_google\Functional;
use Drupal\node\Entity\Node;
use Drupal\Tests\tmgmt\Functional\TMGMTTestBase;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\JobItemInterface;
use Drupal\tmgmt_google\Plugin\tmgmt\Translator\GoogleTranslator;
use Drupal\tmgmt\TranslatorInterface;
use Drupal\Core\Url;
class GoogleTranslatorTest extends TMGMTTestBase {
protected $translator;
public static $modules = array(
'tmgmt_google',
'tmgmt_google_test',
);
public function setUp() {
parent::setUp();
$this
->addLanguage('de');
$url = Url::fromUri('base://tmgmt_google_test', array(
'absolute' => TRUE,
))
->toString();
$this->translator = $this
->createTranslator([
'plugin' => 'google',
'settings' => [
'url' => $url,
],
]);
}
public function testGoogle() {
$plugin = $this->translator
->getPlugin();
$this
->assertTrue($plugin instanceof GoogleTranslator, 'Plugin is a GoogleTranslator');
$job = $this
->createJob();
$job->translator = $this->translator
->id();
$job
->save();
$item = $job
->addItem('test_source', 'test', '1');
$item->data = array(
'wrapper' => array(
'#text' => 'Hello world & welcome',
),
);
$item
->save();
$this
->assertFalse($job
->isTranslatable(), 'Check if the translator is not available at this point because we did not define the API parameters.');
$this->translator
->setSetting('api_key', 'wrong key');
$this->translator
->save();
$languages = $this->translator
->getSupportedTargetLanguages('en');
$this
->assertTrue(empty($languages), t('We can not get the languages using wrong api parameters.'));
$this->translator
->setSetting('api_key', 'correct key');
$this->translator
->save();
$this->translator
->clearLanguageCache();
$languages = $this->translator
->getSupportedTargetLanguages('en');
$this
->assertTrue(isset($languages['de']));
$this
->assertTrue(isset($languages['fr']));
$this
->assertTrue(!isset($languages['en']));
$this
->assertTrue($job
->canRequestTranslation()
->getSuccess());
$job
->requestTranslation();
foreach ($job
->getItems() as $item) {
$this
->assertTrue($item
->isNeedsReview());
}
$items = $job
->getItems();
$item = end($items);
$data = $item
->getData();
$this
->assertEqual('Hallo Welt & willkommen', $data['dummy']['deep_nesting']['#translation']['#text']);
$this
->config('tmgmt.settings')
->set('submit_job_item_on_cron', TRUE)
->save();
$continuous_settings = [
'content' => [
'node' => [
'enabled' => 1,
'bundles' => [
'test' => 1,
],
],
],
];
$continuous_job = $this
->createJob('en', 'de', 0, [
'label' => 'Continuous job',
'job_type' => Job::TYPE_CONTINUOUS,
'translator' => $this->translator,
'continuous_settings' => $continuous_settings,
]);
$continuous_job
->save();
$node = Node::create([
'title' => $this
->randomMachineName(),
'uid' => 0,
'type' => 'test',
'langcode' => 'en',
]);
$node
->save();
$continuous_job
->addItem('test_source', $node
->getEntityTypeId(), $node
->id());
$continuous_job_items = $continuous_job
->getItems();
$continuous_job_item = reset($continuous_job_items);
$this
->assertTrue($continuous_job_item
->getState() == JobItemInterface::STATE_INACTIVE);
tmgmt_cron();
$items = $continuous_job
->getItems();
$item = reset($items);
$data = $item
->getData();
$this
->assertEqual('Hallo Welt & willkommen', $data['dummy']['deep_nesting']['#translation']['#text']);
$this
->assertTrue($continuous_job
->getState() == Job::STATE_CONTINUOUS);
$this
->assertTrue($item
->getState() == JobItemInterface::STATE_REVIEW);
}
public function testGoogleUi() {
$url = Url::fromRoute('entity.tmgmt_translator.edit_form', [
'tmgmt_translator' => $this->translator
->id(),
]);
$this
->loginAsAdmin();
$edit = [
'settings[api_key]' => 'wrong key',
];
$this
->drupalPostForm($url, $edit, t('Connect'));
$this
->assertText(t('The "Google API key" is not correct.'));
$edit = [
'settings[api_key]' => 'correct key',
];
$this
->drupalPostForm($url, $edit, t('Connect'));
$this
->assertText('Successfully connected!');
$this
->assertOptionSelected('edit-remote-languages-mappings-en', 'en');
$this
->assertOptionSelected('edit-remote-languages-mappings-de', 'de');
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->assertText(t('@label configuration has been updated.', [
'@label' => $this->translator
->label(),
]));
}
}