- <?php
- * @file
- * Tests for the Translation module.
- */
-
- * Functional tests for the Translation module.
- */
- class TranslationTestCase extends BackdropWebTestCase {
- protected $book;
-
-
- * @var User
- */
- protected $admin_user;
-
-
- * @var User
- */
- protected $translator;
-
- function setUp() {
- parent::setUp('node', 'language', 'locale', 'translation', 'translation_test');
-
-
- $this->admin_user = $this->backdropCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages', 'translate content'));
- $this->translator = $this->backdropCreateUser(array('create page content', 'edit own page content', 'translate content'));
-
- $this->backdropLogin($this->admin_user);
-
-
- $this->addLanguage('en');
- $this->addLanguage('es');
- $this->addLanguage('it');
-
-
- $edit = array('languages[it][enabled]' => FALSE);
- $this->backdropPost('admin/config/regional/language', $edit, t('Save configuration'));
-
-
-
- $this->backdropGet('admin/structure/types/manage/page');
- $edit = array();
- $edit['language'] = TRANSLATION_ENABLED;
- $this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
- $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Page')), 'Page content type has been updated.');
-
-
- $language_type = LANGUAGE_TYPE_INTERFACE;
- $layout = layout_load('default');
- $layout->addBlock('locale', $language_type, 'sidebar');
- $layout->save();
-
-
- $this->resetCaches();
-
- $this->backdropLogin($this->translator);
- }
-
-
- * Creates, modifies, and updates a page with a translation.
- */
- function testContentTranslation() {
-
- $node_title = $this->randomName();
- $node_body = $this->randomName();
- $node = $this->createPage($node_title, $node_body, 'en');
-
-
- $this->backdropLogin($this->admin_user);
- $this->backdropPost('node/' . $node->nid . '/edit', array('status' => 0), t('Save'));
- $this->backdropGet('node/' . $node->nid . '/translate');
- $this->backdropPost('node/' . $node->nid . '/edit', array('status' => 1), t('Save'));
- $this->backdropLogin($this->translator);
-
-
- $languages = language_list();
- $prefixes = locale_language_negotiation_url_prefixes();
- $this->backdropGet('node/' . $node->nid . '/translate');
- $this->assertLinkByHref($prefixes['es'] . '/node/add/' . str_replace('_', '-', $node->type), 0, format_string('The "Add translation" link for %language points to the localized path of the target language.', array('%language' => $languages['es']->name)));
-
-
- $node_translation_title = $this->randomName();
- $node_translation_body = $this->randomName();
- $node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
-
-
-
- $this->backdropGet('node/' . $node->nid . '/translate');
- $this->assertLinkByHref($prefixes['es'] . '/node/' . $node_translation->nid . '/edit', 0, format_string('The "Edit" link for the translation in %language points to the localized path of the translation language.', array('%language' => $languages['es']->name)));
- $this->assertLinkByHref($prefixes['es'] . '/node/' . $node_translation->nid, 0, format_string('The "View" link for the translation in %language points to the localized path of the translation language.', array('%language' => $languages['es']->name)));
-
-
-
- $this->backdropGet('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']->native)), 'Message regarding attempted duplicate translation is displayed.');
-
-
-
- $edit = array();
- $langcode = LANGUAGE_NONE;
- $edit["title"] = $this->randomName();
- $edit["body[$langcode][0][value]"] = $this->randomName();
- $this->backdropPost('node/add/page', $edit, t('Save'), array('query' => array('translation' => $node->nid, 'language' => 'es')));
- $duplicate = $this->backdropGetNodeByTitle($edit["title"]);
- $this->assertEqual($duplicate->tnid, 0, 'The node does not have a tnid.');
-
-
- $node_body = $this->randomName();
- $node->body[LANGUAGE_NONE][0]['value'] = $node_body;
- $edit = array();
- $edit["body[$langcode][0][value]"] = $node_body;
- $edit['translation[retranslate]'] = TRUE;
- $this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
- $this->assertRaw(t('Page %title has been updated.', array('%title' => $node_title)), 'Original node updated.');
-
-
- $this->backdropGet('node/' . $node->nid . '/translate');
- $this->assertRaw('<span class="marker">' . t('outdated') . '</span>', 'Translation marked as outdated.');
-
-
- $edit = array();
- $edit["body[$langcode][0][value]"] = $this->randomName();
- $edit['translation[status]'] = FALSE;
- $this->backdropPost('node/' . $node_translation->nid . '/edit', $edit, t('Save'));
- $this->assertRaw(t('Page %title has been updated.', array('%title' => $node_translation_title)), 'Translated node updated.');
-
-
-
- $this->backdropGet('node/add/page');
- $this->assertFieldByXPath('//select[@name="langcode"]//option', 'it', t('Italian (disabled) is available in language selection.'));
- $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
- $this->assertRaw($translation_it->body[LANGUAGE_NONE][0]['value'], 'Content created in Italian (disabled).');
-
-
-
- $this->backdropGet('node/add/page');
- $this->assertFieldByXPath('//select[@name="langcode"]//option', LANGUAGE_NONE, t('Language neutral is available in language selection with disabled languages.'));
- $node2 = $this->createPage($this->randomName(), $this->randomName(), LANGUAGE_NONE);
- $this->assertRaw($node2->body[LANGUAGE_NONE][0]['value'], 'Language neutral content created with disabled languages available.');
-
-
-
- $this->backdropLogin($this->admin_user);
- $edit = array('languages[es][enabled]' => FALSE);
- $this->backdropPost('admin/config/regional/language', $edit, t('Save configuration'));
- $this->backdropLogin($this->translator);
- $this->backdropGet('node/' . $node->nid . '/translate');
- $this->assertRaw(t('Translations of %title', array('%title' => $node->title)), 'Translation overview page available with only one language enabled.');
- }
-
-
- * Checks that the language switch links behave properly.
- */
- function testLanguageSwitchLinks() {
-
-
- $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
- $translation_es = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'es');
- $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
-
-
-
- $this->assertLanguageSwitchLinks($node, $translation_es);
- $this->assertLanguageSwitchLinks($translation_es, $node);
- $this->assertLanguageSwitchLinks($node, $translation_it, FALSE);
-
-
-
- $this->assertLanguageSwitchLinks($node, $node, FALSE, 'node');
- $this->assertLanguageSwitchLinks($node, $node, TRUE, 'block-locale');
-
-
-
- $this->backdropLogin($this->admin_user);
- $edit = array('status' => 0);
- $this->backdropPost("node/$translation_es->nid/edit", $edit, t('Save'));
- $this->backdropLogin($this->translator);
- $this->assertLanguageSwitchLinks($node, $translation_es, FALSE);
-
-
-
- $this->backdropLogin($this->admin_user);
- $edit = array('language[locale-url][enabled]' => FALSE);
- $this->backdropPost('admin/config/regional/language/detection', $edit, t('Save settings'));
- $this->resetCaches();
- $edit = array('status' => 1);
- $this->backdropPost("node/$translation_es->nid/edit", $edit, t('Save'));
- $this->backdropLogin($this->translator);
- $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, 'node');
-
-
-
- config_set('node.type.' . $node->type, 'settings.translation_show_links', FALSE);
- $this->assertLanguageSwitchLinks($node, $translation_es, FALSE, 'node');
- }
-
-
- * Tests that the language switcher block alterations work as intended.
- */
- function testLanguageSwitcherBlockIntegration() {
-
- $this->backdropLogin($this->admin_user);
- $edit = array('languages[it][enabled]' => TRUE);
- $this->backdropPost('admin/config/regional/language', $edit, t('Save configuration'));
- $this->resetCaches();
- $this->backdropLogin($this->translator);
-
-
- $type = 'block-locale';
- $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
- $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $this->emptyNode('es'), TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $this->emptyNode('it'), TRUE, $type);
-
-
- $translation_es = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'es');
- $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $this->emptyNode('it'), TRUE, $type);
-
-
- $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
- $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $translation_it, TRUE, $type);
-
-
-
- $node2 = $this->createPage($this->randomName(), $this->randomName(), LANGUAGE_NONE);
- $node2_en = (object) array('nid' => $node2->nid, 'langcode' => 'en');
- $node2_es = (object) array('nid' => $node2->nid, 'langcode' => 'es');
- $node2_it = (object) array('nid' => $node2->nid, 'langcode' => 'it');
- $this->assertLanguageSwitchLinks($node2_en, $node2_en, TRUE, $type);
- $this->assertLanguageSwitchLinks($node2_en, $node2_es, TRUE, $type);
- $this->assertLanguageSwitchLinks($node2_en, $node2_it, TRUE, $type);
-
-
-
- $this->backdropLogin($this->admin_user);
- $edit = array('language' => 0);
- $this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
- $this->backdropLogin($this->translator);
-
-
-
- $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $translation_it, TRUE, $type);
-
-
-
- $node = $this->createPage($this->randomName(), $this->randomName());
- $node_es = (object) array('nid' => $node->nid, 'langcode' => 'es');
- $node_it = (object) array('nid' => $node->nid, 'langcode' => 'it');
- $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $node_es, TRUE, $type);
- $this->assertLanguageSwitchLinks($node, $node_it, TRUE, $type);
- }
-
-
- * Resets static caches to make the test code match the client-side behavior.
- */
- function resetCaches() {
- backdrop_static_reset('language_list');
- backdrop_static_reset('locale_url_outbound_alter');
- backdrop_static_reset('locale_language_url_rewrite_url');
- }
-
-
- * Returns an empty node data structure.
- *
- * @param $langcode
- * The language code.
- *
- * @return
- * An empty node data structure.
- */
- function emptyNode($langcode) {
- return (object) array('nid' => NULL, 'langcode' => $langcode);
- }
-
-
- * Installs the specified language, or enables it if it is already installed.
- *
- * @param $language_code
- * The language code to check.
- */
- function addLanguage($language_code) {
-
- $this->backdropGet('admin/config/regional/language');
-
- if (strpos($this->backdropGetContent(), 'languages[' . $language_code . '][enabled]') === FALSE) {
-
- $edit = array();
- $edit['predefined_langcode'] = $language_code;
- $this->backdropPost('admin/config/regional/language/add', $edit, t('Add language'));
-
-
- backdrop_static_reset('language_list');
- $languages = language_list();
- $this->assertTrue(array_key_exists($language_code, $languages), '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.', array('%language' => $languages[$language_code]->name)), 'Language has been created.');
- }
- }
- elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'languages[' . $language_code . '][enabled]'))) {
-
- $this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
- }
- else {
-
- $this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
- $this->backdropPost(NULL, array('languages[' . $language_code . '][enabled]' => TRUE), t('Save configuration'));
- $this->assertRaw(t('Configuration saved.'), 'Language successfully enabled.');
- }
- }
-
-
- * Creates a "Page" in the specified language.
- *
- * @param $title
- * The title of a page in the specified language.
- * @param $body
- * The body of a page in the specified language.
- * @param $language
- * (optional) Language code.
- *
- * @return
- * A node object.
- */
- function createPage($title, $body, $langcode = NULL) {
- $edit = array();
- $field_langcode = LANGUAGE_NONE;
- $edit["title"] = $title;
- $edit["body[$field_langcode][0][value]"] = $body;
- if (!empty($langcode)) {
- $edit['langcode'] = $langcode;
- }
- $this->backdropPost('node/add/page', $edit, t('Save'));
- $this->assertRaw(t('Page %title has been created.', array('%title' => $title)), 'Page created.');
-
-
- $node = $this->backdropGetNodeByTitle($title);
- $this->assertTrue($node, t('Node found in database.'));
-
- return $node;
- }
-
-
- * Creates a translation for a page in the specified language.
- *
- * @param Node $node
- * The page to create the translation for.
- * @param $title
- * The title of a page in the specified language.
- * @param $body
- * The body of a page in the specified language.
- * @param $langcode
- * Language code.
- *
- * @return
- * Translation object.
- */
- function createTranslation(Node $node, $title, $body, $langcode) {
- $this->backdropGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => $langcode)));
-
- $field_langcode = LANGUAGE_NONE;
- $body_key = "body[$field_langcode][0][value]";
- $this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, 'Original title value correctly populated.');
- $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[LANGUAGE_NONE][0]['value'], 'Original body value correctly populated.');
-
- $edit = array();
- $edit["title"] = $title;
- $edit[$body_key] = $body;
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('Page %title has been created.', array('%title' => $title)), 'Translation created.');
-
-
- $translation = $this->backdropGetNodeByTitle($title);
- $this->assertTrue($translation, t('Node found in database.'));
- $this->assertTrue($translation->tnid == $node->nid, 'Translation set id correctly stored.');
-
- return $translation;
- }
-
-
- * Asserts an element identified by the given XPath has the given content.
- *
- * @param $xpath
- * The 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
- * The 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') {
- $found = $this->findContentByXPath($xpath, $arguments, $value);
- return $this->assertTrue($found, $message, $group);
- }
-
-
- * Tests whether the specified language switch links are found.
- *
- * @param $node
- * The node to display.
- * @param $translation
- * The translation whose link has to be checked.
- * @param $find
- * TRUE if the link must be present in the node page.
- * @param $types
- * The page areas to be checked.
- *
- * @return
- * TRUE if the language switch links are found, FALSE if not.
- */
- function assertLanguageSwitchLinks($node, $translation, $find = TRUE, $types = NULL) {
- $language_default = config_get('system.core', 'language_default');
-
- if (empty($types)) {
- $types = array('node', 'block-locale');
- }
- elseif (is_string($types)) {
- $types = array($types);
- }
-
- $result = TRUE;
- $languages = language_list();
- $node_langcode = $node->langcode === LANGUAGE_NONE ? $language_default : $node->langcode;
- $translation_langcode = $translation->langcode === LANGUAGE_NONE ? $language_default : $translation->langcode;
- $page_language = $languages[$node_langcode];
- $translation_language = $languages[$translation_langcode];
- $url = url("node/$translation->nid", array('language' => $translation_language));
-
- $this->backdropGet("node/$node->nid", array('language' => $page_language));
-
- foreach ($types as $type) {
- $args = array(
- '%translation_language' => $translation_language->native,
- '%page_language' => $page_language->native,
- '%type' => $type,
- );
- if ($find) {
- $message = format_string('[%page_language] Language switch item found for %translation_language language in the %type page area.', $args);
- }
- else {
- $message = format_string('[%page_language] Language switch item not found for %translation_language language in the %type page area.', $args);
- }
-
- if ($type == 'node') {
-
- if (!empty($translation->nid)) {
- $xpath = '//article[contains(@class, :type)]//a[@href=:url]';
- }
- else {
- $xpath = '//article[contains(@class, :type)]//span[contains(@class, "locale-untranslated")]';
- }
- }
- else {
-
- if (!empty($translation->nid)) {
- $xpath = '//div[contains(@class, :type)]//a[@href=:url]';
- }
- else {
- $xpath = '//div[contains(@class, :type)]//span[contains(@class, "locale-untranslated")]';
- }
- }
-
- $found = $this->findContentByXPath($xpath, array(':type' => $type, ':url' => $url), $translation_language->native);
- $result = $this->assertTrue($found == $find, $message) && $result;
- }
-
- return $result;
- }
-
-
- * Searches for elements matching the given xpath and value.
- *
- * @param $xpath
- * The 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.
- *
- * @return
- * TRUE if found, otherwise FALSE.
- */
- function findContentByXPath($xpath, array $arguments = array(), $value = NULL) {
- $elements = $this->xpath($xpath, $arguments);
-
- $found = TRUE;
- if ($value && $elements) {
- $found = FALSE;
- foreach ($elements as $element) {
- if ((string) $element == $value) {
- $found = TRUE;
- break;
- }
- }
- }
-
- return $elements && $found;
- }
- }