UpdatePlugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 updating a plugin.
  8. *
  9. * @group ajax
  10. */
  11. class Tests_Ajax_Update_Plugin extends WP_Ajax_UnitTestCase {
  12. public function test_missing_nonce() {
  13. $this->expectException( 'WPAjaxDieStopException' );
  14. $this->expectExceptionMessage( '-1' );
  15. $this->_handleAjax( 'update-plugin' );
  16. }
  17. public function test_missing_plugin() {
  18. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  19. $_POST['slug'] = 'foo';
  20. // Make the request.
  21. try {
  22. $this->_handleAjax( 'update-plugin' );
  23. } catch ( WPAjaxDieContinueException $e ) {
  24. unset( $e );
  25. }
  26. // Get the response.
  27. $response = json_decode( $this->_last_response, true );
  28. $expected = array(
  29. 'success' => false,
  30. 'data' => array(
  31. 'slug' => '',
  32. 'errorCode' => 'no_plugin_specified',
  33. 'errorMessage' => 'No plugin specified.',
  34. ),
  35. );
  36. $this->assertSameSets( $expected, $response );
  37. }
  38. public function test_missing_slug() {
  39. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  40. $_POST['plugin'] = 'foo/bar.php';
  41. // Make the request.
  42. try {
  43. $this->_handleAjax( 'update-plugin' );
  44. } catch ( WPAjaxDieContinueException $e ) {
  45. unset( $e );
  46. }
  47. // Get the response.
  48. $response = json_decode( $this->_last_response, true );
  49. $expected = array(
  50. 'success' => false,
  51. 'data' => array(
  52. 'slug' => '',
  53. 'errorCode' => 'no_plugin_specified',
  54. 'errorMessage' => 'No plugin specified.',
  55. ),
  56. );
  57. $this->assertSameSets( $expected, $response );
  58. }
  59. public function test_missing_capability() {
  60. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  61. $_POST['plugin'] = 'foo/bar.php';
  62. $_POST['slug'] = 'foo';
  63. // Make the request.
  64. try {
  65. $this->_handleAjax( 'update-plugin' );
  66. } catch ( WPAjaxDieContinueException $e ) {
  67. unset( $e );
  68. }
  69. // Get the response.
  70. $response = json_decode( $this->_last_response, true );
  71. $expected = array(
  72. 'success' => false,
  73. 'data' => array(
  74. 'update' => 'plugin',
  75. 'slug' => 'foo',
  76. 'oldVersion' => '',
  77. 'newVersion' => '',
  78. 'errorMessage' => 'Sorry, you are not allowed to update plugins for this site.',
  79. ),
  80. );
  81. $this->assertSameSets( $expected, $response );
  82. }
  83. public function test_invalid_file() {
  84. $this->_setRole( 'administrator' );
  85. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  86. $_POST['plugin'] = '../foo/bar.php';
  87. $_POST['slug'] = 'foo';
  88. // Make the request.
  89. try {
  90. $this->_handleAjax( 'update-plugin' );
  91. } catch ( WPAjaxDieContinueException $e ) {
  92. unset( $e );
  93. }
  94. // Get the response.
  95. $response = json_decode( $this->_last_response, true );
  96. $expected = array(
  97. 'success' => false,
  98. 'data' => array(
  99. 'update' => 'plugin',
  100. 'slug' => 'foo',
  101. 'oldVersion' => '',
  102. 'newVersion' => '',
  103. 'errorMessage' => 'Sorry, you are not allowed to update plugins for this site.',
  104. ),
  105. );
  106. $this->assertSameSets( $expected, $response );
  107. }
  108. public function test_update_plugin() {
  109. $this->skipWithMultisite();
  110. $this->_setRole( 'administrator' );
  111. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  112. $_POST['plugin'] = 'hello.php';
  113. $_POST['slug'] = 'hello-dolly';
  114. // Make the request.
  115. try {
  116. // Prevent wp_update_plugins() from running.
  117. wp_installing( true );
  118. $this->_handleAjax( 'update-plugin' );
  119. wp_installing( false );
  120. } catch ( WPAjaxDieContinueException $e ) {
  121. unset( $e );
  122. }
  123. // Get the response.
  124. $response = json_decode( $this->_last_response, true );
  125. $expected = array(
  126. 'success' => false,
  127. 'data' => array(
  128. 'update' => 'plugin',
  129. 'slug' => 'hello-dolly',
  130. 'oldVersion' => 'Version 1.7.2',
  131. 'newVersion' => '',
  132. 'plugin' => 'hello.php',
  133. 'pluginName' => 'Hello Dolly',
  134. 'debug' => array( 'The plugin is at the latest version.' ),
  135. 'errorMessage' => 'The plugin is at the latest version.',
  136. ),
  137. );
  138. $this->assertSameSets( $expected, $response );
  139. }
  140. }