Repository navigation
Expand file tree
/
Copy pathWP_MySQL_Token_Tests.php
More file actions
106 lines (93 loc) · 3.9 KB
/
Copy pathWP_MySQL_Token_Tests.php
File metadata and controls
106 lines (93 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
use PHPUnit\Framework\TestCase;
/**
* Tests for WP_MySQL_Token value and name resolution under the Bison vocabulary.
*/
class WP_MySQL_Token_Tests extends TestCase {
/**
* Find the first token with the given terminal name.
*
* @param string $sql The SQL payload to tokenize.
* @param string $name The Bison terminal name to look for.
* @param string[] $sql_modes The SQL modes to activate.
* @return WP_MySQL_Token The first matching token.
*/
private static function first_token( string $sql, string $name, array $sql_modes = array() ): WP_MySQL_Token {
foreach ( ( new WP_MySQL_Lexer( $sql, 80410, $sql_modes ) )->remaining_tokens() as $token ) {
if ( $token->get_name() === $name ) {
return $token;
}
}
self::fail( "No $name token in: $sql" );
}
public function test_get_value_unquotes_string_literals(): void {
$this->assertSame( "a'b", self::first_token( "SELECT 'a''b'", 'TEXT_STRING' )->get_value() );
$this->assertSame( 'a"b', self::first_token( 'SELECT "a""b"', 'TEXT_STRING' )->get_value() );
$this->assertSame( "new\nline", self::first_token( "SELECT 'new\\nline'", 'TEXT_STRING' )->get_value() );
}
public function test_get_value_honors_no_backslash_escapes_mode(): void {
$token = self::first_token( "SELECT 'a\\nb'", 'TEXT_STRING', array( 'NO_BACKSLASH_ESCAPES' ) );
$this->assertSame( 'a\\nb', $token->get_value() );
}
public function test_get_value_preserves_non_utf8_bytes_in_string_literals(): void {
// A quoted literal may legitimately carry non-UTF-8 bytes (binary or
// other-charset payloads). Value extraction must return them unchanged.
$raw = chr( 0xFF ) . chr( 0xFE );
$this->assertSame(
$raw,
self::first_token( "SELECT '$raw'", 'TEXT_STRING' )->get_value()
);
// A backslash escape strips the backslash and preserves the following non-UTF-8 byte.
$this->assertSame(
chr( 0xE9 ),
self::first_token( "SELECT '\\" . chr( 0xE9 ) . "'", 'TEXT_STRING' )->get_value()
);
// Valid multibyte UTF-8 still round-trips.
$this->assertSame(
'café 🙂',
self::first_token( "SELECT 'café 🙂'", 'TEXT_STRING' )->get_value()
);
}
public function test_get_value_unescapes_newlines_and_multibyte_characters(): void {
$this->assertSame(
"\n",
self::first_token( "SELECT '\\\n'", 'TEXT_STRING' )->get_value()
);
$this->assertSame(
'🙂',
self::first_token( "SELECT '\\🙂'", 'TEXT_STRING' )->get_value()
);
}
public function test_get_value_unquotes_backtick_identifiers(): void {
$this->assertSame( 'col name', self::first_token( 'SELECT `col name` FROM t', 'IDENT_QUOTED' )->get_value() );
}
public function test_get_value_unquotes_ansi_identifiers(): void {
$this->assertSame(
'a"b',
self::first_token( 'SELECT "a""b"', 'IDENT_QUOTED', array( 'ANSI_QUOTES' ) )->get_value()
);
}
public function test_get_value_does_not_unquote_unquoted_tokens(): void {
// The SSL keyword's Bison number collides with one of the lexer's
// internal quoted-text constants; value extraction must not be fooled
// by token ids and must return keyword bytes as-is.
$this->assertSame( 'SSL', self::first_token( 'CREATE USER u REQUIRE SSL', 'SSL' )->get_value() );
$this->assertSame( 'SELECT', self::first_token( 'SELECT 1', 'SELECT' )->get_value() );
$this->assertSame( '42', self::first_token( 'SELECT 42', 'NUM' )->get_value() );
}
public function test_get_name_resolves_bison_terminal_names(): void {
$tokens = ( new WP_MySQL_Lexer( "SELECT 'text', (1)" ) )->remaining_tokens();
$names = array();
foreach ( $tokens as $token ) {
$names[] = $token->get_name();
}
$this->assertSame(
array( 'SELECT', 'TEXT_STRING', 'COMMA', 'LEFT_PAREN', 'NUM', 'RIGHT_PAREN', 'END_OF_INPUT', 'YYEOF' ),
$names
);
}
public function test_get_name_returns_unknown_for_unmapped_ids(): void {
$token = new WP_MySQL_Token( 999999, 0, 0, '', false );
$this->assertSame( 'UNKNOWN', $token->get_name() );
}
}