ManageThemes.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Admin Ajax functions to be tested.
  4. */
  5. require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
  6. /**
  7. * Testing Ajax handler for instlaling, updating, and deleting themes.
  8. *
  9. * @group ajax
  10. */
  11. class Tests_Ajax_Manage_Themes extends WP_Ajax_UnitTestCase {
  12. private $orig_theme_dir;
  13. private $theme_root;
  14. function setUp() {
  15. parent::setUp();
  16. $this->theme_root = DIR_TESTDATA . '/themedir1';
  17. $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
  18. // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
  19. $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
  20. add_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
  21. add_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) );
  22. add_filter( 'template_root', array( $this, 'filter_theme_root' ) );
  23. wp_clean_themes_cache();
  24. unset( $GLOBALS['wp_themes'] );
  25. }
  26. function tearDown() {
  27. $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
  28. remove_filter( 'theme_root', array( $this, 'filter_theme_root' ) );
  29. remove_filter( 'stylesheet_root', array( $this, 'filter_theme_root' ) );
  30. remove_filter( 'template_root', array( $this, 'filter_theme_root' ) );
  31. wp_clean_themes_cache();
  32. unset( $GLOBALS['wp_themes'] );
  33. parent::tearDown();
  34. }
  35. /**
  36. * Replace the normal theme root dir with our pre-made test dir.
  37. */
  38. public function filter_theme_root() {
  39. return $this->theme_root;
  40. }
  41. public function test_missing_slug() {
  42. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  43. // Make the request.
  44. try {
  45. $this->_handleAjax( 'update-theme' );
  46. } catch ( WPAjaxDieContinueException $e ) {
  47. unset( $e );
  48. }
  49. // Get the response.
  50. $response = json_decode( $this->_last_response, true );
  51. $expected = array(
  52. 'success' => false,
  53. 'data' => array(
  54. 'slug' => '',
  55. 'errorCode' => 'no_theme_specified',
  56. 'errorMessage' => 'No theme specified.',
  57. ),
  58. );
  59. $this->assertSameSets( $expected, $response );
  60. }
  61. public function test_missing_capability() {
  62. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  63. $_POST['slug'] = 'foo';
  64. // Make the request.
  65. try {
  66. $this->_handleAjax( 'update-theme' );
  67. } catch ( WPAjaxDieContinueException $e ) {
  68. unset( $e );
  69. }
  70. // Get the response.
  71. $response = json_decode( $this->_last_response, true );
  72. $expected = array(
  73. 'success' => false,
  74. 'data' => array(
  75. 'update' => 'theme',
  76. 'slug' => 'foo',
  77. 'oldVersion' => '',
  78. 'newVersion' => '',
  79. 'errorMessage' => 'Sorry, you are not allowed to update themes for this site.',
  80. ),
  81. );
  82. $this->assertSameSets( $expected, $response );
  83. }
  84. public function test_update_theme() {
  85. $this->skipWithMultisite();
  86. $this->_setRole( 'administrator' );
  87. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  88. $_POST['slug'] = 'twentyten';
  89. // Make the request.
  90. try {
  91. // Prevent wp_update_themes() from running.
  92. wp_installing( true );
  93. $this->_handleAjax( 'update-theme' );
  94. wp_installing( false );
  95. } catch ( WPAjaxDieContinueException $e ) {
  96. unset( $e );
  97. }
  98. // Get the response.
  99. $response = json_decode( $this->_last_response, true );
  100. $theme = wp_get_theme( 'twentyten' );
  101. $expected = array(
  102. 'success' => false,
  103. 'data' => array(
  104. 'update' => 'theme',
  105. 'slug' => 'twentyten',
  106. 'oldVersion' => $theme->get( 'Version' ),
  107. 'newVersion' => '',
  108. 'debug' => array( 'The theme is at the latest version.' ),
  109. 'errorMessage' => 'The theme is at the latest version.',
  110. ),
  111. );
  112. $this->assertSameSets( $expected, $response );
  113. }
  114. function test_uppercase_theme_slug() {
  115. $this->skipWithMultisite();
  116. $this->_setRole( 'administrator' );
  117. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  118. $_POST['slug'] = 'camelCase';
  119. // Make the request.
  120. try {
  121. $this->_handleAjax( 'update-theme' );
  122. } catch ( WPAjaxDieContinueException $e ) {
  123. unset( $e );
  124. }
  125. // Get the response.
  126. $response = json_decode( $this->_last_response, true );
  127. $expected = array(
  128. 'success' => false,
  129. 'data' => array(
  130. 'update' => 'theme',
  131. 'slug' => 'camelCase',
  132. 'oldVersion' => '1.0',
  133. 'newVersion' => '',
  134. 'debug' => array( 'The theme is at the latest version.' ),
  135. 'errorMessage' => 'The theme is at the latest version.',
  136. ),
  137. );
  138. $this->assertSameSets( $expected, $response );
  139. }
  140. }