Make WordPress Core

Changeset 56648


Ignore:
Timestamp:
09/21/2023 03:21:45 PM (3 years ago)
Author:
flixos90
Message:

Options, Meta APIs: Add further test coverage for comparison between old and new option value.

This ensures potential future changes to the logic are covered by existing tests that should pass before and after.

Props joemcgill.
See #22192.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/option/option.php

    r56595 r56648  
    369369                );
    370370        }
     371
     372        /**
     373         * Ensure the database is getting updated when type changes, but not otherwise.
     374         *
     375         * @ticket 22192
     376         *
     377         * @covers ::update_option
     378         *
     379         * @dataProvider data_update_option_type_juggling
     380         */
     381        public function test_update_loosey_options( $old_value, $new_value, $update = false ) {
     382                add_option( 'foo', $old_value );
     383
     384                // Comparison will happen against value cached during add_option() above.
     385                $updated = update_option( 'foo', $new_value );
     386
     387                if ( $update ) {
     388                        $this->assertTrue( $updated, 'This loosely equal option should trigger an update.' );
     389                } else {
     390                        $this->assertFalse( $updated, 'Loosely equal option should not trigger an update.' );
     391                }
     392        }
     393
     394        /**
     395         * Ensure the database is getting updated when type changes, but not otherwise.
     396         *
     397         * @ticket 22192
     398         *
     399         * @covers ::update_option
     400         *
     401         * @dataProvider data_update_option_type_juggling
     402         */
     403        public function test_update_loosey_options_from_db( $old_value, $new_value, $update = false ) {
     404                add_option( 'foo', $old_value );
     405
     406                // Delete cache.
     407                wp_cache_delete( 'alloptions', 'options' );
     408                $updated = update_option( 'foo', $new_value );
     409
     410                if ( $update ) {
     411                        $this->assertTrue( $updated, 'This loosely equal option should trigger an update.' );
     412                } else {
     413                        $this->assertFalse( $updated, 'Loosely equal option should not trigger an update.' );
     414                }
     415        }
     416
     417        /**
     418         * Ensure the database is getting updated when type changes, but not otherwise.
     419         *
     420         * @ticket 22192
     421         *
     422         * @covers ::update_option
     423         *
     424         * @dataProvider data_update_option_type_juggling
     425         */
     426        public function test_update_loosey_options_from_refreshed_cache( $old_value, $new_value, $update = false ) {
     427                add_option( 'foo', $old_value );
     428
     429                // Delete and refresh cache from DB.
     430                wp_cache_delete( 'alloptions', 'options' );
     431                wp_load_alloptions();
     432
     433                $updated = update_option( 'foo', $new_value );
     434
     435                if ( $update ) {
     436                        $this->assertTrue( $updated, 'This loosely equal option should trigger an update.' );
     437                } else {
     438                        $this->assertFalse( $updated, 'Loosely equal option should not trigger an update.' );
     439                }
     440        }
     441
     442
     443        /**
     444         * Data provider.
     445         *
     446         * @return array
     447         */
     448        public function data_update_option_type_juggling() {
     449                return array(
     450                        /*
     451                         * Truthy values.
     452                         * Loosely equal truthy scalar values should never result in a DB update.
     453                         */
     454                        array( '1', '1' ),
     455                        array( '1', 1 ),
     456                        array( '1', 1.0 ),
     457                        array( '1', true ),
     458                        array( 1, '1' ),
     459                        array( 1, 1 ),
     460                        array( 1, 1.0 ),
     461                        array( 1, true ),
     462                        array( 1.0, '1' ),
     463                        array( 1.0, 1 ),
     464                        array( 1.0, 1.0 ),
     465                        array( 1.0, true ),
     466                        array( true, '1' ),
     467                        array( true, 1 ),
     468                        array( true, 1.0 ),
     469                        array( true, true ),
     470
     471                        /*
     472                         * Falsey values.
     473                         * Loosely equal falsey scalar values only sometimes result in a DB update.
     474                         */
     475                        array( '0', '0' ),
     476                        array( '0', 0 ),
     477                        array( '0', 0.0 ),
     478                        array( '0', false, true ), // Should update.
     479                        array( '', '' ),
     480                        array( '', 0, true ), // Should update.
     481                        array( '', 0.0, true ), // Should update.
     482                        array( '', false ),
     483                        array( 0, '0' ),
     484                        array( 0, '', true ), // Should update.
     485                        array( 0, 0 ),
     486                        array( 0, 0.0 ),
     487                        array( 0, false, true ), // Should update.
     488                        array( 0.0, '0' ),
     489                        array( 0.0, '', true ), // Should update.
     490                        array( 0.0, 0 ),
     491                        array( 0.0, 0.0 ),
     492                        array( 0.0, false, true ), // Should update.
     493                        array( false, '0', true ), // Should update.
     494                        array( false, '' ),
     495                        array( false, 0, true ), // Should update.
     496                        array( false, 0.0, true ), // Should update.
     497                        array( false, false ),
     498
     499                        /*
     500                         * Non scalar values.
     501                         * Loosely equal non-scalar values should almost always result in an update.
     502                         */
     503                        array( false, array(), true ),
     504                        array( 'false', array(), true ),
     505                        array( '', array(), true ),
     506                        array( 0, array(), true ),
     507                        array( '0', array(), true ),
     508                        array( false, null ), // Does not update.
     509                        array( 'false', null, true ),
     510                        array( '', null ), // Does not update.
     511                        array( 0, null, true ),
     512                        array( '0', null, true ),
     513                        array( array(), false, true ),
     514                        array( array(), 'false', true ),
     515                        array( array(), '', true ),
     516                        array( array(), 0, true ),
     517                        array( array(), '0', true ),
     518                        array( array(), null, true ),
     519                        array( null, false ), // Does not update.
     520                        array( null, 'false', true ),
     521                        array( null, '' ), // Does not update.
     522                        array( null, 0, true ),
     523                        array( null, '0', true ),
     524                        array( null, array(), true ),
     525                );
     526        }
    371527}
Note: See TracChangeset for help on using the changeset viewer.