<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[38168] trunk: Plugins: Move capability checks further up in `wp_ajax_update_plugin()` and `wp_ajax_delete_plugin()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/38168">38168</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/38168","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-07-27 17:42:01 +0000 (Wed, 27 Jul 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Plugins: Move capability checks further up in `wp_ajax_update_plugin()` and `wp_ajax_delete_plugin()`.

Add tests for both Ajax handlers.

Props Yorick Koster, swissspidy.
Fixes <a href="https://core.trac.wordpress.org/ticket/37490">#37490</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesajaxactionsphp">trunk/src/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunksrcwpadminjsupdatesjs">trunk/src/wp-admin/js/updates.js</a></li>
<li><a href="#trunktestsphpunitincludestestcaseajaxphp">trunk/tests/phpunit/includes/testcase-ajax.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsajaxDeletePluginphp">trunk/tests/phpunit/tests/ajax/DeletePlugin.php</a></li>
<li><a href="#trunktestsphpunittestsajaxUpdatePluginphp">trunk/tests/phpunit/tests/ajax/UpdatePlugin.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/ajax-actions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/ajax-actions.php      2016-07-27 17:09:27 UTC (rev 38167)
+++ trunk/src/wp-admin/includes/ajax-actions.php        2016-07-27 17:42:01 UTC (rev 38168)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3653,28 +3653,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $plugin      = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) );
-       $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $status = array(
</span><span class="cx" style="display: block; padding: 0 10px">                'update'     => 'plugin',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'plugin'     => $plugin,
</del><span class="cx" style="display: block; padding: 0 10px">                 'slug'       => sanitize_key( wp_unslash( $_POST['slug'] ) ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'pluginName' => $plugin_data['Name'],
</del><span class="cx" style="display: block; padding: 0 10px">                 'oldVersion' => '',
</span><span class="cx" style="display: block; padding: 0 10px">                'newVersion' => '',
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) {
+               $status['errorMessage'] = __( 'Sorry, you are not allowed to update plugins for this site.' );
+               wp_send_json_error( $status );
+       }
+
+       $plugin_data          = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
+       $status['plugin']     = $plugin;
+       $status['pluginName'] = $plugin_data['Name'];
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( $plugin_data['Version'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                /* translators: %s: Plugin version */
</span><span class="cx" style="display: block; padding: 0 10px">                $status['oldVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! current_user_can( 'update_plugins' ) ) {
-               $status['errorMessage'] = __( 'Sorry, you are not allowed to update plugins for this site.' );
-               wp_send_json_error( $status );
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        wp_update_plugins();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3748,24 +3749,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">        check_ajax_referer( 'updates' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $_POST['slug'] ) || empty( $_POST['plugin'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                wp_send_json_error( array( 'errorCode' => 'no_plugin_specified' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_send_json_error( array(
+                       'slug'         => '',
+                       'errorCode'    => 'no_plugin_specified',
+                       'errorMessage' => __( 'No plugin specified.' ),
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $plugin      = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) );
-       $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $status = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'delete'     => 'plugin',
-               'slug'       => sanitize_key( wp_unslash( $_POST['slug'] ) ),
-               'plugin'     => $plugin,
-               'pluginName' => $plugin_data['Name'],
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'delete' => 'plugin',
+               'slug'   => sanitize_key( wp_unslash( $_POST['slug'] ) ),
</ins><span class="cx" style="display: block; padding: 0 10px">         );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! current_user_can( 'delete_plugins' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! current_user_can( 'delete_plugins' ) || 0 !== validate_file( $plugin ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $status['errorMessage'] = __( 'Sorry, you are not allowed to delete plugins for this site.' );
</span><span class="cx" style="display: block; padding: 0 10px">                wp_send_json_error( $status );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $plugin_data          = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
+       $status['plugin']     = $plugin;
+       $status['pluginName'] = $plugin_data['Name'];
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( is_plugin_active( $plugin ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $status['errorMessage'] = __( 'You cannot delete a plugin while it is active on the main site.' );
</span><span class="cx" style="display: block; padding: 0 10px">                wp_send_json_error( $status );
</span></span></pre></div>
<a id="trunksrcwpadminjsupdatesjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/updates.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/updates.js  2016-07-27 17:09:27 UTC (rev 38167)
+++ trunk/src/wp-admin/js/updates.js    2016-07-27 17:42:01 UTC (rev 38168)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -447,7 +447,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                errorMessage = wp.updates.l10n.updateFailed.replace( '%s', response.errorMessage );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $message = $( 'tr[data-plugin="' + response.plugin + '"]' ).find( '.update-message' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( response.plugin ) {
+                               $message = $( 'tr[data-plugin="' + response.plugin + '"]' ).find( '.update-message' );
+                       } else {
+                               $message = $( 'tr[data-slug="' + response.slug + '"]' ).find( '.update-message' );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                         $message.removeClass( 'updating-message notice-warning' ).addClass( 'notice-error' ).find( 'p' ).html( errorMessage );
</span><span class="cx" style="display: block; padding: 0 10px">                } else if ( 'plugin-install' === pagenow || 'plugin-install-network' === pagenow ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $card = $( '.plugin-card-' + response.slug )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -458,9 +462,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                } ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $card.find( '.update-now' )
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                .attr( 'aria-label', wp.updates.l10n.updateFailedLabel.replace( '%s', response.pluginName ) )
</del><span class="cx" style="display: block; padding: 0 10px">                                 .text( wp.updates.l10n.updateFailedShort ).removeClass( 'updating-message' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( response.pluginName ) {
+                               $card.find( '.update-now' )
+                                       .attr( 'aria-label', wp.updates.l10n.updateFailedLabel.replace( '%s', response.pluginName ) );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $card.on( 'click', '.notice.is-dismissible .notice-dismiss', function() {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // Use same delay as the total duration of the notice fadeTo + slideUp animation.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -814,14 +822,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param {string}  response.errorMessage The error that occurred.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        wp.updates.deletePluginError = function( response ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                var $plugin          = $( 'tr.inactive[data-plugin="' + response.plugin + '"]' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         var $plugin, $pluginUpdateRow,
</ins><span class="cx" style="display: block; padding: 0 10px">                         pluginUpdateRow  = wp.template( 'item-update-row' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $pluginUpdateRow = $plugin.siblings( '[data-plugin="' + response.plugin + '"]' ),
</del><span class="cx" style="display: block; padding: 0 10px">                         noticeContent    = wp.updates.adminNotice( {
</span><span class="cx" style="display: block; padding: 0 10px">                                className: 'update-message notice-error notice-alt',
</span><span class="cx" style="display: block; padding: 0 10px">                                message:   response.errorMessage
</span><span class="cx" style="display: block; padding: 0 10px">                        } );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( response.plugin ) {
+                       $plugin          = $( 'tr.inactive[data-plugin="' + response.plugin + '"]' );
+                       $pluginUpdateRow = $plugin.siblings( '[data-plugin="' + response.plugin + '"]' );
+               } else {
+                       $plugin          = $( 'tr.inactive[data-slug="' + response.slug + '"]' );
+                       $pluginUpdateRow = $plugin.siblings( '[data-slug="' + response.slug + '"]' );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! wp.updates.isValidResponse( response, 'delete' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -835,7 +850,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $plugin.addClass( 'update' ).after(
</span><span class="cx" style="display: block; padding: 0 10px">                                pluginUpdateRow( {
</span><span class="cx" style="display: block; padding: 0 10px">                                        slug:    response.slug,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        plugin:  response.plugin,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 plugin:  response.plugin || response.slug,
</ins><span class="cx" style="display: block; padding: 0 10px">                                         colspan: $( '#bulk-action-form' ).find( 'thead th:not(.hidden), thead td' ).length,
</span><span class="cx" style="display: block; padding: 0 10px">                                        content: noticeContent
</span><span class="cx" style="display: block; padding: 0 10px">                                } )
</span></span></pre></div>
<a id="trunktestsphpunitincludestestcaseajaxphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/testcase-ajax.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/testcase-ajax.php    2016-07-27 17:09:27 UTC (rev 38167)
+++ trunk/tests/phpunit/includes/testcase-ajax.php      2016-07-27 17:42:01 UTC (rev 38168)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -18,13 +18,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Last AJAX response.  This is set via echo -or- wp_die.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @var type
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @var string
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        protected $_last_response = '';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * List of ajax actions called via POST
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @var type
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @var array
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        protected static $_core_actions_get = array(
</span><span class="cx" style="display: block; padding: 0 10px">                'fetch-list', 'ajax-tag-search', 'wp-compression-test', 'imgedit-preview', 'oembed-cache',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -39,7 +39,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * List of ajax actions called via GET
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @var type
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @var array
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        protected static $_core_actions_post = array(
</span><span class="cx" style="display: block; padding: 0 10px">                'oembed_cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -53,7 +53,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'wp-remove-post-lock', 'dismiss-wp-pointer', 'send-attachment-to-editor', 'heartbeat', 'nopriv_heartbeat', 'get-revision-diffs',
</span><span class="cx" style="display: block; padding: 0 10px">                'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
</span><span class="cx" style="display: block; padding: 0 10px">                'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'press-this-add-category', 'crop-image', 'generate-password',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',
+               'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',
+               'install-theme', 'get-post-thumbnail-html',
</ins><span class="cx" style="display: block; padding: 0 10px">         );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public static function setUpBeforeClass() {
</span></span></pre></div>
<a id="trunktestsphpunittestsajaxDeletePluginphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/ajax/DeletePlugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/ajax/DeletePlugin.php                           (rev 0)
+++ trunk/tests/phpunit/tests/ajax/DeletePlugin.php     2016-07-27 17:42:01 UTC (rev 38168)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,158 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Admin ajax functions to be tested
+ */
+require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
+
+/**
+ * Testing Ajax handler for deleting a plugin.
+ *
+ * @group ajax
+ */
+class Tests_Ajax_Delete_Plugin extends WP_Ajax_UnitTestCase {
+       /**
+        * @expectedException WPAjaxDieStopException
+        * @expectedExceptionMessage -1
+        */
+       public function test_missing_nonce() {
+               $this->_handleAjax( 'delete-plugin' );
+       }
+
+       public function test_missing_plugin() {
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['slug']        = 'foo';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'delete-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'slug'         => '',
+                               'errorCode'    => 'no_plugin_specified',
+                               'errorMessage' => 'No plugin specified.',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+
+       public function test_missing_slug() {
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['plugin']      = 'foo/bar.php';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'delete-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'slug'         => '',
+                               'errorCode'    => 'no_plugin_specified',
+                               'errorMessage' => 'No plugin specified.',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+
+       public function test_missing_capability() {
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['plugin']      = 'foo/bar.php';
+               $_POST['slug']        = 'foo';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'delete-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'delete'       => 'plugin',
+                               'slug'         => 'foo',
+                               'errorMessage' => 'Sorry, you are not allowed to delete plugins for this site.',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+
+       public function test_invalid_file() {
+               $this->_setRole( 'administrator' );
+
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['plugin']      = '../foo/bar.php';
+               $_POST['slug']        = 'foo';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'delete-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'delete'       => 'plugin',
+                               'slug'         => 'foo',
+                               'errorMessage' => 'Sorry, you are not allowed to delete plugins for this site.',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+
+       public function test_delete_plugin() {
+               $this->_setRole( 'administrator' );
+
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['plugin']      = 'foo.php';
+               $_POST['slug']        = 'foo';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'delete-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => true,
+                       'data'    => array(
+                               'delete'       => 'plugin',
+                               'slug'         => 'foo',
+                               'plugin'       => 'foo.php',
+                               'pluginName'   => '',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsajaxUpdatePluginphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/ajax/UpdatePlugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/ajax/UpdatePlugin.php                           (rev 0)
+++ trunk/tests/phpunit/tests/ajax/UpdatePlugin.php     2016-07-27 17:42:01 UTC (rev 38168)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,169 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Admin ajax functions to be tested
+ */
+require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
+
+/**
+ * Testing Ajax handler for updating a plugin.
+ *
+ * @group ajax
+ */
+class Tests_Ajax_Update_Plugin extends WP_Ajax_UnitTestCase {
+       /**
+        * @expectedException WPAjaxDieStopException
+        * @expectedExceptionMessage -1
+        */
+       public function test_missing_nonce() {
+               $this->_handleAjax( 'update-plugin' );
+       }
+
+       public function test_missing_plugin() {
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['slug']        = 'foo';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'update-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'slug'         => '',
+                               'errorCode'    => 'no_plugin_specified',
+                               'errorMessage' => 'No plugin specified.',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+
+       public function test_missing_slug() {
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['plugin']      = 'foo/bar.php';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'update-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'slug'         => '',
+                               'errorCode'    => 'no_plugin_specified',
+                               'errorMessage' => 'No plugin specified.',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+
+       public function test_missing_capability() {
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['plugin']      = 'foo/bar.php';
+               $_POST['slug']        = 'foo';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'update-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'update'       => 'plugin',
+                               'slug'         => 'foo',
+                               'errorMessage' => 'Sorry, you are not allowed to update plugins for this site.',
+                               'oldVersion'   => '',
+                               'newVersion'   => '',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+
+       public function test_invalid_file() {
+               $this->_setRole( 'administrator' );
+
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['plugin']      = '../foo/bar.php';
+               $_POST['slug']        = 'foo';
+
+               // Make the request
+               try {
+                       $this->_handleAjax( 'update-plugin' );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'update'       => 'plugin',
+                               'slug'         => 'foo',
+                               'errorMessage' => 'Sorry, you are not allowed to update plugins for this site.',
+                               'oldVersion'   => '',
+                               'newVersion'   => '',
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+
+       public function test_update_plugin() {
+               $this->_setRole( 'administrator' );
+
+               $_POST['_ajax_nonce'] = wp_create_nonce( 'updates' );
+               $_POST['plugin']      = 'hello.php';
+               $_POST['slug']        = 'hello-dolly';
+
+               // Make the request
+               try {
+                       // Prevent wp_update_plugins() from running
+                       wp_installing( true );
+                       $this->_handleAjax( 'update-plugin' );
+                       wp_installing( false );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+
+               // Get the response.
+               $response = json_decode( $this->_last_response, true );
+
+               $expected = array(
+                       'success' => false,
+                       'data'    => array(
+                               'update'       => 'plugin',
+                               'slug'         => 'hello-dolly',
+                               'plugin'       => 'hello.php',
+                               'pluginName'   => 'Hello Dolly',
+                               'errorMessage' => 'Plugin update failed.',
+                               'oldVersion'   => 'Version 1.6',
+                               'newVersion'   => '',
+                               'debug'        => array( 'The plugin is at the latest version.' ),
+                       ),
+               );
+
+               $this->assertEqualSets( $expected, $response );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>