-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbHelpersTest.php
More file actions
58 lines (52 loc) · 2.17 KB
/
Copy pathDbHelpersTest.php
File metadata and controls
58 lines (52 loc) · 2.17 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
<?php
/**
* Unit tests for the SQL-safety helpers in Emanager_DB.
*
* These guard the single choke point for all custom-table SQL: identifiers are
* whitelisted and field types map to a fixed set of column types.
*
* @package eManager
*/
declare( strict_types=1 );
use PHPUnit\Framework\TestCase;
/**
* Exercises the pure helpers Emanager_DB::safe_ident() and Emanager_DB::column_type().
*/
final class DbHelpersTest extends TestCase {
public function test_safe_ident_strips_unsafe_characters(): void {
$this->assertSame( 'em_rfis', Emanager_DB::safe_ident( 'em_rfis' ) );
$this->assertSame( 'em_rfis', Emanager_DB::safe_ident( 'EM_RFIs' ) );
// Quote/semicolon injection attempts are stripped to harmless chars.
$this->assertSame( 'droptableusers', Emanager_DB::safe_ident( 'DROP TABLE users;' ) );
$this->assertSame( 'abc123', Emanager_DB::safe_ident( 'abc-`123`' ) );
$this->assertSame( 'a_b_c', Emanager_DB::safe_ident( 'a_b_c' ) );
$this->assertSame( '', Emanager_DB::safe_ident( '"; --' ) );
}
/**
* @dataProvider column_type_cases
*
* @param string $field_type eManager field type.
* @param string $expected Expected MySQL column type.
*/
public function test_column_type_maps_field_types( string $field_type, string $expected ): void {
$this->assertSame( $expected, Emanager_DB::column_type( $field_type ) );
}
/**
* @return array<string, array{0:string,1:string}>
*/
public static function column_type_cases(): array {
return array(
'textarea -> text' => array( 'textarea', 'text' ),
'richtext -> text' => array( 'richtext', 'text' ),
'json -> longtext' => array( 'json', 'longtext' ),
'signature -> mediumtext' => array( 'signature', 'mediumtext' ),
'number -> decimal' => array( 'number', 'decimal(20,4)' ),
'currency -> decimal' => array( 'currency', 'decimal(20,4)' ),
'date -> date' => array( 'date', 'date' ),
'datetime -> datetime' => array( 'datetime', 'datetime' ),
'checkbox -> tinyint' => array( 'checkbox', 'tinyint(1)' ),
'text -> varchar default' => array( 'text', 'varchar(191)' ),
'unknown -> varchar' => array( 'something-else', 'varchar(191)' ),
);
}
}