Make WordPress Core


Ignore:
Timestamp:
09/26/2023 03:53:39 PM (18 months ago)
Author:
flixos90
Message:

Options, Meta APIs: Fix follow up bug when comparing values for options using the pre_option_{$option} filter.

This fix is relevant for options such as gmt_offset that use a filter to force a specific value regardless of what is stored in the database.

Props mamaduka, flixos90, mukesh27, spacedmonkey.
See #22192.

File:
1 edited

Legend:

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

    r56681 r56717  
    439439        }
    440440    }
    441 
    442441
    443442    /**
     
    582581        $this->assertSame( 1, $actual );
    583582    }
     583
     584    /**
     585     * Tests that a non-existing option is added even when its pre filter returns a value.
     586     *
     587     * @ticket 22192
     588     *
     589     * @covers ::update_option
     590     */
     591    public function test_update_option_with_pre_filter_adds_missing_option() {
     592        // Force a return value of integer 0.
     593        add_filter( 'pre_option_foo', '__return_zero' );
     594
     595        /*
     596         * This should succeed, since the 'foo' option does not exist in the database.
     597         * The default value is false, so it differs from 0.
     598         */
     599        $this->assertTrue( update_option( 'foo', 0 ) );
     600    }
     601
     602    /**
     603     * Tests that an existing option is updated even when its pre filter returns the same value.
     604     *
     605     * @ticket 22192
     606     *
     607     * @covers ::update_option
     608     */
     609    public function test_update_option_with_pre_filter_updates_option_with_different_value() {
     610        // Add the option with a value of 1 to the database.
     611        add_option( 'foo', 1 );
     612
     613        // Force a return value of integer 0.
     614        add_filter( 'pre_option_foo', '__return_zero' );
     615
     616        /*
     617         * This should succeed, since the 'foo' option has a value of 1 in the database.
     618         * Therefore it differs from 0 and should be updated.
     619         */
     620        $this->assertTrue( update_option( 'foo', 0 ) );
     621    }
     622
     623    /**
     624     * Tests that calling update_option() does not permanently remove pre filters.
     625     *
     626     * @ticket 22192
     627     *
     628     * @covers ::update_option
     629     */
     630    public function test_update_option_maintains_pre_filters() {
     631        add_filter( 'pre_option_foo', '__return_zero' );
     632        update_option( 'foo', 0 );
     633
     634        // Assert that the filter is still present.
     635        $this->assertSame( 10, has_filter( 'pre_option_foo', '__return_zero' ) );
     636    }
    584637}
Note: See TracChangeset for help on using the changeset viewer.