|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test Akismet Integration. |
| 4 | + * |
| 5 | + * @package ActivityPub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Activitypub\Tests\Integration; |
| 9 | + |
| 10 | +use Activitypub\Integration\Akismet; |
| 11 | +use WP_UnitTestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test Akismet Integration class. |
| 15 | + * |
| 16 | + * @coversDefaultClass \Activitypub\Integration\Akismet |
| 17 | + */ |
| 18 | +class Test_Akismet extends WP_UnitTestCase { |
| 19 | + /** |
| 20 | + * A test post. |
| 21 | + * |
| 22 | + * @var int |
| 23 | + */ |
| 24 | + protected static $post_id; |
| 25 | + |
| 26 | + /** |
| 27 | + * Create fake data before tests run. |
| 28 | + * |
| 29 | + * @param WP_UnitTest_Factory $factory Helper that creates fake data. |
| 30 | + */ |
| 31 | + public static function wpSetUpBeforeClass( $factory ) { |
| 32 | + self::$post_id = $factory->post->create(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Clean up after tests. |
| 37 | + */ |
| 38 | + public static function wpTearDownAfterClass() { |
| 39 | + wp_delete_post( self::$post_id, true ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Test comment_row_actions method. |
| 44 | + * |
| 45 | + * @covers ::comment_row_actions |
| 46 | + */ |
| 47 | + public function test_comment_row_actions() { |
| 48 | + // Create a normal comment. |
| 49 | + $normal_comment_id = $this->factory->comment->create( |
| 50 | + array( |
| 51 | + 'comment_post_ID' => self::$post_id, |
| 52 | + 'comment_content' => 'Normal comment', |
| 53 | + ) |
| 54 | + ); |
| 55 | + |
| 56 | + // Create an ActivityPub comment. |
| 57 | + $ap_comment_id = $this->factory->comment->create( |
| 58 | + array( |
| 59 | + 'comment_post_ID' => self::$post_id, |
| 60 | + 'comment_content' => 'ActivityPub comment', |
| 61 | + ) |
| 62 | + ); |
| 63 | + add_comment_meta( $ap_comment_id, 'protocol', 'activitypub' ); |
| 64 | + |
| 65 | + // Test actions for normal comment. |
| 66 | + $actions = array( |
| 67 | + 'approve' => 'Approve', |
| 68 | + 'spam' => 'Spam', |
| 69 | + 'history' => 'History', |
| 70 | + ); |
| 71 | + |
| 72 | + $filtered_actions = Akismet::comment_row_actions( $actions, get_comment( $normal_comment_id ) ); |
| 73 | + $this->assertArrayHasKey( 'history', $filtered_actions, 'History action should remain for normal comments' ); |
| 74 | + |
| 75 | + // Test actions for ActivityPub comment. |
| 76 | + $filtered_actions = Akismet::comment_row_actions( $actions, get_comment( $ap_comment_id ) ); |
| 77 | + $this->assertArrayNotHasKey( 'history', $filtered_actions, 'History action should be removed for ActivityPub comments' ); |
| 78 | + $this->assertArrayHasKey( 'approve', $filtered_actions, 'Other actions should remain untouched' ); |
| 79 | + $this->assertArrayHasKey( 'spam', $filtered_actions, 'Other actions should remain untouched' ); |
| 80 | + |
| 81 | + // Clean up. |
| 82 | + wp_delete_comment( $normal_comment_id, true ); |
| 83 | + wp_delete_comment( $ap_comment_id, true ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Test init method. |
| 88 | + * |
| 89 | + * @covers ::init |
| 90 | + */ |
| 91 | + public function test_init() { |
| 92 | + Akismet::init(); |
| 93 | + |
| 94 | + $this->assertEquals( 10, has_filter( 'comment_row_actions', array( Akismet::class, 'comment_row_actions' ) ) ); |
| 95 | + } |
| 96 | +} |
0 commit comments