Unit Tests: one $factory to rule them all, and it shall be static.

Using more than one instance of `WP_UnitTest_Factory` causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use `setUpBeforeClass`, we were creating ad hoc instances. To avoid that, we were injecting one `static` instance via Dependency Injection in `wpSetUpBeforeClass`. All tests should really use the `static` instance, so we will remove the instance prop `$factory`.

Replace `$this->factory` with `self::$factory` over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values. 

#YOLOFriday



git-svn-id: https://develop.svn.wordpress.org/trunk@35225 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2015-10-16 21:04:12 +00:00
parent 84272ff8cd
commit e70ebea219
169 changed files with 2631 additions and 2616 deletions

View File

@@ -7,26 +7,26 @@
class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
public function test_last_comment() {
$p = $this->factory->post->create();
$p = self::$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' ) );
$comment_last = self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-24 00:00:00' ) );
self::$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' ) );
self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-22 00:00:00' ) );
self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-21 00:00:00' ) );
self::$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' ) );
self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-19 00:00:00' ) );
self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-18 00:00:00' ) );
self::$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' ) );
self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-16 00:00:00' ) );
self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-15 00:00:00' ) );
$comment_first = self::$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 ) ) );
@@ -36,18 +36,18 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
}
public function test_type_pings() {
$p = $this->factory->post->create();
$p = self::$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 ) ) );
$trackbacks[ $i ] = self::$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 ) ) );
$pingbacks[ $i ] = self::$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;
}
@@ -62,8 +62,8 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
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 ) );
$p = self::$factory->post->create();
$c = self::$factory->comment->create( array( 'comment_post_ID' => $p ) );
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
@@ -81,13 +81,13 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
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' ) );
$p = self::$factory->post->create();
$comment = self::$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 ) ) ) );
$trackbacks[ $i ] = self::$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.
@@ -105,8 +105,8 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
* @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 ) );
$p = self::$factory->post->create();
$c = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
@@ -121,8 +121,8 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
* @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 ) );
$p = self::$factory->post->create();
$c = self::$factory->comment->create( array( 'comment_post_ID' => $p ) );
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
@@ -137,8 +137,8 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
* @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 ) );
$p = self::$factory->post->create();
$c = self::$factory->comment->create( array( 'comment_post_ID' => $p ) );
// Prime cache.
$page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
@@ -155,10 +155,10 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
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 ) ) );
$p = self::$factory->post->create();
$c1 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$c2 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
$c3 = self::$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 ) ) );
@@ -171,13 +171,13 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
* @ticket 34057
*/
public function test_query_should_be_limited_to_comments_on_the_proper_post() {
$posts = $this->factory->post->create_many( 2 );
$posts = self::$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 ) ) ) );
$comments_0[] = self::$factory->comment->create( array( 'comment_post_ID' => $posts[0], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
$comments_1[] = self::$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 ) );
@@ -191,15 +191,15 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
* @ticket 13939
*/
public function test_only_top_level_comments_should_be_included_in_older_count() {
$post = $this->factory->post->create();
$post = self::$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 ) ) ) );
$parent = self::$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 ) );
$child = self::$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;
}
@@ -228,10 +228,10 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
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 ) ) );
$p = self::$factory->post->create();
$c1 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
$c2 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
$c3 = self::$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 );