|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cleantalk\Antispam\Integrations; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +/** |
| 8 | + * Covers skip rules for pingback/trackback in preprocess comment flow (see PR #764 / #773). |
| 9 | + */ |
| 10 | +class TestCleantalkPreprocessComment extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var mixed |
| 14 | + */ |
| 15 | + private $savedCurrentUser; |
| 16 | + /** |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + private $savedDisallowedKeys; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + global $current_user; |
| 25 | + $this->savedCurrentUser = isset($current_user) ? $current_user : null; |
| 26 | + $this->savedDisallowedKeys = (string) get_option('disallowed_keys', ''); |
| 27 | + } |
| 28 | + |
| 29 | + protected function tearDown(): void |
| 30 | + { |
| 31 | + global $current_user; |
| 32 | + $current_user = $this->savedCurrentUser; |
| 33 | + if (function_exists('wp_set_current_user')) { |
| 34 | + wp_set_current_user(0); |
| 35 | + } |
| 36 | + update_option('disallowed_keys', $this->savedDisallowedKeys); |
| 37 | + parent::tearDown(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return \stdClass |
| 42 | + */ |
| 43 | + private function makeSubscriberUser() |
| 44 | + { |
| 45 | + $subscriber = new \stdClass(); |
| 46 | + $subscriber->ID = 91234; |
| 47 | + $subscriber->roles = array('subscriber'); |
| 48 | + $subscriber->caps = array('subscriber' => true); |
| 49 | + |
| 50 | + return $subscriber; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param array<string, mixed> $wp_comment |
| 55 | + * @param object $current_user |
| 56 | + * @param bool $ct_comment_done |
| 57 | + * @param object|null $apbct |
| 58 | + * |
| 59 | + * @return mixed |
| 60 | + */ |
| 61 | + private function invokeDoSkipReason(array $wp_comment, $current_user, $ct_comment_done = false, $apbct = null) |
| 62 | + { |
| 63 | + $integration = new CleantalkPreprocessComment(); |
| 64 | + $reflection = new \ReflectionClass($integration); |
| 65 | + |
| 66 | + $wp_comment_prop = $reflection->getProperty('wp_comment'); |
| 67 | + $wp_comment_prop->setAccessible(true); |
| 68 | + $wp_comment_prop->setValue($integration, $wp_comment); |
| 69 | + |
| 70 | + if ($apbct !== null) { |
| 71 | + $apbct_prop = $reflection->getProperty('apbct'); |
| 72 | + $apbct_prop->setAccessible(true); |
| 73 | + $apbct_prop->setValue($integration, $apbct); |
| 74 | + } |
| 75 | + |
| 76 | + $method = $reflection->getMethod('doSkipReason'); |
| 77 | + $method->setAccessible(true); |
| 78 | + |
| 79 | + return $method->invoke($integration, $current_user, $ct_comment_done); |
| 80 | + } |
| 81 | + |
| 82 | + public function testDoSkipReasonSkipsAdministratorBeforePingbackHandling(): void |
| 83 | + { |
| 84 | + $user = new \stdClass(); |
| 85 | + $user->roles = array('administrator'); |
| 86 | + |
| 87 | + $result = $this->invokeDoSkipReason( |
| 88 | + array( |
| 89 | + 'comment_type' => 'pingback', |
| 90 | + 'comment_post_ID' => 1, |
| 91 | + ), |
| 92 | + $user |
| 93 | + ); |
| 94 | + |
| 95 | + $this->assertNotFalse($result); |
| 96 | + $this->assertStringContainsString('doSkipReason', $result); |
| 97 | + } |
| 98 | + |
| 99 | + public function testDoSkipReasonSkipsPingback(): void |
| 100 | + { |
| 101 | + $user = new \stdClass(); |
| 102 | + $user->roles = array('subscriber'); |
| 103 | + |
| 104 | + $result = $this->invokeDoSkipReason( |
| 105 | + array( |
| 106 | + 'comment_type' => 'pingback', |
| 107 | + 'comment_post_ID' => 1, |
| 108 | + ), |
| 109 | + $user |
| 110 | + ); |
| 111 | + |
| 112 | + $this->assertNotFalse($result); |
| 113 | + $this->assertStringContainsString('doSkipReason', $result); |
| 114 | + } |
| 115 | + |
| 116 | + public function testDoSkipReasonSkipsTrackback(): void |
| 117 | + { |
| 118 | + $user = new \stdClass(); |
| 119 | + $user->roles = array('subscriber'); |
| 120 | + |
| 121 | + $result = $this->invokeDoSkipReason( |
| 122 | + array( |
| 123 | + 'comment_type' => 'trackback', |
| 124 | + 'comment_post_ID' => 1, |
| 125 | + ), |
| 126 | + $user |
| 127 | + ); |
| 128 | + |
| 129 | + $this->assertNotFalse($result); |
| 130 | + $this->assertStringContainsString('doSkipReason', $result); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Missing comment_type must default to "comment" and continue into the main ruleset (not pingback skip). |
| 135 | + */ |
| 136 | + public function testDoSkipReasonDefaultsMissingCommentTypeToComment(): void |
| 137 | + { |
| 138 | + global $current_user; |
| 139 | + $subscriber = $this->makeSubscriberUser(); |
| 140 | + $current_user = $subscriber; |
| 141 | + |
| 142 | + $apbct = (object) array( |
| 143 | + 'settings' => array( |
| 144 | + 'forms__comments_test' => 0, |
| 145 | + ), |
| 146 | + ); |
| 147 | + |
| 148 | + $wp_comment = array( |
| 149 | + 'comment_post_ID' => 1, |
| 150 | + ); |
| 151 | + |
| 152 | + $result = $this->invokeDoSkipReason($wp_comment, $subscriber, false, $apbct); |
| 153 | + |
| 154 | + $this->assertNotFalse($result); |
| 155 | + $this->assertStringContainsString('doSkipReason', $result); |
| 156 | + } |
| 157 | + |
| 158 | + public function testDoSkipReasonSkipsWhenCommentAlreadyHandled(): void |
| 159 | + { |
| 160 | + global $current_user; |
| 161 | + $subscriber = $this->makeSubscriberUser(); |
| 162 | + $current_user = $subscriber; |
| 163 | + |
| 164 | + $apbct = (object) array( |
| 165 | + 'settings' => array( |
| 166 | + 'forms__comments_test' => 1, |
| 167 | + ), |
| 168 | + ); |
| 169 | + |
| 170 | + $wp_comment = array( |
| 171 | + 'comment_type' => 'comment', |
| 172 | + 'comment_post_ID' => 1, |
| 173 | + 'comment_author' => 'John', |
| 174 | + 'comment_author_email' => 'john@example.com', |
| 175 | + 'comment_author_url' => 'https://example.com', |
| 176 | + 'comment_content' => 'Regular text', |
| 177 | + ); |
| 178 | + |
| 179 | + $result = $this->invokeDoSkipReason($wp_comment, $subscriber, true, $apbct); |
| 180 | + |
| 181 | + $this->assertNotFalse($result); |
| 182 | + $this->assertStringContainsString('doSkipReason', $result); |
| 183 | + } |
| 184 | + |
| 185 | + public function testDoSkipReasonReturnsFalseForRegularCommentWithoutBlacklistedContent(): void |
| 186 | + { |
| 187 | + global $current_user; |
| 188 | + $subscriber = $this->makeSubscriberUser(); |
| 189 | + $current_user = $subscriber; |
| 190 | + |
| 191 | + update_option('disallowed_keys', 'forbidden-keyword-that-is-not-used'); |
| 192 | + |
| 193 | + $apbct = (object) array( |
| 194 | + 'settings' => array( |
| 195 | + 'forms__comments_test' => 1, |
| 196 | + ), |
| 197 | + ); |
| 198 | + |
| 199 | + $wp_comment = array( |
| 200 | + 'comment_type' => 'comment', |
| 201 | + 'comment_post_ID' => 1, |
| 202 | + 'comment_author' => 'John', |
| 203 | + 'comment_author_email' => 'john@example.com', |
| 204 | + 'comment_author_url' => 'https://example.com', |
| 205 | + 'comment_content' => 'Absolutely clean content', |
| 206 | + ); |
| 207 | + |
| 208 | + $result = $this->invokeDoSkipReason($wp_comment, $subscriber, false, $apbct); |
| 209 | + |
| 210 | + $this->assertFalse($result); |
| 211 | + } |
| 212 | + |
| 213 | + public function testDoSkipReasonSkipsWhenWordPressBlacklistMatches(): void |
| 214 | + { |
| 215 | + global $current_user; |
| 216 | + $subscriber = $this->makeSubscriberUser(); |
| 217 | + $current_user = $subscriber; |
| 218 | + |
| 219 | + update_option('disallowed_keys', 'blacklisted-fragment'); |
| 220 | + |
| 221 | + $apbct = (object) array( |
| 222 | + 'settings' => array( |
| 223 | + 'forms__comments_test' => 1, |
| 224 | + ), |
| 225 | + ); |
| 226 | + |
| 227 | + $wp_comment = array( |
| 228 | + 'comment_type' => 'comment', |
| 229 | + 'comment_post_ID' => 1, |
| 230 | + 'comment_author' => 'John', |
| 231 | + 'comment_author_email' => 'john@example.com', |
| 232 | + 'comment_author_url' => 'https://example.com', |
| 233 | + 'comment_content' => 'Message contains blacklisted-fragment inside', |
| 234 | + ); |
| 235 | + |
| 236 | + $result = $this->invokeDoSkipReason($wp_comment, $subscriber, false, $apbct); |
| 237 | + |
| 238 | + $this->assertNotFalse($result); |
| 239 | + $this->assertStringContainsString('doSkipReason', $result); |
| 240 | + } |
| 241 | +} |
| 242 | + |
0 commit comments