ソースを参照

Merge pull request #68 from dereuromark/master-slug

Tests that you can easily pass more transliterations to Inflector
Mark 11 年 前
コミット
3f8c658057

+ 3 - 1
Model/Behavior/SluggedBehavior.php

@@ -42,6 +42,9 @@ class SluggedBehavior extends ModelBehavior {
 	 * 	title - force title case. E.g. "This-Is-The-Slug"
 	 * 	camel - force CamelCase. E.g. "ThisIsTheSlug"
 	 *
+	 * If you need to support more transliterations, make sure you adjust Inflection accordingly:
+	 *   Inflector::rules('transliteration', array('/α/' => 'a', '/β/' => 'b'));
+	 *
 	 * @var array
 	 */
 	protected $_defaultConfig = array(
@@ -249,7 +252,6 @@ class SluggedBehavior extends ModelBehavior {
 			$string = str_replace(array_keys($replace), array_values($replace), $string);
 		}
 
-		// TODO: Use `if (function_exists('transliterator_transliterate')) {}` for PHP5.4+
 		if ($mode === 'ascii') {
 			$slug = $this->_slug($Model, $string, $separator);
 		} else {

+ 6 - 1
Test/Case/Console/Command/EncodingShellTest.php

@@ -2,6 +2,7 @@
 
 App::uses('EncodingShell', 'Tools.Console/Command');
 App::uses('MyCakeTestCase', 'Tools.TestSuite');
+App::uses('TestConsoleOutput', 'Tools.TestSuite');
 
 class EncodingShellTest extends MyCakeTestCase {
 
@@ -10,7 +11,11 @@ class EncodingShellTest extends MyCakeTestCase {
 	public function setUp() {
 		parent::setUp();
 
-		$this->EncodingShell = new EncodingShell();
+		$output = new TestConsoleOutput();
+		$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
+		$input = $this->getMock('ConsoleInput', array(), array(), '', false);
+
+		$this->EncodingShell = new EncodingShell($output, $error, $input);
 		$this->EncodingShell->initialize();
 		$this->EncodingShell->startup();
 	}

+ 63 - 26
Test/Case/Model/Behavior/SluggedBehaviorTest.php

@@ -57,33 +57,9 @@ class SluggedBehaviorTest extends CakeTestCase {
 		'latin_symbols' => array (
 			'©' => 'c'
 		),
-		'el' => array ( /* Greek */
-			'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => 'th',
-			'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x', 'ο' => 'o', 'π' => 'p',
-			'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
-			'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
-			'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
-			'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => 'TH',
-			'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => 'X', 'Ο' => 'O', 'Π' => 'P',
-			'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
-			'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
-			'Ϋ' => 'Y'
-		),
 		'tr' => array ( /* Turkish */
 			'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ç' => 'c', 'Ç' => 'C', 'ğ' => 'g', 'Ğ' => 'G'
 		),
-		'ru' => array ( /* Russian */
-			'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
-			'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
-			'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
-			'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ы' => 'y', 'э' => 'e', 'ю' => 'yu',
-			'я' => 'ya',
-			'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
-			'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
-			'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
-			'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ы' => 'Y', 'Э' => 'E', 'Ю' => 'Yu',
-			'Я' => 'Ya',
-		),
 		'uk' => array ( /* Ukrainian */
 			'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g'
 		),
@@ -109,6 +85,39 @@ class SluggedBehaviorTest extends CakeTestCase {
 			'ą' => 'a', 'č' => 'c', 'ę' => 'e', 'ė' => 'e', 'į' => 'i', 'š' => 's', 'ų' => 'u', 'ū' => 'u', 'ž' => 'z',
 			'Ą' => 'A', 'Č' => 'C', 'Ę' => 'E', 'Ė' => 'E', 'Į' => 'I', 'Š' => 'S', 'Ų' => 'U', 'Ū' => 'U', 'Ž' => 'Z'
 		),
+	);
+
+	/**
+	 * An additional list of char maps to test transliteration/slugging.
+	 * The following are currently not part of the CakePHP core.
+	 *
+	 * @var array
+	 */
+	public static $customMaps = array(
+		'el' => array ( /* Greek */
+			'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => 'th',
+			'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x', 'ο' => 'o', 'π' => 'p',
+			'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
+			'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
+			'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
+			'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => 'TH',
+			'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => 'X', 'Ο' => 'O', 'Π' => 'P',
+			'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
+			'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
+			'Ϋ' => 'Y'
+		),
+		'ru' => array ( /* Russian */
+			'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
+			'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
+			'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
+			'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ы' => 'y', 'э' => 'e', 'ю' => 'yu',
+			'я' => 'ya',
+			'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
+			'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
+			'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
+			'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ы' => 'Y', 'Э' => 'E', 'Ю' => 'Yu',
+			'Я' => 'Ya',
+		),
 		'vn' => array ( /* Vietnamese */
 			'Á' => 'A', 'À' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Ă' => 'A', 'Ắ' => 'A', 'Ằ' => 'A', 'Ẳ' => 'A', 'Ẵ' => 'A', 'Ặ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'Ầ' => 'A', 'Ẩ' => 'A', 'Ẫ' => 'A', 'Ậ' => 'A',
 			'á' => 'a', 'à' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'ă' => 'a', 'ắ' => 'a', 'ằ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ặ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ậ' => 'a',
@@ -694,12 +703,13 @@ class SluggedBehaviorTest extends CakeTestCase {
 	}
 
 	/**
-	 * SluggedBehaviorTest::testCurrencies()
+	 * Test that the majority of custom UTF8 (language specific) chars transcribe
+	 * to a proper ascii char.
 	 *
 	 * @return void
 	 */
 	public function testCustomChars() {
-		$this->skipIf(true, 'TODO based on https://github.com/cakephp/cakephp/pull/3379');
+		$this->skipIf((float)Configure::version() < 2.5, 'See https://github.com/cakephp/cakephp/pull/3379');
 
 		$this->Model->Behaviors->unload('Slugged');
 		$this->Model->Behaviors->load('Tools.Slugged', array('mode' => 'ascii'));
@@ -713,6 +723,33 @@ class SluggedBehaviorTest extends CakeTestCase {
 	}
 
 	/**
+	 * Tests that you can easily pass more (like Arabic) transliterations to Inflector and
+	 * thus enhance the slugging.
+	 *
+	 * @return void
+	 */
+	public function testCustomCharsAdditional() {
+		$rules = array();
+		foreach (self::$customMaps as $language => $map) {
+			foreach ($map as $from => $to) {
+				$rules['/' . $from . '/'] = $to;
+			}
+		}
+
+		Inflector::rules('transliteration', $rules);
+
+		$this->Model->Behaviors->unload('Slugged');
+		$this->Model->Behaviors->load('Tools.Slugged', array('mode' => 'ascii'));
+
+		foreach (self::$customMaps as $language => $map) {
+			foreach ($map as $from => $to) {
+				$result = $this->Model->slug($from, false);
+				$this->assertEquals($to, $result, $from . ' (' . $language . ') should become ' . $to . ' - but became ' . $result);
+			}
+		}
+	}
+
+	/**
 	 * TestSection0 method
 	 *
 	 * Testing characters 0000 - 0fff