wordpress-develop/tests/phpunit/tests/ajax/DeletePlugin.php
Sergey Biryukov e72fff9cef Code Modernization: Replace dirname( __FILE__ ) calls with __DIR__ magic constant.
This avoids the performance overhead of the function call every time `dirname( __FILE__ )` was used instead of `__DIR__`.

This commit also includes:

* Removing unnecessary parentheses from `include`/`require` statements. These are language constructs, not function calls.
* Replacing `include` statements for several files with `require_once`, for consistency:
 * `wp-admin/admin-header.php`
 * `wp-admin/admin-footer.php`
 * `wp-includes/version.php`

Props ayeshrajans, desrosj, valentinbora, jrf, joostdevalk, netweb.
Fixes #48082.

git-svn-id: https://develop.svn.wordpress.org/trunk@47198 602fd350-edb4-49c9-b593-d223f7449a82
2020-02-06 06:31:22 +00:00

159 lines
3.6 KiB
PHP

<?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 );
}
}