bin
3 months ago
fixtures
3 months ago
AdminCodeCleanupAndPDFTest.php
3 months ago
AdminCodesByProductTest.php
3 months ago
AdminCustomerNameTest.php
3 months ago
AdminErrorLogTest.php
3 months ago
AdminFormatWarningTest.php
3 months ago
AdminListAndCodeOpsTest.php
3 months ago
AdminListQueryTest.php
3 months ago
AdminMetaAndErrorTest.php
3 months ago
AdminOptionsAndMigrationTest.php
3 months ago
AdminOrderInfoTest.php
3 months ago
AdminRedeemAmountTest.php
3 months ago
AdminRestrictionAndRstrTest.php
3 months ago
AdminSettingsAndDatepickerTest.php
3 months ago
AdminSettingsCodeTest.php
3 months ago
AdminTicketInfoTest.php
3 months ago
AdminTimezoneAndMiscTest.php
3 months ago
AdminWCCodeOpsTest.php
3 months ago
AuthtokenAccessTest.php
3 months ago
AuthtokenCRUDTest.php
3 months ago
AuthtokenEditAndRemoveTest.php
3 months ago
AuthtokenTest.php
3 months ago
BaseLimitsTest.php
3 months ago
CartCodeRestrictionAndAssignTest.php
3 months ago
CartMetaAndBeforeCartTest.php
3 months ago
CartValidationTest.php
3 months ago
ContextWizardTest.php
3 months ago
CoreAlignAndSearchTest.php
3 months ago
CoreCodeOpsAndDBLogTest.php
3 months ago
CoreDecodeAndReplaceTest.php
3 months ago
CoreEncodeMetaTest.php
3 months ago
CoreExpiredAndRegisteredTest.php
3 months ago
CoreMetaAndUrlTest.php
3 months ago
CoreMetaObjectStructureTest.php
3 months ago
CoreMetaTest.php
3 months ago
CoreMethodsTest.php
3 months ago
CoreOrderAndIPTest.php
3 months ago
CoreParserAndQueryTest.php
3 months ago
CoreParserURLAndMergePDFTest.php
3 months ago
CoreReplaceAndExpireTest.php
3 months ago
CoreTicketIdAndExpirationTest.php
3 months ago
CoreTicketIdAndUrlTest.php
3 months ago
CoreTicketURLComponentsTest.php
3 months ago
CountRedeemsTodayTest.php
3 months ago
CronjobTest.php
3 months ago
DBOperationsTest.php
3 months ago
DBSanitizeAndQueryTest.php
3 months ago
DBSanitizeTest.php
3 months ago
DBTablesAndUpdateTest.php
3 months ago
DatabaseTest.php
3 months ago
DateCalcAndCorrectProductTest.php
3 months ago
DaychooserTest.php
3 months ago
EmailHandlerTest.php
3 months ago
EmailOrderMetaAndAttachmentsTest.php
3 months ago
ErrorLogAndSanitizeTest.php
3 months ago
EventTimeWindowTest.php
3 months ago
FrontendCheckCodeTest.php
3 months ago
FrontendCountConfirmedTest.php
3 months ago
FrontendExecuteJSONAndCheckCodeTest.php
3 months ago
FrontendIsUsedAndOptionsTest.php
3 months ago
FrontendMarkUsedTest.php
3 months ago
FrontendValidationTest.php
3 months ago
FullTicketFlowTest.php
3 months ago
HelpersTest.php
3 months ago
ListManagementTest.php
3 months ago
MaxRedeemPerDayTest.php
3 months ago
MessengerAndRESTEndpointsTest.php
3 months ago
MessengerTypeCheckTest.php
3 months ago
OldPremiumGuardTest.php
3 months ago
OneTimeUseTest.php
3 months ago
OptionsChangeAndLoadTest.php
3 months ago
OptionsDeleteAndResetTest.php
3 months ago
OptionsExportImportTest.php
3 months ago
OptionsFormatsTest.php
3 months ago
OptionsHistoryTest.php
3 months ago
OptionsMigrationTest.php
3 months ago
OptionsResetAndFormatTest.php
3 months ago
OptionsTest.php
3 months ago
OrderDeleteAndRefundDeleteTest.php
3 months ago
OrderMetaKeyValueDisplayTest.php
3 months ago
OrderStatusHandlerTest.php
3 months ago
OrderTicketGenerationTest.php
3 months ago
OrderTicketQueryTest.php
3 months ago
PDFAndQRConfigTest.php
3 months ago
PDFMergeAndPWAServiceWorkerTest.php
3 months ago
PDFSettersTest.php
3 months ago
PDFSettingsTest.php
3 months ago
PDFUtilsTest.php
3 months ago
PWAManifestAndSessionTest.php
3 months ago
PartialRefundAndCodeCleanupTest.php
3 months ago
PluginInitAndShortcodeTest.php
3 months ago
ProductSettingsTest.php
3 months ago
QRCodeAndDesignerTest.php
3 months ago
QRPDFAndBadgePDFTest.php
3 months ago
RESTRoutesAndPingTest.php
3 months ago
RefundTest.php
3 months ago
RestApiTest.php
3 months ago
RestApiTicketTest.php
3 months ago
ScannerBoilerplateAndFormatWarningsTest.php
3 months ago
SeatingBlockTest.php
3 months ago
SeatingCoordinatorTest.php
3 months ago
SeatingExportTest.php
3 months ago
SeatingMetaTest.php
3 months ago
SeatingPlanCRUDTest.php
3 months ago
SeatingPlanTest.php
3 months ago
SeatingSeatCRUDTest.php
3 months ago
SetupWizardTest.php
3 months ago
ShortcodeAndCartCheckTest.php
3 months ago
ShortcodeAndCodesDisplayTest.php
3 months ago
ShortcodesAndCodeListTest.php
3 months ago
StaticDateAndTimeTest.php
3 months ago
StaticHelpersAndMessengerTest.php
3 months ago
StaticUtilMethodsTest.php
3 months ago
TicketBadgeTemplateTest.php
3 months ago
TicketCalcDateTest.php
3 months ago
TicketDesignerAndBadgeTest.php
3 months ago
TicketDesignerTemplateTest.php
3 months ago
TicketDesignerTest.php
3 months ago
TicketDisplayTest.php
3 months ago
TicketExpirationAndICSTest.php
3 months ago
TicketLabelAndDisplayTest.php
3 months ago
TicketLabelsAndStaticsTest.php
3 months ago
TicketQRSettersTest.php
3 months ago
TicketRedeemTest.php
3 months ago
TicketRoutingTest.php
3 months ago
TicketScannerMethodsTest.php
3 months ago
TicketSettersAndOrderTest.php
3 months ago
TicketUtilAndICSTest.php
3 months ago
TicketUtilMethodsTest.php
3 months ago
TicketWPMLAndSubscriptionTest.php
3 months ago
TimezoneRedeemBlockingTest.php
3 months ago
UrlParsingTest.php
3 months ago
VariationTicketTest.php
3 months ago
WCBaseMetaConstantsTest.php
3 months ago
WCCheckoutCartFlowTest.php
3 months ago
WCEmailAttachmentTest.php
3 months ago
WCFrontendAjaxAndDatepickerTest.php
3 months ago
WCFrontendCartHandlerTest.php
3 months ago
WCFrontendCartHandlersTest.php
3 months ago
WCFrontendCartTest.php
3 months ago
WCFrontendCartValidationTest.php
3 months ago
WCFrontendHandlersTest.php
3 months ago
WCFrontendLabelsTest.php
3 months ago
WCFrontendMethodsTest.php
3 months ago
WCOrderDeleteTest.php
3 months ago
WCOrderDisplayMetaTest.php
3 months ago
WCOrderHasTicketsTest.php
3 months ago
WCOrderHooksTest.php
3 months ago
WCOrderItemMetaDisplayTest.php
3 months ago
WCOrderItemOpsTest.php
3 months ago
WCOrderLifecycleTest.php
3 months ago
WCOrderMethodsTest.php
3 months ago
WCOrderTicketDetectionTest.php
3 months ago
WCOrderTicketNumberTest.php
3 months ago
WCProductAndVariationTest.php
3 months ago
WCProductColumnsTest.php
3 months ago
WCProductConfigAndColumnsTest.php
3 months ago
WCProductIsTicketAndListsTest.php
3 months ago
WCProductMetaSaveTest.php
3 months ago
WCProductMethodsTest.php
3 months ago
WCProductTabsAndSideBoxTest.php
3 months ago
WCProductTicketTest.php
3 months ago
WebhooksAndMigrationTest.php
3 months ago
bootstrap.php
3 months ago
composer.json
3 months ago
phpunit.xml
3 months ago
test-premium-compat.sh
3 months ago
FrontendCheckCodeTest.php
291 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Batch 42 — Frontend checkCode, registerToCode, and helper methods: |
| 4 | * - checkCode: main validation flow (valid, invalid, inactive, CVV, registered, expired, stolen) |
| 5 | * - registerToCode: user registration to ticket code |
| 6 | * - isUsed / markAsUsed: one-time-use logic |
| 7 | * - countConfirmedStatus: counter increment |
| 8 | * - getOptions: public options retrieval |
| 9 | */ |
| 10 | |
| 11 | class FrontendCheckCodeTest extends WP_UnitTestCase { |
| 12 | |
| 13 | private $main; |
| 14 | private $frontend; |
| 15 | |
| 16 | public function set_up(): void { |
| 17 | parent::set_up(); |
| 18 | $this->main = sasoEventtickets::Instance(); |
| 19 | $this->frontend = $this->main->getFrontend(); |
| 20 | } |
| 21 | |
| 22 | private function createActiveCode(string $code = '', string $cvv = ''): array { |
| 23 | $listId = $this->main->getDB()->insert('lists', [ |
| 24 | 'name' => 'FE Check List ' . uniqid(), |
| 25 | 'aktiv' => 1, |
| 26 | 'meta' => '{}', |
| 27 | ]); |
| 28 | $metaObj = $this->main->getCore()->getMetaObject(); |
| 29 | $metaJson = $this->main->getCore()->json_encode_with_error_handling($metaObj); |
| 30 | |
| 31 | if (empty($code)) { |
| 32 | $code = 'FECHECK' . strtoupper(uniqid()); |
| 33 | } |
| 34 | |
| 35 | $this->main->getDB()->insert('codes', [ |
| 36 | 'list_id' => $listId, |
| 37 | 'code' => $code, |
| 38 | 'aktiv' => 1, |
| 39 | 'cvv' => $cvv, |
| 40 | 'order_id' => 0, |
| 41 | 'user_id' => 0, |
| 42 | 'meta' => $metaJson, |
| 43 | ]); |
| 44 | |
| 45 | $codeObj = $this->main->getCore()->retrieveCodeByCode($code); |
| 46 | return ['list_id' => $listId, 'code' => $code, 'codeObj' => $codeObj]; |
| 47 | } |
| 48 | |
| 49 | private function createInactiveCode(): array { |
| 50 | $listId = $this->main->getDB()->insert('lists', [ |
| 51 | 'name' => 'FE Inactive List ' . uniqid(), |
| 52 | 'aktiv' => 1, |
| 53 | 'meta' => '{}', |
| 54 | ]); |
| 55 | $metaObj = $this->main->getCore()->getMetaObject(); |
| 56 | $metaJson = $this->main->getCore()->json_encode_with_error_handling($metaObj); |
| 57 | $code = 'FEINACT' . strtoupper(uniqid()); |
| 58 | |
| 59 | $this->main->getDB()->insert('codes', [ |
| 60 | 'list_id' => $listId, |
| 61 | 'code' => $code, |
| 62 | 'aktiv' => 0, |
| 63 | 'cvv' => '', |
| 64 | 'order_id' => 0, |
| 65 | 'user_id' => 0, |
| 66 | 'meta' => $metaJson, |
| 67 | ]); |
| 68 | |
| 69 | return ['list_id' => $listId, 'code' => $code]; |
| 70 | } |
| 71 | |
| 72 | private function createStolenCode(): array { |
| 73 | $listId = $this->main->getDB()->insert('lists', [ |
| 74 | 'name' => 'FE Stolen List ' . uniqid(), |
| 75 | 'aktiv' => 1, |
| 76 | 'meta' => '{}', |
| 77 | ]); |
| 78 | $metaObj = $this->main->getCore()->getMetaObject(); |
| 79 | $metaJson = $this->main->getCore()->json_encode_with_error_handling($metaObj); |
| 80 | $code = 'FESTOLEN' . strtoupper(uniqid()); |
| 81 | |
| 82 | $this->main->getDB()->insert('codes', [ |
| 83 | 'list_id' => $listId, |
| 84 | 'code' => $code, |
| 85 | 'aktiv' => 2, // stolen |
| 86 | 'cvv' => '', |
| 87 | 'order_id' => 0, |
| 88 | 'user_id' => 0, |
| 89 | 'meta' => $metaJson, |
| 90 | ]); |
| 91 | |
| 92 | return ['list_id' => $listId, 'code' => $code]; |
| 93 | } |
| 94 | |
| 95 | // ── checkCode ──────────────────────────────────────────── |
| 96 | |
| 97 | public function test_checkCode_valid_code(): void { |
| 98 | $data = $this->createActiveCode(); |
| 99 | $result = $this->frontend->checkCode(['code' => $data['code']]); |
| 100 | $this->assertIsArray($result); |
| 101 | $this->assertEquals(1, $result['valid']); |
| 102 | } |
| 103 | |
| 104 | public function test_checkCode_nonexistent_returns_zero(): void { |
| 105 | $result = $this->frontend->checkCode(['code' => 'NONEXISTENT_XYZ_' . uniqid()]); |
| 106 | $this->assertIsArray($result); |
| 107 | $this->assertEquals(0, $result['valid']); |
| 108 | } |
| 109 | |
| 110 | public function test_checkCode_inactive_returns_two(): void { |
| 111 | $data = $this->createInactiveCode(); |
| 112 | $result = $this->frontend->checkCode(['code' => $data['code']]); |
| 113 | $this->assertIsArray($result); |
| 114 | $this->assertEquals(2, $result['valid']); |
| 115 | } |
| 116 | |
| 117 | public function test_checkCode_stolen_returns_seven(): void { |
| 118 | $data = $this->createStolenCode(); |
| 119 | $result = $this->frontend->checkCode(['code' => $data['code']]); |
| 120 | $this->assertIsArray($result); |
| 121 | $this->assertEquals(7, $result['valid']); |
| 122 | } |
| 123 | |
| 124 | public function test_checkCode_throws_for_empty_code(): void { |
| 125 | $this->expectException(Exception::class); |
| 126 | $this->expectExceptionMessageMatches('/#1001/'); |
| 127 | $this->frontend->checkCode(['code' => '']); |
| 128 | } |
| 129 | |
| 130 | public function test_checkCode_throws_for_missing_code(): void { |
| 131 | $this->expectException(Exception::class); |
| 132 | $this->expectExceptionMessageMatches('/#1001/'); |
| 133 | $this->frontend->checkCode([]); |
| 134 | } |
| 135 | |
| 136 | public function test_checkCode_cvv_required_returns_six(): void { |
| 137 | $data = $this->createActiveCode('', 'SECRET123'); |
| 138 | $result = $this->frontend->checkCode(['code' => $data['code']]); |
| 139 | $this->assertEquals(6, $result['valid']); |
| 140 | } |
| 141 | |
| 142 | public function test_checkCode_cvv_correct_returns_valid(): void { |
| 143 | $data = $this->createActiveCode('', 'SECRET123'); |
| 144 | $result = $this->frontend->checkCode(['code' => $data['code'], 'cvv' => 'SECRET123']); |
| 145 | $this->assertEquals(1, $result['valid']); |
| 146 | } |
| 147 | |
| 148 | public function test_checkCode_cvv_wrong_stays_six(): void { |
| 149 | $data = $this->createActiveCode('', 'SECRET123'); |
| 150 | $result = $this->frontend->checkCode(['code' => $data['code'], 'cvv' => 'WRONG']); |
| 151 | $this->assertEquals(6, $result['valid']); |
| 152 | } |
| 153 | |
| 154 | public function test_checkCode_cvv_case_insensitive(): void { |
| 155 | $data = $this->createActiveCode('', 'ABC'); |
| 156 | $result = $this->frontend->checkCode(['code' => $data['code'], 'cvv' => 'abc']); |
| 157 | $this->assertEquals(1, $result['valid']); |
| 158 | } |
| 159 | |
| 160 | public function test_checkCode_returns_retObject(): void { |
| 161 | $data = $this->createActiveCode(); |
| 162 | $result = $this->frontend->checkCode(['code' => $data['code']]); |
| 163 | $this->assertArrayHasKey('retObject', $result); |
| 164 | $this->assertArrayHasKey('message', $result['retObject']); |
| 165 | } |
| 166 | |
| 167 | public function test_checkCode_registered_code_returns_three(): void { |
| 168 | $data = $this->createActiveCode(); |
| 169 | // Register a user to the code |
| 170 | $metaObj = $this->main->getCore()->encodeMetaValuesAndFillObject($data['codeObj']['meta'], $data['codeObj']); |
| 171 | $metaObj['user']['value'] = 'Test User'; |
| 172 | $this->main->getCore()->saveMetaObject($data['codeObj'], $metaObj); |
| 173 | |
| 174 | $result = $this->frontend->checkCode(['code' => $data['code']]); |
| 175 | $this->assertEquals(3, $result['valid']); |
| 176 | } |
| 177 | |
| 178 | // ── isUsed ─────────────────────────────────────────────── |
| 179 | |
| 180 | public function test_isUsed_false_for_new_code(): void { |
| 181 | $data = $this->createActiveCode(); |
| 182 | $this->assertFalse($this->frontend->isUsed($data['codeObj'])); |
| 183 | } |
| 184 | |
| 185 | public function test_isUsed_true_after_marking_used(): void { |
| 186 | $data = $this->createActiveCode(); |
| 187 | $codeObj = $data['codeObj']; |
| 188 | $metaObj = $this->main->getCore()->encodeMetaValuesAndFillObject($codeObj['meta'], $codeObj); |
| 189 | $metaObj['used']['reg_request'] = wp_date("Y-m-d H:i:s"); |
| 190 | $codeObj['meta'] = $this->main->getCore()->json_encode_with_error_handling($metaObj); |
| 191 | |
| 192 | $this->assertTrue($this->frontend->isUsed($codeObj)); |
| 193 | } |
| 194 | |
| 195 | // ── countConfirmedStatus ───────────────────────────────── |
| 196 | |
| 197 | public function test_countConfirmedStatus_increments(): void { |
| 198 | $data = $this->createActiveCode(); |
| 199 | $codeObj = $data['codeObj']; |
| 200 | $codeObj['_valid'] = 1; |
| 201 | |
| 202 | $result = $this->frontend->countConfirmedStatus($codeObj); |
| 203 | $this->assertIsArray($result); |
| 204 | |
| 205 | // Reload and check confirmedCount |
| 206 | $reloaded = $this->main->getCore()->retrieveCodeByCode($data['code']); |
| 207 | $reloadedMeta = $this->main->getCore()->encodeMetaValuesAndFillObject($reloaded['meta'], $reloaded); |
| 208 | $this->assertEquals(1, (int) $reloadedMeta['confirmedCount']); |
| 209 | } |
| 210 | |
| 211 | public function test_countConfirmedStatus_skips_when_not_valid(): void { |
| 212 | $data = $this->createActiveCode(); |
| 213 | $codeObj = $data['codeObj']; |
| 214 | $codeObj['_valid'] = 0; // not valid |
| 215 | |
| 216 | $this->frontend->countConfirmedStatus($codeObj); |
| 217 | |
| 218 | $reloaded = $this->main->getCore()->retrieveCodeByCode($data['code']); |
| 219 | $reloadedMeta = $this->main->getCore()->encodeMetaValuesAndFillObject($reloaded['meta'], $reloaded); |
| 220 | $this->assertEquals(0, (int) $reloadedMeta['confirmedCount']); |
| 221 | } |
| 222 | |
| 223 | public function test_countConfirmedStatus_force_increments(): void { |
| 224 | $data = $this->createActiveCode(); |
| 225 | $codeObj = $data['codeObj']; |
| 226 | $codeObj['_valid'] = 0; // normally skipped |
| 227 | |
| 228 | $this->frontend->countConfirmedStatus($codeObj, true); // force |
| 229 | |
| 230 | $reloaded = $this->main->getCore()->retrieveCodeByCode($data['code']); |
| 231 | $reloadedMeta = $this->main->getCore()->encodeMetaValuesAndFillObject($reloaded['meta'], $reloaded); |
| 232 | $this->assertEquals(1, (int) $reloadedMeta['confirmedCount']); |
| 233 | } |
| 234 | |
| 235 | public function test_countConfirmedStatus_sets_first_success(): void { |
| 236 | $data = $this->createActiveCode(); |
| 237 | $codeObj = $data['codeObj']; |
| 238 | $codeObj['_valid'] = 1; |
| 239 | |
| 240 | $this->frontend->countConfirmedStatus($codeObj); |
| 241 | |
| 242 | $reloaded = $this->main->getCore()->retrieveCodeByCode($data['code']); |
| 243 | $reloadedMeta = $this->main->getCore()->encodeMetaValuesAndFillObject($reloaded['meta'], $reloaded); |
| 244 | $this->assertNotEmpty($reloadedMeta['validation']['first_success']); |
| 245 | $this->assertNotEmpty($reloadedMeta['validation']['last_success']); |
| 246 | } |
| 247 | |
| 248 | public function test_countConfirmedStatus_multiple_calls(): void { |
| 249 | $data = $this->createActiveCode(); |
| 250 | $codeObj = $data['codeObj']; |
| 251 | $codeObj['_valid'] = 1; |
| 252 | |
| 253 | $codeObj = $this->frontend->countConfirmedStatus($codeObj); |
| 254 | $codeObj['_valid'] = 1; // reset for second call |
| 255 | $codeObj = $this->frontend->countConfirmedStatus($codeObj, true); |
| 256 | |
| 257 | $reloaded = $this->main->getCore()->retrieveCodeByCode($data['code']); |
| 258 | $reloadedMeta = $this->main->getCore()->encodeMetaValuesAndFillObject($reloaded['meta'], $reloaded); |
| 259 | $this->assertEquals(2, (int) $reloadedMeta['confirmedCount']); |
| 260 | } |
| 261 | |
| 262 | // ── getOptions ─────────────────────────────────────────── |
| 263 | |
| 264 | public function test_getOptions_returns_array(): void { |
| 265 | $result = $this->frontend->getOptions(); |
| 266 | $this->assertIsArray($result); |
| 267 | } |
| 268 | |
| 269 | // ── markAsUsed ─────────────────────────────────────────── |
| 270 | |
| 271 | public function test_markAsUsed_returns_codeObj(): void { |
| 272 | $data = $this->createActiveCode(); |
| 273 | $result = $this->frontend->markAsUsed($data['codeObj']); |
| 274 | $this->assertIsArray($result); |
| 275 | $this->assertArrayHasKey('id', $result); |
| 276 | } |
| 277 | |
| 278 | public function test_markAsUsed_with_force(): void { |
| 279 | $data = $this->createActiveCode(); |
| 280 | $codeObj = $data['codeObj']; |
| 281 | |
| 282 | // Force mark as used with 1 count |
| 283 | $result = $this->frontend->markAsUsed($codeObj, true); |
| 284 | $this->assertIsArray($result); |
| 285 | |
| 286 | // After forced mark, isUsed should be true |
| 287 | $reloaded = $this->main->getCore()->retrieveCodeByCode($data['code']); |
| 288 | $this->assertTrue($this->frontend->isUsed($reloaded)); |
| 289 | } |
| 290 | } |
| 291 |