Make WordPress Core

Ticket #24686: 24686.diff

File 24686.diff, 2.7 KB (added by welcher, 8 years ago)

Adds unit tests

  • src/wp-includes/option.php

     
    661661         * @param mixed  $value     Value of transient.
    662662         * @param string $transient Transient name.
    663663         */
    664         return apply_filters( "transient_{$transient}", $value, $transient );
     664        $value = apply_filters( "transient_{$transient}", $value, $transient );
     665
     666        /**
     667         * Fires after the value for a transient has been set.
     668         *
     669         * @since 4.7.0
     670         *
     671         * @param mixed  $value      Transient value.
     672         * @param string $transient  The name of the transient.
     673         */
     674        return apply_filters( 'getted_transient', $value, $transient );
     675
    665676}
    666677
    667678/**
  • tests/phpunit/tests/option/transient.php

     
    154154                );
    155155                $this->assertEquals( $expected, $a->get_events() );
    156156        }
     157
     158        /**
     159         * @ticket 24686
     160         */
     161        function test_getted_transient_filter() {
     162
     163                $key = 'test_getted_transient';
     164                set_transient( $key, 'test_value', MINUTE_IN_SECONDS );
     165
     166                $transient = get_transient( $key );
     167
     168                $this->assertSame( 'test_value', $transient );
     169        }
     170
     171        /**
     172         * @ticket 24686
     173         */
     174        function test_getted_transient_return_false() {
     175
     176                $key = 'test_getted_transient';
     177                set_transient( $key, 'test_value', MINUTE_IN_SECONDS );
     178
     179                add_filter( 'getted_transient', '__return_false' );
     180
     181                $transient = get_transient( $key );
     182
     183                remove_filter( 'getted_transient', '__return_false' );
     184
     185                $this->assertSame( false, $transient );
     186        }
     187
     188        /**
     189         * @ticket 24686
     190         */
     191        function test_getted_transient_bypass_single_transient() {
     192                $key1 = 'test_getted_transient_1';
     193                $key2 = 'test_getted_transient_2';
     194                set_transient( $key1, 'test_value_1', MINUTE_IN_SECONDS );
     195                set_transient( $key2, 'test_value_2', MINUTE_IN_SECONDS );
     196
     197                add_filter( 'getted_transient', array( $this, 'bypass_first_transient_only' ), 10, 2 );
     198
     199                $transient_1 = get_transient( $key1 );
     200                $transient_2 = get_transient( $key2 );
     201
     202                remove_filter( 'getted_transient', array( $this, 'bypass_first_transient_only' ) );
     203
     204                $this->assertSame( false, $transient_1 );
     205                $this->assertSame( 'test_value_2', $transient_2 );
     206        }
     207
     208        /**
     209         * Callback for the test above.
     210         *
     211         * @param string $value     The value of the transient.
     212         * @param string $transient The name of the transient.
     213         *
     214         * @return bool|string|mixed
     215         */
     216        function bypass_first_transient_only( $value, $transient ) {
     217                if ( 'test_getted_transient_1' === $transient ) {
     218                        $value = false;
     219                }
     220                return $value;
     221        }
     222
    157223}