Skip to content

Commit

Permalink
Merge pull request #1040 from Automattic/amedina/check-for-empty-anal…
Browse files Browse the repository at this point in the history
…ytics-arg

Check if  argument is empty and initialize it properly
  • Loading branch information
westonruter authored Mar 23, 2018
2 parents 7870892 + f29146f commit b2e24ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,12 @@ function amp_get_analytics( $analytics = array() ) {
*
* @since 0.7
*
* @param array $analytics Analytics entries.
* @param array|string $analytics Analytics entries, or empty string when called via wp_footer action.
*/
function amp_print_analytics( $analytics ) {
if ( '' === $analytics ) {
$analytics = array();
}
$analytics_entries = amp_get_analytics( $analytics );

if ( empty( $analytics_entries ) ) {
Expand Down
29 changes: 28 additions & 1 deletion tests/test-amp-analytics-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,34 @@ public function test_amp_print_analytics() {
amp_print_analytics( $analytics );
$output = ob_get_clean();

$this->assertEquals( 0, strpos( $output, '<amp-analytics' ) );
$this->assertStringStartsWith( '<amp-analytics', $output );
$this->assertContains( 'type="googleanalytics"><script type="application/json">{"requests":{"event":', $output );
}

/**
* Test amp_print_analytics() when empty, called via wp_footer.
*
* Note that wp_footer action passes empty string to any handlers.
* This test asserts that an issue discovered in PHP 7.1 is fixed.
*
* @see AMP_Theme_Support::add_hooks() Where add_action( 'wp_footer', 'amp_print_analytics' ) is done.
* @covers \amp_print_analytics()
*/
public function test_amp_print_analytics_when_empty() {

ob_start();
amp_print_analytics( '' );
$this->assertEmpty( ob_get_clean() );

$this->insert_one_option(
$this->vendor,
$this->config_one
);
ob_start();
amp_print_analytics( '' );
$output = ob_get_clean();
$this->assertStringStartsWith( '<amp-analytics', $output );
$this->assertContains( 'type="googleanalytics"><script type="application/json">{"requests":{"event":', $output );
}

}

0 comments on commit b2e24ce

Please sign in to comment.