theme.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * test wp-includes/theme.php
  4. *
  5. * @group themes
  6. */
  7. class Tests_Theme extends WP_UnitTestCase {
  8. protected $theme_slug = 'twentyeleven';
  9. protected $theme_name = 'Twenty Eleven';
  10. protected $default_themes = array( 'twentyten', 'twentyeleven', 'twentytwelve', 'twentythirteen', 'twentyfourteen' );
  11. function setUp() {
  12. parent::setUp();
  13. add_filter( 'extra_theme_headers', array( $this, '_theme_data_extra_headers' ) );
  14. wp_clean_themes_cache();
  15. unset( $GLOBALS['wp_themes'] );
  16. }
  17. function tearDown() {
  18. remove_filter( 'extra_theme_headers', array( $this, '_theme_data_extra_headers' ) );
  19. wp_clean_themes_cache();
  20. unset( $GLOBALS['wp_themes'] );
  21. parent::tearDown();
  22. }
  23. function test_wp_get_themes_default() {
  24. $themes = wp_get_themes();
  25. $this->assertInstanceOf( 'WP_Theme', $themes[ $this->theme_slug ] );
  26. $this->assertEquals( $this->theme_name, $themes[ $this->theme_slug ]->get('Name') );
  27. $single_theme = wp_get_theme( $this->theme_slug );
  28. $this->assertEquals( $single_theme->get('Name'), $themes[ $this->theme_slug ]->get('Name') );
  29. $this->assertEquals( $themes[ $this->theme_slug ], $single_theme );
  30. }
  31. /**
  32. * @expectedDeprecated get_theme
  33. * @expectedDeprecated get_themes
  34. */
  35. function test_get_themes_default() {
  36. $themes = get_themes();
  37. $this->assertInstanceOf( 'WP_Theme', $themes[ $this->theme_name ] );
  38. $this->assertEquals( $themes[ $this->theme_name ], get_theme( $this->theme_name ) );
  39. $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]['Name'] );
  40. $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]->Name );
  41. $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]->name );
  42. }
  43. /**
  44. * @expectedDeprecated get_theme
  45. * @expectedDeprecated get_themes
  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. function test_wp_get_theme() {
  58. $themes = wp_get_themes();
  59. foreach ( $themes as $theme ) {
  60. $this->assertInstanceOf( 'WP_Theme', $theme );
  61. $this->assertFalse( $theme->errors() );
  62. $_theme = wp_get_theme( $theme->get_stylesheet() );
  63. // This primes internal WP_Theme caches for the next assertion (headers_sanitized, textdomain_loaded)
  64. $this->assertEquals( $theme->get('Name'), $_theme->get('Name') );
  65. $this->assertEquals( $theme, $_theme );
  66. }
  67. }
  68. /**
  69. * @expectedDeprecated get_themes
  70. */
  71. function test_get_themes_contents() {
  72. $themes = get_themes();
  73. // Generic tests that should hold true for any theme
  74. foreach ( $themes as $k => $theme ) {
  75. $this->assertEquals( $theme['Name'], $k );
  76. $this->assertNotEmpty( $theme['Title'] );
  77. // important attributes should all be set
  78. $default_headers = array(
  79. 'Title' => 'Theme Title',
  80. 'Version' => 'Version',
  81. 'Parent Theme' => 'Parent Theme',
  82. 'Template Dir' => 'Template Dir',
  83. 'Stylesheet Dir' => 'Stylesheet Dir',
  84. 'Template' => 'Template',
  85. 'Stylesheet' => 'Stylesheet',
  86. 'Screenshot' => 'Screenshot',
  87. 'Description' => 'Description',
  88. 'Author' => 'Author',
  89. 'Tags' => 'Tags',
  90. // Introduced in WordPress 2.9
  91. 'Theme Root' => 'Theme Root',
  92. 'Theme Root URI' => 'Theme Root URI'
  93. );
  94. foreach ($default_headers as $name => $value) {
  95. $this->assertTrue(isset($theme[$name]));
  96. }
  97. // Make the tests work both for WordPress 2.8.5 and WordPress 2.9-rare
  98. $dir = isset($theme['Theme Root']) ? '' : WP_CONTENT_DIR;
  99. // important attributes should all not be empty as well
  100. $this->assertNotEmpty( $theme['Description'] );
  101. $this->assertNotEmpty( $theme['Author'] );
  102. $this->assertTrue(version_compare($theme['Version'], 0) > 0);
  103. $this->assertNotEmpty( $theme['Template'] );
  104. $this->assertNotEmpty( $theme['Stylesheet'] );
  105. // template files should all exist
  106. $this->assertTrue(is_array($theme['Template Files']));
  107. $this->assertTrue(count($theme['Template Files']) > 0);
  108. foreach ($theme['Template Files'] as $file) {
  109. $this->assertTrue(is_file($dir . $file));
  110. $this->assertTrue(is_readable($dir . $file));
  111. }
  112. // css files should all exist
  113. $this->assertTrue(is_array($theme['Stylesheet Files']));
  114. $this->assertTrue(count($theme['Stylesheet Files']) > 0);
  115. foreach ($theme['Stylesheet Files'] as $file) {
  116. $this->assertTrue(is_file($dir . $file));
  117. $this->assertTrue(is_readable($dir . $file));
  118. }
  119. $this->assertTrue(is_dir($dir . $theme['Template Dir']));
  120. $this->assertTrue(is_dir($dir . $theme['Stylesheet Dir']));
  121. $this->assertEquals('publish', $theme['Status']);
  122. $this->assertTrue(is_file($dir . $theme['Stylesheet Dir'] . '/' . $theme['Screenshot']));
  123. $this->assertTrue(is_readable($dir . $theme['Stylesheet Dir'] . '/' . $theme['Screenshot']));
  124. }
  125. }
  126. function test_wp_get_theme_contents() {
  127. $theme = wp_get_theme( $this->theme_slug );
  128. $this->assertEquals( $this->theme_name, $theme->get( 'Name' ) );
  129. $this->assertNotEmpty( $theme->get( 'Description' ) );
  130. $this->assertNotEmpty( $theme->get( 'Author' ) );
  131. $this->assertNotEmpty( $theme->get( 'Version' ) );
  132. $this->assertNotEmpty( $theme->get( 'AuthorURI' ) );
  133. $this->assertNotEmpty( $theme->get( 'ThemeURI' ) );
  134. $this->assertEquals( $this->theme_slug, $theme->get_stylesheet() );
  135. $this->assertEquals( $this->theme_slug, $theme->get_template() );
  136. $this->assertEquals('publish', $theme->get( 'Status' ) );
  137. $this->assertEquals( WP_CONTENT_DIR . '/themes/' . $this->theme_slug, $theme->get_stylesheet_directory(), 'get_stylesheet_directory' );
  138. $this->assertEquals( WP_CONTENT_DIR . '/themes/' . $this->theme_slug, $theme->get_template_directory(), 'get_template_directory' );
  139. $this->assertEquals( content_url( 'themes/' . $this->theme_slug ), $theme->get_stylesheet_directory_uri(), 'get_stylesheet_directory_uri' );
  140. $this->assertEquals( content_url( 'themes/' . $this->theme_slug ), $theme->get_template_directory_uri(), 'get_template_directory_uri' );
  141. }
  142. function test_default_themes_have_textdomain() {
  143. $this->assertContains( WP_DEFAULT_THEME, $this->default_themes );
  144. foreach ( $this->default_themes as $theme ) {
  145. $this->assertEquals( $theme, wp_get_theme( $theme )->get( 'TextDomain' ) );
  146. }
  147. }
  148. /**
  149. * @ticket 20897
  150. * @expectedDeprecated get_theme_data
  151. */
  152. function test_extra_theme_headers() {
  153. $wp_theme = wp_get_theme( $this->theme_slug );
  154. $this->assertNotEmpty( $wp_theme->get('License') );
  155. $path_to_style_css = $wp_theme->get_theme_root() . '/' . $wp_theme->get_stylesheet() . '/style.css';
  156. $this->assertTrue( file_exists( $path_to_style_css ) );
  157. $theme_data = get_theme_data( $path_to_style_css );
  158. $this->assertArrayHasKey( 'License', $theme_data );
  159. $this->assertArrayNotHasKey( 'Not a Valid Key', $theme_data );
  160. $this->assertNotEmpty( $theme_data['License'] );
  161. $this->assertSame( $theme_data['License'], $wp_theme->get('License') );
  162. }
  163. function _theme_data_extra_headers() {
  164. return array( 'License' );
  165. }
  166. /**
  167. * @expectedDeprecated get_themes
  168. * @expectedDeprecated get_current_theme
  169. */
  170. function test_switch_theme() {
  171. $themes = get_themes();
  172. // Switch to each theme in sequence.
  173. // Do it twice to make sure we switch to the first theme, even if it's our starting theme.
  174. // Do it a third time to ensure switch_theme() works with one argument.
  175. for ( $i = 0; $i < 3; $i++ ) {
  176. foreach ( $themes as $name => $theme ) {
  177. // switch to this theme
  178. if ( $i === 2 )
  179. switch_theme( $theme['Template'], $theme['Stylesheet'] );
  180. else
  181. switch_theme( $theme['Stylesheet'] );
  182. $this->assertEquals($name, get_current_theme());
  183. // make sure the various get_* functions return the correct values
  184. $this->assertEquals($theme['Template'], get_template());
  185. $this->assertEquals($theme['Stylesheet'], get_stylesheet());
  186. $root_fs = get_theme_root();
  187. $this->assertTrue(is_dir($root_fs));
  188. $root_uri = get_theme_root_uri();
  189. $this->assertTrue(!empty($root_uri));
  190. $this->assertEquals($root_fs . '/' . get_stylesheet(), get_stylesheet_directory());
  191. $this->assertEquals($root_uri . '/' . get_stylesheet(), get_stylesheet_directory_uri());
  192. $this->assertEquals($root_uri . '/' . get_stylesheet() . '/style.css', get_stylesheet_uri());
  193. # $this->assertEquals($root_uri . '/' . get_stylesheet(), get_locale_stylesheet_uri());
  194. $this->assertEquals($root_fs . '/' . get_template(), get_template_directory());
  195. $this->assertEquals($root_uri . '/' . get_template(), get_template_directory_uri());
  196. //get_query_template
  197. // template file that doesn't exist
  198. $this->assertEquals('', get_query_template(rand_str()));
  199. // template files that do exist
  200. //foreach ($theme['Template Files'] as $path) {
  201. //$file = basename($path, '.php');
  202. // FIXME: untestable because get_query_template uses TEMPLATEPATH
  203. //$this->assertEquals('', get_query_template($file));
  204. //}
  205. // these are kind of tautologies but at least exercise the code
  206. $this->assertEquals(get_404_template(), get_query_template('404'));
  207. $this->assertEquals(get_archive_template(), get_query_template('archive'));
  208. $this->assertEquals(get_author_template(), get_query_template('author'));
  209. $this->assertEquals(get_category_template(), get_query_template('category'));
  210. $this->assertEquals(get_date_template(), get_query_template('date'));
  211. $this->assertEquals(get_home_template(), get_query_template('home', array('home.php','index.php')));
  212. $this->assertEquals(get_page_template(), get_query_template('page'));
  213. $this->assertEquals(get_paged_template(), get_query_template('paged'));
  214. $this->assertEquals(get_search_template(), get_query_template('search'));
  215. $this->assertEquals(get_single_template(), get_query_template('single'));
  216. $this->assertEquals(get_attachment_template(), get_query_template('attachment'));
  217. // this one doesn't behave like the others
  218. if (get_query_template('comments-popup'))
  219. $this->assertEquals(get_comments_popup_template(), get_query_template('comments-popup'));
  220. else
  221. $this->assertEquals(get_comments_popup_template(), ABSPATH.'wp-includes/theme-compat/comments-popup.php');
  222. $this->assertEquals(get_tag_template(), get_query_template('tag'));
  223. // nb: this probably doesn't run because WP_INSTALLING is defined
  224. $this->assertTrue(validate_current_theme());
  225. }
  226. }
  227. }
  228. function test_switch_theme_bogus() {
  229. // try switching to a theme that doesn't exist
  230. $template = rand_str();
  231. $style = rand_str();
  232. update_option('template', $template);
  233. update_option('stylesheet', $style);
  234. $theme = wp_get_theme();
  235. $this->assertEquals( $style, (string) $theme );
  236. $this->assertNotSame( false, $theme->errors() );
  237. $this->assertFalse( $theme->exists() );
  238. // these return the bogus name - perhaps not ideal behaviour?
  239. $this->assertEquals($template, get_template());
  240. $this->assertEquals($style, get_stylesheet());
  241. }
  242. }