View source
<?php
namespace Drupal\lingotek\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Tests\TaxonomyTestTrait;
class LingotekTaxonomyTermTranslationTest extends LingotekTestBase {
use TaxonomyTestTrait;
public static $modules = [
'block',
'taxonomy',
];
protected $vocabulary;
protected $term;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$this->vocabulary = $this
->createVocabulary();
ConfigurableLanguage::createFromLangcode('es')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $this->vocabulary
->id())
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('taxonomy_term', $this->vocabulary
->id(), TRUE);
\Drupal::service('entity.definition_update_manager')
->applyUpdates();
$this
->rebuildContainer();
$bundle = $this->vocabulary
->id();
$edit = [
"taxonomy_term[{$bundle}][enabled]" => 1,
"taxonomy_term[{$bundle}][profiles]" => 'automatic',
"taxonomy_term[{$bundle}][fields][name]" => 1,
"taxonomy_term[{$bundle}][fields][description]" => 1,
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-content-form');
\Drupal::state()
->set('lingotek.uploaded_content_type', 'taxonomy_term');
}
public function testTermTranslation() {
$this
->drupalLogin($this->rootUser);
$bundle = $this->vocabulary
->id();
$edit = array();
$edit['name[0][value]'] = 'Llamas are cool';
$edit['description[0][value]'] = 'Llamas are very cool';
$edit['langcode[0][value]'] = 'en';
$this
->drupalPostForm("admin/structure/taxonomy/manage/{$bundle}/add", $edit, t('Save'));
$this->term = Term::load(1);
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), true);
$this
->assertUploadedDataFieldCount($data, 2);
$this
->assertTrue(isset($data['name'][0]['value']));
$this
->assertEqual(1, count($data['description'][0]));
$this
->assertTrue(isset($data['description'][0]['value']));
$this
->drupalGet('taxonomy/term/1');
$this
->clickLink('Translate');
$this
->clickLink('Check Upload Status');
$this
->assertText('The import for taxonomy_term Llamas are cool is complete.');
$this
->clickLink('Request translation');
$this
->assertText("Locale 'es_ES' was added as a translation target for taxonomy_term Llamas are cool.");
$this
->clickLink('Check translation status');
$this
->assertText('The es_ES translation for taxonomy_term Llamas are cool is ready for download.');
$this
->clickLink('Download completed translation');
$this
->assertText('The translation of taxonomy_term Llamas are cool into es_ES has been downloaded.');
$this
->clickLink('Las llamas son chulas');
$this
->assertText('Las llamas son chulas');
$this
->assertText('Las llamas son muy chulas');
}
public function testTermTranslationViaAPIWithAutomatedUpload() {
$this
->drupalLogin($this->rootUser);
$this->term = Term::create([
'name' => 'Llamas are cool',
'description' => 'Llamas are very cool',
'langcode' => 'en',
'vid' => $this->vocabulary
->id(),
]);
$this->term
->save();
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), true);
$this
->assertUploadedDataFieldCount($data, 2);
$this
->assertTrue(isset($data['name'][0]['value']));
$this
->assertEqual(1, count($data['description'][0]));
$this
->assertTrue(isset($data['description'][0]['value']));
$this
->drupalGet('taxonomy/term/1');
$this
->clickLink('Translate');
$this
->clickLink('Check Upload Status');
$this
->assertText('The import for taxonomy_term Llamas are cool is complete.');
$this
->clickLink('Request translation');
$this
->assertText("Locale 'es_ES' was added as a translation target for taxonomy_term Llamas are cool.");
$this
->clickLink('Check translation status');
$this
->assertText('The es_ES translation for taxonomy_term Llamas are cool is ready for download.');
$this
->clickLink('Download completed translation');
$this
->assertText('The translation of taxonomy_term Llamas are cool into es_ES has been downloaded.');
$this
->clickLink('Las llamas son chulas');
$this
->assertText('Las llamas son chulas');
$this
->assertText('Las llamas son muy chulas');
}
public function testTermTranslationViaAPIWithManualUpload() {
$this
->drupalLogin($this->rootUser);
$bundle = $this->vocabulary
->id();
$edit = [
"taxonomy_term[{$bundle}][enabled]" => 1,
"taxonomy_term[{$bundle}][profiles]" => 'manual',
"taxonomy_term[{$bundle}][fields][name]" => 1,
"taxonomy_term[{$bundle}][fields][description]" => 1,
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-content-form');
$this->term = Term::create([
'name' => 'Llamas are cool',
'description' => 'Llamas are very cool',
'langcode' => 'en',
'vid' => $this->vocabulary
->id(),
]);
$this->term
->save();
$this
->drupalGet('taxonomy/term/1');
$this
->clickLink('Translate');
$this
->clickLink('Upload');
$this
->assertText('Uploaded 1 document to Lingotek.');
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), true);
$this
->assertUploadedDataFieldCount($data, 2);
$this
->assertTrue(isset($data['name'][0]['value']));
$this
->assertEqual(1, count($data['description'][0]));
$this
->assertTrue(isset($data['description'][0]['value']));
$this
->clickLink('Check Upload Status');
$this
->assertText('The import for taxonomy_term Llamas are cool is complete.');
$this
->clickLink('Request translation');
$this
->assertText("Locale 'es_ES' was added as a translation target for taxonomy_term Llamas are cool.");
$this
->clickLink('Check translation status');
$this
->assertText('The es_ES translation for taxonomy_term Llamas are cool is ready for download.');
$this
->clickLink('Download completed translation');
$this
->assertText('The translation of taxonomy_term Llamas are cool into es_ES has been downloaded.');
$this
->clickLink('Las llamas son chulas');
$this
->assertText('Las llamas son chulas');
$this
->assertText('Las llamas son muy chulas');
}
}