WPTheme.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @group themes
  4. */
  5. class Tests_Theme_WPTheme extends WP_UnitTestCase {
  6. function setUp() {
  7. parent::setUp();
  8. $this->theme_root = realpath( DIR_TESTDATA . '/themedir1' );
  9. $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
  10. $GLOBALS['wp_theme_directories'] = array( $this->theme_root );
  11. add_filter('theme_root', array($this, '_theme_root'));
  12. add_filter( 'stylesheet_root', array($this, '_theme_root') );
  13. add_filter( 'template_root', array($this, '_theme_root') );
  14. // clear caches
  15. wp_clean_themes_cache();
  16. unset( $GLOBALS['wp_themes'] );
  17. }
  18. function tearDown() {
  19. $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
  20. remove_filter('theme_root', array($this, '_theme_root'));
  21. remove_filter( 'stylesheet_root', array($this, '_theme_root') );
  22. remove_filter( 'template_root', array($this, '_theme_root') );
  23. wp_clean_themes_cache();
  24. unset( $GLOBALS['wp_themes'] );
  25. parent::tearDown();
  26. }
  27. // replace the normal theme root dir with our premade test dir
  28. function _theme_root($dir) {
  29. return $this->theme_root;
  30. }
  31. function test_new_WP_Theme_top_level() {
  32. $theme = new WP_Theme( 'theme1', $this->theme_root );
  33. //Meta
  34. $this->assertEquals( 'My Theme', $theme->get('Name') );
  35. $this->assertEquals( 'http://example.org/',$theme->get('ThemeURI') );
  36. $this->assertEquals( 'An example theme', $theme->get('Description') );
  37. $this->assertEquals( 'Minnie Bannister', $theme->get('Author') );
  38. $this->assertEquals( 'http://example.com/', $theme->get('AuthorURI') );
  39. $this->assertEquals( '1.3', $theme->get('Version') );
  40. $this->assertEquals( '', $theme->get('Template') );
  41. $this->assertEquals( 'publish', $theme->get('Status') );
  42. $this->assertEquals( array(), $theme->get('Tags') );
  43. //Important
  44. $this->assertEquals( 'theme1', $theme->get_stylesheet() );
  45. $this->assertEquals( 'theme1', $theme->get_template() );
  46. }
  47. function test_new_WP_Theme_subdir() {
  48. $theme = new WP_Theme( 'subdir/theme2', $this->theme_root );
  49. //Meta
  50. $this->assertEquals( 'My Subdir Theme', $theme->get('Name') );
  51. $this->assertEquals( 'http://example.org/',$theme->get('ThemeURI') );
  52. $this->assertEquals( 'An example theme in a sub directory', $theme->get('Description') );
  53. $this->assertEquals( 'Mr. WordPress', $theme->get('Author') );
  54. $this->assertEquals( 'http://wordpress.org/', $theme->get('AuthorURI') );
  55. $this->assertEquals( '0.1', $theme->get('Version') );
  56. $this->assertEquals( '', $theme->get('Template') );
  57. $this->assertEquals( 'publish', $theme->get('Status') );
  58. $this->assertEquals( array(), $theme->get('Tags') );
  59. //Important
  60. $this->assertEquals( 'subdir/theme2', $theme->get_stylesheet() );
  61. $this->assertEquals( 'subdir/theme2', $theme->get_template() );
  62. }
  63. /**
  64. * @ticket 20313
  65. */
  66. function test_new_WP_Theme_subdir_bad_root() {
  67. // This is what get_theme_data() does when you pass it a style.css file for a theme in a subdir.
  68. $theme = new WP_Theme( 'theme2', $this->theme_root . '/subdir' );
  69. //Meta
  70. $this->assertEquals( 'My Subdir Theme', $theme->get('Name') );
  71. $this->assertEquals( 'http://example.org/',$theme->get('ThemeURI') );
  72. $this->assertEquals( 'An example theme in a sub directory', $theme->get('Description') );
  73. $this->assertEquals( 'Mr. WordPress', $theme->get('Author') );
  74. $this->assertEquals( 'http://wordpress.org/', $theme->get('AuthorURI') );
  75. $this->assertEquals( '0.1', $theme->get('Version') );
  76. $this->assertEquals( '', $theme->get('Template') );
  77. $this->assertEquals( 'publish', $theme->get('Status') );
  78. $this->assertEquals( array(), $theme->get('Tags') );
  79. //Important
  80. $this->assertEquals( 'subdir/theme2', $theme->get_stylesheet() );
  81. $this->assertEquals( 'subdir/theme2', $theme->get_template() );
  82. }
  83. /**
  84. * @ticket 21749
  85. */
  86. function test_wp_theme_uris_with_spaces() {
  87. $theme = new WP_Theme( 'theme with spaces', $this->theme_root . '/subdir' );
  88. // Make sure subdir/ is considered part of the stylesheet, as we must avoid encoding /'s.
  89. $this->assertEquals( 'subdir/theme with spaces', $theme->get_stylesheet() );
  90. // Check that in a URI path, we have raw url encoding (spaces become %20)
  91. // Don't try to verify the complete URI path. get_theme_root_uri() breaks down quickly.
  92. $this->assertEquals( 'theme%20with%20spaces', basename( $theme->get_stylesheet_directory_uri() ) );
  93. $this->assertEquals( 'theme%20with%20spaces', basename( $theme->get_template_directory_uri() ) );
  94. // Check that wp_customize_url() uses url encoding, as it is a query arg (spaces become +)
  95. $this->assertEquals( admin_url( 'customize.php?theme=theme+with+spaces' ), wp_customize_url( 'theme with spaces' ) );
  96. }
  97. }