drupal/modules/translation/translation.test

256 lines
11 KiB
Plaintext

<?php
// $Id$
class TranslationTestCase extends DrupalWebTestCase {
protected $book;
public static function getInfo() {
return array(
'name' => 'Translation functionality',
'description' => 'Create a basic page with translation, modify the page outdating translation, and update translation.',
'group' => 'Translation'
);
}
function setUp() {
parent::setUp('locale', 'translation', 'translation_test');
// Setup users.
$this->admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
$this->translator = $this->drupalCreateUser(array('create page content', 'edit own page content', 'translate content'));
$this->drupalLogin($this->admin_user);
// Add languages.
$this->addLanguage('en');
$this->addLanguage('es');
// Set "Basic page" content type to use multilingual support with translation.
$this->drupalGet('admin/structure/types/manage/page');
$edit = array();
$edit['language_content_type'] = 2;
$this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
$this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.'));
$this->drupalLogin($this->translator);
}
/**
* Create a basic page with translation, modify the basic page outdating
* translation, and update translation.
*/
function testContentTranslation() {
// Create Basic page in English.
$node_title = $this->randomName();
$node_body = $this->randomName();
$node = $this->createPage($node_title, $node_body, 'en');
// Submit translation in Spanish.
$node_translation_title = $this->randomName();
$node_translation_body = $this->randomName();
$node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
// Attempt to submit a duplicate translation by visiting the node/add page
// with identical query string.
$languages = language_list();
$this->drupalGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => 'es')));
$this->assertRaw(t('A translation of %title in %language already exists', array('%title' => $node_title, '%language' => $languages['es']->name)), t('Message regarding attempted duplicate translation is displayed.'));
// Attempt a resubmission of the form - this emulates using the back button
// to return to the page then resubmitting the form without a refresh.
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = $this->randomName();
$edit["body[$langcode][0][value]"] = $this->randomName();
$this->drupalPost('node/add/page', $edit, t('Save'), array('query' => array('translation' => $node->nid, 'language' => 'es')));
$duplicate = $this->drupalGetNodeByTitle($edit["title"]);
$this->assertEqual($duplicate->tnid, 0, t('The node does not have a tnid.'));
// Update original and mark translation as outdated.
$edit = array();
$edit["body[$node->language][0][value]"] = $this->randomName();
$edit['translation[retranslate]'] = TRUE;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertRaw(t('Basic page %title has been updated.', array('%title' => $node_title)), t('Original node updated.'));
// Check to make sure that interface shows translation as outdated
$this->drupalGet('node/' . $node->nid . '/translate');
$this->assertRaw('<span class="marker">' . t('outdated') . '</span>', t('Translation marked as outdated.'));
// Update translation and mark as updated.
$edit = array();
$edit["body[$node_translation->language][0][value]"] = $this->randomName();
$edit['translation[status]'] = FALSE;
$this->drupalPost('node/' . $node_translation->nid . '/edit', $edit, t('Save'));
$this->assertRaw(t('Basic page %title has been updated.', array('%title' => $node_translation_title)), t('Translated node updated.'));
$this->drupalLogin($this->admin_user);
// Disable Spanish and confirm that links to the Spanish translations do
// not appear on the English node.
$edit = array();
$edit['enabled[es]'] = FALSE;
$this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
$this->drupalGet('node/' . $node->nid);
$languages = language_list();
$this->assertNoText($languages['es']->native);
$this->drupalLogin($this->translator);
// Confirm that Spanish is still an option for translators when creating nodes.
$this->drupalGet('node/add/page');
$this->assertRaw('value="' . 'es' .'"', t('Spanish is available in language selection'));
}
/**
* Check that content translation links behave properly.
*/
function testContentTranslationLinks() {
// Create Basic page in English.
$node_title = $this->randomName();
$node_body = $this->randomName();
$node = $this->createPage($node_title, $node_body, 'en');
// Submit translation in Spanish.
$node_translation_title = $this->randomName();
$node_translation_body = $this->randomName();
$node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
// Check that content translation links are shown even when no language
// negotiation is configured.
$languages = language_list();
$this->drupalGet("node/$node->nid");
$url = url("node/$node_translation->nid");
$this->assertContentByXPath('//a[@href=:url]', array(':url' => $url), $languages['es']->native, t('Spanish translation link found.'));
$this->drupalGet("node/$node_translation->nid");
$url = url("node/$node->nid");
$this->assertContentByXPath('//a[@href=:url]', array(':url' => $url), $languages['en']->native, t('English translation link found.'));
}
/**
* Install a the specified language if it has not been already. Otherwise make sure that
* the language is enabled.
*
* @param string $language_code The language code the check.
*/
function addLanguage($language_code) {
// Check to make sure that language has not already been installed.
$this->drupalGet('admin/config/regional/language');
if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
// Doesn't have language installed so add it.
$edit = array();
$edit['langcode'] = $language_code;
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Make sure we're not using a stale list.
drupal_static_reset('language_list');
$languages = language_list('language');
$this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
if (array_key_exists($language_code, $languages)) {
$this->assertRaw(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => $languages[$language_code]->name, '@locale-help' => url('admin/help/locale'))), t('Language has been created.'));
}
}
elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
// It's installed and enabled. No need to do anything.
$this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
}
else {
// It's installed but not enabled. Enable it.
$this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
$this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
$this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
}
}
/**
* Create a "Basic page" in the specified language.
*
* @param string $title Title of basic page in specified language.
* @param string $body Body of basic page in specified language.
* @param string $language Language code.
*/
function createPage($title, $body, $language) {
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = $title;
$edit["body[$langcode][0][value]"] = $body;
$edit['language'] = $language;
$this->drupalPost('node/add/page', $edit, t('Save'));
$this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), t('Basic page created.'));
// Check to make sure the node was created.
$node = $this->drupalGetNodeByTitle($title);
$this->assertTrue($node, t('Node found in database.'));
return $node;
}
/**
* Assert that an element identified by the given XPath has the given content.
*
* @param $xpath
* XPath used to find the element.
* @param array $arguments
* An array of arguments with keys in the form ':name' matching the
* placeholders in the query. The values may be either strings or numeric
* values.
* @param $value
* The text content of the matched element to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
*
* @return
* TRUE on pass, FALSE on fail.
*/
function assertContentByXPath($xpath, array $arguments = array(), $value = NULL, $message = '', $group = 'Other') {
$elements = $this->xpath($xpath, $arguments);
$found = TRUE;
if ($value && $elements) {
$found = FALSE;
foreach ($elements as $element) {
if ((string) $element == $value) {
$found = TRUE;
break;
}
}
}
return $this->assertTrue($elements && $found, $message, $group);
}
/**
* Create a translation for the specified basic page in the specified language.
*
* @param object $node The basic page to create translation for.
* @param string $title Title of basic page in specified language.
* @param string $body Body of basic page in specified language.
* @param string $language Language code.
*/
function createTranslation($node, $title, $body, $language) {
$this->drupalGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => $language)));
$body_key = "body[$language][0][value]";
$this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
$this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[$node->language][0]['value'], "Original body value correctly populated.");
$edit = array();
$edit["title"] = $title;
$edit[$body_key] = $body;
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), t('Translation created.'));
// Check to make sure that translation was successful.
$translation = $this->drupalGetNodeByTitle($title);
$this->assertTrue($translation, t('Node found in database.'));
$this->assertTrue($translation->tnid == $node->nid, t('Translation set id correctly stored.'));
return $translation;
}
}