mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-16 23:00:21 +00:00
Move PHPUnit tests into a tests/phpunit directory.
wp-tests-config.php can/should reside in the root of a develop checkout. `phpunit` should be run from the root. see #25088. git-svn-id: https://develop.svn.wordpress.org/trunk@25165 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Tests to make sure querying posts based on various date parameters
|
||||
* using "date_query" works as expected.
|
||||
*
|
||||
* No need to do a full repeat of all of the post tests again since
|
||||
* the query SQL is the same for both just with a different column.
|
||||
*
|
||||
* @ticket 18694
|
||||
*
|
||||
* @group comment
|
||||
* @group date
|
||||
* @group datequery
|
||||
*/
|
||||
class Tests_Comment_DateQuery extends WP_UnitTestCase {
|
||||
|
||||
public $posts = array();
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Just some dummy posts to use as parents for comments
|
||||
for ( $i = 1; $i <= 2; $i++ ) {
|
||||
$this->posts[$i] = $this->factory->post->create();
|
||||
}
|
||||
|
||||
// Be careful modifying this. Tests are coded to expect this exact sample data.
|
||||
// Format is 'datetime' => 'post number (not ID)'
|
||||
$comment_dates = array(
|
||||
'2007-01-22 03:49:21' => 1,
|
||||
'2007-05-16 17:32:22' => 1,
|
||||
'2007-09-24 07:17:23' => 1,
|
||||
'2008-03-29 09:04:25' => 1,
|
||||
'2008-07-15 11:32:26' => 2, // This one should never be in the results
|
||||
'2008-12-10 13:06:27' => 1,
|
||||
'2009-06-11 21:30:28' => 1,
|
||||
'2009-12-18 10:42:29' => 1,
|
||||
);
|
||||
|
||||
foreach ( $comment_dates as $comment_date => $comment_parent ) {
|
||||
$result = $this->factory->comment->create( array(
|
||||
'comment_date' => $comment_date,
|
||||
'comment_post_ID' => $this->posts[ $comment_parent ],
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
public function _get_query_result( $args = array() ) {
|
||||
$args = wp_parse_args( $args, array(
|
||||
'post_id' => $this->posts[1],
|
||||
'orderby' => 'comment_ID', // Same order they were created
|
||||
'order' => 'ASC',
|
||||
) );
|
||||
|
||||
return get_comments( $args );
|
||||
}
|
||||
|
||||
public function test_year() {
|
||||
$comments = $this->_get_query_result( array(
|
||||
'date_query' => array(
|
||||
array(
|
||||
'year' => 2008,
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$expected_dates = array(
|
||||
'2008-03-29 09:04:25',
|
||||
'2008-12-10 13:06:27',
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected_dates, wp_list_pluck( $comments, 'comment_date' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
// Test the output of Comment Querying functions
|
||||
|
||||
/**
|
||||
* @group comment
|
||||
*/
|
||||
class Tests_Comment_Query extends WP_UnitTestCase {
|
||||
var $comment_id;
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21101
|
||||
*/
|
||||
function test_get_comment_comment_approved_0() {
|
||||
$comment_id = $this->factory->comment->create();
|
||||
$comments_approved_0 = get_comments( array( 'status' => 'hold' ) );
|
||||
$this->assertEquals( 0, count( $comments_approved_0 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21101
|
||||
*/
|
||||
function test_get_comment_comment_approved_1() {
|
||||
$comment_id = $this->factory->comment->create();
|
||||
$comments_approved_1 = get_comments( array( 'status' => 'approve' ) );
|
||||
|
||||
$this->assertEquals( 1, count( $comments_approved_1 ) );
|
||||
$result = $comments_approved_1[0];
|
||||
|
||||
$this->assertEquals( $comment_id, $result->comment_ID );
|
||||
}
|
||||
|
||||
function test_get_comments_for_post() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$this->factory->comment->create_post_comments( $post_id, 10 );
|
||||
$comments = get_comments( array( 'post_id' => $post_id ) );
|
||||
$this->assertEquals( 10, count( $comments ) );
|
||||
foreach ( $comments as $comment ) {
|
||||
$this->assertEquals( $post_id, $comment->comment_post_ID );
|
||||
}
|
||||
|
||||
$post_id2 = $this->factory->post->create();
|
||||
$this->factory->comment->create_post_comments( $post_id2, 10 );
|
||||
$comments = get_comments( array( 'post_id' => $post_id2 ) );
|
||||
$this->assertEquals( 10, count( $comments ) );
|
||||
foreach ( $comments as $comment ) {
|
||||
$this->assertEquals( $post_id2, $comment->comment_post_ID );
|
||||
}
|
||||
|
||||
$post_id3 = $this->factory->post->create();
|
||||
$this->factory->comment->create_post_comments( $post_id3, 10, array( 'comment_approved' => '0' ) );
|
||||
$comments = get_comments( array( 'post_id' => $post_id3 ) );
|
||||
$this->assertEquals( 10, count( $comments ) );
|
||||
foreach ( $comments as $comment ) {
|
||||
$this->assertEquals( $post_id3, $comment->comment_post_ID );
|
||||
}
|
||||
|
||||
$comments = get_comments( array( 'post_id' => $post_id3, 'status' => 'hold' ) );
|
||||
$this->assertEquals( 10, count( $comments ) );
|
||||
foreach ( $comments as $comment ) {
|
||||
$this->assertEquals( $post_id3, $comment->comment_post_ID );
|
||||
}
|
||||
|
||||
$comments = get_comments( array( 'post_id' => $post_id3, 'status' => 'approve' ) );
|
||||
$this->assertEquals( 0, count( $comments ) );
|
||||
|
||||
$this->factory->comment->create_post_comments( $post_id3, 10, array( 'comment_approved' => '1' ) );
|
||||
$comments = get_comments( array( 'post_id' => $post_id3 ) );
|
||||
$this->assertEquals( 20, count( $comments ) );
|
||||
foreach ( $comments as $comment ) {
|
||||
$this->assertEquals( $post_id3, $comment->comment_post_ID );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21003
|
||||
*/
|
||||
function test_orderby_meta() {
|
||||
$comment_id = $this->factory->comment->create();
|
||||
$comment_id2 = $this->factory->comment->create();
|
||||
$comment_id3 = $this->factory->comment->create();
|
||||
|
||||
add_comment_meta( $comment_id, 'key', 'value1', true );
|
||||
add_comment_meta( $comment_id, 'key1', 'value1', true );
|
||||
add_comment_meta( $comment_id, 'key3', 'value3', true );
|
||||
add_comment_meta( $comment_id2, 'key', 'value2', true );
|
||||
add_comment_meta( $comment_id2, 'key2', 'value2', true );
|
||||
add_comment_meta( $comment_id3, 'key3', 'value3', true );
|
||||
|
||||
$comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'key' ) ) );
|
||||
$this->assertEquals( 2, count( $comments ) );
|
||||
$this->assertEquals( $comment_id2, $comments[0]->comment_ID );
|
||||
$this->assertEquals( $comment_id, $comments[1]->comment_ID );
|
||||
|
||||
$comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'meta_value' ) ) );
|
||||
$this->assertEquals( 2, count( $comments ) );
|
||||
$this->assertEquals( $comment_id2, $comments[0]->comment_ID );
|
||||
$this->assertEquals( $comment_id, $comments[1]->comment_ID );
|
||||
|
||||
$comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'key' ), 'order' => 'ASC' ) );
|
||||
$this->assertEquals( 2, count( $comments ) );
|
||||
$this->assertEquals( $comment_id, $comments[0]->comment_ID );
|
||||
$this->assertEquals( $comment_id2, $comments[1]->comment_ID );
|
||||
|
||||
$comments = get_comments( array( 'meta_key' => 'key', 'orderby' => array( 'meta_value' ), 'order' => 'ASC' ) );
|
||||
$this->assertEquals( 2, count( $comments ) );
|
||||
$this->assertEquals( $comment_id, $comments[0]->comment_ID );
|
||||
$this->assertEquals( $comment_id2, $comments[1]->comment_ID );
|
||||
|
||||
$comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'key' ) ) );
|
||||
$this->assertEquals( 2, count( $comments ) );
|
||||
$this->assertEquals( $comment_id, $comments[0]->comment_ID );
|
||||
$this->assertEquals( $comment_id3, $comments[1]->comment_ID );
|
||||
|
||||
$comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'meta_value' ) ) );
|
||||
$this->assertEquals( 2, count( $comments ) );
|
||||
$this->assertEquals( $comment_id, $comments[0]->comment_ID );
|
||||
$this->assertEquals( $comment_id3, $comments[1]->comment_ID );
|
||||
|
||||
// value1 is present on two different keys for $comment_id yet we should get only one instance
|
||||
// of that comment in the results
|
||||
$comments = get_comments( array( 'meta_value' => 'value1', 'orderby' => array( 'key' ) ) );
|
||||
$this->assertEquals( 1, count( $comments ) );
|
||||
|
||||
$comments = get_comments( array( 'meta_value' => 'value1', 'orderby' => array( 'meta_value' ) ) );
|
||||
$this->assertEquals( 1, count( $comments ) );
|
||||
}
|
||||
|
||||
function test_get_status() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$this->factory->comment->create_post_comments( $post_id, 10 );
|
||||
|
||||
$post_id2 = $this->factory->post->create();
|
||||
$this->factory->comment->create_post_comments( $post_id2, 10 );
|
||||
|
||||
$post_id3 = $this->factory->post->create();
|
||||
$this->factory->comment->create_post_comments( $post_id3, 10, array( 'comment_approved' => '0' ) );
|
||||
|
||||
$post_id4 = $this->factory->post->create();
|
||||
$this->factory->comment->create_post_comments( $post_id4, 10, array( 'comment_approved' => 'trash' ) );
|
||||
|
||||
$post_id5 = $this->factory->post->create();
|
||||
$this->factory->comment->create_post_comments( $post_id5, 10, array( 'comment_approved' => 'spam' ) );
|
||||
|
||||
$this->assertEquals( 30, count( get_comments() ) );
|
||||
$this->assertEquals( 30, count( get_comments( array( 'status' => 'all' ) ) ) );
|
||||
$this->assertEquals( 20, count( get_comments( array( 'status' => 'approve' ) ) ) );
|
||||
$this->assertEquals( 10, count( get_comments( array( 'status' => 'hold' ) ) ) );
|
||||
$this->assertEquals( 10, count( get_comments( array( 'status' => 'trash' ) ) ) );
|
||||
$this->assertEquals( 10, count( get_comments( array( 'status' => 'spam' ) ) ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group comment
|
||||
* @group slashes
|
||||
* @ticket 21767
|
||||
*/
|
||||
class Tests_Comment_Slashes extends WP_UnitTestCase {
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
// we need an admin user to bypass comment flood protection
|
||||
$this->author_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
|
||||
$this->old_current_user = get_current_user_id();
|
||||
wp_set_current_user( $this->author_id );
|
||||
|
||||
// it is important to test with both even and odd numbered slashes as
|
||||
// kses does a strip-then-add slashes in some of it's function calls
|
||||
$this->slash_1 = 'String with 1 slash \\';
|
||||
$this->slash_2 = 'String with 2 slashes \\\\';
|
||||
$this->slash_3 = 'String with 3 slashes \\\\\\';
|
||||
$this->slash_4 = 'String with 4 slashes \\\\\\\\';
|
||||
$this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
|
||||
$this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
|
||||
$this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
wp_set_current_user( $this->old_current_user );
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the extended model function that expects slashed data
|
||||
*
|
||||
*/
|
||||
function test_wp_new_comment() {
|
||||
$post_id = $this->factory->post->create();
|
||||
|
||||
// not testing comment_author_email or comment_author_url
|
||||
// as slashes are not permitted in that data
|
||||
$data = array(
|
||||
'comment_post_ID' => $post_id,
|
||||
'comment_author' => $this->slash_1,
|
||||
'comment_content' => $this->slash_7,
|
||||
);
|
||||
$id = wp_new_comment( $data );
|
||||
|
||||
$comment = get_comment($id);
|
||||
|
||||
$this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
|
||||
$this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
|
||||
|
||||
$data = array(
|
||||
'comment_post_ID' => $post_id,
|
||||
'comment_author' => $this->slash_2,
|
||||
'comment_content' => $this->slash_4,
|
||||
);
|
||||
$id = wp_new_comment( $data );
|
||||
|
||||
$comment = get_comment($id);
|
||||
|
||||
$this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
|
||||
$this->assertEquals( wp_unslash( $this->slash_4 ), $comment->comment_content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the controller function that expects slashed data
|
||||
*
|
||||
*/
|
||||
function test_edit_comment() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$comment_id = $this->factory->comment->create(array(
|
||||
'comment_post_ID' => $post_id
|
||||
));
|
||||
|
||||
// not testing comment_author_email or comment_author_url
|
||||
// as slashes are not permitted in that data
|
||||
$_POST = array();
|
||||
$_POST['comment_ID'] = $comment_id;
|
||||
$_POST['newcomment_author'] = $this->slash_1;
|
||||
$_POST['content'] = $this->slash_7;
|
||||
$_POST = add_magic_quotes( $_POST );
|
||||
|
||||
edit_comment();
|
||||
$comment = get_comment( $comment_id );
|
||||
|
||||
$this->assertEquals( $this->slash_1, $comment->comment_author );
|
||||
$this->assertEquals( $this->slash_7, $comment->comment_content );
|
||||
|
||||
$_POST = array();
|
||||
$_POST['comment_ID'] = $comment_id;
|
||||
$_POST['newcomment_author'] = $this->slash_2;
|
||||
$_POST['content'] = $this->slash_4;
|
||||
$_POST = add_magic_quotes( $_POST );
|
||||
|
||||
edit_comment();
|
||||
$comment = get_comment( $comment_id );
|
||||
|
||||
$this->assertEquals( $this->slash_2, $comment->comment_author );
|
||||
$this->assertEquals( $this->slash_4, $comment->comment_content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the model function that expects slashed data
|
||||
*
|
||||
*/
|
||||
function test_wp_insert_comment() {
|
||||
$post_id = $this->factory->post->create();
|
||||
|
||||
$comment_id = wp_insert_comment(array(
|
||||
'comment_post_ID' => $post_id,
|
||||
'comment_author' => $this->slash_1,
|
||||
'comment_content' => $this->slash_7,
|
||||
));
|
||||
$comment = get_comment( $comment_id );
|
||||
|
||||
$this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
|
||||
$this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
|
||||
|
||||
$comment_id = wp_insert_comment(array(
|
||||
'comment_post_ID' => $post_id,
|
||||
'comment_author' => $this->slash_2,
|
||||
'comment_content' => $this->slash_4,
|
||||
));
|
||||
$comment = get_comment( $comment_id );
|
||||
|
||||
$this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
|
||||
$this->assertEquals( wp_unslash( $this->slash_4 ), $comment->comment_content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the model function that expects slashed data
|
||||
*
|
||||
*/
|
||||
function test_wp_update_comment() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$comment_id = $this->factory->comment->create(array(
|
||||
'comment_post_ID' => $post_id
|
||||
));
|
||||
|
||||
wp_update_comment(array(
|
||||
'comment_ID' => $comment_id,
|
||||
'comment_author' => $this->slash_1,
|
||||
'comment_content' => $this->slash_7,
|
||||
));
|
||||
$comment = get_comment( $comment_id );
|
||||
|
||||
$this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
|
||||
$this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
|
||||
|
||||
wp_update_comment(array(
|
||||
'comment_ID' => $comment_id,
|
||||
'comment_author' => $this->slash_2,
|
||||
'comment_content' => $this->slash_4,
|
||||
));
|
||||
$comment = get_comment( $comment_id );
|
||||
|
||||
$this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
|
||||
$this->assertEquals( wp_unslash( $this->slash_4 ), $comment->comment_content );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user