DeletePlugin.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 deleting a plugin.
  8. *
  9. * @group ajax
  10. */
  11. class Tests_Ajax_Delete_Plugin extends WP_Ajax_UnitTestCase {
  12. public function test_missing_nonce() {
  13. $this->expectException( 'WPAjaxDieStopException' );
  14. $this->expectExceptionMessage( '-1' );
  15. $this->_handleAjax( 'delete-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( 'delete-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( 'delete-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( 'delete-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. 'delete' => 'plugin',
  75. 'slug' => 'foo',
  76. 'errorMessage' => 'Sorry, you are not allowed to delete plugins for this site.',
  77. ),
  78. );
  79. $this->assertSameSets( $expected, $response );
  80. }
  81. public function test_invalid_file() {
  82. $this->_setRole( 'administrator' );
  83. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  84. $_POST['plugin'] = '../foo/bar.php';
  85. $_POST['slug'] = 'foo';
  86. // Make the request.
  87. try {
  88. $this->_handleAjax( 'delete-plugin' );
  89. } catch ( WPAjaxDieContinueException $e ) {
  90. unset( $e );
  91. }
  92. // Get the response.
  93. $response = json_decode( $this->_last_response, true );
  94. $expected = array(
  95. 'success' => false,
  96. 'data' => array(
  97. 'delete' => 'plugin',
  98. 'slug' => 'foo',
  99. 'errorMessage' => 'Sorry, you are not allowed to delete plugins for this site.',
  100. ),
  101. );
  102. $this->assertSameSets( $expected, $response );
  103. }
  104. public function test_delete_plugin() {
  105. $this->skipWithMultisite();
  106. $this->_setRole( 'administrator' );
  107. $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
  108. $_POST['plugin'] = 'foo.php';
  109. $_POST['slug'] = 'foo';
  110. // Make the request.
  111. try {
  112. $this->_handleAjax( 'delete-plugin' );
  113. } catch ( WPAjaxDieContinueException $e ) {
  114. unset( $e );
  115. }
  116. // Get the response.
  117. $response = json_decode( $this->_last_response, true );
  118. $expected = array(
  119. 'success' => true,
  120. 'data' => array(
  121. 'delete' => 'plugin',
  122. 'slug' => 'foo',
  123. 'plugin' => 'foo.php',
  124. 'pluginName' => '',
  125. ),
  126. );
  127. $this->assertSameSets( $expected, $response );
  128. }
  129. }