|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cleantalk\Antispam\Integrations; |
| 4 | + |
| 5 | +use Cleantalk\ApbctWP\Variables\Post; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class TestBloomForms extends TestCase |
| 9 | +{ |
| 10 | + private $integration; |
| 11 | + |
| 12 | + protected function setUp(): void |
| 13 | + { |
| 14 | + parent::setUp(); |
| 15 | + $this->integration = new BloomForms(); |
| 16 | + } |
| 17 | + |
| 18 | + protected function tearDown(): void |
| 19 | + { |
| 20 | + // Clean up global state |
| 21 | + $_POST = []; |
| 22 | + Post::getInstance()->variables = []; |
| 23 | + parent::tearDown(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Test extraction of nickname from subscribe_data_array with valid JSON |
| 28 | + */ |
| 29 | + public function testGetDataForCheckingWithValidJsonName() |
| 30 | + { |
| 31 | + $_POST['action'] = 'bloom_subscribe'; |
| 32 | + $_POST['subscribe_data_array'] = '{"list_id":123,"name":"TestUser","email":"test@example.com"}'; |
| 33 | + |
| 34 | + $result = $this->integration->getDataForChecking(null); |
| 35 | + |
| 36 | + $this->assertIsArray($result); |
| 37 | + $this->assertEquals('TestUser', $result['nickname']); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Test extraction of nickname from subscribe_data_array with escaped JSON (real-world case) |
| 42 | + */ |
| 43 | + public function testGetDataForCheckingWithEscapedJsonName() |
| 44 | + { |
| 45 | + $_POST['action'] = 'bloom_subscribe'; |
| 46 | + $_POST['subscribe_data_array'] = '{\"list_id\":113164156,\"account_name\":\"test@example\",\"service\":\"mailerlite\",\"name\":\"qwe123\",\"email\":\"test@example.com\",\"page_id\":245335,\"optin_id\":\"optin_40\",\"last_name\":\"\",\"ip_address\":\"true\"}'; |
| 47 | + |
| 48 | + $result = $this->integration->getDataForChecking(null); |
| 49 | + |
| 50 | + $this->assertIsArray($result); |
| 51 | + $this->assertEquals('qwe123', $result['nickname']); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Test with empty name in subscribe_data_array |
| 56 | + */ |
| 57 | + public function testGetDataForCheckingWithEmptyName() |
| 58 | + { |
| 59 | + $_POST['action'] = 'bloom_subscribe'; |
| 60 | + $_POST['subscribe_data_array'] = '{"list_id":123,"name":"","email":"test@example.com"}'; |
| 61 | + |
| 62 | + $result = $this->integration->getDataForChecking(null); |
| 63 | + |
| 64 | + $this->assertIsArray($result); |
| 65 | + $this->assertEquals('', $result['nickname']); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test without subscribe_data_array field |
| 70 | + */ |
| 71 | + public function testGetDataForCheckingWithoutSubscribeDataArray() |
| 72 | + { |
| 73 | + $_POST['action'] = 'bloom_subscribe'; |
| 74 | + $_POST['email'] = 'test@example.com'; |
| 75 | + |
| 76 | + $result = $this->integration->getDataForChecking(null); |
| 77 | + |
| 78 | + $this->assertIsArray($result); |
| 79 | + $this->assertEquals('', $result['nickname']); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test with invalid JSON in subscribe_data_array |
| 84 | + */ |
| 85 | + public function testGetDataForCheckingWithInvalidJson() |
| 86 | + { |
| 87 | + $_POST['action'] = 'bloom_subscribe'; |
| 88 | + $_POST['subscribe_data_array'] = 'not a valid json string'; |
| 89 | + |
| 90 | + $result = $this->integration->getDataForChecking(null); |
| 91 | + |
| 92 | + $this->assertIsArray($result); |
| 93 | + $this->assertEquals('', $result['nickname']); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test with special characters in name |
| 98 | + */ |
| 99 | + public function testGetDataForCheckingWithSpecialCharactersInName() |
| 100 | + { |
| 101 | + $_POST['action'] = 'bloom_subscribe'; |
| 102 | + $_POST['subscribe_data_array'] = '{"name":"O\'Brien Müller","email":"special@example.com"}'; |
| 103 | + |
| 104 | + $result = $this->integration->getDataForChecking(null); |
| 105 | + |
| 106 | + $this->assertIsArray($result); |
| 107 | + $this->assertStringContainsString("O'Brien", $result['nickname']); |
| 108 | + $this->assertStringContainsString('Müller', $result['nickname']); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test with name containing only whitespace |
| 113 | + */ |
| 114 | + public function testGetDataForCheckingWithWhitespaceName() |
| 115 | + { |
| 116 | + $_POST['action'] = 'bloom_subscribe'; |
| 117 | + $_POST['subscribe_data_array'] = '{"name":" ","email":"test@example.com"}'; |
| 118 | + |
| 119 | + $result = $this->integration->getDataForChecking(null); |
| 120 | + |
| 121 | + $this->assertIsArray($result); |
| 122 | + // sanitize_text_field trims whitespace, so it should be empty |
| 123 | + $this->assertEquals('', $result['nickname']); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Test with numeric name value |
| 128 | + */ |
| 129 | + public function testGetDataForCheckingWithNumericName() |
| 130 | + { |
| 131 | + $_POST['action'] = 'bloom_subscribe'; |
| 132 | + $_POST['subscribe_data_array'] = '{"name":12345,"email":"test@example.com"}'; |
| 133 | + |
| 134 | + $result = $this->integration->getDataForChecking(null); |
| 135 | + |
| 136 | + $this->assertIsArray($result); |
| 137 | + // Numeric value should be converted to string |
| 138 | + $this->assertEquals('12345', $result['nickname']); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Test with null name value in JSON |
| 143 | + */ |
| 144 | + public function testGetDataForCheckingWithNullName() |
| 145 | + { |
| 146 | + $_POST['action'] = 'bloom_subscribe'; |
| 147 | + $_POST['subscribe_data_array'] = '{"name":null,"email":"test@example.com"}'; |
| 148 | + |
| 149 | + $result = $this->integration->getDataForChecking(null); |
| 150 | + |
| 151 | + $this->assertIsArray($result); |
| 152 | + $this->assertEquals('', $result['nickname']); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Test with missing name field in JSON |
| 157 | + */ |
| 158 | + public function testGetDataForCheckingWithMissingNameField() |
| 159 | + { |
| 160 | + $_POST['action'] = 'bloom_subscribe'; |
| 161 | + $_POST['subscribe_data_array'] = '{"email":"test@example.com","list_id":123}'; |
| 162 | + |
| 163 | + $result = $this->integration->getDataForChecking(null); |
| 164 | + |
| 165 | + $this->assertIsArray($result); |
| 166 | + $this->assertEquals('', $result['nickname']); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Test with empty POST data |
| 171 | + */ |
| 172 | + public function testGetDataForCheckingWithEmptyPost() |
| 173 | + { |
| 174 | + $_POST = []; |
| 175 | + |
| 176 | + $result = $this->integration->getDataForChecking(null); |
| 177 | + |
| 178 | + $this->assertIsArray($result); |
| 179 | + $this->assertEquals('', $result['nickname']); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test with array value for subscribe_data_array (should not decode) |
| 184 | + */ |
| 185 | + public function testGetDataForCheckingWithArraySubscribeData() |
| 186 | + { |
| 187 | + $_POST['action'] = 'bloom_subscribe'; |
| 188 | + $_POST['subscribe_data_array'] = ['name' => 'TestUser', 'email' => 'test@example.com']; |
| 189 | + |
| 190 | + $result = $this->integration->getDataForChecking(null); |
| 191 | + |
| 192 | + $this->assertIsArray($result); |
| 193 | + // Array is not a string, so nickname extraction should not work |
| 194 | + $this->assertEquals('', $result['nickname']); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Test with double-escaped JSON |
| 199 | + */ |
| 200 | + public function testGetDataForCheckingWithDoubleEscapedJson() |
| 201 | + { |
| 202 | + $_POST['action'] = 'bloom_subscribe'; |
| 203 | + // Double escaped - after wp_unslash should become valid JSON |
| 204 | + $_POST['subscribe_data_array'] = '{\\\"name\\\":\\\"DoubleEscaped\\\",\\\"email\\\":\\\"test@example.com\\\"}'; |
| 205 | + |
| 206 | + $result = $this->integration->getDataForChecking(null); |
| 207 | + |
| 208 | + $this->assertIsArray($result); |
| 209 | + // This depends on how wp_unslash handles double escaping |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Test email extraction from subscribe_data_array |
| 214 | + */ |
| 215 | + public function testGetDataForCheckingEmailExtraction() |
| 216 | + { |
| 217 | + $_POST['action'] = 'bloom_subscribe'; |
| 218 | + $_POST['subscribe_data_array'] = '{"name":"TestUser","email":"bloom@example.com"}'; |
| 219 | + |
| 220 | + $result = $this->integration->getDataForChecking(null); |
| 221 | + |
| 222 | + $this->assertIsArray($result); |
| 223 | + $this->assertArrayHasKey('email', $result); |
| 224 | + } |
| 225 | +} |
0 commit comments