Commit 2f35944a authored by catch's avatar catch

Issue #1591950 by aspilicious: Convert locale tests to PSR-0.

parent 1f4736c1
<?php
/**
* @file
* Definition of Drupal\locale\Tests\LocaleCommentLanguageTest.
*/
namespace Drupal\locale\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Functional tests for comment language.
*/
class LocaleCommentLanguageTest extends WebTestBase {
protected $profile = 'standard';
public static function getInfo() {
return array(
'name' => 'Comment language',
'description' => 'Tests for comment language.',
'group' => 'Locale',
);
}
function setUp() {
// We also use language_test module here to be able to turn on content
// language negotiation. Drupal core does not provide a way in itself
// to do that.
parent::setUp('locale', 'language_test');
// Create and login user.
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'create article content'));
$this->drupalLogin($admin_user);
// Add language.
$edit = array('predefined_langcode' => 'fr');
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Set "Article" content type to use multilingual support.
$edit = array('node_type_language' => 1);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
// Enable content language negotiation UI.
variable_set('language_test_content_language_type', TRUE);
// Set interface language detection to user and content language detection
// to URL. Disable inheritance from interface language to ensure content
// language will fall back to the default language if no URL language can be
// detected.
$edit = array(
'language_interface[enabled][language-user]' => TRUE,
'language_content[enabled][language-url]' => TRUE,
'language_content[enabled][language-interface]' => FALSE,
);
$this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings'));
// Change user language preference, this way interface language is always
// French no matter what path prefix the URLs have.
$edit = array('preferred_langcode' => 'fr');
$this->drupalPost("user/{$admin_user->uid}/edit", $edit, t('Save'));
}
/**
* Test that comment language is properly set.
*/
function testCommentLanguage() {
drupal_static_reset('language_list');
// Create two nodes, one for english and one for french, and comment each
// node using both english and french as content language by changing URL
// language prefixes. Meanwhile interface language is always French, which
// is the user language preference. This way we can ensure that node
// language and interface language do not influence comment language, as
// only content language has to.
foreach (language_list() as $node_langcode => $node_language) {
$langcode_not_specified = LANGUAGE_NOT_SPECIFIED;
// Create "Article" content.
$title = $this->randomName();
$edit = array(
"title" => $title,
"body[$langcode_not_specified][0][value]" => $this->randomName(),
"langcode" => $node_langcode,
);
$this->drupalPost("node/add/article", $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($title);
$prefixes = language_negotiation_url_prefixes();
foreach (language_list() as $langcode => $language) {
// Post a comment with content language $langcode.
$prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/';
$edit = array("comment_body[$langcode_not_specified][0][value]" => $this->randomName());
$this->drupalPost("{$prefix}node/{$node->nid}", $edit, t('Save'));
// Check that comment language matches the current content language.
$comment = db_select('comment', 'c')
->fields('c')
->condition('nid', $node->nid)
->orderBy('cid', 'DESC')
->execute()
->fetchObject();
$args = array('%node_language' => $node_langcode, '%comment_language' => $comment->langcode, '%langcode' => $langcode);
$this->assertEqual($comment->langcode, $langcode, t('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args));
}
}
}
}
<?php
/**
* @file
* Definition of Drupal\locale\Tests\LocaleContentTest.
*/
namespace Drupal\locale\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Functional tests for multilingual support on nodes.
*/
class LocaleContentTest extends WebTestBase {
protected $profile = 'standard';
public static function getInfo() {
return array(
'name' => 'Content language settings',
'description' => 'Checks you can enable multilingual support on content types and configure a language for a node.',
'group' => 'Locale',
);
}
function setUp() {
parent::setUp('locale');
}
/**
* Verifies that machine name fields are always LTR.
*/
function testMachineNameLTR() {
// User to add and remove language.
$admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
// Log in as admin.
$this->drupalLogin($admin_user);
// Verify that the machine name field is LTR for a new content type.
$this->drupalGet('admin/structure/types/add');
$this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when no additional language is configured.');
// Install the Arabic language (which is RTL) and configure as the default.
$edit = array();
$edit['predefined_langcode'] = 'ar';
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
$edit = array();
$edit['site_default'] = 'ar';
$this->drupalPost(NULL, $edit, t('Save configuration'));
// Verify that the machine name field is still LTR for a new content type.
$this->drupalGet('admin/structure/types/add');
$this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when the default language is RTL.');
}
/**
* Test if a content type can be set to multilingual and language is present.
*/
function testContentTypeLanguageConfiguration() {
global $base_url;
// User to add and remove language.
$admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
// User to create a node.
$web_user = $this->drupalCreateUser(array('create article content', 'create page content', 'edit any page content'));
// Add custom language.
$this->drupalLogin($admin_user);
// Code for the language.
$langcode = 'xx';
// The English name for the language.
$name = $this->randomName(16);
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'name' => $name,
'direction' => '0',
);
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
// Set "Basic page" content type to use multilingual support.
$this->drupalGet('admin/structure/types/manage/page');
$this->assertText(t('Multilingual support'), t('Multilingual support fieldset present on content type configuration form.'));
$edit = array(
'node_type_language' => 1,
);
$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->drupalLogout();
// Verify language selection is not present on add article form.
$this->drupalLogin($web_user);
$this->drupalGet('node/add/article');
// Verify language select list is not present.
$this->assertNoFieldByName('language', NULL, t('Language select not present on add article form.'));
// Verify language selection appears on add "Basic page" form.
$this->drupalGet('node/add/page');
// Verify language select list is present.
$this->assertFieldByName('langcode', NULL, t('Language select present on add Basic page form.'));
// Ensure language appears.
$this->assertText($name, t('Language present.'));
// Create "Basic page" content.
$node_title = $this->randomName();
$node_body = $this->randomName();
$edit = array(
'type' => 'page',
'title' => $node_title,
'body' => array($langcode => array(array('value' => $node_body))),
'langcode' => $langcode,
);
$node = $this->drupalCreateNode($edit);
// Edit the content and ensure correct language is selected.
$path = 'node/' . $node->nid . '/edit';
$this->drupalGet($path);
$this->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', t('Correct language selected.'));
// Ensure we can change the node language.
$edit = array(
'langcode' => 'en',
);
$this->drupalPost($path, $edit, t('Save'));
$this->assertRaw(t('%title has been updated.', array('%title' => $node_title)), t('Basic page content updated.'));
$this->drupalLogout();
}
/**
* Test if a dir and lang tags exist in node's attributes.
*/
function testContentTypeDirLang() {
// User to add and remove language.
$admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
// User to create a node.
$web_user = $this->drupalCreateUser(array('create article content', 'edit own article content'));
// Login as admin.
$this->drupalLogin($admin_user);
// Install Arabic language.
$edit = array();
$edit['predefined_langcode'] = 'ar';
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Install Spanish language.
$edit = array();
$edit['predefined_langcode'] = 'es';
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Set "Article" content type to use multilingual support.
$this->drupalGet('admin/structure/types/manage/article');
$edit = array(
'node_type_language' => 1,
);
$this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
$this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Article')), t('Article content type has been updated.'));
$this->drupalLogout();
// Login as web user to add new article.
$this->drupalLogin($web_user);
// Create three nodes: English, Arabic and Spanish.
$node_en = $this->createNodeArticle('en');
$node_ar = $this->createNodeArticle('ar');
$node_es = $this->createNodeArticle('es');
$this->drupalGet('node');
// Check if English node does not have lang tag.
$pattern = '|id="node-' . $node_en->nid . '"[^<>]*lang="en"|';
$this->assertNoPattern($pattern, t('The lang tag has not been assigned to the English node.'));
// Check if English node does not have dir tag.
$pattern = '|id="node-' . $node_en->nid . '"[^<>]*dir="ltr"|';
$this->assertNoPattern($pattern, t('The dir tag has not been assigned to the English node.'));
// Check if Arabic node has lang="ar" & dir="rtl" tags.
$pattern = '|id="node-' . $node_ar->nid . '"[^<>]*lang="ar" dir="rtl"|';
$this->assertPattern($pattern, t('The lang and dir tags have been assigned correctly to the Arabic node.'));
// Check if Spanish node has lang="es" tag.
$pattern = '|id="node-' . $node_es->nid . '"[^<>]*lang="es"|';
$this->assertPattern($pattern, t('The lang tag has been assigned correctly to the Spanish node.'));
// Check if Spanish node does not have dir="ltr" tag.
$pattern = '|id="node-' . $node_es->nid . '"[^<>]*lang="es" dir="ltr"|';
$this->assertNoPattern($pattern, t('The dir tag has not been assigned to the Spanish node.'));
$this->drupalLogout();
}
/**
* Create node in a specific language.
*/
protected function createNodeArticle($langcode) {
$this->drupalGet('node/add/article');
$node_title = $this->randomName();
$node_body = $this->randomName();
$edit = array(
'type' => 'article',
'title' => $node_title,
'body' => array($langcode => array(array('value' => $node_body))),
'langcode' => $langcode,
'promote' => 1,
);
return $this->drupalCreateNode($edit);
}
}
<?php
/**
* @file
* Definition of Drupal\locale\Tests\LocaleDateFormatsTest.
*/
namespace Drupal\locale\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Functional tests for localizing date formats.
*/
class LocaleDateFormatsTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Localize date formats',
'description' => 'Tests for the localization of date formats.',
'group' => 'Locale',
);
}
function setUp() {
parent::setUp(array('node', 'locale'));
// Create Article node type.
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
// Create and login user.
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'create article content'));
$this->drupalLogin($admin_user);
}
/**
* Functional tests for localizing date formats.
*/
function testLocalizeDateFormats() {
// Add language.
$edit = array(
'predefined_langcode' => 'fr',
);
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
// Set language negotiation.
$language_type = LANGUAGE_TYPE_INTERFACE;
$edit = array(
"{$language_type}[enabled][language-url]" => TRUE,
);
$this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings'));
// Configure date formats.
$this->drupalGet('admin/config/regional/date-time/locale');
$this->assertText('French', 'Configured languages appear.');
$edit = array(
'date_format_long' => 'd.m.Y - H:i',
'date_format_medium' => 'd.m.Y - H:i',
'date_format_short' => 'd.m.Y - H:i',
);
$this->drupalPost('admin/config/regional/date-time/locale/fr/edit', $edit, t('Save configuration'));
$this->assertText(t('Configuration saved.'), 'French date formats updated.');
$edit = array(
'date_format_long' => 'j M Y - g:ia',
'date_format_medium' => 'j M Y - g:ia',
'date_format_short' => 'j M Y - g:ia',
);
$this->drupalPost('admin/config/regional/date-time/locale/en/edit', $edit, t('Save configuration'));
$this->assertText(t('Configuration saved.'), 'English date formats updated.');
// Create node content.
$node = $this->drupalCreateNode(array('type' => 'article'));
// Configure format for the node posted date changes with the language.
$this->drupalGet('node/' . $node->nid);
$english_date = format_date($node->created, 'custom', 'j M Y');
$this->assertText($english_date, t('English date format appears'));
$this->drupalGet('fr/node/' . $node->nid);
$french_date = format_date($node->created, 'custom', 'd.m.Y');
$this->assertText($french_date, t('French date format appears'));
}
}
<?php
/**
* @file
* Definition of Drupal\locale\Tests\LocaleExportTest.
*/
namespace Drupal\locale\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests for the export of translation files.
*/
class LocaleExportTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Translation export',
'description' => 'Tests the exportation of locale files.',
'group' => 'Locale',
);
}
/**
* A user able to create languages and export translations.
*/
protected $admin_user = NULL;
function setUp() {
parent::setUp('locale');
$this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
$this->drupalLogin($this->admin_user);
}
/**
* Test exportation of translations.
*/
function testExportTranslation() {
// First import some known translations.
// This will also automatically enable the 'fr' language.
$name = tempnam('temporary://', "po_") . '.po';
file_put_contents($name, $this->getPoFile());
$this->drupalPost('admin/config/regional/translate/import', array(
'langcode' => 'fr',
'files[file]' => $name,
), t('Import'));
drupal_unlink($name);
// Get the French translations.
$this->drupalPost('admin/config/regional/translate/export', array(
'langcode' => 'fr',
), t('Export'));
// Ensure we have a translation file.
$this->assertRaw('# French translation of Drupal', t('Exported French translation file.'));
// Ensure our imported translations exist in the file.
$this->assertRaw('msgstr "lundi"', t('French translations present in exported file.'));
// Import some more French translations which will be marked as customized.
$name = tempnam('temporary://', "po2_") . '.po';
file_put_contents($name, $this->getCustomPoFile());
$this->drupalPost('admin/config/regional/translate/import', array(
'langcode' => 'fr',
'files[file]' => $name,
'customized' => 1,
), t('Import'));
drupal_unlink($name);
// We can't import a string with an empty translation, but calling
// locale() for an new string creates an entry in the locales_source table.
locale('February', NULL, 'fr');
// Export only customized French translations.
$this->drupalPost('admin/config/regional/translate/export', array(
'langcode' => 'fr',
'content_options[not_customized]' => FALSE,
'content_options[customized]' => TRUE,
'content_options[not_translated]' => FALSE,
), t('Export'));
// Ensure we have a translation file.
$this->assertRaw('# French translation of Drupal', t('Exported French translation file with only customized strings.'));
// Ensure the customized translations exist in the file.
$this->assertRaw('msgstr "janvier"', t('French custom translation present in exported file.'));
// Ensure no untranslated strings exist in the file.
$this->assertNoRaw('msgid "February"', t('Untranslated string not present in exported file.'));
// Export only untranslated French translations.
$this->drupalPost('admin/config/regional/translate/export', array(
'langcode' => 'fr',
'content_options[not_customized]' => FALSE,
'content_options[customized]' => FALSE,
'content_options[not_translated]' => TRUE,
), t('Export'));
// Ensure we have a translation file.
$this->assertRaw('# French translation of Drupal', t('Exported French translation file with only untranslated strings.'));
// Ensure no customized translations exist in the file.
$this->assertNoRaw('msgstr "janvier"', t('French custom translation not present in exported file.'));
// Ensure the untranslated strings exist in the file.
$this->assertRaw('msgid "February"', t('Untranslated string present in exported file.'));
}
/**
* Test exportation of translation template file.
*/
function testExportTranslationTemplateFile() {
// Get the translation template file.
$this->drupalPost('admin/config/regional/translate/export', array(), t('Export'));
// Ensure we have a translation file.
$this->assertRaw('# LANGUAGE translation of PROJECT', t('Exported translation template file.'));
}
/**
* Helper function that returns a proper .po file.
*/
function getPoFile() {
return <<< EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"
msgid "Monday"
msgstr "lundi"
EOF;
}
/**
* Helper function that returns a .po file which strings will be marked
* as customized.
*/
function getCustomPoFile() {
return <<< EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"
msgid "January"
msgstr "janvier"
EOF;
}
}
<?php
/**
* @file
* Definition of Drupal\locale\Tests\LocaleInstallTest.
*/
namespace Drupal\locale\Tests;
use Drupal\simpletest\WebTestBase;
use ReflectionFunction;
/**
* Tests for the st() function.
*/
class LocaleInstallTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'String translation using st()',
'description' => 'Tests that st() works like t().',
'group' => 'Locale',
);
}
function setUp() {
parent::setUp('locale');
// st() lives in install.inc, so ensure that it is loaded for all tests.
require_once DRUPAL_ROOT . '/core/includes/install.inc';
}
/**
* Verify that function signatures of t() and st() are equal.
*/
function testFunctionSignatures() {
$reflector_t = new ReflectionFunction('t');
$reflector_st = new ReflectionFunction('st');
$this->assertEqual($reflector_t->getParameters(), $reflector_st->getParameters(), t('Function signatures of t() and st() are equal.'));
}
}
<?php
/**
* @file
* Definition of Drupal\locale\Tests\LocaleJavascriptTranslation.
*/
namespace Drupal\locale\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Functional tests for JavaScript parsing for translatable strings.
*/
class LocaleJavascriptTranslation extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Javascript translation',
'description' => 'Tests parsing js files for translatable strings',
'group' => 'Locale',
);
}
function setUp() {
parent::setUp('locale');
}
function testFileParsing() {
$filename = drupal_get_path('module', 'locale') . '/tests/locale_test.js';
// Parse the file to look for source strings.
_locale_parse_js_file($filename);
// Get all of the source strings that were found.
$source_strings = db_select('locales_source', 's')
->fields('s', array('source', 'context'))
->condition('s.location', $filename)
->execute()
->fetchAllKeyed();
// List of all strings that should be in the file.
$test_strings = array(
"Standard Call t" => '',
"Whitespace Call t" => '',
"Single Quote t" => '',
"Single Quote \\'Escaped\\' t" => '',
"Single Quote Concat strings t" => '',
"Double Quote t" => '',
"Double Quote \\\"Escaped\\\" t" => '',
"Double Quote Concat strings t" => '',
"Context !key Args t" => "Context string",
"Context Unquoted t" => "Context string unquoted",
"Context Single Quoted t" => "Context string single quoted",
"Context Double Quoted t" => "Context string double quoted",
"Standard Call plural" => '',
"Standard Call @count plural" => '',
"Whitespace Call plural" => '',
"Whitespace Call @count plural" => '',
"Single Quote plural" => '',
"Single Quote @count plural" => '',
"Single Quote \\'Escaped\\' plural" => '',
"Single Quote \\'Escaped\\' @count plural" => '',
"Double Quote plural" => '',
"Double Quote @count plural" => '',
"Double Quote \\\"Escaped\\\" plural" => '',
"Double Quote \\\"Escaped\\\" @count plural" => '',
"Context !key Args plural" => "Context string",
"Context !key Args @count plural" => "Context string",
"Context Unquoted plural" => "Context string unquoted",
"Context Unquoted @count plural" => "Context string unquoted",
"Context Single Quoted plural" => "Context string single quoted",
"Context Single Quoted @count plural" => "Context string single quoted",
"Context Double Quoted plural" => "Context string double quoted",
"Context Double Quoted @count plural" => "Context string double quoted",
);
// Assert that all strings were found properly.
foreach ($test_strings as $str => $context) {
$args = array('%source' => $str, '%context' => $context);
// Make sure that the string was found in the file.
$this->assertTrue(isset($source_strings[$str]), t("Found source string: %source", $args));