Issue #1842726 by sun: Fixed Transliteration component must not contain drupal_alter().

8.0.x
catch 2012-11-22 11:16:12 +00:00
parent 793974d922
commit ca820c7ef4
7 changed files with 76 additions and 60 deletions

View File

@ -2,7 +2,7 @@
/**
* @file
* Definition of \Drupal\Component\Transliteration\PhpTransliteration.
* Definition of \Drupal\Component\Transliteration\PHPTransliteration.
*
* Some parts of this code were derived from the MediaWiki project's UtfNormal
* class, Copyright © 2004 Brion Vibber <brion@pobox.com>,
@ -15,20 +15,16 @@ namespace Drupal\Component\Transliteration;
* Implements transliteration without using the PECL extensions.
*
* Transliterations are done character-by-character, by looking up non-US-ASCII
* characters in a transliteration database. The database comes from two types
* of files, both of which are searched for in the
* PHPTransliteration::$dataDirectory directory. First, language-specific
* overrides are searched (see PHPTranslation::readLanguageOverrides() for
* details of these files). If there is no language-specific override for a
* character, the generic transliteration character tables are searched (see
* PHPTranslation::readGenericData() for details of these files). If looking up
* the character in the generic table results in a NULL value, or an illegal
* character is encountered, then a substitute character is returned.
* characters in a transliteration database.
*
* This class is the registered transliteration class returned from
* drupal_container()->get('transliteration') by default.
*
* @ingroup transliteration
* The database comes from two types of files, both of which are searched for in
* the PHPTransliteration::$dataDirectory directory. First, language-specific
* overrides are searched (see PHPTransliteration::readLanguageOverrides()). If
* there is no language-specific override for a character, the generic
* transliteration character tables are searched (see
* PHPTransliteration::readGenericData()). If looking up the character in the
* generic table results in a NULL value, or an illegal character is
* encountered, then a substitute character is returned.
*/
class PHPTransliteration implements TransliterationInterface {
@ -67,13 +63,6 @@ class PHPTransliteration implements TransliterationInterface {
*/
protected $genericMap = array();
/**
* Returns this PHPTransliteration object (for the Drupal Container).
*/
public function get() {
return $this;
}
/**
* Constructs a transliteration object.
*
@ -83,7 +72,6 @@ class PHPTransliteration implements TransliterationInterface {
* file resides.
*/
public function __construct($data_directory = NULL) {
// Set up data directory and tail bytes table.
$this->dataDirectory = (isset($data_directory)) ? $data_directory : __DIR__ . '/data';
}
@ -184,9 +172,7 @@ class PHPTransliteration implements TransliterationInterface {
* PHPTransliteration::$dataDirectory. These files should set up an array
* variable $overrides with an element whose key is $langcode and whose value
* is an array whose keys are character codes, and whose values are their
* transliterations in this language. The resulting $overrides array is
* altered by invoking hook_transliteration_overrides_alter() to let modules
* add additional overrides.
* transliterations in this language.
*
* @param $langcode
* Code for the language to read.
@ -199,14 +185,11 @@ class PHPTransliteration implements TransliterationInterface {
// Read in this file, which should set up a variable called $overrides,
// which will be local to this function.
if (is_file($file)) {
include($file);
include $file;
}
if (!isset($overrides) || !is_array($overrides)) {
$overrides = array($langcode => array());
}
// Let modules alter the list, and save it.
drupal_alter('transliteration_overrides', $overrides, $langcode);
$this->languageOverrides[$langcode] = $overrides[$langcode];
}
@ -229,7 +212,7 @@ class PHPTransliteration implements TransliterationInterface {
// Read in this file, which should set up a variable called $base, which
// will be local to this function.
if (is_file($file)) {
include($file);
include $file;
}
if (!isset($base) || !is_array($base)) {
$base = array();

View File

@ -195,7 +195,8 @@ class CoreBundle extends Bundle {
->setFactoryClass('Drupal\Core\ExceptionController')
->setFactoryMethod('getExceptionListener');
$container->register('transliteration', 'Drupal\Component\Transliteration\PHPTransliteration');
$container
->register('transliteration', 'Drupal\Core\Transliteration\PHPTransliteration');
// Add Serializer with arguments to be replaced in the compiler pass.
$container->register('serializer', 'Symfony\Component\Serializer\Serializer')

View File

@ -0,0 +1,32 @@
<?php
/**
* @file
* Contains \Drupal\Core\Transliteration\PHPTransliteration.
*/
namespace Drupal\Core\Transliteration;
use Drupal\Component\Transliteration\PHPTransliteration as BaseTransliteration;
/**
* Enhances PHPTransliteration with an alter hook.
*
* @ingroup transliteration
*/
class PHPTransliteration extends BaseTransliteration {
/**
* Overrides \Drupal\Component\Transliteration\PHPTransliteration::readLanguageOverrides().
*
* Allows modules to alter the language-specific $overrides array by invoking
* hook_transliteration_overrides_alter().
*/
protected function readLanguageOverrides($langcode) {
parent::readLanguageOverrides($langcode);
// Let modules alter the language-specific overrides.
drupal_alter('transliteration_overrides', $this->languageOverrides[$langcode], $langcode);
}
}

View File

@ -228,11 +228,10 @@ function hook_language_fallback_candidates_alter(array &$fallback_candidates) {
*/
/**
* Provide language overrides for transliteration.
* Provide language-specific overrides for transliteration.
*
* @param array $overrides
* Associative array of language overrides. The outermost key is the language
* code, and the corresponding value is an array whose keys are integer
* Associative array of language-specific overrides whose keys are integer
* Unicode character codes, and whose values are the transliterations of those
* characters in the given language, to override default transliterations.
* @param string $langcode
@ -244,7 +243,7 @@ function hook_transliteration_overrides_alter(&$overrides, $langcode) {
// Provide special overrides for German for a custom site.
if ($langcode == 'de') {
// The core-provided transliteration of Ä is Ae, but we want just A.
$overrides['de'][0xC4] = 'A';
$overrides[0xC4] = 'A';
}
}

View File

@ -7,15 +7,13 @@
namespace Drupal\system\Tests\Transliteration;
use Drupal\Component\Transliteration\PHPTransliteration;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Transliteration\PHPTransliteration;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Tests the transliteration class.
*
* We need this to be a WebTestBase class because it uses drupal_container().
* Tests Transliteration component functionality.
*/
class TransliterationTest extends WebTestBase {
class TransliterationTest extends DrupalUnitTestBase {
/**
* Modules to enable.
*
@ -26,7 +24,7 @@ class TransliterationTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Transliteration functionality',
'description' => 'Tests the transliteration component',
'description' => 'Tests Transliteration component functionality.',
'group' => 'Transliteration',
);
}
@ -76,16 +74,26 @@ class TransliterationTest extends WebTestBase {
// Test each case both with a new instance of the transliteration class,
// and with one that builds as it goes.
$common_transliterator = drupal_container()->get('transliteration');
$transliterator_service = $this->container->get('transliteration');
foreach($cases as $case) {
list($langcode, $before, $after) = $case;
$transliterator = new PHPTransliteration();
$actual = $transliterator->transliterate($before, $langcode);
$this->assertEqual($after, $actual, format_string('@before is correctly transliterated to @after in new class (@actual) in language @langcode', array('@before' => $before, '@langcode' => $langcode, '@after' => $after, '@actual' => $actual)));
list($langcode, $original, $expected) = $case;
$transliterator_class = new PHPTransliteration();
$actual = $transliterator_class->transliterate($original, $langcode);
$this->assertIdentical($actual, $expected, format_string('@original transliteration to @actual is identical to @expected for language @langcode in new class instance.', array(
'@original' => $original,
'@langcode' => $langcode,
'@expected' => $expected,
'@actual' => $actual,
)));
$actual = $common_transliterator->transliterate($before, $langcode);
$this->assertEqual($after, $actual, format_string('@before is correctly transliterated to @after in previously-used class (@actual) in language @langcode', array('@before' => $before, '@langcode' => $langcode, '@after' => $after, '@actual' => $actual)));
$actual = $transliterator_service->transliterate($original, $langcode);
$this->assertIdentical($actual, $expected, format_string('@original transliteration to @actual is identical to @expected for language @langcode in service instance.', array(
'@original' => $original,
'@langcode' => $langcode,
'@expected' => $expected,
'@actual' => $actual,
)));
}
}
}

View File

@ -1,5 +1,5 @@
name = "Transliteration test module"
description = "Tests the transliteration hook"
name = "Transliteration test"
description = "Helper module for Transliteration system tests."
package = Testing
version = VERSION
core = 8.x

View File

@ -2,22 +2,15 @@
/**
* @file
* Test module for Transliteration hook.
*
* @see hook_transliteration_overrides_alter()
* Test module for Transliteration system.
*/
/**
* Implements hook_transliteration_overrides_alter().
*
* Provides overrides for a fake language with language code 'zz'.
*/
function transliterate_test_transliteration_overrides_alter(&$overrides, $langcode) {
if ($langcode == 'zz') {
// The default transliteration of Ä is A, but change it to Z for testing.
$overrides['zz'][0xC4] = 'Z';
}
else {
$overrides['zz'][0xC4] = 'W';
$overrides[0xC4] = 'Z';
}
}