Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F5956465
StringUtilsTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
StringUtilsTest.php
View Options
<?php
namespace
Wikimedia\Tests
;
use
MediaWikiCoversValidator
;
use
PHPUnit\Framework\TestCase
;
use
StringUtils
;
/**
* @covers \StringUtils
*/
class
StringUtilsTest
extends
TestCase
{
use
MediaWikiCoversValidator
;
/**
* @dataProvider provideStringsForIsUtf8Check
*/
public
function
testIsUtf8
(
bool
$expected
,
string
$string
)
{
$this
->
assertEquals
(
$expected
,
StringUtils
::
isUtf8
(
$string
),
'Testing string "'
.
$this
->
escaped
(
$string
)
.
'"'
);
}
/**
* Print high range characters as a hexadecimal
*/
private
function
escaped
(
string
$string
):
string
{
return
preg_replace_callback
(
'/[
\x
80-
\x
FF]/'
,
static
fn
(
$m
)
=>
'
\x
'
.
dechex
(
ord
(
$m
[
0
]
)
),
$string
);
}
/**
* See also "UTF-8 decoder capability and stress test" by
* Markus Kuhn:
* http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
*/
public
static
function
provideStringsForIsUtf8Check
()
{
// Expected return values for StringUtils::isUtf8()
$PASS
=
true
;
$FAIL
=
false
;
return
[
'some ASCII'
=>
[
$PASS
,
'Some ASCII'
],
'euro sign'
=>
[
$PASS
,
"Euro sign €"
],
'first possible sequence 1 byte'
=>
[
$PASS
,
"
\x
00"
],
'first possible sequence 2 bytes'
=>
[
$PASS
,
"
\x
c2
\x
80"
],
'first possible sequence 3 bytes'
=>
[
$PASS
,
"
\x
e0
\x
a0
\x
80"
],
'first possible sequence 4 bytes'
=>
[
$PASS
,
"
\x
f0
\x
90
\x
80
\x
80"
],
'first possible sequence 5 bytes'
=>
[
$FAIL
,
"
\x
f8
\x
88
\x
80
\x
80
\x
80"
],
'first possible sequence 6 bytes'
=>
[
$FAIL
,
"
\x
fc
\x
84
\x
80
\x
80
\x
80
\x
80"
],
'last possible sequence 1 byte'
=>
[
$PASS
,
"
\x
7f"
],
'last possible sequence 2 bytes'
=>
[
$PASS
,
"
\x
df
\x
bf"
],
'last possible sequence 3 bytes'
=>
[
$PASS
,
"
\x
ef
\x
bf
\x
bf"
],
'last possible sequence 4 bytes (U+1FFFFF)'
=>
[
$FAIL
,
"
\x
f7
\x
bf
\x
bf
\x
bf"
],
'last possible sequence 5 bytes'
=>
[
$FAIL
,
"
\x
fb
\x
bf
\x
bf
\x
bf
\x
bf"
],
'last possible sequence 6 bytes'
=>
[
$FAIL
,
"
\x
fd
\x
bf
\x
bf
\x
bf
\x
bf
\x
bf"
],
'boundary 1'
=>
[
$PASS
,
"
\x
ed
\x
9f
\x
bf"
],
'boundary 2'
=>
[
$PASS
,
"
\x
ee
\x
80
\x
80"
],
'boundary 3'
=>
[
$PASS
,
"
\x
ef
\x
bf
\x
bd"
],
'boundary 4'
=>
[
$PASS
,
"
\x
f2
\x
80
\x
80
\x
80"
],
'boundary 5 (U+FFFFF)'
=>
[
$PASS
,
"
\x
f3
\x
bf
\x
bf
\x
bf"
],
'boundary 6 (U+100000)'
=>
[
$PASS
,
"
\x
f4
\x
80
\x
80
\x
80"
],
'boundary 7 (U+10FFFF)'
=>
[
$PASS
,
"
\x
f4
\x
8f
\x
bf
\x
bf"
],
'boundary 8 (U+110000)'
=>
[
$FAIL
,
"
\x
f4
\x
90
\x
80
\x
80"
],
'malformed 1'
=>
[
$FAIL
,
"
\x
80"
],
'malformed 2'
=>
[
$FAIL
,
"
\x
bf"
],
'malformed 3'
=>
[
$FAIL
,
"
\x
80
\x
bf"
],
'malformed 4'
=>
[
$FAIL
,
"
\x
80
\x
bf
\x
80"
],
'malformed 5'
=>
[
$FAIL
,
"
\x
80
\x
bf
\x
80
\x
bf"
],
'malformed 6'
=>
[
$FAIL
,
"
\x
80
\x
bf
\x
80
\x
bf
\x
80"
],
'malformed 7'
=>
[
$FAIL
,
"
\x
80
\x
bf
\x
80
\x
bf
\x
80
\x
bf"
],
'malformed 8'
=>
[
$FAIL
,
"
\x
80
\x
bf
\x
80
\x
bf
\x
80
\x
bf
\x
80"
],
'last byte missing 1'
=>
[
$FAIL
,
"
\x
c0"
],
'last byte missing 2'
=>
[
$FAIL
,
"
\x
e0
\x
80"
],
'last byte missing 3'
=>
[
$FAIL
,
"
\x
f0
\x
80
\x
80"
],
'last byte missing 4'
=>
[
$FAIL
,
"
\x
f8
\x
80
\x
80
\x
80"
],
'last byte missing 5'
=>
[
$FAIL
,
"
\x
fc
\x
80
\x
80
\x
80
\x
80"
],
'last byte missing 6'
=>
[
$FAIL
,
"
\x
df"
],
'last byte missing 7'
=>
[
$FAIL
,
"
\x
ef
\x
bf"
],
'last byte missing 8'
=>
[
$FAIL
,
"
\x
f7
\x
bf
\x
bf"
],
'last byte missing 9'
=>
[
$FAIL
,
"
\x
fb
\x
bf
\x
bf
\x
bf"
],
'last byte missing 10'
=>
[
$FAIL
,
"
\x
fd
\x
bf
\x
bf
\x
bf
\x
bf"
],
'extra continuation byte 1'
=>
[
$FAIL
,
"e
\x
af"
],
'extra continuation byte 2'
=>
[
$FAIL
,
"
\x
c3
\x
89
\x
af"
],
'extra continuation byte 3'
=>
[
$FAIL
,
"
\x
ef
\x
bc
\x
a5
\x
af"
],
'extra continuation byte 4'
=>
[
$FAIL
,
"
\x
f0
\x
9d
\x
99
\x
b4
\x
af"
],
'impossible bytes 1'
=>
[
$FAIL
,
"
\x
fe"
],
'impossible bytes 2'
=>
[
$FAIL
,
"
\x
ff"
],
'impossible bytes 3'
=>
[
$FAIL
,
"
\x
fe
\x
fe
\x
ff
\x
ff"
],
'overlong sequences 1'
=>
[
$FAIL
,
"
\x
c0
\x
af"
],
'overlong sequences 2'
=>
[
$FAIL
,
"
\x
c1
\x
af"
],
'overlong sequences 3'
=>
[
$FAIL
,
"
\x
e0
\x
80
\x
af"
],
'overlong sequences 4'
=>
[
$FAIL
,
"
\x
f0
\x
80
\x
80
\x
af"
],
'overlong sequences 5'
=>
[
$FAIL
,
"
\x
f8
\x
80
\x
80
\x
80
\x
af"
],
'overlong sequences 6'
=>
[
$FAIL
,
"
\x
fc
\x
80
\x
80
\x
80
\x
80
\x
af"
],
'maximum overlong sequences 1'
=>
[
$FAIL
,
"
\x
c1
\x
bf"
],
'maximum overlong sequences 2'
=>
[
$FAIL
,
"
\x
e0
\x
9f
\x
bf"
],
'maximum overlong sequences 3'
=>
[
$FAIL
,
"
\x
f0
\x
8f
\x
bf
\x
bf"
],
'maximum overlong sequences 4'
=>
[
$FAIL
,
"
\x
f8
\x
87
\x
bf
\x
bf"
],
'maximum overlong sequences 5'
=>
[
$FAIL
,
"
\x
fc
\x
83
\x
bf
\x
bf
\x
bf
\x
bf"
],
'surrogates 1 (U+D799)'
=>
[
$PASS
,
"
\x
ed
\x
9f
\x
bf"
],
'surrogates 2 (U+E000)'
=>
[
$PASS
,
"
\x
ee
\x
80
\x
80"
],
'surrogates 3 (U+D800)'
=>
[
$FAIL
,
"
\x
ed
\x
a0
\x
80"
],
'surrogates 4 (U+DBFF)'
=>
[
$FAIL
,
"
\x
ed
\x
af
\x
bf"
],
'surrogates 5 (U+DC00)'
=>
[
$FAIL
,
"
\x
ed
\x
b0
\x
80"
],
'surrogates 6 (U+DFFF)'
=>
[
$FAIL
,
"
\x
ed
\x
bf
\x
bf"
],
'surrogates 7 (U+D800 U+DC00)'
=>
[
$FAIL
,
"
\x
ed
\x
a0
\x
80
\x
ed
\x
b0
\x
80"
],
'noncharacters 1'
=>
[
$PASS
,
"
\x
ef
\x
bf
\x
be"
],
'noncharacters 2'
=>
[
$PASS
,
"
\x
ef
\x
bf
\x
bf"
],
];
}
/**
* @dataProvider provideRegexps
*/
public
function
testIsValidPCRERegex
(
string
$input
,
bool
$expected
)
{
$this
->
assertSame
(
$expected
,
StringUtils
::
isValidPCRERegex
(
$input
)
);
}
public
static
function
provideRegexps
()
{
return
[
[
'foo'
,
false
],
[
'/foo/'
,
true
],
[
'//'
,
true
],
[
'/(foo/'
,
false
],
[
'!(f[o]{2})!'
,
true
],
[
'/foo
\/
'
,
false
]
];
}
/**
* @dataProvider provideRegexReplacements
*/
public
function
testEscapeRegexReplacement
(
string
$replacement
)
{
$escaped
=
StringUtils
::
escapeRegexReplacement
(
$replacement
);
$result
=
preg_replace
(
'/(placeholder)/'
,
$escaped
,
'placeholder'
);
$this
->
assertSame
(
$replacement
,
$result
);
}
public
function
provideRegexReplacements
()
{
return
[
'meaningless characters'
=>
[
' "()*+-./?[]^a{|}ä'
],
'backslash'
=>
[
'
\\
1'
],
'backslash, already escaped'
=>
[
'
\\\\
1'
],
'dollar'
=>
[
'$1'
],
'dollar, already escaped'
=>
[
'
\$
1'
],
];
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Oct 3, 04:27 (3 w, 3 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
6a/90/814ae673579996608a52ffad88a3
Default Alt Text
StringUtilsTest.php (5 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment