@@ -12,7 +12,9 @@ class WP_MySQL_On_SQLite_Tests extends TestCase {
1212 private const SQL_MODE_NOT_USED = 1 << 4 ;
1313 private const SQL_MODE_POSTGRESQL = 1 << 8 ;
1414 private const SQL_MODE_ORACLE = 1 << 9 ;
15+ private const SQL_MODE_ANSI = 1 << 18 ;
1516 private const SQL_MODE_NO_BACKSLASH_ESCAPES = 1 << 20 ;
17+ private const SQL_MODE_TRADITIONAL = 1 << 27 ;
1618 private const SQL_MODE_TIME_TRUNCATE_FRACTIONAL = 1 << 32 ;
1719 private const UNKNOWN_SQL_MODE_BIT = 1 << 33 ;
1820
@@ -3338,6 +3340,134 @@ public function testCompositeAnsiModeExpandsAlongsideOtherModes() {
33383340 );
33393341 }
33403342
3343+ /**
3344+ * @dataProvider compositeTraditionalModeValues
3345+ */
3346+ public function testCompositeTraditionalModeExpandsToComponentModes (
3347+ int $ mysql_version ,
3348+ string $ value ,
3349+ string $ expected_modes
3350+ ): void {
3351+ $ this ->engine = new WP_MySQL_On_SQLite (
3352+ 'mysql-on-sqlite:dbname=wp ' ,
3353+ null ,
3354+ null ,
3355+ array (
3356+ 'sqlite_pdo ' => $ this ->sqlite ,
3357+ 'mysql_version ' => $ mysql_version ,
3358+ )
3359+ );
3360+
3361+ $ this ->assertQuery ( "SET sql_mode = 'ANSI' " );
3362+ $ this ->assertQuery ( "SET sql_mode = $ value " );
3363+ $ this ->assertQuery ( 'SELECT @@sql_mode AS mode ' );
3364+ $ this ->assertSame ( $ expected_modes , $ this ->last_result [0 ]->mode );
3365+ foreach ( explode ( ', ' , $ expected_modes ) as $ mode ) {
3366+ $ this ->assertTrue ( $ this ->engine ->is_sql_mode_active ( $ mode ), $ mode );
3367+ }
3368+ $ this ->assertFalse ( $ this ->engine ->is_sql_mode_active ( 'ANSI ' ) );
3369+ $ this ->assertFalse ( $ this ->engine ->is_sql_mode_active ( 'ONLY_FULL_GROUP_BY ' ) );
3370+
3371+ // Reassigning the expanded value must preserve the same modes.
3372+ $ this ->assertQuery ( 'SET sql_mode = @@sql_mode ' );
3373+ $ this ->assertQuery ( 'SELECT @@sql_mode AS mode ' );
3374+ $ this ->assertSame ( $ expected_modes , $ this ->last_result [0 ]->mode );
3375+
3376+ $ this ->assertQuery ( "SET sql_mode = '' " );
3377+ $ this ->assertQuery ( 'SELECT @@sql_mode AS mode ' );
3378+ $ this ->assertSame ( '' , $ this ->last_result [0 ]->mode );
3379+ foreach ( explode ( ', ' , $ expected_modes ) as $ mode ) {
3380+ $ this ->assertFalse ( $ this ->engine ->is_sql_mode_active ( $ mode ), $ mode );
3381+ }
3382+ }
3383+
3384+ public static function compositeTraditionalModeValues (): array {
3385+ $ mysql_80_modes = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_ENGINE_SUBSTITUTION ' ;
3386+ $ mysql_57_modes = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION ' ;
3387+ $ versions = array (
3388+ 50744 => $ mysql_57_modes ,
3389+ 80010 => $ mysql_57_modes ,
3390+ 80011 => $ mysql_80_modes ,
3391+ 80400 => $ mysql_80_modes ,
3392+ );
3393+ $ values = array (
3394+ 'name ' => "'TRADITIONAL' " ,
3395+ 'identifier ' => 'TRADITIONAL ' ,
3396+ 'lowercase and overlap ' => "'traditional,STRICT_ALL_TABLES,TRADITIONAL' " ,
3397+ 'bitmap ' => (string ) self ::SQL_MODE_TRADITIONAL ,
3398+ );
3399+ $ cases = array ();
3400+ foreach ( $ versions as $ version => $ expected_modes ) {
3401+ foreach ( $ values as $ name => $ value ) {
3402+ $ cases [ "$ version $ name " ] = array ( $ version , $ value , $ expected_modes );
3403+ }
3404+ }
3405+ return $ cases ;
3406+ }
3407+
3408+ public function testCompositeTraditionalModeExpandsAlongsideAnsi (): void {
3409+ $ values = array (
3410+ "'TRADITIONAL,ANSI,STRICT_ALL_TABLES' " ,
3411+ self ::SQL_MODE_TRADITIONAL | self ::SQL_MODE_ANSI ,
3412+ );
3413+ foreach ( $ values as $ value ) {
3414+ $ this ->assertQuery ( "SET sql_mode = $ value " );
3415+ $ this ->assertQuery ( 'SELECT @@sql_mode AS mode ' );
3416+ $ this ->assertSame (
3417+ 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_ENGINE_SUBSTITUTION ' ,
3418+ $ this ->last_result [0 ]->mode
3419+ );
3420+ }
3421+ }
3422+
3423+ /**
3424+ * @dataProvider compositeTraditionalModeInvalidDates
3425+ */
3426+ public function testCompositeTraditionalModeRejectsInvalidDates ( string $ value ): void {
3427+ $ this ->assertQuery ( "SET sql_mode = 'TRADITIONAL' " );
3428+ $ this ->assertQuery ( "INSERT INTO _dates (option_value) VALUES ('2020-01-15 00:00:00') " );
3429+ $ this ->assertQueryError (
3430+ "INSERT INTO _dates (option_value) VALUES (' $ value') " ,
3431+ "Incorrect datetime value: ' $ value' "
3432+ );
3433+ $ this ->assertQueryError (
3434+ "UPDATE _dates SET option_value = ' $ value' " ,
3435+ "Incorrect datetime value: ' $ value' "
3436+ );
3437+ $ this ->assertQuery ( 'SELECT option_value FROM _dates ' );
3438+ $ this ->assertEquals ( array ( (object ) array ( 'option_value ' => '2020-01-15 00:00:00 ' ) ), $ this ->last_result );
3439+ }
3440+
3441+ public static function compositeTraditionalModeInvalidDates (): array {
3442+ return array (
3443+ 'zero date ' => array ( '0000-00-00 00:00:00 ' ),
3444+ 'zero month ' => array ( '2020-00-15 00:00:00 ' ),
3445+ 'zero day ' => array ( '2020-01-00 00:00:00 ' ),
3446+ 'invalid date ' => array ( 'not-a-date ' ),
3447+ );
3448+ }
3449+
3450+ public function testCompositeTraditionalModeEnablesStrictMode (): void {
3451+ $ this ->assertQuery ( 'SET sql_mode = ' . self ::SQL_MODE_TRADITIONAL );
3452+ $ this ->assertQuery ( 'CREATE TABLE t (id INT, value TEXT NOT NULL) ' );
3453+ $ this ->assertQueryError (
3454+ 'INSERT INTO t (id) VALUES (1) ' ,
3455+ 'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t.value '
3456+ );
3457+ $ this ->assertQuery ( "INSERT INTO t (id, value) VALUES (1, 'initial-value') " );
3458+ $ this ->assertQueryError (
3459+ 'UPDATE t SET value = NULL ' ,
3460+ 'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: t.value '
3461+ );
3462+ $ this ->assertQuery ( 'SELECT value FROM t ' );
3463+ $ this ->assertSame ( 'initial-value ' , $ this ->last_result [0 ]->value );
3464+
3465+ $ this ->assertQuery ( "SET sql_mode = '' " );
3466+ $ this ->assertQuery ( 'UPDATE t SET value = NULL ' );
3467+ $ this ->assertQuery ( 'SELECT value FROM t ' );
3468+ $ this ->assertSame ( '' , $ this ->last_result [0 ]->value );
3469+ }
3470+
33413471 public function testCaseInsensitiveSelect () {
33423472 $ this ->assertQuery (
33433473 "CREATE TABLE _tmp_table (
0 commit comments