bin
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
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
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
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
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
AdminCodeCleanupAndPDFTest.php
262 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Batch 50 — Admin order/ticket cleanup + PDF generation (file mode): |
| 4 | * - removeWoocommerceOrderInfoFromCode: clears WC order link from code |
| 5 | * - removeWoocommerceTicketForCode: removes ticket meta from code |
| 6 | * - outputPDFTicketsForOrder: generate merged PDF for order (file mode) |
| 7 | * - generateOnePDFForCodes: generate merged PDF for code list (file mode) |
| 8 | * - outputPDF: generate single ticket PDF (file mode) |
| 9 | */ |
| 10 | |
| 11 | class AdminCodeCleanupAndPDFTest extends WP_UnitTestCase { |
| 12 | |
| 13 | private $main; |
| 14 | |
| 15 | public function set_up(): void { |
| 16 | parent::set_up(); |
| 17 | $this->main = sasoEventtickets::Instance(); |
| 18 | |
| 19 | if (!class_exists('WC_Product_Simple')) { |
| 20 | $this->markTestSkipped('WooCommerce not available'); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Create a full ticket setup: product, order, code linked to order item. |
| 26 | */ |
| 27 | private function createFullTicketSetup(): array { |
| 28 | $product = new WC_Product_Simple(); |
| 29 | $product->set_name('Cleanup Test ' . uniqid()); |
| 30 | $product->set_regular_price('10.00'); |
| 31 | $product->set_status('publish'); |
| 32 | $product->save(); |
| 33 | $pid = $product->get_id(); |
| 34 | |
| 35 | update_post_meta($pid, 'saso_eventtickets_is_ticket', 'yes'); |
| 36 | update_post_meta($pid, 'saso_eventtickets_ticket_start_date', '2026-12-25'); |
| 37 | |
| 38 | $listId = $this->main->getDB()->insert('lists', [ |
| 39 | 'name' => 'Cleanup List ' . uniqid(), |
| 40 | 'aktiv' => 1, |
| 41 | 'meta' => '{}', |
| 42 | ]); |
| 43 | |
| 44 | $order = wc_create_order(); |
| 45 | $order->add_product($product, 1); |
| 46 | $order->set_status('completed'); |
| 47 | $order->save(); |
| 48 | |
| 49 | $items = $order->get_items(); |
| 50 | $firstItemId = array_key_first($items); |
| 51 | |
| 52 | $metaObj = $this->main->getCore()->getMetaObject(); |
| 53 | $metaObj['woocommerce']['product_id'] = $pid; |
| 54 | $metaObj['woocommerce']['item_id'] = $firstItemId; |
| 55 | $metaObj['woocommerce']['order_id'] = $order->get_id(); |
| 56 | $metaJson = $this->main->getCore()->json_encode_with_error_handling($metaObj); |
| 57 | |
| 58 | $codeStr = 'CLEAN' . strtoupper(uniqid()); |
| 59 | $this->main->getDB()->insert('codes', [ |
| 60 | 'list_id' => $listId, |
| 61 | 'code' => $codeStr, |
| 62 | 'aktiv' => 1, |
| 63 | 'cvv' => '', |
| 64 | 'order_id' => $order->get_id(), |
| 65 | 'user_id' => 0, |
| 66 | 'meta' => $metaJson, |
| 67 | ]); |
| 68 | |
| 69 | // Store code reference in order item meta |
| 70 | $orderItem = $items[$firstItemId]; |
| 71 | $orderItem->update_meta_data('_saso_eventtickets_product_code', $codeStr); |
| 72 | $orderItem->update_meta_data('_saso_eventtickets_is_ticket', 'yes'); |
| 73 | $orderItem->save(); |
| 74 | |
| 75 | $codeObj = $this->main->getCore()->retrieveCodeByCode($codeStr); |
| 76 | |
| 77 | return [ |
| 78 | 'product_id' => $pid, |
| 79 | 'list_id' => $listId, |
| 80 | 'order' => $order, |
| 81 | 'order_id' => $order->get_id(), |
| 82 | 'item_id' => $firstItemId, |
| 83 | 'code' => $codeStr, |
| 84 | 'codeObj' => $codeObj, |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | // ── removeWoocommerceOrderInfoFromCode ─────────────────────── |
| 89 | |
| 90 | public function test_removeOrderInfoFromCode_clears_order_id(): void { |
| 91 | $setup = $this->createFullTicketSetup(); |
| 92 | |
| 93 | $result = $this->main->getAdmin()->removeWoocommerceOrderInfoFromCode([ |
| 94 | 'code' => $setup['code'], |
| 95 | ]); |
| 96 | |
| 97 | $this->assertIsArray($result); |
| 98 | |
| 99 | // Reload and check order_id was reset |
| 100 | $reloaded = $this->main->getCore()->retrieveCodeByCode($setup['code']); |
| 101 | $this->assertEquals(0, intval($reloaded['order_id'])); |
| 102 | } |
| 103 | |
| 104 | public function test_removeOrderInfoFromCode_clears_wc_meta(): void { |
| 105 | $setup = $this->createFullTicketSetup(); |
| 106 | |
| 107 | $this->main->getAdmin()->removeWoocommerceOrderInfoFromCode([ |
| 108 | 'code' => $setup['code'], |
| 109 | ]); |
| 110 | |
| 111 | $reloaded = $this->main->getCore()->retrieveCodeByCode($setup['code']); |
| 112 | $metaObj = $this->main->getCore()->encodeMetaValuesAndFillObject($reloaded['meta'], $reloaded); |
| 113 | $this->assertEquals(0, intval($metaObj['woocommerce']['order_id'])); |
| 114 | $this->assertEquals(0, intval($metaObj['woocommerce']['product_id'])); |
| 115 | } |
| 116 | |
| 117 | public function test_removeOrderInfoFromCode_throws_without_code(): void { |
| 118 | $this->expectException(Exception::class); |
| 119 | $this->expectExceptionMessageMatches('/#9611/'); |
| 120 | $this->main->getAdmin()->removeWoocommerceOrderInfoFromCode([]); |
| 121 | } |
| 122 | |
| 123 | public function test_removeOrderInfoFromCode_throws_for_nonexistent(): void { |
| 124 | $this->expectException(Exception::class); |
| 125 | $this->main->getAdmin()->removeWoocommerceOrderInfoFromCode([ |
| 126 | 'code' => 'NONEXISTENT_' . uniqid(), |
| 127 | ]); |
| 128 | } |
| 129 | |
| 130 | // ── removeWoocommerceTicketForCode ─────────────────────────── |
| 131 | |
| 132 | public function test_removeTicketForCode_clears_wc_ticket(): void { |
| 133 | $setup = $this->createFullTicketSetup(); |
| 134 | |
| 135 | $result = $this->main->getAdmin()->removeWoocommerceTicketForCode([ |
| 136 | 'code' => $setup['code'], |
| 137 | ]); |
| 138 | |
| 139 | $this->assertIsArray($result); |
| 140 | |
| 141 | $reloaded = $this->main->getCore()->retrieveCodeByCode($setup['code']); |
| 142 | $metaObj = $this->main->getCore()->encodeMetaValuesAndFillObject($reloaded['meta'], $reloaded); |
| 143 | // wc_ticket should be reset to defaults (empty redeemed_date, etc.) |
| 144 | $this->assertEmpty($metaObj['wc_ticket']['redeemed_date']); |
| 145 | } |
| 146 | |
| 147 | public function test_removeTicketForCode_preserves_idcode(): void { |
| 148 | $setup = $this->createFullTicketSetup(); |
| 149 | |
| 150 | // Set an idcode first |
| 151 | $codeObj = $setup['codeObj']; |
| 152 | $metaObj = $this->main->getCore()->encodeMetaValuesAndFillObject($codeObj['meta'], $codeObj); |
| 153 | $metaObj['wc_ticket']['idcode'] = 'TK-12345'; |
| 154 | $this->main->getCore()->saveMetaObject($codeObj, $metaObj); |
| 155 | |
| 156 | $this->main->getAdmin()->removeWoocommerceTicketForCode([ |
| 157 | 'code' => $setup['code'], |
| 158 | ]); |
| 159 | |
| 160 | $reloaded = $this->main->getCore()->retrieveCodeByCode($setup['code']); |
| 161 | $metaObj2 = $this->main->getCore()->encodeMetaValuesAndFillObject($reloaded['meta'], $reloaded); |
| 162 | // idcode should be preserved |
| 163 | $this->assertEquals('TK-12345', $metaObj2['wc_ticket']['idcode']); |
| 164 | } |
| 165 | |
| 166 | public function test_removeTicketForCode_throws_without_code(): void { |
| 167 | $this->expectException(Exception::class); |
| 168 | $this->expectExceptionMessageMatches('/#9625/'); |
| 169 | $this->main->getAdmin()->removeWoocommerceTicketForCode([]); |
| 170 | } |
| 171 | |
| 172 | // ── PDF generation (file mode) ────────────────────────────── |
| 173 | |
| 174 | public function test_outputPDF_file_mode_creates_file(): void { |
| 175 | $setup = $this->createFullTicketSetup(); |
| 176 | $ticket = $this->main->getTicketHandler(); |
| 177 | |
| 178 | $ticket->setCodeObj($setup['codeObj']); |
| 179 | $ticket->setOrder($setup['order']); |
| 180 | |
| 181 | $filepath = $ticket->outputPDF('F'); |
| 182 | $this->assertNotEmpty($filepath); |
| 183 | $this->assertFileExists($filepath); |
| 184 | |
| 185 | // Cleanup |
| 186 | @unlink($filepath); |
| 187 | } |
| 188 | |
| 189 | public function test_outputPDF_file_is_valid_pdf(): void { |
| 190 | $setup = $this->createFullTicketSetup(); |
| 191 | $ticket = $this->main->getTicketHandler(); |
| 192 | |
| 193 | $ticket->setCodeObj($setup['codeObj']); |
| 194 | $ticket->setOrder($setup['order']); |
| 195 | |
| 196 | $filepath = $ticket->outputPDF('F'); |
| 197 | $content = file_get_contents($filepath); |
| 198 | $this->assertStringStartsWith('%PDF', $content); |
| 199 | |
| 200 | @unlink($filepath); |
| 201 | } |
| 202 | |
| 203 | public function test_outputPDFTicketsForOrder_file_mode(): void { |
| 204 | $setup = $this->createFullTicketSetup(); |
| 205 | $ticket = $this->main->getTicketHandler(); |
| 206 | |
| 207 | // outputPDFTicketsForOrder uses ob_start internally, capture any stray output |
| 208 | $this->expectOutputRegex('/.*/'); |
| 209 | |
| 210 | $filepath = $ticket->outputPDFTicketsForOrder($setup['order'], 'F'); |
| 211 | $this->assertNotEmpty($filepath); |
| 212 | $this->assertFileExists($filepath); |
| 213 | |
| 214 | $content = file_get_contents($filepath); |
| 215 | $this->assertStringStartsWith('%PDF', $content); |
| 216 | |
| 217 | @unlink($filepath); |
| 218 | } |
| 219 | |
| 220 | public function test_generateOnePDFForCodes_file_mode(): void { |
| 221 | $setup = $this->createFullTicketSetup(); |
| 222 | $ticket = $this->main->getTicketHandler(); |
| 223 | |
| 224 | $filepath = $ticket->generateOnePDFForCodes( |
| 225 | [$setup['code']], |
| 226 | 'test_merged.pdf', |
| 227 | 'F' |
| 228 | ); |
| 229 | |
| 230 | $this->assertNotEmpty($filepath); |
| 231 | $this->assertFileExists($filepath); |
| 232 | |
| 233 | $content = file_get_contents($filepath); |
| 234 | $this->assertStringStartsWith('%PDF', $content); |
| 235 | |
| 236 | @unlink($filepath); |
| 237 | } |
| 238 | |
| 239 | public function test_generateOnePDFForCodes_empty_array(): void { |
| 240 | $ticket = $this->main->getTicketHandler(); |
| 241 | $result = $ticket->generateOnePDFForCodes([], null, 'F'); |
| 242 | $this->assertNull($result); |
| 243 | } |
| 244 | |
| 245 | public function test_generateOnePDFForCodes_ignores_invalid_codes(): void { |
| 246 | $setup = $this->createFullTicketSetup(); |
| 247 | $ticket = $this->main->getTicketHandler(); |
| 248 | |
| 249 | // Mix valid and invalid codes - should skip invalid |
| 250 | $filepath = $ticket->generateOnePDFForCodes( |
| 251 | ['NONEXISTENT123', $setup['code']], |
| 252 | 'test_mixed.pdf', |
| 253 | 'F' |
| 254 | ); |
| 255 | |
| 256 | $this->assertNotEmpty($filepath); |
| 257 | $this->assertFileExists($filepath); |
| 258 | |
| 259 | @unlink($filepath); |
| 260 | } |
| 261 | } |
| 262 |