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
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 );
}
}
83 changes: 83 additions & 0 deletions tests/phpunit/tests/functions/wpDie.php
Comment thread
pbearne marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* Tests the wp_die() function.
*
* @group functions
*
* @covers ::wp_die
*/
class Tests_Functions_WpDie 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' ) );

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.' );
}

/**
* 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 ) {
}
ob_end_clean();

$this->assertSame( 1, $filter->get_call_count(), 'The wp_die_ajax_handler filter should have been called once.' );
}

/**
* 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