mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
Better flexibility for 'type' in WP_Comment_Query.
* Add support for an array of values in 'type'. * Introduce `type__in` parameter. This duplicates 'type' but is added for better consistency with other query classes. * Introduce `type__not_in`. Among other things, these changes will make it easier for plugin authors to manage the appearance of custom comment types in various WP interfaces. Props dancameron, mordauk. See #12668. git-svn-id: https://develop.svn.wordpress.org/trunk@30096 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -15,6 +15,372 @@ class Tests_Comment_Query extends WP_UnitTestCase {
|
||||
$this->post_id = $this->factory->post->create();
|
||||
}
|
||||
|
||||
public function test_query() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_query_type_empty_string() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => '',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_query_type_comment() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => 'comment',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1 ), $found );
|
||||
}
|
||||
|
||||
public function test_query_type_pingback() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => 'pingback',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2, $c3 ), $found );
|
||||
|
||||
}
|
||||
|
||||
public function test_query_type_trackback() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => 'trackback',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2, $c3 ), $found );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 'pings' is an alias for 'trackback' + 'pingback'.
|
||||
*/
|
||||
public function test_query_type_pings() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => 'pings',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2, $c3 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* Comments and custom
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_comments_and_custom() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
$c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'comments', 'mario' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1, $c4, $c6 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_not__in_array_custom() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
$c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type__not_in' => array( 'luigi' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1, $c2, $c3, $c4, $c6 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type__in_array_and_not_type_array_custom() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
$c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type__in' => array( 'comments' ),
|
||||
'type__not_in' => array( 'luigi' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_and_type__not_in_array_custom() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
$c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'pings' ),
|
||||
'type__not_in' => array( 'mario' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2, $c3 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type__not_in_custom() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
$c6 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type__not_in' => 'luigi',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1, $c2, $c3, $c4, $c6 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_comments_and_pings() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
|
||||
$c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'comments', 'pings' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1, $c2, $c3 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_comment_pings() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'comment', 'pings' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c1, $c2, $c3 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_pingback() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'pingback' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_custom_pingpack() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'peach', 'pingback' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_pings() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'pings' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2, $c3 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_status_approved_array_comment_pings() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0', 'comment_type' => 'pingback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'status' => 'approve',
|
||||
'type' => array( 'pings' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c3, $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_trackback() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'trackback' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_custom_trackback() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'type' => array( 'toad', 'trackback' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 12668
|
||||
*/
|
||||
public function test_type_array_pings_approved() {
|
||||
$c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
|
||||
$c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
|
||||
$c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0', 'comment_type' => 'trackback' ) );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'status' => 'approve',
|
||||
'type' => array( 'pings' ),
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $c3, $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 29612
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user