Skip to content

Commit 55084d2

Browse files
committed
Add tests
1 parent bb0623d commit 55084d2

File tree

1 file changed

+87
-15
lines changed

1 file changed

+87
-15
lines changed

tests/phpunit/tests/template.php

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ public function test_wp_start_template_enhancement_output_buffer_begins_without_
594594
* Tests that wp_start_template_enhancement_output_buffer() does not start a buffer even when there are filters present due to override.
595595
*
596596
* @ticket 43258
597+
*
597598
* @covers ::wp_should_output_buffer_template_for_enhancement
598599
* @covers ::wp_start_template_enhancement_output_buffer
599600
*/
@@ -617,19 +618,34 @@ static function () {
617618
* an HTML document and that the response is not incrementally flushable.
618619
*
619620
* @ticket 43258
621+
* @ticket 64126
622+
*
620623
* @covers ::wp_start_template_enhancement_output_buffer
621624
* @covers ::wp_finalize_template_enhancement_output_buffer
622625
*/
623626
public function test_wp_start_template_enhancement_output_buffer_for_html(): void {
624627
// Start a wrapper output buffer so that we can flush the inner buffer.
625628
ob_start();
626629

627-
$filter_args = null;
630+
$mock_filter_callback = new MockAction();
628631
add_filter(
629632
'wp_template_enhancement_output_buffer',
630-
static function ( string $buffer ) use ( &$filter_args ): string {
631-
$filter_args = func_get_args();
633+
array( $mock_filter_callback, 'filter' ),
634+
10,
635+
PHP_INT_MAX
636+
);
632637

638+
$mock_action_callback = new MockAction();
639+
add_filter(
640+
'wp_send_late_headers',
641+
array( $mock_action_callback, 'action' ),
642+
10,
643+
PHP_INT_MAX
644+
);
645+
646+
add_filter(
647+
'wp_template_enhancement_output_buffer',
648+
static function ( string $buffer ): string {
633649
$p = WP_HTML_Processor::create_full_parser( $buffer );
634650
while ( $p->next_tag() ) {
635651
switch ( $p->get_tag() ) {
@@ -647,9 +663,7 @@ static function ( string $buffer ) use ( &$filter_args ): string {
647663
}
648664
}
649665
return $p->get_updated_html();
650-
},
651-
10,
652-
PHP_INT_MAX
666+
}
653667
);
654668

655669
$this->assertCount( 0, headers_list(), 'Expected no headers to have been sent during unit tests.' );
@@ -686,6 +700,8 @@ static function ( string $buffer ) use ( &$filter_args ): string {
686700
ob_end_flush(); // End the buffer started by wp_start_template_enhancement_output_buffer().
687701
$this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' );
688702

703+
$this->assertSame( 1, $mock_filter_callback->get_call_count(), 'Expected the wp_template_enhancement_output_buffer filter to have applied.' );
704+
$filter_args = $mock_filter_callback->get_args()[0];
689705
$this->assertIsArray( $filter_args, 'Expected the wp_template_enhancement_output_buffer filter to have applied.' );
690706
$this->assertCount( 2, $filter_args, 'Expected two args to be supplied to the wp_template_enhancement_output_buffer filter.' );
691707
$this->assertIsString( $filter_args[0], 'Expected the $filtered_output param to the wp_template_enhancement_output_buffer filter to be a string.' );
@@ -707,25 +723,36 @@ static function ( string $buffer ) use ( &$filter_args ): string {
707723
$this->assertStringContainsString( '<title>Saludo</title>', $processed_output, 'Expected processed output to contain string.' );
708724
$this->assertStringContainsString( '<h1>¡Hola, mundo!</h1>', $processed_output, 'Expected processed output to contain string.' );
709725
$this->assertStringContainsString( '</html>', $processed_output, 'Expected processed output to contain string.' );
726+
727+
$this->assertSame( 1, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to have fired.' );
728+
$this->assertSame( 1, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' );
729+
$action_args = $mock_action_callback->get_args()[0];
730+
$this->assertCount( 1, $action_args, 'Expected the wp_send_late_headers action to have been passed only one argument.' );
731+
$this->assertSame( $processed_output, $action_args[0], 'Expected the arg passed to wp_send_late_headers to be the same as the processed output buffer.' );
710732
}
711733

712734
/**
713735
* Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer but ending with cleaning prevents any processing.
714736
*
715737
* @ticket 43258
738+
* @ticket 64126
739+
*
716740
* @covers ::wp_start_template_enhancement_output_buffer
717741
* @covers ::wp_finalize_template_enhancement_output_buffer
718742
*/
719743
public function test_wp_start_template_enhancement_output_buffer_ended_cleaned(): void {
720744
// Start a wrapper output buffer so that we can flush the inner buffer.
721745
ob_start();
722746

723-
$applied_filter = false;
747+
$mock_filter_callback = new MockAction();
724748
add_filter(
725749
'wp_template_enhancement_output_buffer',
726-
static function ( string $buffer ) use ( &$applied_filter ): string {
727-
$applied_filter = true;
750+
array( $mock_filter_callback, 'filter' )
751+
);
728752

753+
add_filter(
754+
'wp_template_enhancement_output_buffer',
755+
static function ( string $buffer ): string {
729756
$p = WP_HTML_Processor::create_full_parser( $buffer );
730757
if ( $p->next_tag( array( 'tag_name' => 'TITLE' ) ) ) {
731758
$p->set_modifiable_text( 'Processed' );
@@ -734,6 +761,14 @@ static function ( string $buffer ) use ( &$applied_filter ): string {
734761
}
735762
);
736763

764+
$mock_action_callback = new MockAction();
765+
add_filter(
766+
'wp_send_late_headers',
767+
array( $mock_action_callback, 'action' ),
768+
10,
769+
PHP_INT_MAX
770+
);
771+
737772
$this->assertCount( 0, headers_list(), 'Expected no headers to have been sent during unit tests.' );
738773
ini_set( 'default_mimetype', 'text/html' ); // Since sending a header won't work.
739774

@@ -765,34 +800,41 @@ static function ( string $buffer ) use ( &$applied_filter ): string {
765800

766801
$this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' );
767802

768-
$this->assertFalse( $applied_filter, 'Expected the wp_template_enhancement_output_buffer filter to not have applied.' );
769-
$this->assertSame( 0, did_action( 'wp_final_template_output_buffer' ), 'Expected the wp_final_template_output_buffer action to not have fired.' );
803+
$this->assertSame( 0, $mock_filter_callback->get_call_count(), 'Expected the wp_template_enhancement_output_buffer filter to not have applied.' );
770804

771805
// Obtain the output via the wrapper output buffer.
772806
$output = ob_get_clean();
773807
$this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' );
774808
$this->assertStringNotContainsString( '<title>Unprocessed</title>', $output, 'Expected output buffer to not have string since the template was overridden.' );
775809
$this->assertStringNotContainsString( '<title>Processed</title>', $output, 'Expected output buffer to not have string since the filter did not apply.' );
776810
$this->assertStringContainsString( '<title>Output Buffer Not Processed</title>', $output, 'Expected output buffer to have string since the output buffer was ended with cleaning.' );
811+
812+
$this->assertSame( 0, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to not have fired.' );
813+
$this->assertSame( 0, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' );
777814
}
778815

779816
/**
780817
* Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer and cleaning allows the template to be replaced.
781818
*
782819
* @ticket 43258
820+
* @ticket 64126
821+
*
783822
* @covers ::wp_start_template_enhancement_output_buffer
784823
* @covers ::wp_finalize_template_enhancement_output_buffer
785824
*/
786825
public function test_wp_start_template_enhancement_output_buffer_cleaned_and_replaced(): void {
787826
// Start a wrapper output buffer so that we can flush the inner buffer.
788827
ob_start();
789828

790-
$called_filter = false;
829+
$mock_filter_callback = new MockAction();
791830
add_filter(
792831
'wp_template_enhancement_output_buffer',
793-
static function ( string $buffer ) use ( &$called_filter ): string {
794-
$called_filter = true;
832+
array( $mock_filter_callback, 'filter' )
833+
);
795834

835+
add_filter(
836+
'wp_template_enhancement_output_buffer',
837+
static function ( string $buffer ): string {
796838
$p = WP_HTML_Processor::create_full_parser( $buffer );
797839
if ( $p->next_tag( array( 'tag_name' => 'TITLE' ) ) ) {
798840
$p->set_modifiable_text( 'Processed' );
@@ -801,6 +843,14 @@ static function ( string $buffer ) use ( &$called_filter ): string {
801843
}
802844
);
803845

846+
$mock_action_callback = new MockAction();
847+
add_filter(
848+
'wp_send_late_headers',
849+
array( $mock_action_callback, 'action' ),
850+
10,
851+
PHP_INT_MAX
852+
);
853+
804854
$this->assertCount( 0, headers_list(), 'Expected no headers to have been sent during unit tests.' );
805855
ini_set( 'default_mimetype', 'application/xhtml+xml' ); // Since sending a header won't work.
806856

@@ -837,20 +887,28 @@ static function ( string $buffer ) use ( &$called_filter ): string {
837887
ob_end_flush(); // End the buffer started by wp_start_template_enhancement_output_buffer().
838888
$this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' );
839889

840-
$this->assertTrue( $called_filter, 'Expected the wp_template_enhancement_output_buffer filter to have applied.' );
890+
$this->assertSame( 1, $mock_filter_callback->get_call_count(), 'Expected the wp_template_enhancement_output_buffer filter to have applied.' );
841891

842892
// Obtain the output via the wrapper output buffer.
843893
$output = ob_get_clean();
844894
$this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' );
845895
$this->assertStringNotContainsString( '<title>Unprocessed</title>', $output, 'Expected output buffer to not have string due to template override.' );
846896
$this->assertStringContainsString( '<title>Processed</title>', $output, 'Expected output buffer to have string due to filtering.' );
847897
$this->assertStringContainsString( '<h1>Template Replaced</h1>', $output, 'Expected output buffer to have string due to replaced template.' );
898+
899+
$this->assertSame( 1, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to have fired.' );
900+
$this->assertSame( 1, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' );
901+
$action_args = $mock_action_callback->get_args()[0];
902+
$this->assertCount( 1, $action_args, 'Expected the wp_send_late_headers action to have been passed only one argument.' );
903+
$this->assertSame( $output, $action_args[0], 'Expected the arg passed to wp_send_late_headers to be the same as the processed output buffer.' );
848904
}
849905

850906
/**
851907
* Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer and that the output buffer is not processed.
852908
*
853909
* @ticket 43258
910+
* @ticket 64126
911+
*
854912
* @covers ::wp_start_template_enhancement_output_buffer
855913
* @covers ::wp_finalize_template_enhancement_output_buffer
856914
*/
@@ -861,6 +919,14 @@ public function test_wp_start_template_enhancement_output_buffer_for_json(): voi
861919
$mock_filter_callback = new MockAction();
862920
add_filter( 'wp_template_enhancement_output_buffer', array( $mock_filter_callback, 'filter' ) );
863921

922+
$mock_action_callback = new MockAction();
923+
add_filter(
924+
'wp_send_late_headers',
925+
array( $mock_action_callback, 'action' ),
926+
10,
927+
PHP_INT_MAX
928+
);
929+
864930
$initial_ob_level = ob_get_level();
865931
$this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' );
866932
$this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' );
@@ -894,6 +960,12 @@ public function test_wp_start_template_enhancement_output_buffer_for_json(): voi
894960
$output = ob_get_clean();
895961
$this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' );
896962
$this->assertSame( $json, $output, 'Expected output to not be processed.' );
963+
964+
$this->assertSame( 1, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to have fired even though the wp_template_enhancement_output_buffer filter did not apply.' );
965+
$this->assertSame( 1, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' );
966+
$action_args = $mock_action_callback->get_args()[0];
967+
$this->assertCount( 1, $action_args, 'Expected the wp_send_late_headers action to have been passed only one argument.' );
968+
$this->assertSame( $output, $action_args[0], 'Expected the arg passed to wp_send_late_headers to be the same as the processed output buffer.' );
897969
}
898970

899971
/**

0 commit comments

Comments
 (0)