| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group themes |
| 5 | * @group temp |
| 6 | */ |
| 7 | class Tests_Autoload_Theme_Mods extends WP_UnitTestCase { |
| 8 | |
| 9 | function setUp() { |
| 10 | parent::setUp(); |
| 11 | $this->theme_root = realpath( DIR_TESTDATA . '/themedir1' ); |
| 12 | |
| 13 | $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; |
| 14 | $GLOBALS['wp_theme_directories'] = array( $this->theme_root ); |
| 15 | |
| 16 | add_filter( 'theme_root', array( $this, '_theme_root' ) ); |
| 17 | add_filter( 'stylesheet_root', array( $this, '_theme_root' ) ); |
| 18 | add_filter( 'template_root', array( $this, '_theme_root' ) ); |
| 19 | // clear caches |
| 20 | wp_clean_themes_cache(); |
| 21 | unset( $GLOBALS['wp_themes'] ); |
| 22 | } |
| 23 | |
| 24 | function tearDown() { |
| 25 | $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; |
| 26 | remove_filter( 'theme_root', array( $this, '_theme_root' ) ); |
| 27 | remove_filter( 'stylesheet_root', array( $this, '_theme_root' ) ); |
| 28 | remove_filter( 'template_root', array( $this, '_theme_root' ) ); |
| 29 | wp_clean_themes_cache(); |
| 30 | unset( $GLOBALS['wp_themes'] ); |
| 31 | parent::tearDown(); |
| 32 | } |
| 33 | |
| 34 | // replace the normal theme root dir with our premade test dir |
| 35 | function _theme_root( $dir ) { |
| 36 | return $this->theme_root; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | function test_on_switch_theme_previous_mods_should_not_be_autoload() { |
| 41 | global $wpdb; |
| 42 | |
| 43 | $current_theme_stylesheet = get_stylesheet(); |
| 44 | |
| 45 | // Set a theme mod for the current theme |
| 46 | $new_theme_stylesheet = 'theme1'; |
| 47 | set_theme_mod( 'default-theme-option', 'a-value' ); |
| 48 | |
| 49 | switch_theme( $new_theme_stylesheet ); |
| 50 | |
| 51 | $this->assertEquals( 'no', $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'theme_mods_$current_theme_stylesheet'" ) ); |
| 52 | $this->assertEquals( 'yes', $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'theme_mods_$new_theme_stylesheet'" ) ); |
| 53 | |
| 54 | // Make sure that autoloaded options are cached properly |
| 55 | $autoloaded_options = wp_cache_get( 'alloptions', 'options' ); |
| 56 | $this->assertTrue( array_key_exists( "theme_mods_$new_theme_stylesheet", $autoloaded_options ) ); |
| 57 | $this->assertFalse( array_key_exists( "theme_mods_$current_theme_stylesheet", $autoloaded_options ) ); |
| 58 | |
| 59 | switch_theme( $current_theme_stylesheet ); |
| 60 | |
| 61 | $this->assertEquals( 'yes', $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'theme_mods_$current_theme_stylesheet'" ) ); |
| 62 | $this->assertEquals( 'no', $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = 'theme_mods_$new_theme_stylesheet'" ) ); |
| 63 | |
| 64 | // Make sure that autoloaded options are cached properly |
| 65 | $autoloaded_options = wp_cache_get( 'alloptions', 'options' ); |
| 66 | $this->assertFalse( array_key_exists( "theme_mods_$new_theme_stylesheet", $autoloaded_options ) ); |
| 67 | $this->assertTrue( array_key_exists( "theme_mods_$current_theme_stylesheet", $autoloaded_options ) ); |
| 68 | |
| 69 | // And that we haven't lost the mods |
| 70 | $this->assertEquals( 'a-value', get_theme_mod( 'default-theme-option' ) ); |
| 71 | } |
| 72 | |
| 73 | } |