wordpress-develop/tests/phpunit/tests/comment/getPageOfComment.php
Boone Gorges 578134d4ff Use wp_installing() instead of WP_INSTALLING constant.
The `WP_INSTALLING` constant is a flag that WordPress sets in a number of
places, telling the system that options should be fetched directly from the
database instead of from the cache, that WP should not ping wordpress.org for
updates, that the normal "not installed" checks should be bypassed, and so on.

A constant is generally necessary for this purpose, because the flag is
typically set before the WP bootstrap, meaning that WP functions are not yet
available.  However, it is possible - notably, during `wpmu_create_blog()` -
for the "installing" flag to be set after WP has already loaded. In these
cases, `WP_INSTALLING` would be set for the remainder of the process, since
there's no way to change a constant once it's defined. This, in turn, polluted
later function calls that ought to have been outside the scope of site
creation, particularly the non-caching of option data. The problem was
particularly evident in the case of the automated tests, where `WP_INSTALLING`
was set the first time a site was created, and remained set for the rest of the
suite.

The new `wp_installing()` function allows developers to fetch the current
installation status (when called without any arguments) or to set the
installation status (when called with a boolean `true` or `false`). Use of
the `WP_INSTALLING` constant is still supported; `wp_installing()` will default
to `true` if the constant is defined during the bootstrap.

Props boonebgorges, jeremyfelt.
See #31130.

git-svn-id: https://develop.svn.wordpress.org/trunk@34828 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-05 15:05:26 +00:00

241 lines
8.9 KiB
PHP

<?php
/**
* @group comment
* @covers ::get_page_of_comment
*/
class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_last_comment() {
$p = $this->factory->post->create();
// page 4
$comment_last = $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-24 00:00:00' ) );
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-23 00:00:00' ) );
// page 3
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-22 00:00:00' ) );
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-21 00:00:00' ) );
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-20 00:00:00' ) );
// page 2
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-19 00:00:00' ) );
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-18 00:00:00' ) );
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-17 00:00:00' ) );
// page 1
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-16 00:00:00' ) );
$this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-15 00:00:00' ) );
$comment_first = $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-14 00:00:00' ) );
$this->assertEquals( 4, get_page_of_comment( $comment_last[0], array( 'per_page' => 3 ) ) );
$this->assertEquals( 2, get_page_of_comment( $comment_last[0], array( 'per_page' => 10 ) ) );
$this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 3 ) ) );
$this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) );
}
public function test_type_pings() {
$p = $this->factory->post->create();
$now = time();
$trackbacks = array();
for ( $i = 0; $i <= 3; $i++ ) {
$trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$now -= 10 * $i;
}
$pingbacks = array();
for ( $i = 0; $i <= 6; $i++ ) {
$pingbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'pingback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$now -= 10 * $i;
}
$this->assertEquals( 2, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'trackback' ) ) );
$this->assertEquals( 3, get_page_of_comment( $pingbacks[0], array( 'per_page' => 2, 'type' => 'pingback' ) ) );
$this->assertEquals( 5, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'pings' ) ) );
}
/**
* @ticket 11334
*/
public function test_subsequent_calls_should_hit_cache() {
global $wpdb;
$p = $this->factory->post->create();
$c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
$num_queries = $wpdb->num_queries;
$page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
$this->assertSame( $page_1, $page_2 );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 11334
*/
public function test_cache_hits_should_be_sensitive_to_comment_type() {
global $wpdb;
$p = $this->factory->post->create();
$comment = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'comment' ) );
$now = time();
$trackbacks = array();
for ( $i = 0; $i <= 5; $i++ ) {
$trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 10 * $i ) ) ) );
}
// Prime cache for trackbacks.
$page_trackbacks = get_page_of_comment( $trackbacks[1], array( 'per_page' => 3, 'type' => 'trackback' ) );
$this->assertEquals( 2, $page_trackbacks );
$num_queries = $wpdb->num_queries;
$page_comments = get_page_of_comment( $comment, array( 'per_page' => 3, 'type' => 'comment' ) );
$this->assertEquals( 1, $page_comments );
$this->assertNotEquals( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 11334
*/
public function test_cache_should_be_invalidated_when_comment_is_approved() {
$p = $this->factory->post->create();
$c = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
// Approve comment.
wp_set_comment_status( $c, 'approve' );
$this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
}
/**
* @ticket 11334
*/
public function test_cache_should_be_invalidated_when_comment_is_deleted() {
$p = $this->factory->post->create();
$c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
// Trash comment.
wp_trash_comment( $c );
$this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
}
/**
* @ticket 11334
*/
public function test_cache_should_be_invalidated_when_comment_is_spammed() {
$p = $this->factory->post->create();
$c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
// Spam comment.
wp_spam_comment( $c );
$this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
}
/**
* @ticket 11334
*/
public function test_cache_should_be_invalidated_when_older_comment_is_published() {
$now = time();
$p = $this->factory->post->create();
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
$this->assertEquals( 1, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
wp_set_comment_status( $c3, '1' );
$this->assertEquals( 2, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
}
/**
* @ticket 34057
*/
public function test_query_should_be_limited_to_comments_on_the_proper_post() {
$posts = $this->factory->post->create_many( 2 );
$now = time();
$comments_0 = $comments_1 = array();
for ( $i = 0; $i < 5; $i++ ) {
$comments_0[] = $this->factory->comment->create( array( 'comment_post_ID' => $posts[0], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
$comments_1[] = $this->factory->comment->create( array( 'comment_post_ID' => $posts[1], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
}
$found_0 = get_page_of_comment( $comments_0[0], array( 'per_page' => 2 ) );
$this->assertEquals( 3, $found_0 );
$found_1 = get_page_of_comment( $comments_1[1], array( 'per_page' => 2 ) );
$this->assertEquals( 2, $found_1 );
}
/**
* @ticket 13939
*/
public function test_only_top_level_comments_should_be_included_in_older_count() {
$post = $this->factory->post->create();
$now = time();
$comment_parents = $comment_children = array();
for ( $i = 0; $i < 5; $i++ ) {
$parent = $this->factory->comment->create( array( 'comment_post_ID' => $post, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
$comment_parents[ $i ] = $parent;
$child = $this->factory->comment->create( array( 'comment_post_ID' => $post, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 59 ) ), 'comment_parent' => $parent ) );
$comment_children[ $i ] = $child;
}
$page_1_indicies = array( 2, 3, 4 );
$page_2_indicies = array( 0, 1 );
$args = array(
'per_page' => 3,
'max_depth' => 2,
);
foreach ( $page_1_indicies as $p1i ) {
$this->assertSame( 1, (int) get_page_of_comment( $comment_parents[ $p1i ], $args ) );
$this->assertSame( 1, (int) get_page_of_comment( $comment_children[ $p1i ], $args ) );
}
foreach ( $page_2_indicies as $p2i ) {
$this->assertSame( 2, (int) get_page_of_comment( $comment_parents[ $p2i ], $args ) );
$this->assertSame( 2, (int) get_page_of_comment( $comment_children[ $p2i ], $args ) );
}
}
/**
* @ticket 13939
*/
public function test_comments_per_page_option_should_be_fallback_when_query_var_is_not_available() {
$now = time();
$p = $this->factory->post->create();
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
update_option( 'comments_per_page', 2 );
$this->assertEquals( 2, get_page_of_comment( $c1 ) );
}
}