-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathCommentTest.php
71 lines (57 loc) · 2.12 KB
/
CommentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace AntispamBee\Tests\Unit\Handlers;
use AntispamBee\Handlers\Comment;
use AntispamBee\Handlers\Reaction;
use Yoast\WPTestUtils\BrainMonkey\TestCase;
use function Brain\Monkey\Functions\stubs;
/**
* Unit tests for {@see Comment}.
*/
class CommentTest extends TestCase {
public function test_process() {
global $_POST;
global $_SERVER;
$_POST = null;
$_SERVER = [
'HTTP_CLIENT_IP' => '192.0.2.100',
'SCRIPT_NAME' => '/index.php'
];
stubs(
[
'esc_url_raw' => function (string $url) {
return $url;
},
'wp_parse_url' => 'parse_url',
'wp_unslash' => function ($value) {
return $value;
},
]
);
$processed = [];
mock('overload:' . Reaction::class )
->expects( 'process' )
->withArgs( function( $input ) use ( &$processed ) {
$processed[] = $input;
return true;
} );
$comment = [ 'comment_type' => 'comment' ];
$result = Comment::process( $comment );
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on index.php' );
self::assertEmpty( $processed, 'Comment should no have been processed on index.php' );
$_SERVER['SCRIPT_NAME'] = '';
$result = Comment::process( $comment );
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on invalid request' );
self::assertSame( 1, $result['ab_spam__invalid_request'], 'Invalid request not detected' );
self::assertEmpty( $processed, 'Comment should no have been processed on invalid request' );
$_SERVER['SCRIPT_NAME'] = '/wp-comments-post.php';
$result = Comment::process( $comment );
self::assertSame( '192.0.2.100', $result['comment_author_IP'], 'Unexpected author IP on invalid request' );
self::assertArrayNotHasKey( 'processed', $result, 'Comment should no have been processed without POST data' );
$_POST = 'test me';
$result = Comment::process( $comment );
self::assertSame( [ $result ], $processed, 'Comment was not processed' );
$comment = [ 'comment_type' => 'linkback' ];
$result = Comment::process( $comment );
self::assertSame( $comment, $result, 'Linkback should not be modified by comment handler' );
}
}