Changeset 909 in tests for trunk/tests/theme/themeDir.php
- Timestamp:
- 07/19/2012 01:52:37 AM (14 years ago)
- File:
-
- 1 copied
-
trunk/tests/theme/themeDir.php (copied) (copied from trunk/tests/test_includes_theme.php ) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/theme/themeDir.php
r903 r909 1 1 <?php 2 3 /**4 * test wp-includes/theme.php5 *6 * @group themes7 */8 class TestDefaultThemes extends WP_UnitTestCase {9 10 var $theme_slug = 'twentyeleven';11 var $theme_name = 'Twenty Eleven';12 13 function setUp() {14 parent::setUp();15 add_filter( 'extra_theme_headers', array( $this, '_theme_data_extra_headers' ) );16 wp_clean_themes_cache();17 unset( $GLOBALS['wp_themes'] );18 }19 20 function tearDown() {21 remove_filter( 'extra_theme_headers', array( $this, '_theme_data_extra_headers' ) );22 wp_clean_themes_cache();23 unset( $GLOBALS['wp_themes'] );24 parent::tearDown();25 }26 27 function test_wp_get_themes_default() {28 $themes = wp_get_themes();29 $this->assertInstanceOf( 'WP_Theme', $themes[ $this->theme_slug ] );30 $this->assertEquals( $this->theme_name, $themes[ $this->theme_slug ]->get('Name') );31 32 $single_theme = wp_get_theme( $this->theme_slug );33 $this->assertEquals( $single_theme->get('Name'), $themes[ $this->theme_slug ]->get('Name') );34 $this->assertEquals( $themes[ $this->theme_slug ], $single_theme );35 }36 37 function test_get_themes_default() {38 $themes = get_themes();39 $this->assertInstanceOf( 'WP_Theme', $themes[ $this->theme_name ] );40 $this->assertEquals( $themes[ $this->theme_name ], get_theme( $this->theme_name ) );41 42 $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]['Name'] );43 $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]->Name );44 $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]->name );45 }46 47 function test_get_theme() {48 $themes = get_themes();49 foreach (array_keys($themes) as $name) {50 $theme = get_theme($name);51 // WP_Theme implements ArrayAccess. Even ArrayObject returns false for is_array().52 $this->assertFalse( is_array( $theme ) );53 $this->assertInstanceOf( 'WP_Theme', $theme );54 $this->assertEquals($theme, $themes[$name]);55 }56 }57 58 function test_wp_get_theme() {59 $themes = wp_get_themes();60 foreach ( $themes as $theme ) {61 $this->assertInstanceOf( 'WP_Theme', $theme );62 $this->assertFalse( $theme->errors() );63 $_theme = wp_get_theme( $theme->get_stylesheet() );64 // This primes internal WP_Theme caches for the next assertion (headers_sanitized, textdomain_loaded)65 $this->assertEquals( $theme->get('Name'), $_theme->get('Name') );66 $this->assertEquals( $theme, $_theme );67 }68 }69 70 function test_get_themes_contents() {71 $themes = get_themes();72 73 // Generic tests that should hold true for any theme74 foreach ($themes as $k=>$theme) {75 $this->assertEquals($theme['Name'], $k);76 $this->assertTrue(!empty($theme['Title']));77 78 // important attributes should all be set79 $default_headers = array(80 'Title' => 'Theme Title',81 'Version' => 'Version',82 'Parent Theme' => 'Parent Theme',83 'Template Dir' => 'Template Dir',84 'Stylesheet Dir' => 'Stylesheet Dir',85 'Template' => 'Template',86 'Stylesheet' => 'Stylesheet',87 'Screenshot' => 'Screenshot',88 'Description' => 'Description',89 'Author' => 'Author',90 'Tags' => 'Tags',91 // Introduced in WordPress 2.992 'Theme Root' => 'Theme Root',93 'Theme Root URI' => 'Theme Root URI'94 );95 foreach ($default_headers as $name => $value) {96 $this->assertTrue(isset($theme[$name]));97 }98 99 // Make the tests work both for WordPress 2.8.5 and WordPress 2.9-rare100 $dir = isset($theme['Theme Root']) ? '' : WP_CONTENT_DIR;101 102 // important attributes should all not be empty as well103 $this->assertTrue(!empty($theme['Description']));104 $this->assertTrue(!empty($theme['Author']));105 $this->assertTrue(is_numeric($theme['Version']));106 $this->assertTrue(!empty($theme['Template']));107 $this->assertTrue(!empty($theme['Stylesheet']));108 109 // template files should all exist110 $this->assertTrue(is_array($theme['Template Files']));111 $this->assertTrue(count($theme['Template Files']) > 0);112 foreach ($theme['Template Files'] as $file) {113 $this->assertTrue(is_file($dir . $file));114 $this->assertTrue(is_readable($dir . $file));115 }116 117 // css files should all exist118 $this->assertTrue(is_array($theme['Stylesheet Files']));119 $this->assertTrue(count($theme['Stylesheet Files']) > 0);120 foreach ($theme['Stylesheet Files'] as $file) {121 $this->assertTrue(is_file($dir . $file));122 $this->assertTrue(is_readable($dir . $file));123 }124 125 $this->assertTrue(is_dir($dir . $theme['Template Dir']));126 $this->assertTrue(is_dir($dir . $theme['Stylesheet Dir']));127 128 $this->assertEquals('publish', $theme['Status']);129 130 $this->assertTrue(is_file($dir . $theme['Stylesheet Dir'] . '/' . $theme['Screenshot']));131 $this->assertTrue(is_readable($dir . $theme['Stylesheet Dir'] . '/' . $theme['Screenshot']));132 }133 }134 135 /**136 * @ticket 20897137 */138 function test_extra_theme_headers() {139 $wp_theme = wp_get_theme( $this->theme_slug );140 $this->assertNotEmpty( $wp_theme->get('License') );141 $path_to_style_css = $wp_theme->get_theme_root() . '/' . $wp_theme->get_stylesheet() . '/style.css';142 $this->assertTrue( file_exists( $path_to_style_css ) );143 $theme_data = get_theme_data( $path_to_style_css );144 $this->assertArrayHasKey( 'License', $theme_data );145 $this->assertArrayNotHasKey( 'Not a Valid Key', $theme_data );146 $this->assertNotEmpty( $theme_data['License'] );147 $this->assertSame( $theme_data['License'], $wp_theme->get('License') );148 }149 150 function _theme_data_extra_headers() {151 return array( 'License' );152 }153 154 function test_switch_theme() {155 $themes = get_themes();156 157 $switch_theme_one_argument = version_compare( $GLOBALS['wp_version'], '3.5-alpha-21103', '>=' );158 159 // switch to each theme in sequence160 // do it twice to make sure we switch to the first theme, even if it's our starting theme161 for ( $i = 0; $i < 3; $i++ ) {162 foreach ($themes as $name=>$theme) {163 // switch to this theme164 if ( $i === 2 || $switch_theme_one_argument )165 switch_theme( $theme['Template'], $theme['Stylesheet'] );166 else167 switch_theme( $theme['Stylesheet'] );168 169 $this->assertEquals($name, get_current_theme());170 171 // make sure the various get_* functions return the correct values172 $this->assertEquals($theme['Template'], get_template());173 $this->assertEquals($theme['Stylesheet'], get_stylesheet());174 175 $root_fs = get_theme_root();176 $this->assertTrue(is_dir($root_fs));177 178 $root_uri = get_theme_root_uri();179 $this->assertTrue(!empty($root_uri));180 181 $this->assertEquals($root_fs . '/' . get_stylesheet(), get_stylesheet_directory());182 $this->assertEquals($root_uri . '/' . get_stylesheet(), get_stylesheet_directory_uri());183 $this->assertEquals($root_uri . '/' . get_stylesheet() . '/style.css', get_stylesheet_uri());184 # $this->assertEquals($root_uri . '/' . get_stylesheet(), get_locale_stylesheet_uri());185 186 $this->assertEquals($root_fs . '/' . get_template(), get_template_directory());187 $this->assertEquals($root_uri . '/' . get_template(), get_template_directory_uri());188 189 //get_query_template190 191 // template file that doesn't exist192 $this->assertEquals('', get_query_template(rand_str()));193 194 // template files that do exist195 //foreach ($theme['Template Files'] as $path) {196 //$file = basename($path, '.php');197 // FIXME: untestable because get_query_template uses TEMPLATEPATH198 //$this->assertEquals('', get_query_template($file));199 //}200 201 // these are kind of tautologies but at least exercise the code202 $this->assertEquals(get_404_template(), get_query_template('404'));203 $this->assertEquals(get_archive_template(), get_query_template('archive'));204 $this->assertEquals(get_author_template(), get_query_template('author'));205 $this->assertEquals(get_category_template(), get_query_template('category'));206 $this->assertEquals(get_date_template(), get_query_template('date'));207 $this->assertEquals(get_home_template(), get_query_template('home', array('home.php','index.php')));208 $this->assertEquals(get_page_template(), get_query_template('page'));209 $this->assertEquals(get_paged_template(), get_query_template('paged'));210 $this->assertEquals(get_search_template(), get_query_template('search'));211 $this->assertEquals(get_single_template(), get_query_template('single'));212 $this->assertEquals(get_attachment_template(), get_query_template('attachment'));213 214 // this one doesn't behave like the others215 if (get_query_template('comments-popup'))216 $this->assertEquals(get_comments_popup_template(), get_query_template('comments-popup'));217 else218 $this->assertEquals(get_comments_popup_template(), ABSPATH.'wp-includes/theme-compat/comments-popup.php');219 220 $this->assertEquals(get_tag_template(), get_query_template('tag'));221 222 // nb: this probably doesn't run because WP_INSTALLING is defined223 $this->assertTrue(validate_current_theme());224 }225 }226 }227 228 function test_switch_theme_bogus() {229 // try switching to a theme that doesn't exist230 $template = rand_str();231 $style = rand_str();232 update_option('template', $template);233 update_option('stylesheet', $style);234 235 $theme = wp_get_theme();236 $this->assertEquals( $style, (string) $theme );237 $this->assertNotSame( false, $theme->errors() );238 $this->assertFalse( $theme->exists() );239 240 // these return the bogus name - perhaps not ideal behaviour?241 $this->assertEquals($template, get_template());242 $this->assertEquals($style, get_stylesheet());243 }244 }245 2 246 3 /** … … 249 6 * @group themes 250 7 */ 251 class Test ThemeDir extends WP_UnitTestCase {8 class Tests_Theme_ThemeDir extends WP_UnitTestCase { 252 9 function setUp() { 253 10 parent::setUp(); … … 444 201 445 202 } 446 447 /**448 * @group themes449 */450 class TestLargeThemeDir extends WP_UnitTestCase {451 function setUp() {452 parent::setUp();453 $this->theme_root = DIR_TESTDATA . '/wpcom-themes';454 455 $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];456 $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );457 458 add_filter('theme_root', array(&$this, '_theme_root'));459 460 // clear caches461 wp_clean_themes_cache();462 unset( $GLOBALS['wp_themes'] );463 }464 465 function tearDown() {466 $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;467 remove_filter('theme_root', array(&$this, '_theme_root'));468 wp_clean_themes_cache();469 unset( $GLOBALS['wp_themes'] );470 parent::tearDown();471 }472 473 // replace the normal theme root dir with our premade test dir474 function _theme_root($dir) {475 return $this->theme_root;476 }477 478 function _filter_out_themes_not_in_root( &$themes ) {479 foreach ( $themes as $key => $theme ) {480 if ( $theme->get_theme_root() != $this->theme_root )481 unset( $themes[ $key ] );482 }483 }484 485 function test_theme_list() {486 $themes = get_themes();487 $this->_filter_out_themes_not_in_root( $themes );488 $theme_names = array_keys( $themes );489 $this->assertEquals(87, count( $theme_names ) );490 $length = strlen( serialize( $themes ) );491 492 //2.9 pre [12226]493 $this->assertLessThanOrEqual(387283, $length );494 //2.8.5495 $this->assertLessThanOrEqual(368319, $length );496 //2.9 post [12226]497 $this->assertLessThanOrEqual(261998, $length );498 //3.4 post [20029], #20103499 $this->assertLessThanOrEqual(100000, $length );500 }501 502 /**503 * Reducing in-memory size further.504 *505 * @ticket 11214506 */507 function test_smaller_storage() {508 $themes = get_themes();509 $this->_filter_out_themes_not_in_root( $themes );510 $theme_names = array_keys($themes);511 $this->assertEquals(87, count($theme_names));512 $this->assertLessThanOrEqual(136342, strlen(serialize($themes)));513 }514 }515 516 /**517 * @group themes518 */519 class TestThemeSupport extends WP_UnitTestCase {520 function setUp() {521 parent::setUp();522 }523 524 function tearDown() {525 parent::tearDown();526 }527 528 function test_the_basics() {529 add_theme_support( 'automatic-feed-links' );530 $this->assertTrue( current_theme_supports( 'automatic-feed-links' ) );531 remove_theme_support( 'automatic-feed-links' );532 $this->assertFalse( current_theme_supports( 'automatic-feed-links' ) );533 add_theme_support( 'automatic-feed-links' );534 $this->assertTrue( current_theme_supports( 'automatic-feed-links' ) );535 }536 537 function test_admin_bar() {538 add_theme_support( 'admin-bar' );539 $this->assertTrue( current_theme_supports( 'admin-bar' ) );540 remove_theme_support( 'admin-bar' );541 $this->assertFalse( current_theme_supports( 'admin-bar' ) );542 add_theme_support( 'admin-bar' );543 $this->assertTrue( current_theme_supports( 'admin-bar' ) );544 545 add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );546 $this->assertTrue( current_theme_supports( 'admin-bar' ) );547 548 $this->assertEquals(549 array( 0 => array( 'callback' => '__return_false' ) ),550 get_theme_support( 'admin-bar' )551 );552 remove_theme_support( 'admin-bar' );553 $this->assertFalse( current_theme_supports( 'admin-bar' ) );554 $this->assertFalse( get_theme_support( 'admin-bar' ) );555 }556 557 function test_post_thumbnails() {558 add_theme_support( 'post-thumbnails' );559 $this->assertTrue( current_theme_supports( 'post-thumbnails' ) );560 remove_theme_support( 'post-thumbnails' );561 $this->assertFalse( current_theme_supports( 'post-thumbnails' ) );562 add_theme_support( 'post-thumbnails' );563 $this->assertTrue( current_theme_supports( 'post-thumbnails' ) );564 565 // simple array of post types.566 add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );567 $this->assertTrue( current_theme_supports( 'post-thumbnails' ) );568 $this->assertTrue( current_theme_supports( 'post-thumbnails', 'post' ) );569 $this->assertFalse( current_theme_supports( 'post-thumbnails', 'book' ) );570 remove_theme_support( 'post-thumbnails' );571 $this->assertFalse( current_theme_supports( 'post-thumbnails' ) );572 573 #WP18548574 if ( ! function_exists( '_wp_render_title_tag' ) )575 return;576 577 // array of arguments, with the key of 'types' holding the post types.578 add_theme_support( 'post-thumbnails', array( 'types' => array( 'post', 'page' ) ) );579 $this->assertTrue( current_theme_supports( 'post-thumbnails' ) );580 $this->assertTrue( current_theme_supports( 'post-thumbnails', 'post' ) );581 $this->assertFalse( current_theme_supports( 'post-thumbnails', 'book' ) );582 remove_theme_support( 'post-thumbnails' );583 $this->assertFalse( current_theme_supports( 'post-thumbnails' ) );584 585 // array of arguments, with the key of 'types' holding the post types.586 add_theme_support( 'post-thumbnails', array( 'types' => true ) );587 $this->assertTrue( current_theme_supports( 'post-thumbnails' ) );588 $this->assertTrue( current_theme_supports( 'post-thumbnails', rand_str() ) ); // any type589 remove_theme_support( 'post-thumbnails' );590 $this->assertFalse( current_theme_supports( 'post-thumbnails' ) );591 592 // array of arguments, with some other argument, and no 'types' argument.593 add_theme_support( 'post-thumbnails', array( rand_str() => rand_str() ) );594 $this->assertTrue( current_theme_supports( 'post-thumbnails' ) );595 $this->assertTrue( current_theme_supports( 'post-thumbnails', rand_str() ) ); // any type596 remove_theme_support( 'post-thumbnails' );597 $this->assertFalse( current_theme_supports( 'post-thumbnails' ) );598 599 }600 601 function supports_foobar( $yesno, $args, $feature ) {602 if ( $args[0] == $feature[0] )603 return true;604 return false;605 }606 607 function test_plugin_hook() {608 $this->assertFalse( current_theme_supports( 'foobar' ) );609 add_theme_support( 'foobar' );610 $this->assertTrue( current_theme_supports( 'foobar' ) );611 612 add_filter( 'current_theme_supports-foobar', array( $this, 'supports_foobar'), 10, 3 );613 614 add_theme_support( 'foobar', 'bar' );615 $this->assertFalse( current_theme_supports( 'foobar', 'foo' ) );616 $this->assertTrue( current_theme_supports( 'foobar', 'bar' ) );617 618 remove_theme_support( 'foobar' );619 $this->assertFalse( current_theme_supports( 'foobar', 'bar' ) );620 }621 }
Note:
See TracChangeset
for help on using the changeset viewer.