Files
wordpress-develop/tests/phpunit/tests/ajax/EditComment.php
T
Sergey Biryukov e72fff9cef Code Modernization: Replace dirname( __FILE__ ) calls with __DIR__ magic constant.
This avoids the performance overhead of the function call every time `dirname( __FILE__ )` was used instead of `__DIR__`.

This commit also includes:

* Removing unnecessary parentheses from `include`/`require` statements. These are language constructs, not function calls.
* Replacing `include` statements for several files with `require_once`, for consistency:
 * `wp-admin/admin-header.php`
 * `wp-admin/admin-footer.php`
 * `wp-includes/version.php`

Props ayeshrajans, desrosj, valentinbora, jrf, joostdevalk, netweb.
Fixes #48082.

git-svn-id: https://develop.svn.wordpress.org/trunk@47198 602fd350-edb4-49c9-b593-d223f7449a82
2020-02-06 06:31:22 +00:00

208 lines
5.7 KiB
PHP

<?php
/**
* Admin Ajax functions to be tested.
*/
require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
/**
* Testing Ajax comment functionality.
*
* @package WordPress
* @subpackage UnitTests
* @since 3.4.0
* @group ajax
*/
class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
/**
* A post with at least one comment
*
* @var mixed
*/
protected $_comment_post = null;
/**
* Set up the test fixture
*/
public function setUp() {
parent::setUp();
$post_id = self::factory()->post->create();
self::factory()->comment->create_post_comments( $post_id, 5 );
$this->_comment_post = get_post( $post_id );
}
/**
* Get comments as a privilged user (administrator)
* Expects test to pass
*
* @return void
*/
public function test_as_admin() {
// Become an administrator.
$this->_setRole( 'administrator' );
// Get a comment.
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request.
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
$_POST['comment_ID'] = $comment->comment_ID;
$_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
// Make the request.
try {
$this->_handleAjax( 'edit-comment' );
} catch ( WPAjaxDieContinueException $e ) {
unset( $e );
}
// Get the response.
$xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
// Check the meta data.
$this->assertEquals( -1, (string) $xml->response[0]->edit_comment['position'] );
$this->assertEquals( $comment->comment_ID, (string) $xml->response[0]->edit_comment['id'] );
$this->assertEquals( 'edit-comment_' . $comment->comment_ID, (string) $xml->response['action'] );
// Check the payload.
$this->assertNotEmpty( (string) $xml->response[0]->edit_comment[0]->response_data );
// And supplemental is empty.
$this->assertEmpty( (string) $xml->response[0]->edit_comment[0]->supplemental );
}
/**
* @ticket 33154
*/
function test_editor_can_edit_orphan_comments() {
global $wpdb;
// Become an editor.
$this->_setRole( 'editor' );
// Get a comment.
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Manually update the comment_post_ID, because wp_update_comment() will prevent it..
$wpdb->update( $wpdb->comments, array( 'comment_post_ID' => 0 ), array( 'comment_ID' => $comment->comment_ID ) );
clean_comment_cache( $comment->comment_ID );
// Set up a default request.
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
$_POST['comment_ID'] = $comment->comment_ID;
$_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
// Make the request.
try {
$this->_handleAjax( 'edit-comment' );
} catch ( WPAjaxDieContinueException $e ) {
unset( $e );
}
// Get the response.
$xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
// Check the meta data.
$this->assertEquals( -1, (string) $xml->response[0]->edit_comment['position'] );
$this->assertEquals( $comment->comment_ID, (string) $xml->response[0]->edit_comment['id'] );
$this->assertEquals( 'edit-comment_' . $comment->comment_ID, (string) $xml->response['action'] );
// Check the payload.
$this->assertNotEmpty( (string) $xml->response[0]->edit_comment[0]->response_data );
// And supplemental is empty.
$this->assertEmpty( (string) $xml->response[0]->edit_comment[0]->supplemental );
}
/**
* Get comments as a non-privileged user (subscriber)
* Expects test to fail
*
* @return void
*/
public function test_as_subscriber() {
// Become a subscriber.
$this->_setRole( 'subscriber' );
// Get a comment.
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request.
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
$_POST['comment_ID'] = $comment->comment_ID;
$_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
// Make the request.
$this->setExpectedException( 'WPAjaxDieStopException', '-1' );
$this->_handleAjax( 'edit-comment' );
}
/**
* Get comments with a bad nonce
* Expects test to fail
*
* @return void
*/
public function test_bad_nonce() {
// Become an administrator.
$this->_setRole( 'administrator' );
// Get a comment.
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request.
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( uniqid() );
$_POST['comment_ID'] = $comment->comment_ID;
$_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
// Make the request.
$this->setExpectedException( 'WPAjaxDieStopException', '-1' );
$this->_handleAjax( 'get-comments' );
}
/**
* Get comments for an invalid post
* This should return valid XML
*
* @return void
*/
public function test_invalid_comment() {
// Become an administrator.
$this->_setRole( 'administrator' );
// Set up a default request.
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
$_POST['comment_ID'] = 123456789;
$_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
// Make the request.
$this->setExpectedException( 'WPAjaxDieStopException', '-1' );
$this->_handleAjax( 'edit-comment' );
}
}