wordpress-develop/tests/phpunit/tests/post/getTheContent.php
Boone Gorges 30c21d2031 Posts: Avoid the use of globals in get_the_content() and related functions.
This changeset introduces `$post` parameters to `get_the_content()` and
`wp_trim_excerpt()`. When a `$post` object is passed to one of these functions,
the functions will operate on the data from that object, rather than from the
post globals (`$authordata`, `$page`, etc). This ensures that the functions work
in a predictable manner when used outside of the regular post loop.

The global-mismatch problem is surfaced in cases where `get_the_excerpt()` is
called outside of the post loop, on posts that don't have a defined excerpt. In
these cases, the post globals - used to generate a fallback excerpt - may refer
to the incorrect object, resulting in PHP notices or other unpredictable
behavior. See #36934 for a related issue.

Props spacedmonkey, kraftbj, Shital Patel.
Fixes #42814.

git-svn-id: https://develop.svn.wordpress.org/trunk@44941 602fd350-edb4-49c9-b593-d223f7449a82
2019-03-20 15:48:46 +00:00

79 lines
2.0 KiB
PHP

<?php
/**
* @group post
* @group formatting
*/
class Tests_Post_GetTheContent extends WP_UnitTestCase {
/**
* @ticket 42814
*/
public function test_argument_back_compat_more_link_text() {
$text = 'Foo<!--more-->Bar';
$p = self::factory()->post->create( array( 'post_content' => $text ) );
$q = new WP_Query( array( 'p' => $p ) );
while ( $q->have_posts() ) {
$q->the_post();
$found = get_the_content( 'Ping' );
}
$this->assertContains( '>Ping<', $found );
}
/**
* @ticket 42814
*/
public function test_argument_back_compat_strip_teaser() {
$text = 'Foo<!--more-->Bar';
$p = self::factory()->post->create( array( 'post_content' => $text ) );
$this->go_to( get_permalink( $p ) );
$q = new WP_Query( array( 'p' => $p ) );
while ( $q->have_posts() ) {
$q->the_post();
$found = get_the_content( null, true );
}
$this->assertNotContains( 'Foo', $found );
}
/**
* @ticket 42814
*/
public function test_content_other_post() {
$text_1 = 'Foo<!--nextpage-->Bar<!--nextpage-->Baz';
$post_1 = self::factory()->post->create_and_get( array( 'post_content' => $text_1 ) );
$text_2 = 'Bing<!--nextpage-->Bang<!--nextpage-->Boom';
$post_2 = self::factory()->post->create_and_get( array( 'post_content' => $text_2 ) );
setup_postdata( $post_1 );
$found = get_the_content( null, true, $post_2 );
$this->assertSame( 'Bing', $found );
}
/**
* @ticket 42814
*/
public function test_should_respect_pagination_of_inner_post() {
$text_1 = 'Foo<!--nextpage-->Bar<!--nextpage-->Baz';
$post_1 = self::factory()->post->create_and_get( array( 'post_content' => $text_1 ) );
$text_2 = 'Bing<!--nextpage-->Bang<!--nextpage-->Boom';
$post_2 = self::factory()->post->create_and_get( array( 'post_content' => $text_2 ) );
$go_to = add_query_arg( 'page', '2', get_permalink( $post_1->ID ) );
$this->go_to( $go_to );
while ( have_posts() ) {
the_post();
$found = get_the_content( '', false, $post_2 );
}
$this->assertSame( 'Bang', $found );
}
}