Skip to content

Commit 231db4b

Browse files
committed
Add test coverage for errors in action
1 parent 24b3171 commit 231db4b

File tree

1 file changed

+89
-45
lines changed

1 file changed

+89
-45
lines changed

tests/phpunit/tests/template.php

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -983,11 +983,11 @@ public function test_wp_start_template_enhancement_output_buffer_for_json(): voi
983983
}
984984

985985
/**
986-
* Data provider for data_provider_to_test_wp_finalize_template_enhancement_output_buffer_with_errors_while_filtering.
986+
* Data provider for data_provider_to_test_wp_finalize_template_enhancement_output_buffer_with_errors_while_processing.
987987
*
988988
* @return array
989989
*/
990-
public function data_provider_to_test_wp_finalize_template_enhancement_output_buffer_with_errors_while_filtering(): array {
990+
public function data_provider_to_test_wp_finalize_template_enhancement_output_buffer_with_errors_while_processing(): array {
991991
$log_and_display_all = array(
992992
'error_reporting' => E_ALL,
993993
'display_errors' => true,
@@ -998,86 +998,114 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
998998
$tests = array(
999999
'deprecated' => array(
10001000
'ini_config_options' => $log_and_display_all,
1001-
'emit_errors' => static function () {
1002-
trigger_error( 'You are history.', E_USER_DEPRECATED );
1001+
'emit_filter_errors' => static function () {
1002+
trigger_error( 'You are history during filter.', E_USER_DEPRECATED );
1003+
},
1004+
'emit_action_errors' => static function () {
1005+
trigger_error( 'You are history during action.', E_USER_DEPRECATED );
10031006
},
10041007
'expected_processed' => true,
10051008
'expected_error_log' => array(
1006-
'PHP Deprecated: You are history. in __FILE__ on line __LINE__',
1009+
'PHP Deprecated: You are history during filter. in __FILE__ on line __LINE__',
1010+
'PHP Deprecated: You are history during action. in __FILE__ on line __LINE__',
10071011
),
10081012
'expected_displayed_errors' => array(
1009-
'<b>Deprecated</b>: You are history. in <b>__FILE__</b> on line <b>__LINE__</b>',
1013+
'<b>Deprecated</b>: You are history during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
10101014
),
10111015
),
10121016
'notice' => array(
10131017
'ini_config_options' => $log_and_display_all,
1014-
'emit_errors' => static function () {
1015-
trigger_error( 'POSTED: No trespassing.', E_USER_NOTICE );
1018+
'emit_filter_errors' => static function () {
1019+
trigger_error( 'POSTED: No trespassing during filter.', E_USER_NOTICE );
1020+
},
1021+
'emit_action_errors' => static function () {
1022+
trigger_error( 'POSTED: No trespassing during action.', E_USER_NOTICE );
10161023
},
10171024
'expected_processed' => true,
10181025
'expected_error_log' => array(
1019-
'PHP Notice: POSTED: No trespassing. in __FILE__ on line __LINE__',
1026+
'PHP Notice: POSTED: No trespassing during filter. in __FILE__ on line __LINE__',
1027+
'PHP Notice: POSTED: No trespassing during action. in __FILE__ on line __LINE__',
10201028
),
10211029
'expected_displayed_errors' => array(
1022-
'<b>Notice</b>: POSTED: No trespassing. in <b>__FILE__</b> on line <b>__LINE__</b>',
1030+
'<b>Notice</b>: POSTED: No trespassing during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
10231031
),
10241032
),
10251033
'warning' => array(
10261034
'ini_config_options' => $log_and_display_all,
1027-
'emit_errors' => static function () {
1028-
trigger_error( 'AVISO: Piso mojado.', E_USER_WARNING );
1035+
'emit_filter_errors' => static function () {
1036+
trigger_error( 'AVISO: Piso mojado durante filtro.', E_USER_WARNING );
1037+
},
1038+
'emit_action_errors' => static function () {
1039+
trigger_error( 'AVISO: Piso mojado durante acción.', E_USER_WARNING );
10291040
},
10301041
'expected_processed' => true,
10311042
'expected_error_log' => array(
1032-
'PHP Warning: AVISO: Piso mojado. in __FILE__ on line __LINE__',
1043+
'PHP Warning: AVISO: Piso mojado durante filtro. in __FILE__ on line __LINE__',
1044+
'PHP Warning: AVISO: Piso mojado durante acción. in __FILE__ on line __LINE__',
10331045
),
10341046
'expected_displayed_errors' => array(
1035-
'<b>Warning</b>: AVISO: Piso mojado. in <b>__FILE__</b> on line <b>__LINE__</b>',
1047+
'<b>Warning</b>: AVISO: Piso mojado durante filtro. in <b>__FILE__</b> on line <b>__LINE__</b>',
10361048
),
10371049
),
10381050
'error' => array(
10391051
'ini_config_options' => $log_and_display_all,
1040-
'emit_errors' => static function () {
1041-
@trigger_error( 'ERROR: Can this mistake be rectified?', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1052+
'emit_filter_errors' => static function () {
1053+
@trigger_error( 'ERROR: Can this mistake be rectified during filter?', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1054+
},
1055+
'emit_action_errors' => static function () {
1056+
@trigger_error( 'ERROR: Can this mistake be rectified during action?', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
10421057
},
10431058
'expected_processed' => false,
10441059
'expected_error_log' => array(
1045-
'PHP Warning: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified? in __FILE__ on line __LINE__',
1060+
'PHP Warning: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during filter? in __FILE__ on line __LINE__',
1061+
'PHP Warning: Exception thrown during wp_finalized_template_enhancement_output_buffer action: User error triggered: ERROR: Can this mistake be rectified during action? in __FILE__ on line __LINE__',
10461062
),
10471063
'expected_displayed_errors' => array(
1048-
'<b>Error</b>: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified? in <b>__FILE__</b> on line <b>__LINE__</b>',
1064+
'<b>Error</b>: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during filter? in <b>__FILE__</b> on line <b>__LINE__</b>',
10491065
),
10501066
),
10511067
'exception' => array(
10521068
'ini_config_options' => $log_and_display_all,
1053-
'emit_errors' => static function () {
1054-
throw new Exception( 'I take exception to this!' );
1069+
'emit_filter_errors' => static function () {
1070+
throw new Exception( 'I take exception to this filter!' );
1071+
},
1072+
'emit_action_errors' => static function () {
1073+
throw new Exception( 'I take exception to this action!' );
10551074
},
10561075
'expected_processed' => false,
10571076
'expected_error_log' => array(
1058-
'PHP Warning: Uncaught exception "Exception" thrown: I take exception to this! in __FILE__ on line __LINE__',
1077+
'PHP Warning: Uncaught exception "Exception" thrown: I take exception to this filter! in __FILE__ on line __LINE__',
1078+
'PHP Warning: Exception thrown during wp_finalized_template_enhancement_output_buffer action: I take exception to this action! in __FILE__ on line __LINE__',
10591079
),
10601080
'expected_displayed_errors' => array(
1061-
'<b>Error</b>: Uncaught exception "Exception" thrown: I take exception to this! in <b>__FILE__</b> on line <b>__LINE__</b>',
1081+
'<b>Error</b>: Uncaught exception "Exception" thrown: I take exception to this filter! in <b>__FILE__</b> on line <b>__LINE__</b>',
10621082
),
10631083
),
10641084
'multiple_non_errors' => array(
10651085
'ini_config_options' => $log_and_display_all,
1066-
'emit_errors' => static function () {
1067-
trigger_error( 'You are history.', E_USER_DEPRECATED );
1068-
trigger_error( 'POSTED: No trespassing.', E_USER_NOTICE );
1069-
trigger_error( 'AVISO: Piso mojado.', E_USER_WARNING );
1086+
'emit_filter_errors' => static function () {
1087+
trigger_error( 'You are history during filter.', E_USER_DEPRECATED );
1088+
trigger_error( 'POSTED: No trespassing during filter.', E_USER_NOTICE );
1089+
trigger_error( 'AVISO: Piso mojado durante filtro.', E_USER_WARNING );
1090+
},
1091+
'emit_action_errors' => static function () {
1092+
trigger_error( 'You are history during action.', E_USER_DEPRECATED );
1093+
trigger_error( 'POSTED: No trespassing during action.', E_USER_NOTICE );
1094+
trigger_error( 'AVISO: Piso mojado durante acción.', E_USER_WARNING );
10701095
},
10711096
'expected_processed' => true,
10721097
'expected_error_log' => array(
1073-
'PHP Deprecated: You are history. in __FILE__ on line __LINE__',
1074-
'PHP Notice: POSTED: No trespassing. in __FILE__ on line __LINE__',
1075-
'PHP Warning: AVISO: Piso mojado. in __FILE__ on line __LINE__',
1098+
'PHP Deprecated: You are history during filter. in __FILE__ on line __LINE__',
1099+
'PHP Notice: POSTED: No trespassing during filter. in __FILE__ on line __LINE__',
1100+
'PHP Warning: AVISO: Piso mojado durante filtro. in __FILE__ on line __LINE__',
1101+
'PHP Deprecated: You are history during action. in __FILE__ on line __LINE__',
1102+
'PHP Notice: POSTED: No trespassing during action. in __FILE__ on line __LINE__',
1103+
'PHP Warning: AVISO: Piso mojado durante acción. in __FILE__ on line __LINE__',
10761104
),
10771105
'expected_displayed_errors' => array(
1078-
'<b>Deprecated</b>: You are history. in <b>__FILE__</b> on line <b>__LINE__</b>',
1079-
'<b>Notice</b>: POSTED: No trespassing. in <b>__FILE__</b> on line <b>__LINE__</b>',
1080-
'<b>Warning</b>: AVISO: Piso mojado. in <b>__FILE__</b> on line <b>__LINE__</b>',
1106+
'<b>Deprecated</b>: You are history during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
1107+
'<b>Notice</b>: POSTED: No trespassing during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
1108+
'<b>Warning</b>: AVISO: Piso mojado durante filtro. in <b>__FILE__</b> on line <b>__LINE__</b>',
10811109
),
10821110
),
10831111
'deprecated_without_html' => array(
@@ -1087,28 +1115,33 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10871115
'html_errors' => false,
10881116
)
10891117
),
1090-
'emit_errors' => static function () {
1091-
trigger_error( 'You are history.', E_USER_DEPRECATED );
1118+
'emit_filter_errors' => static function () {
1119+
trigger_error( 'You are history during filter.', E_USER_DEPRECATED );
10921120
},
1121+
'emit_action_errors' => null,
10931122
'expected_processed' => true,
10941123
'expected_error_log' => array(
1095-
'PHP Deprecated: You are history. in __FILE__ on line __LINE__',
1124+
'PHP Deprecated: You are history during filter. in __FILE__ on line __LINE__',
10961125
),
10971126
'expected_displayed_errors' => array(
1098-
'Deprecated: You are history. in __FILE__ on line __LINE__',
1127+
'Deprecated: You are history during filter. in __FILE__ on line __LINE__',
10991128
),
11001129
),
11011130
'warning_in_eval' => array(
11021131
'ini_config_options' => $log_and_display_all,
1103-
'emit_errors' => static function () {
1104-
eval( "trigger_error( 'AVISO: Piso mojado.', E_USER_WARNING );" ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- We're in a test!
1132+
'emit_filter_errors' => static function () {
1133+
eval( "trigger_error( 'AVISO: Piso mojado durante filtro.', E_USER_WARNING );" ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- We're in a test!
1134+
},
1135+
'emit_action_errors' => static function () {
1136+
eval( "trigger_error( 'AVISO: Piso mojado durante acción.', E_USER_WARNING );" ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- We're in a test!
11051137
},
11061138
'expected_processed' => true,
11071139
'expected_error_log' => array(
1108-
'PHP Warning: AVISO: Piso mojado. in __FILE__ : eval()\'d code on line __LINE__',
1140+
'PHP Warning: AVISO: Piso mojado durante filtro. in __FILE__ : eval()\'d code on line __LINE__',
1141+
'PHP Warning: AVISO: Piso mojado durante acción. in __FILE__ : eval()\'d code on line __LINE__',
11091142
),
11101143
'expected_displayed_errors' => array(
1111-
'<b>Warning</b>: AVISO: Piso mojado. in <b>__FILE__ : eval()\'d code</b> on line <b>__LINE__</b>',
1144+
'<b>Warning</b>: AVISO: Piso mojado durante filtro. in <b>__FILE__ : eval()\'d code</b> on line <b>__LINE__</b>',
11121145
),
11131146
),
11141147
);
@@ -1160,16 +1193,16 @@ static function ( $log_entry ) {
11601193
}
11611194

11621195
/**
1163-
* Tests that errors emitted when filtering wp_template_enhancement_output_buffer are handled as expected.
1196+
* Tests that errors are handled as expected when errors are emitted when filtering wp_template_enhancement_output_buffer or doing the wp_finalize_template_enhancement_output_buffer action.
11641197
*
11651198
* @ticket 43258
11661199
* @ticket 64108
11671200
*
11681201
* @covers ::wp_finalize_template_enhancement_output_buffer
11691202
*
1170-
* @dataProvider data_provider_to_test_wp_finalize_template_enhancement_output_buffer_with_errors_while_filtering
1203+
* @dataProvider data_provider_to_test_wp_finalize_template_enhancement_output_buffer_with_errors_while_processing
11711204
*/
1172-
public function test_wp_finalize_template_enhancement_output_buffer_with_errors_while_filtering( array $ini_config_options, Closure $emit_errors, bool $expected_processed, array $expected_error_log, array $expected_displayed_errors ): void {
1205+
public function test_wp_finalize_template_enhancement_output_buffer_with_errors_while_processing( array $ini_config_options, ?Closure $emit_filter_errors, ?Closure $emit_action_errors, bool $expected_processed, array $expected_error_log, array $expected_displayed_errors ): void {
11731206
// Start a wrapper output buffer so that we can flush the inner buffer.
11741207
ob_start();
11751208

@@ -1180,13 +1213,24 @@ public function test_wp_finalize_template_enhancement_output_buffer_with_errors_
11801213

11811214
add_filter(
11821215
'wp_template_enhancement_output_buffer',
1183-
static function ( string $buffer ) use ( $emit_errors ): string {
1216+
static function ( string $buffer ) use ( $emit_filter_errors ): string {
11841217
$buffer = str_replace( 'Hello', 'Goodbye', $buffer );
1185-
$emit_errors();
1218+
if ( $emit_filter_errors ) {
1219+
$emit_filter_errors();
1220+
}
11861221
return $buffer;
11871222
}
11881223
);
11891224

1225+
if ( $emit_action_errors ) {
1226+
add_action(
1227+
'wp_finalized_template_enhancement_output_buffer',
1228+
static function () use ( $emit_action_errors ): void {
1229+
$emit_action_errors();
1230+
}
1231+
);
1232+
}
1233+
11901234
$this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' );
11911235

11921236
?>

0 commit comments

Comments
 (0)