mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
Tests: Introduce WP_UnitTestCase::reset_permalinks(), an attempt to DRY up logic for resetting and restoring default permalinks on setUp() and tearDown().
See #33968. git-svn-id: https://develop.svn.wordpress.org/trunk@34802 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -16,6 +16,8 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
|
||||
|
||||
protected $db_version;
|
||||
|
||||
public static $default_permalink_structure;
|
||||
|
||||
/**
|
||||
* @var WP_UnitTest_Factory
|
||||
*/
|
||||
@@ -70,6 +72,10 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
|
||||
if ( is_multisite() ) {
|
||||
add_filter( 'pre_option_db_version', array( $this, 'db_version' ) );
|
||||
}
|
||||
|
||||
self::$default_permalink_structure = get_option( 'permalink_structure' );
|
||||
|
||||
$this->reset_permalinks();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,6 +105,8 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
|
||||
remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
|
||||
$this->_restore_hooks();
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$this->reset_permalinks( $restore = true );
|
||||
}
|
||||
|
||||
function clean_up_global_scope() {
|
||||
@@ -645,4 +653,30 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
|
||||
public function db_version() {
|
||||
return $this->db_version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that resets permalinks and flushes rewrites.
|
||||
*
|
||||
* Collects the current permalink structure and stores it in a class property for use
|
||||
* by sub-classes.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @global WP_Rewrite $wp_rewrite
|
||||
*
|
||||
* @param bool $restore_default Optional. Whether to restore the default permalink structure.
|
||||
* Default false.
|
||||
*/
|
||||
public function reset_permalinks( $restore_default = false ) {
|
||||
global $wp_rewrite;
|
||||
|
||||
if ( ! $restore_default ) {
|
||||
$wp_rewrite->init();
|
||||
$wp_rewrite->set_permalink_structure( '' );
|
||||
} else {
|
||||
$wp_rewrite->set_permalink_structure( self::$default_permalink_structure );
|
||||
}
|
||||
|
||||
$wp_rewrite->flush_rules();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user