Make WordPress Core

Ticket #30380: patch-transient-wtests-5.2.diff

File patch-transient-wtests-5.2.diff, 3.0 KB (added by ericmann, 8 years ago)

Refactor the tests to remove closures (to make things PHP 5.2-compatible)

  • src/wp-includes/option.php

     
    622622                        $alloptions = wp_load_alloptions();
    623623                        if ( !isset( $alloptions[$transient_option] ) ) {
    624624                                $transient_timeout = '_transient_timeout_' . $transient;
    625                                 if ( get_option( $transient_timeout ) < time() ) {
     625                                $timeout = get_option( $transient_timeout );
     626                                if ( false !== $timeout && $timeout < time() ) {
    626627                                        delete_option( $transient_option  );
    627628                                        delete_option( $transient_timeout );
    628629                                        $value = false;
  • tests/phpunit/tests/option/transient.php

     
    8383                update_option( '_transient_timeout_' . $key, $now - 1 );
    8484                $this->assertFalse( get_transient( $key ) );
    8585        }
     86
     87        /**
     88         * If get_option( $transient_timeout ) returns false, don't bother trying to delete the transient.
     89         *
     90         * @ticket 30380
     91         */
     92        function test_nonexistent_key_dont_delete_if_false() {
     93                // Create a bogus a transient
     94                $key = 'test_transient';
     95                set_transient( $key, 'test', 60 * 10 );
     96                $this->assertEquals( 'test', get_transient( $key ) );
     97
     98                // Useful variables for tracking
     99                $transient_timeout = '_transient_timeout_' . $key;
     100
     101                // Mock an action for tracking action calls
     102                $a = new MockAction();
     103
     104                // Make sure the timeout option returns false
     105                add_filter( 'option_' . $transient_timeout, '__return_false' );
     106
     107                // Add some actions to make sure options are _not_ deleted
     108                add_action( 'delete_option', array( $a, 'delete_option' ) );
     109
     110                // Act
     111                get_transient( $key );
     112
     113                // Make sure delete option was not called for both the transient and the timeout
     114                $this->assertEquals( 0, $a->get_call_count() );
     115        }
     116
     117        /**
     118         * @ticket 30380
     119         */
     120        function test_nonexistent_key_old_timeout() {
     121                // Create a transient
     122                $key = 'test_transient';
     123                set_transient( $key, 'test', 60 * 10 );
     124                $this->assertEquals( 'test', get_transient( $key ) );
     125
     126                // Make sure the timeout option returns false
     127                $timeout = '_transient_timeout_' . $key;
     128                $transient_option = '_transient_' . $key;
     129                add_filter( 'option_' . $timeout, '__return_zero' );
     130
     131                // Mock an action for tracking action calls
     132                $a = new MockAction();
     133
     134                // Add some actions to make sure options are deleted
     135                add_action( 'delete_option', array( $a, 'action' ) );
     136
     137                // Act
     138                get_transient( $key );
     139
     140                // Make sure delete option was called for both the transient and the timeout
     141                $this->assertEquals( 2, $a->get_call_count() );
     142
     143                $expected = array(
     144                        array(
     145                                'action' => 'action',
     146                                'tag'    => 'delete_option',
     147                                'args'   => array( $transient_option ),
     148                        ),
     149                        array(
     150                                'action' => 'action',
     151                                'tag'    => 'delete_option',
     152                                'args'   => array( $timeout ),
     153                        ),
     154                );
     155                $this->assertEquals( $expected, $a->get_events() );
     156        }
    86157}