Skip to content
78 changes: 78 additions & 0 deletions tests/phpunit/tests/functions/_defaultWpDieHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Tests the _default_wp_die_handler() function.
*
* @group functions
*
* @covers ::_default_wp_die_handler
*/
class Tests_Functions_DefaultWpDieHandler extends WP_UnitTestCase {

/**
* Tests the default HTML output of the handler.
*
* @ticket 65655
*/
public function test_default_wp_die_handler_output() {
ob_start();
try {
_default_wp_die_handler( 'Error Message', 'Error Title', array( 'exit' => false ) );
} catch ( WPDieException $e ) {
}
$output = ob_get_clean();

$this->assertStringContainsString( '<div class="wp-die-message">Error Message</div>', $output );
$this->assertStringContainsString( '<title>Error Title</title>', $output );
}

/**
* Tests handling of additional errors.
*
* @ticket 65655
*/
public function test_default_wp_die_handler_additional_errors() {
$args = array(
'exit' => false,
'additional_errors' => array(
array( 'message' => 'Extra Error 1' ),
array( 'message' => 'Extra Error 2' ),
),
);

ob_start();
try {
_default_wp_die_handler( 'Main Error', '', $args );
} catch ( WPDieException $e ) {
}
$output = ob_get_clean();

$this->assertStringContainsString( '<li>Main Error</li>', $output );
$this->assertStringContainsString( '<li>Extra Error 1</li>', $output );
$this->assertStringContainsString( '<li>Extra Error 2</li>', $output );
}

/**
* Tests handling of a link.
*
* @ticket 65655
*/
public function test_default_wp_die_handler_link() {
$args = array(
'exit' => false,
'link_url' => 'https://example.com',
'link_text' => 'Go to Example',
);

ob_start();
try {
_default_wp_die_handler( 'Error', '', $args );
} catch ( WPDieException $e ) {
}
$output = ob_get_clean();

// The default handler uses single quotes for attributes in some cases.
$this->assertStringContainsString( "href='https://example.com'", $output );
$this->assertStringContainsString( '>Go to Example</a>', $output );
}
}
93 changes: 93 additions & 0 deletions tests/phpunit/tests/wp-includes/functions/WpDieTest.php
Comment thread
pbearne marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace WordPress\Tests\WP_Includes\Functions;

use MockAction;
use WP_Error;
use WP_UnitTestCase;
use WPDieException;

/**
* Tests for the `wp_die()` function.
*
* @group functions
*
* @covers ::wp_die
*/
class WpDieTest extends WP_UnitTestCase {

/**
* Tests that wp_die() calls the expected handler.
*
* @ticket 65655
*/
public function test_wp_die_handler_selection() {
$filter = new MockAction();
add_filter( 'wp_die_handler', array( $filter, 'filter' ), 9 );

try {
wp_die( 'Message', 'Title', array( 'exit' => false ) );
} catch ( WPDieException $e ) {
// The default test handler throws this.
}

$this->assertSame( 1, $filter->get_call_count(), 'The wp_die_handler filter should have been called once.' );
$this->assertSame( array( array( '_default_wp_die_handler' ) ), $filter->get_args(), 'The standard handler should be supplied to the filter.' );
}

/**
* Tests that wp_die() respects the Ajax handler filter.
*
* @ticket 65655
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_wp_die_ajax_handler_selection() {
if ( ! defined( 'DOING_AJAX' ) ) {
define( 'DOING_AJAX', true );
}

$filter = new MockAction();
add_filter( 'wp_die_ajax_handler', array( $filter, 'filter' ) );

ob_start();
try {
wp_die( 'Ajax Message', 'Ajax Title', array( 'exit' => false ) );
} catch ( WPDieException $e ) {
}
$output = ob_get_clean();

$this->assertSame( 1, $filter->get_call_count(), 'The wp_die_ajax_handler filter should have been called once.' );
$this->assertSame( array( array( '_ajax_wp_die_handler' ) ), $filter->get_args(), 'The Ajax handler should be supplied to the filter.' );
$this->assertSame( 'Ajax Message', $output, 'The Ajax handler should output only the message.' );
}

/**
* Tests that wp_die() handles WP_Error objects.
*
* @ticket 65655
*/
public function test_wp_die_with_wp_error() {
$error = new WP_Error( 'test_error', 'Test Error Message' );

// Use a simple variable to capture the first argument.
$captured_message = null;
add_filter(
'wp_die_handler',
function ( $callback ) use ( &$captured_message ) {
return function ( $message ) use ( &$captured_message ) {
$captured_message = $message;
throw new WPDieException( 'Intercepted' );
};
}
);

try {
wp_die( $error, '', array( 'exit' => false ) );
} catch ( WPDieException $e ) {
}

$this->assertSame( $error, $captured_message, 'The error object should be passed as the first argument to the handler.' );
}
}
Loading