mirror of
https://github.com/yiisoft/yii2.git
synced 2025-10-29 17:48:15 +08:00
Fix #20032: Added mask method for string masking with multibyte support
This commit is contained in:
@ -474,4 +474,36 @@ class StringHelperTest extends TestCase
|
||||
['', ''],
|
||||
];
|
||||
}
|
||||
|
||||
public function testMask()
|
||||
{
|
||||
// Standard masking
|
||||
$this->assertSame('12******90', StringHelper::mask('1234567890', 2, 6));
|
||||
$this->assertSame('a********j', StringHelper::mask('abcdefghij', 1, 8));
|
||||
$this->assertSame('*************', StringHelper::mask('Hello, World!', 0, 13));
|
||||
$this->assertSame('************!', StringHelper::mask('Hello, World!', 0, 12));
|
||||
$this->assertSame('Hello, *orld!', StringHelper::mask('Hello, World!', 7, 1));
|
||||
$this->assertSame('Saleh Hashemi', StringHelper::mask('Saleh Hashemi', 0, 0));
|
||||
|
||||
// Different Mask Character
|
||||
$this->assertSame('12######90', StringHelper::mask('1234567890', 2, 6, '#'));
|
||||
|
||||
// Positions outside the string
|
||||
$this->assertSame('1234567890', StringHelper::mask('1234567890', 20, 6));
|
||||
$this->assertSame('1234567890', StringHelper::mask('1234567890', -20, 6));
|
||||
|
||||
// Negative values for start
|
||||
$this->assertSame('1234****90', StringHelper::mask('1234567890', -6, 4));
|
||||
|
||||
// type-related edge case
|
||||
$this->assertSame('1234****90', StringHelper::mask(1234567890, -6, 4));
|
||||
|
||||
// Multibyte characters
|
||||
$this->assertSame('你**', StringHelper::mask('你好吗', 1, 2));
|
||||
$this->assertSame('你好吗', StringHelper::mask('你好吗', 4, 2));
|
||||
|
||||
// Special characters
|
||||
$this->assertSame('em**l@email.com', StringHelper::mask('email@email.com', 2, 2));
|
||||
$this->assertSame('******email.com', StringHelper::mask('email@email.com', 0, 6));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user