themeDir.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Test functions that fetch stuff from the theme directory
  4. *
  5. * @group themes
  6. */
  7. class Tests_Theme_ThemeDir extends WP_UnitTestCase {
  8. function setUp() {
  9. parent::setUp();
  10. $this->theme_root = DIR_TESTDATA . '/themedir1';
  11. $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
  12. // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
  13. $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
  14. add_filter('theme_root', array($this, '_theme_root'));
  15. add_filter( 'stylesheet_root', array($this, '_theme_root') );
  16. add_filter( 'template_root', array($this, '_theme_root') );
  17. // clear caches
  18. wp_clean_themes_cache();
  19. unset( $GLOBALS['wp_themes'] );
  20. }
  21. function tearDown() {
  22. $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
  23. remove_filter('theme_root', array($this, '_theme_root'));
  24. remove_filter( 'stylesheet_root', array($this, '_theme_root') );
  25. remove_filter( 'template_root', array($this, '_theme_root') );
  26. wp_clean_themes_cache();
  27. unset( $GLOBALS['wp_themes'] );
  28. parent::tearDown();
  29. }
  30. // replace the normal theme root dir with our premade test dir
  31. function _theme_root($dir) {
  32. return $this->theme_root;
  33. }
  34. /**
  35. * @expectedDeprecated get_theme
  36. * @expectedDeprecated get_themes
  37. */
  38. function test_theme_default() {
  39. $themes = get_themes();
  40. $theme = get_theme('WordPress Default');
  41. $this->assertEquals( $themes['WordPress Default'], $theme );
  42. $this->assertFalse( empty($theme) );
  43. #echo gen_tests_array('theme', $theme);
  44. $this->assertEquals( 'WordPress Default', $theme['Name'] );
  45. $this->assertEquals( 'WordPress Default', $theme['Title'] );
  46. $this->assertEquals( 'The default WordPress theme based on the famous <a href="http://binarybonsai.com/kubrick/">Kubrick</a>.', $theme['Description'] );
  47. $this->assertEquals( '<a href="http://binarybonsai.com/" title="Visit author homepage">Michael Heilemann</a>', $theme['Author'] );
  48. $this->assertEquals( '1.6', $theme['Version'] );
  49. $this->assertEquals( 'default', $theme['Template'] );
  50. $this->assertEquals( 'default', $theme['Stylesheet'] );
  51. $this->assertContains( $this->theme_root . '/default/functions.php', $theme['Template Files'] );
  52. $this->assertContains( $this->theme_root . '/default/index.php', $theme['Template Files'] );
  53. $this->assertContains( $this->theme_root . '/default/style.css', $theme['Stylesheet Files'] );
  54. $this->assertEquals( $this->theme_root.'/default', $theme['Template Dir'] );
  55. $this->assertEquals( $this->theme_root.'/default', $theme['Stylesheet Dir'] );
  56. $this->assertEquals( 'publish', $theme['Status'] );
  57. $this->assertEquals( '', $theme['Parent Theme'] );
  58. }
  59. /**
  60. * @expectedDeprecated get_theme
  61. * @expectedDeprecated get_themes
  62. */
  63. function test_theme_sandbox() {
  64. $theme = get_theme('Sandbox');
  65. $this->assertFalse( empty($theme) );
  66. #echo gen_tests_array('theme', $theme);
  67. $this->assertEquals( 'Sandbox', $theme['Name'] );
  68. $this->assertEquals( 'Sandbox', $theme['Title'] );
  69. $this->assertEquals( 'A theme with powerful, semantic CSS selectors and the ability to add new skins.', $theme['Description'] );
  70. $this->assertEquals( '<a href="http://andy.wordpress.com/">Andy Skelton</a> &amp; <a href="http://www.plaintxt.org/">Scott Allan Wallick</a>', $theme['Author'] );
  71. $this->assertEquals( '0.6.1-wpcom', $theme['Version'] );
  72. $this->assertEquals( 'sandbox', $theme['Template'] );
  73. $this->assertEquals( 'sandbox', $theme['Stylesheet'] );
  74. $template_files = $theme['Template Files'];
  75. $this->assertEquals( $this->theme_root.'/sandbox/functions.php', reset( $template_files ) );
  76. $this->assertEquals( $this->theme_root.'/sandbox/index.php', next( $template_files ) );
  77. $stylesheet_files = $theme['Stylesheet Files'];
  78. $this->assertEquals( $this->theme_root.'/sandbox/style.css', reset( $stylesheet_files ) );
  79. $this->assertEquals( $this->theme_root.'/sandbox', $theme['Template Dir'] );
  80. $this->assertEquals( $this->theme_root.'/sandbox', $theme['Stylesheet Dir'] );
  81. $this->assertEquals( 'publish', $theme['Status'] );
  82. $this->assertEquals( '', $theme['Parent Theme'] );
  83. }
  84. /**
  85. * A CSS-only theme
  86. *
  87. * @expectedDeprecated get_themes
  88. */
  89. function test_theme_stylesheet_only() {
  90. $themes = get_themes();
  91. $theme = $themes['Stylesheet Only'];
  92. $this->assertFalse( empty($theme) );
  93. #echo gen_tests_array('theme', $theme);
  94. $this->assertEquals( 'Stylesheet Only', $theme['Name'] );
  95. $this->assertEquals( 'Stylesheet Only', $theme['Title'] );
  96. $this->assertEquals( 'A three-column widget-ready theme in dark blue.', $theme['Description'] );
  97. $this->assertEquals( '<a href="http://www.example.com/" title="Visit author homepage">Henry Crun</a>', $theme['Author'] );
  98. $this->assertEquals( '1.0', $theme['Version'] );
  99. $this->assertEquals( 'sandbox', $theme['Template'] );
  100. $this->assertEquals( 'stylesheetonly', $theme['Stylesheet'] );
  101. $this->assertContains( $this->theme_root.'/sandbox/functions.php', $theme['Template Files'] );
  102. $this->assertContains( $this->theme_root.'/sandbox/index.php', $theme['Template Files'] );
  103. $this->assertContains( $this->theme_root.'/stylesheetonly/style.css', $theme['Stylesheet Files']);
  104. $this->assertEquals( $this->theme_root.'/sandbox', $theme['Template Dir'] );
  105. $this->assertEquals( $this->theme_root.'/stylesheetonly', $theme['Stylesheet Dir'] );
  106. $this->assertEquals( 'publish', $theme['Status'] );
  107. $this->assertEquals( 'Sandbox', $theme['Parent Theme'] );
  108. }
  109. /**
  110. * @expectedDeprecated get_themes
  111. */
  112. function test_theme_list() {
  113. $themes = get_themes();
  114. // Ignore themes in the default /themes directory.
  115. foreach ( $themes as $theme_name => $theme ) {
  116. if ( $theme->get_theme_root() != $this->theme_root )
  117. unset( $themes[ $theme_name ] );
  118. }
  119. $theme_names = array_keys($themes);
  120. $expected = array(
  121. 'WordPress Default',
  122. 'Sandbox',
  123. 'Stylesheet Only',
  124. 'My Theme',
  125. 'My Theme/theme1', // duplicate theme should be given a unique name
  126. 'My Subdir Theme',// theme in a subdirectory should work
  127. 'Page Template Theme', // theme with page templates for other test code
  128. 'Theme with Spaces in the Directory',
  129. );
  130. sort($theme_names);
  131. sort($expected);
  132. $this->assertEquals($expected, $theme_names);
  133. }
  134. /**
  135. * @expectedDeprecated get_themes
  136. * @expectedDeprecated get_broken_themes
  137. */
  138. function test_broken_themes() {
  139. $themes = get_themes();
  140. $expected = array('broken-theme' => array('Name' => 'broken-theme', 'Title' => 'broken-theme', 'Description' => __('Stylesheet is missing.')));
  141. $this->assertEquals($expected, get_broken_themes() );
  142. }
  143. function test_wp_get_theme_with_non_default_theme_root() {
  144. $this->assertFalse( wp_get_theme( 'sandbox', $this->theme_root )->errors() );
  145. $this->assertFalse( wp_get_theme( 'sandbox' )->errors() );
  146. }
  147. /**
  148. * @expectedDeprecated get_themes
  149. */
  150. function test_page_templates() {
  151. $themes = get_themes();
  152. $theme = $themes['Page Template Theme'];
  153. $this->assertFalse( empty($theme) );
  154. $templates = $theme['Template Files'];
  155. $this->assertTrue( in_array( $this->theme_root . '/page-templates/template-top-level.php', $templates));
  156. }
  157. /**
  158. * @expectedDeprecated get_theme_data
  159. */
  160. function test_get_theme_data_top_level() {
  161. $theme_data = get_theme_data( DIR_TESTDATA . '/themedir1/theme1/style.css' );
  162. $this->assertEquals( 'My Theme', $theme_data['Name'] );
  163. $this->assertEquals( 'http://example.org/', $theme_data['URI'] );
  164. $this->assertEquals( 'An example theme', $theme_data['Description'] );
  165. $this->assertEquals( '<a href="http://example.com/" title="Visit author homepage">Minnie Bannister</a>', $theme_data['Author'] );
  166. $this->assertEquals( 'http://example.com/', $theme_data['AuthorURI'] );
  167. $this->assertEquals( '1.3', $theme_data['Version'] );
  168. $this->assertEquals( '', $theme_data['Template'] );
  169. $this->assertEquals( 'publish', $theme_data['Status'] );
  170. $this->assertEquals( array(), $theme_data['Tags'] );
  171. $this->assertEquals( 'My Theme', $theme_data['Title'] );
  172. $this->assertEquals( 'Minnie Bannister', $theme_data['AuthorName'] );
  173. }
  174. /**
  175. * @expectedDeprecated get_theme_data
  176. */
  177. function test_get_theme_data_subdir() {
  178. $theme_data = get_theme_data( $this->theme_root . '/subdir/theme2/style.css' );
  179. $this->assertEquals( 'My Subdir Theme', $theme_data['Name'] );
  180. $this->assertEquals( 'http://example.org/', $theme_data['URI'] );
  181. $this->assertEquals( 'An example theme in a sub directory', $theme_data['Description'] );
  182. $this->assertEquals( '<a href="http://wordpress.org/" title="Visit author homepage">Mr. WordPress</a>', $theme_data['Author'] );
  183. $this->assertEquals( 'http://wordpress.org/', $theme_data['AuthorURI'] );
  184. $this->assertEquals( '0.1', $theme_data['Version'] );
  185. $this->assertEquals( '', $theme_data['Template'] );
  186. $this->assertEquals( 'publish', $theme_data['Status'] );
  187. $this->assertEquals( array(), $theme_data['Tags'] );
  188. $this->assertEquals( 'My Subdir Theme', $theme_data['Title'] );
  189. $this->assertEquals( 'Mr. WordPress', $theme_data['AuthorName'] );
  190. }
  191. }