wordpress-develop/tests/phpunit/tests/comment/slashes.php
Sergey Biryukov ddb409edca Build/Test Tools: Implement use of the void solution.
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case.

Follow-up to [51559-51567].

Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-07 10:29:41 +00:00

187 lines
5.6 KiB
PHP

<?php
/**
* @group comment
* @group slashes
* @ticket 21767
*/
class Tests_Comment_Slashes extends WP_UnitTestCase {
protected static $author_id;
protected static $post_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// We need an admin user to bypass comment flood protection.
self::$author_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$post_id = $factory->post->create();
}
function set_up() {
parent::set_up();
wp_set_current_user( self::$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 its 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 \\\\\\\\\\\\\\';
}
/**
* Tests the extended model function that expects slashed data.
*/
function test_wp_new_comment() {
$post_id = self::$post_id;
// 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_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => $this->slash_7,
);
$comment_id = wp_new_comment( $data );
$comment = get_comment( $comment_id );
$this->assertSame( wp_unslash( $this->slash_1 ), $comment->comment_author );
$this->assertSame( wp_unslash( $this->slash_7 ), $comment->comment_content );
$data = array(
'comment_post_ID' => $post_id,
'comment_author' => $this->slash_2,
'comment_author_url' => '',
'comment_author_email' => '',
'comment_type' => '',
'comment_content' => $this->slash_4,
);
$comment_id = wp_new_comment( $data );
$comment = get_comment( $comment_id );
$this->assertSame( wp_unslash( $this->slash_2 ), $comment->comment_author );
$this->assertSame( wp_unslash( $this->slash_4 ), $comment->comment_content );
}
/**
* Tests the controller function that expects slashed data.
*/
function test_edit_comment() {
$post_id = self::$post_id;
$comment_id = self::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['comment_status'] = '';
$_POST['newcomment_author'] = $this->slash_1;
$_POST['newcomment_author_url'] = '';
$_POST['newcomment_author_email'] = '';
$_POST['content'] = $this->slash_7;
$_POST = add_magic_quotes( $_POST ); // The edit_comment() function will strip slashes.
edit_comment();
$comment = get_comment( $comment_id );
$this->assertSame( $this->slash_1, $comment->comment_author );
$this->assertSame( $this->slash_7, $comment->comment_content );
$_POST = array();
$_POST['comment_ID'] = $comment_id;
$_POST['comment_status'] = '';
$_POST['newcomment_author'] = $this->slash_2;
$_POST['newcomment_author_url'] = '';
$_POST['newcomment_author_email'] = '';
$_POST['content'] = $this->slash_4;
$_POST = add_magic_quotes( $_POST ); // The edit_comment() function will strip slashes.
edit_comment();
$comment = get_comment( $comment_id );
$this->assertSame( $this->slash_2, $comment->comment_author );
$this->assertSame( $this->slash_4, $comment->comment_content );
}
/**
* Tests the model function that expects slashed data.
*/
function test_wp_insert_comment() {
$post_id = self::$post_id;
$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->assertSame( wp_unslash( $this->slash_1 ), $comment->comment_author );
$this->assertSame( 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->assertSame( wp_unslash( $this->slash_2 ), $comment->comment_author );
$this->assertSame( wp_unslash( $this->slash_4 ), $comment->comment_content );
}
/**
* Tests the model function that expects slashed data.
*/
function test_wp_update_comment() {
$post_id = self::$post_id;
$comment_id = self::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->assertSame( wp_unslash( $this->slash_1 ), $comment->comment_author );
$this->assertSame( 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->assertSame( wp_unslash( $this->slash_2 ), $comment->comment_author );
$this->assertSame( wp_unslash( $this->slash_4 ), $comment->comment_content );
}
}