Make WordPress Core

Ticket #22807: 22807.diff

File 22807.diff, 2.1 KB (added by kirasong, 12 years ago)

Comments, Formatting, Tests in same patch.

  • src/wp-includes/option.php

    diff --git src/wp-includes/option.php src/wp-includes/option.php
    index e1ad731..f20e7bf 100644
    function set_transient( $transient, $value, $expiration = 0 ) { 
    642642                        }
    643643                        $result = add_option( $transient, $value, '', $autoload );
    644644                } else {
    645                         if ( $expiration )
    646                                 update_option( $transient_timeout, time() + $expiration );
    647                         $result = update_option( $transient, $value );
     645                        /**
     646                         * If expiration is requested, but the transient has no timeout option,
     647                         * delete, then re-create transient rather than update.
     648                         */
     649                        $skipupdate = false;
     650                        if ( $expiration ) {
     651                                if ( false === get_option( $transient_timeout ) ) {
     652                                        delete_option( $transient );
     653                                        add_option( $transient_timeout, time() + $expiration, '', 'no' );
     654                                        $result = add_option( $transient, $value, '', 'no' );
     655                                        $skipupdate = true;
     656                                } else {
     657                                        update_option( $transient_timeout, time() + $expiration );
     658                                }
     659                        }
     660                        if ( false === $skipupdate ) {
     661                                $result = update_option( $transient, $value );
     662                        }
    648663                }
    649664        }
    650665
  • tests/phpunit/tests/option/transient.php

    diff --git tests/phpunit/tests/option/transient.php tests/phpunit/tests/option/transient.php
    index 1d9c64f..c7eeb1b 100644
    class Tests_Option_Transient extends WP_UnitTestCase { 
    3333                $this->assertEquals( $value, get_transient( $key ) );
    3434                $this->assertTrue( delete_transient( $key ) );
    3535        }
     36
     37        function test_transient_data_with_timeout() {
     38                $key = rand_str();
     39                $value = rand_str();
     40                $value2 = rand_str();
     41
     42                $this->assertTrue( set_transient( $key, $value, 1 ) );
     43                sleep( 2 );
     44                $this->assertFalse( get_transient( $key ) );
     45        }
     46
     47        /**
     48         * @ticket 22807
     49         */
     50        function test_transient_add_timeout() {
     51                $key = rand_str();
     52                $value = rand_str();
     53                $value2 = rand_str();
     54                $this->assertTrue( set_transient( $key, $value) );
     55                $this->assertEquals($value, get_transient( $key) );
     56
     57                // Add timeout to existing timeout-less transient.
     58                $this->assertTrue( set_transient( $key, $value2, 1 ) );
     59                sleep( 2 );
     60                $this->assertFalse( get_transient( $key ) );
     61        }
    3662}