mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 05:45:33 +08:00
Fixes #3939: \yii\Inflector::slug() improvements:
- Added protected `\yii\Inflector::transliterate()` that could be replaced with custom translit implementation. - Added proper tests for both intl-based slug and PHP fallback. - Removed character maps for non-latin languages. - Improved overall slug results. - Added note about the fact that intl is required for non-latin languages to requirements checker.
This commit is contained in:
21
tests/unit/framework/helpers/FallbackInflector.php
Normal file
21
tests/unit/framework/helpers/FallbackInflector.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace yiiunit\framework\helpers;
|
||||
|
||||
|
||||
use yii\helpers\BaseInflector;
|
||||
|
||||
/**
|
||||
* Forces Inflector::slug to use PHP even if intl is available
|
||||
*/
|
||||
class FallbackInflector extends BaseInflector
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected static function hasIntl()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -122,16 +122,57 @@ class InflectorTest extends TestCase
|
||||
$this->assertEquals("customer_tables", Inflector::tableize('customerTable'));
|
||||
}
|
||||
|
||||
public function testSlug()
|
||||
public function testSlugCommons()
|
||||
{
|
||||
$data = [
|
||||
'Привет. Hello, Йии-- Framework !--- Как дела ? How it goes ?' => 'privet-hello-jii-framework-kak-dela-how-it-goes',
|
||||
'this is a title' => 'this-is-a-title',
|
||||
'недвижимость' => 'nedvizimost',
|
||||
'' => '',
|
||||
'hello world' => 'hello-world',
|
||||
'remove.!?[]{}…symbols' => 'removesymbols',
|
||||
'minus-sign' => 'minus-sign',
|
||||
'mdash—sign' => 'mdash-sign',
|
||||
'ndash–sign' => 'ndash-sign',
|
||||
'áàâéèêíìîóòôúùûã' => 'aaaeeeiiiooouuua',
|
||||
'Ναδάλης ṃỹṛèşưḿĕ' => 'nadales-myresume',
|
||||
'E=mc²' => 'e-mc2',
|
||||
'è¼å¥' => 'e14a',
|
||||
'älä lyö ääliö ööliä läikkyy' => 'ala-lyo-aalio-oolia-laikkyy',
|
||||
];
|
||||
|
||||
foreach ($data as $source => $expected) {
|
||||
if (extension_loaded('intl')) {
|
||||
$this->assertEquals($expected, FallbackInflector::slug($source));
|
||||
}
|
||||
$this->assertEquals($expected, Inflector::slug($source));
|
||||
}
|
||||
}
|
||||
|
||||
public function testSlugIntl()
|
||||
{
|
||||
if (!extension_loaded('intl')) {
|
||||
$this->markTestSkipped('intl extension is required.');
|
||||
}
|
||||
|
||||
// Some test strings are from https://github.com/bergie/midgardmvc_helper_urlize. Thank you, Henri Bergius!
|
||||
$data = [
|
||||
// Korean
|
||||
'해동검도' => 'haedong-geomdo',
|
||||
|
||||
// Hiragana
|
||||
'ひらがな' => 'hiragana',
|
||||
|
||||
// Georgian
|
||||
'საქართველო' => 'sakartvelo',
|
||||
|
||||
// Arabic
|
||||
'العربي' => 'alrby',
|
||||
'عرب' => 'rb',
|
||||
|
||||
// Hebrew
|
||||
'עִבְרִית' => 'iberiyt',
|
||||
|
||||
// Turkish
|
||||
'Sanırım hepimiz aynı şeyi düşünüyoruz.' => 'sanrm-hepimiz-ayn-seyi-dusunuyoruz',
|
||||
|
||||
// Russian
|
||||
'недвижимость' => 'nedvizimost',
|
||||
'Контакты' => 'kontakty',
|
||||
];
|
||||
|
||||
foreach ($data as $source => $expected) {
|
||||
@@ -139,6 +180,17 @@ class InflectorTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testSlugPhp()
|
||||
{
|
||||
$data = [
|
||||
'we have недвижимость' => 'we-have',
|
||||
];
|
||||
|
||||
foreach ($data as $source => $expected) {
|
||||
$this->assertEquals($expected, FallbackInflector::slug($source));
|
||||
}
|
||||
}
|
||||
|
||||
public function testClassify()
|
||||
{
|
||||
$this->assertEquals("CustomerTable", Inflector::classify('customer_tables'));
|
||||
|
||||
Reference in New Issue
Block a user