wordpress-develop/tests/phpunit/tests/option/updateOption.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

175 lines
4.8 KiB
PHP

<?php
/**
* @group option
*/
class Tests_Option_UpdateOption extends WP_UnitTestCase {
/**
* @ticket 31047
*/
public function test_should_respect_default_option_filter_when_option_does_not_yet_exist_in_database() {
add_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
$added = update_option( 'doesnotexist', 'bar' );
remove_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
$this->assertTrue( $added );
$this->assertSame( 'bar', get_option( 'doesnotexist' ) );
}
/**
* @ticket 26394
*/
public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_missing() {
global $wpdb;
$this->flush_cache();
update_option( 'test_update_option_default', 'value' );
$this->flush_cache();
// Populate the alloptions cache, which includes autoload=yes options.
wp_load_alloptions();
$before = $wpdb->num_queries;
$value = get_option( 'test_update_option_default' );
$after = $wpdb->num_queries;
$this->assertEquals( $before, $after );
$this->assertEquals( $value, 'value' );
}
/**
* @ticket 26394
*/
public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_yes() {
global $wpdb;
$this->flush_cache();
update_option( 'test_update_option_default', 'value', 'yes' );
$this->flush_cache();
// Populate the alloptions cache, which includes autoload=yes options.
wp_load_alloptions();
$before = $wpdb->num_queries;
$value = get_option( 'test_update_option_default' );
$after = $wpdb->num_queries;
$this->assertEquals( $before, $after );
$this->assertEquals( $value, 'value' );
}
/**
* @ticket 26394
*/
public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_no() {
global $wpdb;
$this->flush_cache();
update_option( 'test_update_option_default', 'value', 'no' );
$this->flush_cache();
// Populate the alloptions cache, which does not include autoload=no options.
wp_load_alloptions();
$before = $wpdb->num_queries;
$value = get_option( 'test_update_option_default' );
$after = $wpdb->num_queries;
// Database has been hit.
$this->assertEquals( $before + 1, $after );
$this->assertEquals( $value, 'value' );
}
/**
* @ticket 26394
*/
public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_false() {
global $wpdb;
$this->flush_cache();
update_option( 'test_update_option_default', 'value', false );
$this->flush_cache();
// Populate the alloptions cache, which does not include autoload=no options.
wp_load_alloptions();
$before = $wpdb->num_queries;
$value = get_option( 'test_update_option_default' );
$after = $wpdb->num_queries;
// Database has been hit.
$this->assertEquals( $before + 1, $after );
$this->assertEquals( $value, 'value' );
}
/**
* @group 26394
*/
public function test_autoload_should_be_updated_for_existing_option_when_value_is_changed() {
global $wpdb;
add_option( 'foo', 'bar', '', 'no' );
$updated = update_option( 'foo', 'bar2', true );
$this->assertTrue( $updated );
$this->flush_cache();
// Populate the alloptions cache, which includes autoload=yes options.
wp_load_alloptions();
$before = $wpdb->num_queries;
$value = get_option( 'foo' );
$this->assertEquals( $before, $wpdb->num_queries );
$this->assertEquals( $value, 'bar2' );
}
/**
* @group 26394
*/
public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_unchanged() {
global $wpdb;
add_option( 'foo', 'bar', '', 'yes' );
$updated = update_option( 'foo', 'bar', false );
$this->assertFalse( $updated );
$this->flush_cache();
// Populate the alloptions cache, which includes autoload=yes options.
wp_load_alloptions();
$before = $wpdb->num_queries;
$value = get_option( 'foo' );
// 'foo' should still be autoload=yes, so we should see no additional querios.
$this->assertEquals( $before, $wpdb->num_queries );
$this->assertEquals( $value, 'bar' );
}
/**
* @group 26394
*/
public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_changed_but_no_value_of_autoload_is_provided() {
global $wpdb;
add_option( 'foo', 'bar', '', 'yes' );
// Don't pass a value for `$autoload`.
$updated = update_option( 'foo', 'bar2' );
$this->assertTrue( $updated );
$this->flush_cache();
// Populate the alloptions cache, which includes autoload=yes options.
wp_load_alloptions();
$before = $wpdb->num_queries;
$value = get_option( 'foo' );
// 'foo' should still be autoload=yes, so we should see no additional querios.
$this->assertEquals( $before, $wpdb->num_queries );
$this->assertEquals( $value, 'bar2' );
}
/**
* `add_filter()` callback for test_should_respect_default_option_filter_when_option_does_not_yet_exist_in_database().
*/
public function __return_foo() {
return 'foo';
}
}