123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- /**
- * Basic Entity Source tests.
- */
- class TMGMTEntitySourceTestCase extends TMGMTEntityTestCaseUtility {
- public $vocabulary;
- static function getInfo() {
- return array(
- 'name' => 'Entity Source tests',
- 'description' => 'Exporting source data from entities and saving translations back to entities.',
- 'group' => 'Translation Management',
- 'dependencies' => array('entity_translation'),
- );
- }
- function setUp() {
- parent::setUp(array('tmgmt_entity', 'taxonomy', 'entity_translation'));
- // Admin user to perform settings on setup.
- $this->loginAsAdmin(array('administer entity translation'));
- $this->vocabulary = $this->createTaxonomyVocab(strtolower($this->randomName()), $this->randomName(), array(FALSE, TRUE, TRUE, TRUE));
- // Enable entity translations for taxonomy.
- $edit['entity_translation_entity_types[taxonomy_term]'] = 1;
- $this->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
- }
- /**
- * Tests nodes field translation.
- */
- function testEntitySourceNode() {
- $this->setEnvironment('de');
- $this->createNodeType('article', 'Article', ENTITY_TRANSLATION_ENABLED);
- // Create a translation job.
- $job = $this->createJob();
- $job->translator = $this->default_translator->name;
- $job->settings = array();
- $job->save();
- // Create some nodes.
- for ($i = 1; $i <= 5; $i++) {
- $node = $this->createNode('article');
- // Create a job item for this node and add it to the job.
- $item = $job->addItem('entity', 'node', $node->nid);
- $this->assertEqual(t('@type (@bundle)', array('@type' => t('Node'), '@bundle' => 'Article')), $item->getSourceType());
- }
- // Translate the job.
- $job->requestTranslation();
- // Check the translated job items.
- foreach ($job->getItems() as $item) {
- // The source is available only for en.
- $this->assertJobItemLangCodes($item, 'en', array('en'));
- $item->acceptTranslation();
- $this->assertTrue($item->isAccepted());
- $entity = entity_load_single($item->item_type, $item->item_id);
- $data = $item->getData();
- $this->checkTranslatedData($entity, $data, 'de');
- $this->checkUntranslatedData($entity, $this->field_names['node']['article'], $data, 'de');
- // The source is now available for both en and de.
- $this->assertJobItemLangCodes($item, 'en', array('de', 'en'));
- }
- }
- /**
- * Tests taxonomy terms field translation.
- */
- function testEntitySourceTerm() {
- $this->setEnvironment('de');
- // Create the job.
- $job = $this->createJob();
- $job->translator = $this->default_translator->name;
- $job->settings = array();
- $job->save();
- $term = NULL;
- //Create some terms.
- for ($i = 1; $i <= 5; $i++) {
- $term = $this->createTaxonomyTerm($this->vocabulary);
- // Create the item and assign it to the job.
- $item = $job->addItem('entity', 'taxonomy_term', $term->tid);
- $this->assertEqual(t('@type (@bundle)', array('@type' => t('Taxonomy term'), '@bundle' => $this->vocabulary->name)), $item->getSourceType());
- }
- // Request the translation and accept it.
- $job->requestTranslation();
- // Check if the fields were translated.
- foreach ($job->getItems() as $item) {
- $this->assertJobItemLangCodes($item, 'en', array('en'));
- $item->acceptTranslation();
- $entity = entity_load_single($item->item_type, $item->item_id);
- $data = $item->getData();
- $this->checkTranslatedData($entity, $data, 'de');
- $this->checkUntranslatedData($entity, $this->field_names['taxonomy_term'][$this->vocabulary->machine_name], $data, 'de');
- $this->assertJobItemLangCodes($item, 'en', array('de', 'en'));
- }
- }
- function testAddingJobItemsWithEmptySourceText() {
- $this->setEnvironment('de');
- // Create term with empty texts.
- $empty_term = new stdClass();
- $empty_term->name = $this->randomName();
- $empty_term->description = $this->randomName();
- $empty_term->vid = $this->vocabulary->vid;
- taxonomy_term_save($empty_term);
- // Create the job.
- $job = tmgmt_job_create('en', NULL);
- try {
- $job->addItem('entity', 'taxonomy_term', $empty_term->tid);
- $this->fail('Job item added with empty source text.');
- }
- catch (TMGMTException $e) {
- $this->assert(empty($job->tjid), 'After adding a job item with empty source text its tjid has to be unset.');
- }
- // Create term with populated source content.
- $populated_content_term = $this->createTaxonomyTerm($this->vocabulary);
- // Lets reuse the last created term with populated source content.
- $job->addItem('entity', 'taxonomy_term', $populated_content_term->tid);
- $this->assert(!empty($job->tjid), 'After adding another job item with populated source text its tjid must be set.');
- }
- /**
- * Test if the source is able to pull content in requested language.
- */
- function testRequestDataForSpecificLanguage() {
- $this->setEnvironment('de');
- $this->setEnvironment('cs');
- $this->createNodeType('article', 'Article', ENTITY_TRANSLATION_ENABLED);
- // Create a translation job.
- $job = $this->createJob('en', 'de');
- $job->translator = $this->default_translator->name;
- $job->settings = array();
- $job->save();
- $node = $this->createNode('article', 'cs');
- $node->body['en'][0]['value'] = 'en translation';
- node_save($node);
- $job->addItem('entity', 'node', $node->nid);
- $data = $job->getData();
- $this->assertEqual($data[1]['body'][0]['value']['#text'], 'en translation');
- }
- /**
- * Compares the data from an entity with the translated data.
- *
- * @param $tentity
- * The translated entity object.
- * @param $data
- * An array with the translated data.
- * @param $langcode
- * The code of the target language.
- */
- function checkTranslatedData($tentity, $data, $langcode) {
- foreach (element_children($data) as $field_name) {
- foreach (element_children($data[$field_name]) as $delta) {
- foreach (element_children($data[$field_name][$delta]) as $column) {
- $column_value = $data[$field_name][$delta][$column];
- if (!empty($column_value['#translate'])) {
- $this->assertEqual($tentity->{$field_name}[$langcode][$delta][$column], $column_value['#translation']['#text'], format_string('The field %field:%delta has been populated with the proper translated data.', array('%field' => $field_name, 'delta' => $delta)));
- }
- else {
- $this->assertEqual($tentity->{$field_name}[$langcode][$delta][$column], $column_value['#text'], format_string('The field %field:%delta has been populated with the proper untranslated data.', array('%field' => $field_name, 'delta' => $delta)));
- }
- }
- }
- }
- }
- /**
- * Checks the fields that should not be translated.
- *
- * @param $tentity
- * The translated entity object.
- * @param $fields
- * An array with the field names to check.
- * @param $translation
- * An array with the translated data.
- * @param $langcode
- * The code of the target language.
- */
- function checkUntranslatedData($tentity, $fields, $data, $langcode) {
- foreach ($fields as $field_name) {
- $field_info = field_info_field($field_name);
- if (!$field_info['translatable']) {
- // Avoid some PHP warnings.
- if (isset($data[$field_name])) {
- $this->assertNull($data[$field_name]['#translation']['#text'], 'The not translatable field was not translated.');
- }
- if (isset($tentity->{$field_name}[$langcode])) {
- $this->assertNull($tentity->{$field_name}[$langcode], 'The entity has translated data in a field that is translatable.');
- }
- }
- }
- }
- }
|