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
+72 -72
View File
@@ -13,7 +13,7 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
}
public function test_get_object_terms_by_slug() {
$post_id = $this->factory->post->create();
$post_id = self::$factory->post->create();
$terms_1 = array('Foo', 'Bar', 'Baz');
$terms_1_slugs = array('foo', 'bar', 'baz');
@@ -31,10 +31,10 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 11003
*/
public function test_should_not_filter_out_duplicate_terms_associated_with_different_objects() {
$post_id1 = $this->factory->post->create();
$post_id2 = $this->factory->post->create();
$cat_id = $this->factory->category->create();
$cat_id2 = $this->factory->category->create();
$post_id1 = self::$factory->post->create();
$post_id2 = self::$factory->post->create();
$cat_id = self::$factory->category->create();
$cat_id2 = self::$factory->category->create();
wp_set_post_categories( $post_id1, array( $cat_id, $cat_id2 ) );
wp_set_post_categories( $post_id2, $cat_id );
@@ -54,7 +54,7 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 17646
*/
public function test_should_return_objects_with_int_properties() {
$post_id = $this->factory->post->create();
$post_id = self::$factory->post->create();
$term = wp_insert_term( 'one', $this->taxonomy );
wp_set_object_terms( $post_id, $term, $this->taxonomy );
@@ -73,7 +73,7 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 26339
*/
public function test_references_should_be_reset_after_wp_get_object_terms_filter() {
$post_id = $this->factory->post->create();
$post_id = self::$factory->post->create();
$terms_1 = array('foo', 'bar', 'baz');
wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
@@ -86,17 +86,17 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
}
public function test_orderby_name() {
$p = $this->factory->post->create();
$p = self::$factory->post->create();
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
) );
@@ -112,17 +112,17 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
}
public function test_orderby_count() {
$posts = $this->factory->post->create_many( 3 );
$posts = self::$factory->post->create_many( 3 );
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
) );
@@ -140,17 +140,17 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
}
public function test_orderby_slug() {
$p = $this->factory->post->create();
$p = self::$factory->post->create();
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'slug' => 'aaa',
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'slug' => 'zzz',
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'slug' => 'jjj',
) );
@@ -166,15 +166,15 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
}
public function test_orderby_term_group() {
$p = $this->factory->post->create();
$p = self::$factory->post->create();
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
@@ -195,15 +195,15 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
}
public function test_orderby_term_order() {
$p = $this->factory->post->create();
$p = self::$factory->post->create();
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
@@ -231,15 +231,15 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 28688
*/
public function test_orderby_parent() {
$p = $this->factory->post->create();
$p = self::$factory->post->create();
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
@@ -269,15 +269,15 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
register_taxonomy( 'wptests_tax_2', 'post' );
register_taxonomy( 'wptests_tax_3', 'post' );
$p = $this->factory->post->create();
$p = self::$factory->post->create();
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => 'wptests_tax_3',
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => 'wptests_tax_2',
) );
@@ -297,15 +297,15 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 28688
*/
public function test_orderby_tt_id() {
$p = $this->factory->post->create();
$p = self::$factory->post->create();
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
@@ -330,17 +330,17 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
}
public function test_order_desc() {
$p = $this->factory->post->create();
$p = self::$factory->post->create();
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
) );
@@ -360,22 +360,22 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 15675
*/
public function test_parent() {
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'parent' => $t1,
) );
$t4 = $this->factory->term->create( array(
$t4 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'parent' => $t2,
) );
$p = $this->factory->post->create();
$p = self::$factory->post->create();
wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
@@ -391,22 +391,22 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 15675
*/
public function test_parent_0() {
$t1 = $this->factory->term->create( array(
$t1 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = $this->factory->term->create( array(
$t2 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = $this->factory->term->create( array(
$t3 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'parent' => $t1,
) );
$t4 = $this->factory->term->create( array(
$t4 = self::$factory->term->create( array(
'taxonomy' => $this->taxonomy,
'parent' => $t2,
) );
$p = $this->factory->post->create();
$p = self::$factory->post->create();
wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
@@ -425,12 +425,12 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
$terms = self::$factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'bar' );
$p = $this->factory->post->create();
$p = self::$factory->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax' );
@@ -451,12 +451,12 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
$terms = self::$factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'bar' );
$p = $this->factory->post->create();
$p = self::$factory->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
@@ -477,13 +477,13 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
*/
public function test_meta_query() {
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
$terms = self::$factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'baz' );
add_term_meta( $terms[3], 'foob', 'ar' );
$p = $this->factory->post->create();
$p = self::$factory->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
@@ -503,8 +503,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
*/
public function test_should_return_wp_term_objects_for_fields_all() {
register_taxonomy( 'wptests_tax', 'post' );
$p = $this->factory->post->create();
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$p = self::$factory->post->create();
$t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $t, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
@@ -522,8 +522,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
*/
public function test_should_return_wp_term_objects_for_fields_all_with_object_id() {
register_taxonomy( 'wptests_tax', 'post' );
$p = $this->factory->post->create();
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$p = self::$factory->post->create();
$t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $t, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
@@ -543,8 +543,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$p = $this->factory->post->create();
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$p = self::$factory->post->create();
$t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $t, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
@@ -561,8 +561,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
*/
public function test_object_id_should_not_be_cached_with_term_object() {
register_taxonomy( 'wptests_tax', 'post' );
$p = $this->factory->post->create();
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$p = self::$factory->post->create();
$t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $t, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
@@ -585,9 +585,9 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
register_taxonomy( 'wptests_tax1', 'post' );
register_taxonomy( 'wptests_tax2', 'post' );
$p = $this->factory->post->create();
$t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
$t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
$p = self::$factory->post->create();
$t1 = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
$t2 = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
wp_set_object_terms( $p, $t1, 'wptests_tax1' );
wp_set_object_terms( $p, $t2, 'wptests_tax2' );
@@ -611,8 +611,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
*/
public function test_object_id_should_be_set_on_objects_that_share_terms() {
register_taxonomy( 'wptests_tax', 'post' );
$posts = $this->factory->post->create_many( 2 );
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$posts = self::$factory->post->create_many( 2 );
$t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $posts[0], $t, 'wptests_tax' );
wp_set_object_terms( $posts[1], $t, 'wptests_tax' );