Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

/**
* Admin Ajax functions to be tested.
*/
require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';

/**
* Testing wp_ajax_health_check_background_updates() functionality.
*
* @package WordPress
* @subpackage UnitTests
* @since 5.2.0
*
* @group ajax
*
* @covers ::wp_ajax_health_check_background_updates
*/
class Tests_wp_ajax_health_check_background_updates extends WP_Ajax_UnitTestCase {

/**
* Administrator user ID.
*
* @var int
*/
protected static $admin_id;

/**
* Setup test fixtures.
*
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void {
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
}

public function set_up(): void {
parent::set_up();
add_action( 'wp_ajax_health-check-background-updates', 'wp_ajax_health_check_background_updates', 1 );

// Hook into wp_die to prevent execution from stopping.
add_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ) );
}

public function tear_down(): void {
remove_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ) );
parent::tear_down();
}

/**
* Returns our custom die handler.
*
* @return callable
*/
public function getDieHandler() {
return array( $this, 'dieHandler' );
}

/**
* Custom die handler that throws an exception.
*
* @param string|WP_Error $message
*/
public function dieHandler( $message ) {
$this->_last_response .= ob_get_clean();

if ( '' === $this->_last_response ) {
if ( is_scalar( $message ) ) {
$this->_last_response = (string) $message;
} else {
$this->_last_response = '0';
}
}

if ( '-1' === $this->_last_response || ( is_int( $message ) && -1 === $message ) ) {
throw new WPAjaxDieStopException( $this->_last_response );
}

throw new WPAjaxDieContinueException( $this->_last_response );
}

/**
* Tests success for wp_ajax_health_check_background_updates().
*
* @ticket 65252
* @expectedIncorrectUsage wp_ajax_health_check_background_updates
*/
public function test_health_check_background_updates_success(): void {
wp_set_current_user( self::$admin_id );

$_POST['_ajax_nonce'] = wp_create_nonce( 'health-check-site-status' );

try {
$this->_handleAjax( 'health-check-background-updates' );
} catch ( WPAjaxDieContinueException $e ) {
}

$response = json_decode( $this->_last_response, true );

$this->assertTrue( $response['success'], 'AJAX response should be successful' );
$this->assertArrayHasKey( 'label', $response['data'], 'Response data should contain a label' );
$this->assertArrayHasKey( 'status', $response['data'], 'Response data should contain a status' );
}

/**
* Tests failure with invalid nonce for wp_ajax_health_check_background_updates().
*
* @ticket 65252
* @expectedIncorrectUsage wp_ajax_health_check_background_updates
*/
public function test_health_check_background_updates_invalid_nonce(): void {
wp_set_current_user( self::$admin_id );

$_POST['_ajax_nonce'] = 'invalid-nonce';

$this->expectException( WPAjaxDieStopException::class );
$this->expectExceptionMessage( '-1' );

$this->_handleAjax( 'health-check-background-updates' );
}

/**
* Tests failure with insufficient permissions for wp_ajax_health_check_background_updates().
*
* @ticket 65252
* @expectedIncorrectUsage wp_ajax_health_check_background_updates
*/
public function test_health_check_background_updates_insufficient_permissions(): void {
$subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
wp_set_current_user( $subscriber_id );

$_POST['_ajax_nonce'] = wp_create_nonce( 'health-check-site-status' );

try {
$this->_handleAjax( 'health-check-background-updates' );
} catch ( WPAjaxDieContinueException $e ) {
}

$response = json_decode( $this->_last_response, true );

$this->assertFalse( $response['success'], 'AJAX response should be unsuccessful' );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

/**
* Admin Ajax functions to be tested.
*/
require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';

/**
* Testing wp_ajax_health_check_dotorg_communication() functionality.
*
* @package WordPress
* @subpackage UnitTests
* @since 5.2.0
*
* @group ajax
*
* @covers ::wp_ajax_health_check_dotorg_communication
*/
class Tests_wp_ajax_health_check_dotorg_communication extends WP_Ajax_UnitTestCase {

/**
* Administrator user ID.
*
* @var int
*/
protected static $admin_id;

/**
* Setup test fixtures.
*
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void {
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
}

public function set_up(): void {
parent::set_up();
add_action( 'wp_ajax_health-check-dotorg-communication', 'wp_ajax_health_check_dotorg_communication', 1 );

// Hook into wp_die to prevent execution from stopping.
add_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ) );
}

public function tear_down(): void {
remove_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ) );
parent::tear_down();
}

/**
* Returns our custom die handler.
*
* @return callable
*/
public function getDieHandler() {
return array( $this, 'dieHandler' );
}

/**
* Custom die handler that throws an exception.
*
* @param string|WP_Error $message
*/
public function dieHandler( $message ) {
$this->_last_response .= ob_get_clean();

if ( '' === $this->_last_response ) {
if ( is_scalar( $message ) ) {
$this->_last_response = (string) $message;
} else {
$this->_last_response = '0';
}
}

if ( '-1' === $this->_last_response || ( is_int( $message ) && -1 === $message ) ) {
throw new WPAjaxDieStopException( $this->_last_response );
}

throw new WPAjaxDieContinueException( $this->_last_response );
}

/**
* Tests success for wp_ajax_health_check_dotorg_communication().
*
* @ticket 65252
* @expectedIncorrectUsage wp_ajax_health_check_dotorg_communication
*/
public function test_health_check_dotorg_communication_success(): void {
wp_set_current_user( self::$admin_id );

$_POST['_ajax_nonce'] = wp_create_nonce( 'health-check-site-status' );

try {
$this->_handleAjax( 'health-check-dotorg-communication' );
} catch ( WPAjaxDieContinueException $e ) {
}

$response = json_decode( $this->_last_response, true );

$this->assertTrue( $response['success'], 'AJAX response should be successful' );
$this->assertArrayHasKey( 'label', $response['data'], 'Response data should contain a label' );
$this->assertArrayHasKey( 'status', $response['data'], 'Response data should contain a status' );
}

/**
* Tests failure with invalid nonce for wp_ajax_health_check_dotorg_communication().
*
* @ticket 65252
* @expectedIncorrectUsage wp_ajax_health_check_dotorg_communication
*/
public function test_health_check_dotorg_communication_invalid_nonce(): void {
wp_set_current_user( self::$admin_id );

$_POST['_ajax_nonce'] = 'invalid-nonce';

$this->expectException( WPAjaxDieStopException::class );
$this->expectExceptionMessage( '-1' );

$this->_handleAjax( 'health-check-dotorg-communication' );
}

/**
* Tests failure with insufficient permissions for wp_ajax_health_check_dotorg_communication().
*
* @ticket 65252
* @expectedIncorrectUsage wp_ajax_health_check_dotorg_communication
*/
public function test_health_check_dotorg_communication_insufficient_permissions(): void {
$subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
wp_set_current_user( $subscriber_id );

$_POST['_ajax_nonce'] = wp_create_nonce( 'health-check-site-status' );

try {
$this->_handleAjax( 'health-check-dotorg-communication' );
} catch ( WPAjaxDieContinueException $e ) {
}

$response = json_decode( $this->_last_response, true );

$this->assertFalse( $response['success'], 'AJAX response should be unsuccessful' );
}
}
Loading
Loading