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