Skip to content

Commit

Permalink
Add tests for AMP_Theme_Support::is_output_buffering()
Browse files Browse the repository at this point in the history
Improve explanation for shutdown check in can_output_buffer.
  • Loading branch information
westonruter committed Apr 29, 2018
1 parent 75c1ba1 commit 3d27037
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion includes/utils/class-amp-validation-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ public static function can_output_buffer() {
return false;
}

// Abort when in shutdown since printing we know to be in buffering display handler, and no output allowed anyway.
// Abort when in shutdown since output has finished, when we're likely in the overall output buffering display handler.
if ( did_action( 'shutdown' ) ) {
return false;
}
Expand Down
19 changes: 11 additions & 8 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,8 @@ public function test_dequeue_customize_preview_scripts() {
* Test start_output_buffering.
*
* @covers AMP_Theme_Support::start_output_buffering()
* @covers AMP_Theme_Support::is_output_buffering()
* @covers AMP_Theme_Support::finish_output_buffering()
*/
public function test_start_output_buffering() {
if ( ! function_exists( 'newrelic_disable_autorum ' ) ) {
Expand All @@ -894,21 +896,18 @@ function newrelic_disable_autorum() {
AMP_Theme_Support::init();
AMP_Theme_Support::finish_init();

$ob_level = ob_get_level();
$initial_ob_level = ob_get_level();
AMP_Theme_Support::start_output_buffering();

$this->assertTrue( ob_get_level() > $ob_level );

// End output buffer.
if ( ob_get_level() > $ob_level ) {
ob_get_clean();
}
$this->assertEquals( $initial_ob_level + 1, ob_get_level() );
ob_end_flush();
$this->assertEquals( $initial_ob_level, ob_get_level() );
}

/**
* Test finish_output_buffering.
*
* @covers AMP_Theme_Support::finish_output_buffering()
* @covers AMP_Theme_Support::is_output_buffering()
*/
public function test_finish_output_buffering() {
add_theme_support( 'amp' );
Expand All @@ -918,10 +917,12 @@ public function test_finish_output_buffering() {
$status = ob_get_status();
$this->assertSame( 1, ob_get_level() );
$this->assertEquals( 'default output handler', $status['name'] );
$this->assertFalse( AMP_Theme_Support::is_output_buffering() );

// start first layer buffer.
ob_start();
AMP_Theme_Support::start_output_buffering();
$this->assertTrue( AMP_Theme_Support::is_output_buffering() );
$this->assertSame( 3, ob_get_level() );

echo '<img src="test.png"><script data-test>document.write(\'Illegal\');</script>';
Expand All @@ -934,9 +935,11 @@ public function test_finish_output_buffering() {
} );
echo 'bar';

$this->assertTrue( AMP_Theme_Support::is_output_buffering() );
while ( ob_get_level() > 2 ) {
ob_end_flush();
}
$this->assertFalse( AMP_Theme_Support::is_output_buffering() );
$output = ob_get_clean();
$this->assertEquals( 1, ob_get_level() );

Expand Down

0 comments on commit 3d27037

Please sign in to comment.