View source
<?php
class TMGMTI18nStringSourceTestCase extends TMGMTBaseTestCase {
static function getInfo() {
return array(
'name' => 'i18n String Source tests',
'description' => 'Exporting source data from i18n string and saving translations back',
'group' => 'Translation Management',
'dependencies' => array(
'i18n_string',
),
);
}
function setUp() {
parent::setUp(array(
'tmgmt_ui',
'tmgmt_i18n_string',
'taxonomy',
'i18n_taxonomy',
'i18n_block',
'i18n_field',
'list',
'i18n_menu',
));
$this
->setEnvironment('de');
$this->translator = $this
->createTranslator();
}
function testI18nStringSourceTaxonomy() {
$config = array(
'name' => $this
->randomName(),
'machine_name' => 'test_vocab',
'i18n_mode' => I18N_MODE_LOCALIZE,
);
$vocabulary = entity_create('taxonomy_vocabulary', $config);
taxonomy_vocabulary_save($vocabulary);
$string_object_name = "taxonomy:vocabulary:" . $vocabulary->vid;
$source_text = $vocabulary->name;
$job = $this
->createJob();
$job->translator = $this->translator->name;
$job->settings = array();
$job
->save();
$item1 = $job
->addItem('i18n_string', 'taxonomy_vocabulary', $string_object_name);
$this
->assertEqual(t('Vocabulary'), $item1
->getSourceType());
$job
->requestTranslation();
foreach ($job
->getItems() as $item) {
$item
->acceptTranslation();
}
$this
->assertEqual($item1->item_id, $string_object_name, 'i18n Strings object correctly saved');
$this
->assertEqual(i18n_string_translate('taxonomy:vocabulary:' . $vocabulary->vid . ':name', $source_text, array(
'langcode' => $job->target_language,
)), $job->target_language . '_' . $source_text);
$term = entity_create('taxonomy_term', array(
'vid' => $vocabulary->vid,
'name' => $this
->randomName(),
'description' => $this
->randomName(),
));
taxonomy_term_save($term);
$string_object_name = "taxonomy:term:" . $term->tid;
$source_text_name = $term->name;
$source_text_description = $term->description;
$job = $this
->createJob();
$job->translator = $this->translator->name;
$job->settings = array();
$job
->save();
$item1 = $job
->addItem('i18n_string', 'taxonomy_term', $string_object_name);
$this
->assertEqual(t('Taxonomy term'), $item1
->getSourceType());
$job
->requestTranslation();
foreach ($job
->getItems() as $item) {
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
));
$item
->acceptTranslation();
$this
->assertJobItemLangCodes($item, 'en', array(
'de',
'en',
));
}
$this
->assertEqual($item1->item_id, $string_object_name);
$this
->assertEqual(i18n_string_translate('taxonomy:term:' . $term->tid . ':name', $source_text_name, array(
'langcode' => $job->target_language,
)), $job->target_language . '_' . $source_text_name);
$this
->assertEqual(i18n_string_translate('taxonomy:term:' . $term->tid . ':description', $source_text_description, array(
'langcode' => $job->target_language,
)), $job->target_language . '_' . $source_text_description);
}
function testRequestDataForSpecificLanguage() {
$this
->setEnvironment('es');
$this
->setEnvironment('cs');
$config = array(
'name' => $this
->randomName(),
'machine_name' => 'test_vocab',
'i18n_mode' => I18N_MODE_LOCALIZE,
);
$vocabulary = entity_create('taxonomy_vocabulary', $config);
taxonomy_vocabulary_save($vocabulary);
$string_object_name = "taxonomy:vocabulary:" . $vocabulary->vid;
i18n_string_translation_update($string_object_name . ':name', 'de translation', 'de');
$job = $this
->createJob('de', 'cs');
$job
->save();
$job
->addItem('i18n_string', 'taxonomy_vocabulary', $string_object_name);
$data = $job
->getData();
$this
->assertEqual($data[1][$string_object_name . ':name']['#text'], 'de translation');
$job = $this
->createJob('es', 'cs');
$job
->save();
try {
$job
->addItem('i18n_string', 'taxonomy_vocabulary', $string_object_name);
$this
->fail('The job item should not be added as there is no translation for language "es"');
} catch (TMGMTException $e) {
$languages = language_list();
$this
->assertEqual(t('Unable to load %language translation for the string %title', array(
'%language' => $languages['es']->name,
'%title' => 'Name',
)), $e
->getMessage());
}
}
function testI18nStringSourceMenu() {
drupal_static_reset('_tmgmt_plugin_info');
drupal_static_reset('_tmgmt_plugin_controller');
$config = array(
'menu_name' => $this
->randomName(),
'title' => $this
->randomName(),
'description' => $this
->randomName(),
'i18n_mode' => I18N_MODE_MULTIPLE,
);
menu_save($config);
$menu = menu_load($config['menu_name']);
$source_text = $menu['title'];
$string_name = 'menu:menu:' . $menu['menu_name'];
$job = $this
->createJob();
$job->translator = $this->translator->name;
$job->settings = array();
$item1 = $job
->addItem('i18n_string', 'menu', $string_name);
$this
->assertEqual(t('Menu'), $item1
->getSourceType());
$job
->requestTranslation();
foreach ($job
->getItems() as $item) {
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
));
$item
->acceptTranslation();
$this
->assertJobItemLangCodes($item, 'en', array(
'de',
'en',
));
}
$data = $item1
->getData();
$this
->assertEqual($data['menu:menu:' . $menu['menu_name'] . ':title']['#text'], $config['title']);
$this
->assertEqual($data['menu:menu:' . $menu['menu_name'] . ':description']['#text'], $config['description']);
$this
->assertEqual($item1->item_id, $string_name, 'String is correctly saved');
$this
->assertEqual(i18n_string_translate($string_name . ':title', $source_text, array(
'langcode' => $job->target_language,
)), $job->target_language . '_' . $source_text);
$source_text = $this
->randomName();
$menu_link = array(
'link_path' => '<front>',
'link_title' => $source_text,
'title' => $source_text,
'menu_name' => $menu['menu_name'],
'customized' => TRUE,
);
menu_link_save($menu_link);
$string_name = 'menu:item:' . $menu_link['mlid'];
$job = $this
->createJob();
$job->translator = $this->translator->name;
$job->settings = array();
$item1 = $job
->addItem('i18n_string', 'menu_link', $string_name);
$this
->assertEqual(t('Menu link'), $item1
->getSourceType());
$job
->requestTranslation();
foreach ($job
->getItems() as $item) {
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
));
$item
->acceptTranslation();
$this
->assertJobItemLangCodes($item, 'en', array(
'de',
'en',
));
}
$data = $item1
->getData();
$this
->assertEqual($data[$string_name . ':title']['#text'], $source_text);
$this
->assertEqual($item1->item_id, $string_name);
$this
->assertEqual(i18n_string_translate($string_name . ':title', $source_text, array(
'langcode' => $job->target_language,
)), $job->target_language . '_' . $source_text);
}
function testI18nStringSourceLangCodes() {
$config = array(
'name' => $this
->randomName(),
'description' => 'description_' . $this
->randomName(),
'machine_name' => 'test_vocab',
'i18n_mode' => I18N_MODE_LOCALIZE,
);
$vocabulary = entity_create('taxonomy_vocabulary', $config);
taxonomy_vocabulary_save($vocabulary);
$string_object_name = "taxonomy:vocabulary:" . $vocabulary->vid;
$job = $this
->createJob();
$job->translator = $this->translator->name;
$job->settings = array();
$job
->save();
$item = $job
->addItem('i18n_string', 'taxonomy_vocabulary', $string_object_name);
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
));
i18n_string_translation_update($string_object_name . ':description', 'de_' . $config['description'], 'de');
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
));
i18n_string_translation_update($string_object_name . ':name', 'de_' . $config['name'], 'de');
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
'de',
));
}
function testI18nStringPluginUI() {
$this
->loginAsAdmin(array(
'administer taxonomy',
'translate interface',
'translate user-defined strings',
));
$vocab_data = array(
'name' => $this
->randomName(),
'machine_name' => 'test_vocab',
'i18n_mode' => I18N_MODE_LOCALIZE,
);
$term_data = array(
'name' => $this
->randomName(),
);
$vocab_data_not_translated = array(
'name' => $this
->randomName(),
'machine_name' => 'test_vocab3',
'i18n_mode' => I18N_MODE_LOCALIZE,
);
$this
->drupalPost('admin/structure/taxonomy/add', $vocab_data, t('Save'));
$this
->drupalGet('admin/structure/taxonomy');
$this
->clickLink(t('add terms'));
$this
->drupalPost(NULL, $term_data, t('Save'));
$this
->drupalPost('admin/structure/taxonomy/add', $vocab_data_not_translated, t('Save'));
$this
->drupalGet('admin/tmgmt/sources/i18n_string_taxonomy_vocabulary');
$this
->assertText($vocab_data['name']);
$this
->drupalPost(NULL, array(
'items[taxonomy:vocabulary:1]' => 1,
), t('Request translation'));
$this
->assertTrue(strpos($this
->getUrl(), 'admin/tmgmt/jobs') !== FALSE);
entity_get_controller('tmgmt_job')
->resetCache();
$jobs = entity_load('tmgmt_job', FALSE);
$job = array_pop($jobs);
$this
->assertFieldByName('label', $job
->label());
$this
->drupalPost('admin/structure/taxonomy/test_vocab/translate', array(
'languages[taxonomy:vocabulary:1:de]' => 1,
), t('Request translation'));
$this
->drupalPost(NULL, array(), t('Submit to translator'));
$this
->assertText(t('Needs review'));
$this
->clickLink(t('review'));
$this
->drupalPost(NULL, array(), t('Save as completed'));
$this
->assertText(t('The translation for @label has been accepted.', array(
'@label' => $job
->label(),
)));
$this
->drupalGet('admin/tmgmt/sources/i18n_string_taxonomy_vocabulary');
$elements = $this
->xpath('//select[@name=:name]//option[@value=:option]', array(
':name' => 'search[target_language]',
':option' => i18n_string_source_language(),
));
$this
->assertTrue(empty($elements));
$edit = array(
'search[target_language]' => 'de',
'search[target_status]' => 'untranslated',
);
$this
->drupalPost('admin/tmgmt/sources/i18n_string_taxonomy_vocabulary', $edit, t('Search'));
$this
->assertNoText($vocab_data['name']);
$this
->assertText($vocab_data_not_translated['name']);
$edit = array(
'search[target_language]' => 'de',
'search[target_status]' => 'untranslated',
);
$this
->drupalPost(NULL, $edit, t('Search'));
$this
->assertNoText($vocab_data['name']);
$this
->assertText($vocab_data_not_translated['name']);
$lid = db_select('locales_source', 's')
->fields('s', array(
'lid',
))
->condition('source', $vocab_data['name'])
->execute()
->fetchField();
db_update('locales_target')
->fields(array(
'i18n_status' => I18N_STRING_STATUS_UPDATE,
))
->condition('lid', $lid)
->execute();
$edit = array(
'search[target_language]' => 'de',
'search[target_status]' => 'outdated',
);
$this
->drupalPost(NULL, $edit, t('Search'));
$this
->assertText($vocab_data['name']);
$this
->assertNoText($vocab_data_not_translated['name']);
$edit = array(
'search[target_language]' => 'de',
'search[target_status]' => 'untranslated_or_outdated',
);
$this
->drupalPost(NULL, $edit, t('Search'));
$this
->assertText($vocab_data['name']);
$this
->assertText($vocab_data_not_translated['name']);
}
function testI18nStringPluginUIBlock() {
$this
->loginAsAdmin(array(
'administer blocks',
'translate interface',
'translate user-defined strings',
));
$navigation_edit = array(
'title' => $this
->randomName(),
'i18n_mode' => 1,
);
$this
->drupalPost('admin/structure/block/manage/system/navigation/configure', $navigation_edit, t('Save block'));
$powered_edit = array(
'title' => $this
->randomName(),
'i18n_mode' => 1,
);
$this
->drupalPost('admin/structure/block/manage/system/powered-by/configure', $powered_edit, t('Save block'));
$this
->drupalGet('admin/tmgmt/sources/i18n_string_block');
$this
->assertText($navigation_edit['title']);
$this
->assertText($powered_edit['title']);
$edit = array(
'items[blocks:system:powered-by]' => 1,
'items[blocks:system:navigation]' => 1,
);
$this
->drupalPost(NULL, $edit, t('Request translation'));
$this
->assertText($navigation_edit['title']);
$this
->assertText($powered_edit['title']);
$this
->drupalPost(NULL, array(), t('Submit to translator'));
$this
->assertRaw(t('Active job item: Needs review'));
}
function testI18nStringPluginUIField() {
$this
->loginAsAdmin(array(
'translate interface',
'translate user-defined strings',
));
$type = $this
->drupalCreateContentType(array(
'type' => $type = $this
->randomName(),
));
$field = array(
'field_name' => 'list_test',
'type' => 'list_text',
);
for ($i = 0; $i < 5; $i++) {
$field['settings']['allowed_values'][$this
->randomName()] = $this
->randomString();
}
field_create_field($field);
$instance = array(
'field_name' => 'list_test',
'entity_type' => 'node',
'bundle' => $type->type,
'label' => $this
->randomName(10),
'description' => $this
->randomString(30),
);
field_create_instance($instance);
$this
->drupalGet('admin/tmgmt/sources/i18n_string_field');
$this
->assertNoText(t('Body'));
$this
->assertText('field:list_test:#allowed_values');
$this
->drupalGet('admin/tmgmt/sources/i18n_string_field_instance');
$this
->assertUniqueText(t('Body'));
$this
->assertUniqueText($instance['label']);
$edit = array(
'items[field:body:' . $type->type . ']' => 1,
'items[field:list_test:' . $type->type . ']' => 1,
);
$this
->drupalPost(NULL, $edit, t('Request translation'));
$this
->assertText(t('Body'));
$this
->assertText($instance['label']);
$this
->drupalPost(NULL, array(), t('Submit to translator'));
$this
->assertRaw(t('Active job item: Needs review'));
$this
->clickLink(t('reviewed'));
$this
->drupalPost(NULL, array(), t('Save as completed'));
$this
->drupalGet('admin/tmgmt/sources/i18n_string_field_instance');
$this
->assertRaw(t('Translation up to date'));
$this
->assertRaw(t('Active job item: Needs review'));
}
function testCart() {
$vocabulary1 = entity_create('taxonomy_vocabulary', array(
'name' => $this
->randomName(),
'description' => 'description_' . $this
->randomName(),
'machine_name' => 'test_vocab1',
'i18n_mode' => I18N_MODE_LOCALIZE,
));
taxonomy_vocabulary_save($vocabulary1);
$string1 = "taxonomy:vocabulary:" . $vocabulary1->vid;
$vocabulary2 = entity_create('taxonomy_vocabulary', array(
'name' => $this
->randomName(),
'description' => 'description_' . $this
->randomName(),
'machine_name' => 'test_vocab2',
'i18n_mode' => I18N_MODE_LOCALIZE,
));
taxonomy_vocabulary_save($vocabulary2);
$string2 = "taxonomy:vocabulary:" . $vocabulary2->vid;
$vocabulary3 = entity_create('taxonomy_vocabulary', array(
'name' => $this
->randomName(),
'description' => 'description_' . $this
->randomName(),
'machine_name' => 'test_vocab3',
'i18n_mode' => I18N_MODE_LOCALIZE,
));
taxonomy_vocabulary_save($vocabulary3);
$this
->loginAsAdmin(array_merge($this->translator_permissions, array(
'translate interface',
'translate user-defined strings',
)));
$this
->drupalPost('admin/tmgmt/sources/i18n_string_taxonomy_vocabulary', array(
'items[' . $string1 . ']' => TRUE,
'items[' . $string2 . ']' => TRUE,
), t('Add to cart'));
$this
->drupalGet('admin/tmgmt/cart');
$this
->assertText($vocabulary1->name);
$this
->assertText($vocabulary2->name);
$this
->drupalGet('admin/structure/taxonomy/test_vocab3/translate');
$this
->assertRaw(t('There are @count items in the <a href="@url">translation cart</a>.', array(
'@count' => 2,
'@url' => url('admin/tmgmt/cart'),
)));
$this
->drupalPost(NULL, array(), t('Add to cart'));
$this
->assertRaw(t('@count content source was added into the <a href="@url">cart</a>.', array(
'@count' => 1,
'@url' => url('admin/tmgmt/cart'),
)));
$this
->assertRaw(t('There are @count items in the <a href="@url">translation cart</a> including the current item.', array(
'@count' => 3,
'@url' => url('admin/tmgmt/cart'),
)));
}
}