|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cleantalk\Antispam\Integrations; |
| 4 | + |
| 5 | +use Cleantalk\ApbctWP\Variables\Post; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class TestEventPrime extends TestCase |
| 9 | +{ |
| 10 | + private $integration; |
| 11 | + |
| 12 | + protected function setUp(): void |
| 13 | + { |
| 14 | + parent::setUp(); |
| 15 | + $_POST = []; |
| 16 | + Post::getInstance()->variables = []; |
| 17 | + $this->integration = new EventPrime(); |
| 18 | + } |
| 19 | + |
| 20 | + protected function tearDown(): void |
| 21 | + { |
| 22 | + $_POST = []; |
| 23 | + Post::getInstance()->variables = []; |
| 24 | + parent::tearDown(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Test returns null when POST has no EventPrime fields |
| 29 | + */ |
| 30 | + public function testGetDataForCheckingReturnsNullWithEmptyPost() |
| 31 | + { |
| 32 | + $result = $this->integration->getDataForChecking(null); |
| 33 | + |
| 34 | + $this->assertNull($result); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Test returns null when POST has unrelated action |
| 39 | + */ |
| 40 | + public function testGetDataForCheckingReturnsNullWithUnrelatedAction() |
| 41 | + { |
| 42 | + $_POST['action'] = 'some_other_action'; |
| 43 | + $_POST['email'] = 'test@example.com'; |
| 44 | + |
| 45 | + $result = $this->integration->getDataForChecking(null); |
| 46 | + |
| 47 | + $this->assertNull($result); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test register form returns array when nonce is present |
| 52 | + */ |
| 53 | + public function testGetDataForCheckingRegisterFormReturnsArray() |
| 54 | + { |
| 55 | + $_POST['ep-attendee-register-nonce'] = 'abc123'; |
| 56 | + $_POST['ep_rg_field_email'] = 'user@example.com'; |
| 57 | + $_POST['ep_rg_field_first_name'] = 'John'; |
| 58 | + |
| 59 | + $result = $this->integration->getDataForChecking(null); |
| 60 | + |
| 61 | + $this->assertIsArray($result); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test register form sets register flag to true |
| 66 | + */ |
| 67 | + public function testGetDataForCheckingRegisterFormHasRegisterFlag() |
| 68 | + { |
| 69 | + $_POST['ep-attendee-register-nonce'] = 'abc123'; |
| 70 | + $_POST['ep_rg_field_email'] = 'user@example.com'; |
| 71 | + |
| 72 | + $result = $this->integration->getDataForChecking(null); |
| 73 | + |
| 74 | + $this->assertIsArray($result); |
| 75 | + $this->assertTrue($result['register']); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test checkout form returns null when ep_rg_field_email is missing |
| 80 | + */ |
| 81 | + public function testGetDataForCheckingCheckoutFormReturnsNullWithoutEmail() |
| 82 | + { |
| 83 | + $_POST['action'] = 'ep_save_event_booking'; |
| 84 | + $_POST['ep_event_id'] = '42'; |
| 85 | + |
| 86 | + $result = $this->integration->getDataForChecking(null); |
| 87 | + |
| 88 | + $this->assertNull($result); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Test checkout form returns array when ep_rg_field_email is present |
| 93 | + */ |
| 94 | + public function testGetDataForCheckingCheckoutFormWithEmail() |
| 95 | + { |
| 96 | + $_POST['action'] = 'ep_save_event_booking'; |
| 97 | + $_POST['ep_rg_field_email'] = 'buyer@example.com'; |
| 98 | + $_POST['ep_event_id'] = '42'; |
| 99 | + |
| 100 | + $result = $this->integration->getDataForChecking(null); |
| 101 | + |
| 102 | + $this->assertIsArray($result); |
| 103 | + $this->assertEquals('buyer@example.com', $result['email']); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test checkout form returns the email in result |
| 108 | + */ |
| 109 | + public function testGetDataForCheckingCheckoutFormResultContainsEmail() |
| 110 | + { |
| 111 | + $_POST['action'] = 'ep_save_event_booking'; |
| 112 | + $_POST['ep_rg_field_email'] = 'buyer@example.com'; |
| 113 | + |
| 114 | + $result = $this->integration->getDataForChecking(null); |
| 115 | + |
| 116 | + $this->assertIsArray($result); |
| 117 | + $this->assertEquals('buyer@example.com', $result['email']); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Test checkout form parses data string and extracts email from it |
| 122 | + */ |
| 123 | + public function testGetDataForCheckingCheckoutFormParsesDataString() |
| 124 | + { |
| 125 | + $_POST['action'] = 'ep_save_event_booking'; |
| 126 | + $_POST['data'] = 'ep_rg_field_email=encoded%40example.com&ep_event_id=5'; |
| 127 | + |
| 128 | + $result = $this->integration->getDataForChecking(null); |
| 129 | + |
| 130 | + $this->assertIsArray($result); |
| 131 | + $this->assertEquals('encoded@example.com', $result['email']); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Test checkout form data string without email still returns null |
| 136 | + */ |
| 137 | + public function testGetDataForCheckingCheckoutFormDataStringWithoutEmailReturnsNull() |
| 138 | + { |
| 139 | + $_POST['action'] = 'ep_save_event_booking'; |
| 140 | + $_POST['data'] = 'ep_event_id=5&ep_attendee_count=1'; |
| 141 | + |
| 142 | + $result = $this->integration->getDataForChecking(null); |
| 143 | + |
| 144 | + $this->assertNull($result); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Test event token is extracted and added to result |
| 149 | + */ |
| 150 | + public function testGetDataForCheckingExtractsEventToken() |
| 151 | + { |
| 152 | + $_POST['ep-attendee-register-nonce'] = 'nonce123'; |
| 153 | + $_POST['ep_rg_field_email'] = 'user@example.com'; |
| 154 | + $_POST['ct_bot_detector_event_token'] = 'token_abc'; |
| 155 | + |
| 156 | + $result = $this->integration->getDataForChecking(null); |
| 157 | + |
| 158 | + $this->assertIsArray($result); |
| 159 | + $this->assertEquals('token_abc', $result['event_token']); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Test event token is removed from main input_data fields |
| 164 | + */ |
| 165 | + public function testGetDataForCheckingEventTokenNotDuplicatedInResult() |
| 166 | + { |
| 167 | + $_POST['ep-attendee-register-nonce'] = 'nonce123'; |
| 168 | + $_POST['ep_rg_field_email'] = 'user@example.com'; |
| 169 | + $_POST['ct_bot_detector_event_token'] = 'token_xyz'; |
| 170 | + |
| 171 | + $result = $this->integration->getDataForChecking(null); |
| 172 | + |
| 173 | + $this->assertIsArray($result); |
| 174 | + $this->assertEquals('token_xyz', $result['event_token']); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Test no event_token key when token is absent |
| 179 | + */ |
| 180 | + public function testGetDataForCheckingNoEventTokenKeyWhenAbsent() |
| 181 | + { |
| 182 | + $_POST['ep-attendee-register-nonce'] = 'nonce123'; |
| 183 | + $_POST['ep_rg_field_email'] = 'user@example.com'; |
| 184 | + |
| 185 | + $result = $this->integration->getDataForChecking(null); |
| 186 | + |
| 187 | + $this->assertIsArray($result); |
| 188 | + $this->assertArrayNotHasKey('event_token', $result); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Test checkout form event token extracted from parsed data string |
| 193 | + */ |
| 194 | + public function testGetDataForCheckingCheckoutFormEventTokenFromDataString() |
| 195 | + { |
| 196 | + $_POST['action'] = 'ep_save_event_booking'; |
| 197 | + $_POST['data'] = 'ep_rg_field_email=token%40example.com&ct_bot_detector_event_token=tok123'; |
| 198 | + |
| 199 | + $result = $this->integration->getDataForChecking(null); |
| 200 | + |
| 201 | + $this->assertIsArray($result); |
| 202 | + $this->assertEquals('tok123', $result['event_token']); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Test register form with special characters in email |
| 207 | + */ |
| 208 | + public function testGetDataForCheckingRegisterFormWithSpecialCharactersInEmail() |
| 209 | + { |
| 210 | + $_POST['ep-attendee-register-nonce'] = 'nonce'; |
| 211 | + $_POST['ep_rg_field_email'] = "special+tag@example.co.uk"; |
| 212 | + |
| 213 | + $result = $this->integration->getDataForChecking(null); |
| 214 | + |
| 215 | + $this->assertIsArray($result); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Test that both nonce and checkout action triggers register form logic (nonce takes precedence) |
| 220 | + */ |
| 221 | + public function testGetDataForCheckingRegisterNonceTakesPrecedenceOverCheckoutAction() |
| 222 | + { |
| 223 | + $_POST['ep-attendee-register-nonce'] = 'nonce'; |
| 224 | + $_POST['action'] = 'ep_save_event_booking'; |
| 225 | + $_POST['ep_rg_field_email'] = 'user@example.com'; |
| 226 | + |
| 227 | + $result = $this->integration->getDataForChecking(null); |
| 228 | + |
| 229 | + $this->assertIsArray($result); |
| 230 | + $this->assertTrue($result['register']); |
| 231 | + } |
| 232 | +} |
0 commit comments