wordpress-develop/tests/phpunit/tests/option/transient.php
Boone Gorges 578134d4ff Use wp_installing() instead of WP_INSTALLING constant.
The `WP_INSTALLING` constant is a flag that WordPress sets in a number of
places, telling the system that options should be fetched directly from the
database instead of from the cache, that WP should not ping wordpress.org for
updates, that the normal "not installed" checks should be bypassed, and so on.

A constant is generally necessary for this purpose, because the flag is
typically set before the WP bootstrap, meaning that WP functions are not yet
available.  However, it is possible - notably, during `wpmu_create_blog()` -
for the "installing" flag to be set after WP has already loaded. In these
cases, `WP_INSTALLING` would be set for the remainder of the process, since
there's no way to change a constant once it's defined. This, in turn, polluted
later function calls that ought to have been outside the scope of site
creation, particularly the non-caching of option data. The problem was
particularly evident in the case of the automated tests, where `WP_INSTALLING`
was set the first time a site was created, and remained set for the rest of the
suite.

The new `wp_installing()` function allows developers to fetch the current
installation status (when called without any arguments) or to set the
installation status (when called with a boolean `true` or `false`). Use of
the `WP_INSTALLING` constant is still supported; `wp_installing()` will default
to `true` if the constant is defined during the bootstrap.

Props boonebgorges, jeremyfelt.
See #31130.

git-svn-id: https://develop.svn.wordpress.org/trunk@34828 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-05 15:05:26 +00:00

158 lines
4.5 KiB
PHP

<?php
/**
* @group option
*/
class Tests_Option_Transient extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
if ( wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'Not testable with an external object cache.' );
}
}
function test_the_basics() {
$key = rand_str();
$value = rand_str();
$value2 = rand_str();
$this->assertFalse( get_transient( 'doesnotexist' ) );
$this->assertTrue( set_transient( $key, $value ) );
$this->assertEquals( $value, get_transient( $key ) );
$this->assertFalse( set_transient( $key, $value ) );
$this->assertTrue( set_transient( $key, $value2 ) );
$this->assertEquals( $value2, get_transient( $key ) );
$this->assertTrue( delete_transient( $key ) );
$this->assertFalse( get_transient( $key ) );
$this->assertFalse( delete_transient( $key ) );
}
function test_serialized_data() {
$key = rand_str();
$value = array( 'foo' => true, 'bar' => true );
$this->assertTrue( set_transient( $key, $value ) );
$this->assertEquals( $value, get_transient( $key ) );
$value = (object) $value;
$this->assertTrue( set_transient( $key, $value ) );
$this->assertEquals( $value, get_transient( $key ) );
$this->assertTrue( delete_transient( $key ) );
}
/**
* @ticket 22807
*/
function test_transient_data_with_timeout() {
$key = rand_str();
$value = rand_str();
$this->assertFalse( get_option( '_transient_timeout_' . $key ) );
$now = time();
$this->assertTrue( set_transient( $key, $value, 100 ) );
// Ensure the transient timeout is set for 100-101 seconds in the future.
$this->assertGreaterThanOrEqual( $now + 100, get_option( '_transient_timeout_' . $key ) );
$this->assertLessThanOrEqual( $now + 101, get_option( '_transient_timeout_' . $key ) );
// Update the timeout to a second in the past and watch the transient be invalidated.
update_option( '_transient_timeout_' . $key, $now - 1 );
$this->assertFalse( get_transient( $key ) );
}
/**
* @ticket 22807
*/
function test_transient_add_timeout() {
$key = rand_str();
$value = rand_str();
$value2 = rand_str();
$this->assertTrue( set_transient( $key, $value ) );
$this->assertEquals( $value, get_transient( $key ) );
$this->assertFalse( get_option( '_transient_timeout_' . $key ) );
$now = time();
// Add timeout to existing timeout-less transient.
$this->assertTrue( set_transient( $key, $value2, 1 ) );
$this->assertGreaterThanOrEqual( $now, get_option( '_transient_timeout_' . $key ) );
update_option( '_transient_timeout_' . $key, $now - 1 );
$this->assertFalse( get_transient( $key ) );
}
/**
* If get_option( $transient_timeout ) returns false, don't bother trying to delete the transient.
*
* @ticket 30380
*/
function test_nonexistent_key_dont_delete_if_false() {
// Create a bogus a transient
$key = 'test_transient';
set_transient( $key, 'test', 60 * 10 );
$this->assertEquals( 'test', get_transient( $key ) );
// Useful variables for tracking
$transient_timeout = '_transient_timeout_' . $key;
// Mock an action for tracking action calls
$a = new MockAction();
// Make sure the timeout option returns false
add_filter( 'option_' . $transient_timeout, '__return_false' );
// Add some actions to make sure options are _not_ deleted
add_action( 'delete_option', array( $a, 'action' ) );
// Act
get_transient( $key );
// Make sure delete option was not called for both the transient and the timeout
$this->assertEquals( 0, $a->get_call_count() );
}
/**
* @ticket 30380
*/
function test_nonexistent_key_old_timeout() {
// Create a transient
$key = 'test_transient';
set_transient( $key, 'test', 60 * 10 );
$this->assertEquals( 'test', get_transient( $key ) );
// Make sure the timeout option returns false
$timeout = '_transient_timeout_' . $key;
$transient_option = '_transient_' . $key;
add_filter( 'option_' . $timeout, '__return_zero' );
// Mock an action for tracking action calls
$a = new MockAction();
// Add some actions to make sure options are deleted
add_action( 'delete_option', array( $a, 'action' ) );
// Act
get_transient( $key );
// Make sure delete option was called for both the transient and the timeout
$this->assertEquals( 2, $a->get_call_count() );
$expected = array(
array(
'action' => 'action',
'tag' => 'delete_option',
'args' => array( $transient_option ),
),
array(
'action' => 'action',
'tag' => 'delete_option',
'args' => array( $timeout ),
),
);
$this->assertEquals( $expected, $a->get_events() );
}
}