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
OrderTicketGenerationTest.php
432 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Integration tests for order → ticket generation pipeline. |
| 4 | * |
| 5 | * These tests create real WooCommerce products/orders and verify |
| 6 | * the ticket generation pipeline works end-to-end. |
| 7 | */ |
| 8 | |
| 9 | class OrderTicketGenerationTest extends WP_UnitTestCase { |
| 10 | |
| 11 | private $main; |
| 12 | |
| 13 | public function set_up(): void { |
| 14 | parent::set_up(); |
| 15 | $this->main = sasoEventtickets::Instance(); |
| 16 | |
| 17 | // Ensure WooCommerce is loaded |
| 18 | if (!class_exists('WC_Product_Simple')) { |
| 19 | $this->markTestSkipped('WooCommerce not available'); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Helper: create a simple ticket product with a list. |
| 25 | */ |
| 26 | private function createTicketProduct(): array { |
| 27 | // Create a ticket list |
| 28 | $listId = $this->main->getDB()->insert('lists', [ |
| 29 | 'name' => 'Order Test List ' . uniqid(), |
| 30 | 'aktiv' => 1, |
| 31 | 'meta' => '{}', |
| 32 | ]); |
| 33 | |
| 34 | // Create WC product |
| 35 | $product = new WC_Product_Simple(); |
| 36 | $product->set_name('Test Ticket Product'); |
| 37 | $product->set_regular_price('10.00'); |
| 38 | $product->set_status('publish'); |
| 39 | $product->save(); |
| 40 | |
| 41 | $productId = $product->get_id(); |
| 42 | |
| 43 | // Mark as ticket product |
| 44 | update_post_meta($productId, 'saso_eventtickets_is_ticket', 'yes'); |
| 45 | update_post_meta($productId, 'saso_eventtickets_list', $listId); |
| 46 | |
| 47 | return ['product' => $product, 'product_id' => $productId, 'list_id' => $listId]; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Helper: create a non-ticket product. |
| 52 | */ |
| 53 | private function createNonTicketProduct(): WC_Product_Simple { |
| 54 | $product = new WC_Product_Simple(); |
| 55 | $product->set_name('Regular Product'); |
| 56 | $product->set_regular_price('5.00'); |
| 57 | $product->set_status('publish'); |
| 58 | $product->save(); |
| 59 | return $product; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Helper: create an order with items. |
| 64 | */ |
| 65 | private function createOrder(array $items, string $status = 'completed'): WC_Order { |
| 66 | $order = wc_create_order(); |
| 67 | foreach ($items as $item) { |
| 68 | $order->add_product($item['product'], $item['quantity'] ?? 1); |
| 69 | } |
| 70 | $order->calculate_totals(); |
| 71 | $order->set_status($status); |
| 72 | $order->save(); |
| 73 | return $order; |
| 74 | } |
| 75 | |
| 76 | // ── hasTicketsInOrder ────────────────────────────────────── |
| 77 | |
| 78 | public function test_hasTicketsInOrder_with_ticket_product(): void { |
| 79 | $ticket = $this->createTicketProduct(); |
| 80 | $order = $this->createOrder([ |
| 81 | ['product' => $ticket['product']], |
| 82 | ]); |
| 83 | |
| 84 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 85 | $this->assertTrue($wcOrder->hasTicketsInOrder($order)); |
| 86 | } |
| 87 | |
| 88 | public function test_hasTicketsInOrder_without_ticket_product(): void { |
| 89 | $product = $this->createNonTicketProduct(); |
| 90 | $order = $this->createOrder([ |
| 91 | ['product' => $product], |
| 92 | ]); |
| 93 | |
| 94 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 95 | $this->assertFalse($wcOrder->hasTicketsInOrder($order)); |
| 96 | } |
| 97 | |
| 98 | public function test_hasTicketsInOrder_mixed_products(): void { |
| 99 | $ticket = $this->createTicketProduct(); |
| 100 | $regular = $this->createNonTicketProduct(); |
| 101 | $order = $this->createOrder([ |
| 102 | ['product' => $ticket['product']], |
| 103 | ['product' => $regular], |
| 104 | ]); |
| 105 | |
| 106 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 107 | $this->assertTrue($wcOrder->hasTicketsInOrder($order)); |
| 108 | } |
| 109 | |
| 110 | // ── Ticket Generation Pipeline ───────────────────────────── |
| 111 | |
| 112 | public function test_add_serialcode_creates_codes(): void { |
| 113 | $ticket = $this->createTicketProduct(); |
| 114 | $order = $this->createOrder([ |
| 115 | ['product' => $ticket['product'], 'quantity' => 2], |
| 116 | ]); |
| 117 | |
| 118 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 119 | $wcOrder->add_serialcode_to_order($order->get_id()); |
| 120 | |
| 121 | // Check codes were created in DB for this order |
| 122 | $codes = $this->main->getCore()->getCodesByOrderId($order->get_id()); |
| 123 | $this->assertGreaterThanOrEqual(2, count($codes), 'Should have created at least 2 codes for quantity 2'); |
| 124 | } |
| 125 | |
| 126 | public function test_add_serialcode_idempotent(): void { |
| 127 | $ticket = $this->createTicketProduct(); |
| 128 | $order = $this->createOrder([ |
| 129 | ['product' => $ticket['product'], 'quantity' => 1], |
| 130 | ]); |
| 131 | |
| 132 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 133 | |
| 134 | // Run twice |
| 135 | $wcOrder->add_serialcode_to_order($order->get_id()); |
| 136 | $countFirst = count($this->main->getCore()->getCodesByOrderId($order->get_id())); |
| 137 | |
| 138 | $wcOrder->add_serialcode_to_order($order->get_id()); |
| 139 | $countSecond = count($this->main->getCore()->getCodesByOrderId($order->get_id())); |
| 140 | |
| 141 | // Should not create duplicate codes |
| 142 | $this->assertSame($countFirst, $countSecond, 'Running add_serialcode twice should not create duplicates'); |
| 143 | } |
| 144 | |
| 145 | // ── getTicketsFromOrder ──────────────────────────────────── |
| 146 | |
| 147 | public function test_getTicketsFromOrder_returns_ticket_items(): void { |
| 148 | $ticket = $this->createTicketProduct(); |
| 149 | $order = $this->createOrder([ |
| 150 | ['product' => $ticket['product'], 'quantity' => 3], |
| 151 | ]); |
| 152 | |
| 153 | // Generate codes first |
| 154 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 155 | $wcOrder->add_serialcode_to_order($order->get_id()); |
| 156 | |
| 157 | // Refresh order |
| 158 | $order = wc_get_order($order->get_id()); |
| 159 | $tickets = $wcOrder->getTicketsFromOrder($order); |
| 160 | |
| 161 | $this->assertNotEmpty($tickets); |
| 162 | $first = reset($tickets); |
| 163 | $this->assertEquals(3, $first['quantity']); |
| 164 | $this->assertEquals($ticket['product_id'], $first['product_id']); |
| 165 | } |
| 166 | |
| 167 | public function test_getTicketsFromOrder_excludes_non_ticket_items(): void { |
| 168 | $ticket = $this->createTicketProduct(); |
| 169 | $regular = $this->createNonTicketProduct(); |
| 170 | $order = $this->createOrder([ |
| 171 | ['product' => $ticket['product']], |
| 172 | ['product' => $regular], |
| 173 | ]); |
| 174 | |
| 175 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 176 | $wcOrder->add_serialcode_to_order($order->get_id()); |
| 177 | |
| 178 | $order = wc_get_order($order->get_id()); |
| 179 | $tickets = $wcOrder->getTicketsFromOrder($order); |
| 180 | |
| 181 | // Should only contain the ticket product, not the regular one |
| 182 | foreach ($tickets as $t) { |
| 183 | $this->assertEquals($ticket['product_id'], $t['product_id']); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // ── Order Status Change Handling ─────────────────────────── |
| 188 | |
| 189 | public function test_status_change_to_completed_generates_tickets(): void { |
| 190 | $ticket = $this->createTicketProduct(); |
| 191 | |
| 192 | // Create order in pending status |
| 193 | $order = $this->createOrder([ |
| 194 | ['product' => $ticket['product'], 'quantity' => 1], |
| 195 | ], 'pending'); |
| 196 | |
| 197 | // No codes yet |
| 198 | $codesBefore = $this->main->getCore()->getCodesByOrderId($order->get_id()); |
| 199 | $this->assertEmpty($codesBefore); |
| 200 | |
| 201 | // Actually set order to completed (so isOrderPaid returns true), |
| 202 | // then call the handler as WooCommerce would. |
| 203 | $order->set_status('completed'); |
| 204 | $order->save(); |
| 205 | |
| 206 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 207 | $wcOrder->woocommerce_order_status_changed($order->get_id(), 'pending', 'completed'); |
| 208 | |
| 209 | $codesAfter = $this->main->getCore()->getCodesByOrderId($order->get_id()); |
| 210 | $this->assertNotEmpty($codesAfter); |
| 211 | } |
| 212 | |
| 213 | public function test_status_change_to_cancelled_does_not_generate_tickets(): void { |
| 214 | $ticket = $this->createTicketProduct(); |
| 215 | $order = $this->createOrder([ |
| 216 | ['product' => $ticket['product']], |
| 217 | ], 'pending'); |
| 218 | |
| 219 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 220 | $wcOrder->woocommerce_order_status_changed($order->get_id(), 'pending', 'cancelled'); |
| 221 | |
| 222 | $codes = $this->main->getCore()->getCodesByOrderId($order->get_id()); |
| 223 | $this->assertEmpty($codes); |
| 224 | } |
| 225 | |
| 226 | // ── Code Metadata ────────────────────────────────────────── |
| 227 | |
| 228 | public function test_generated_code_has_correct_metadata(): void { |
| 229 | $ticket = $this->createTicketProduct(); |
| 230 | $order = $this->createOrder([ |
| 231 | ['product' => $ticket['product'], 'quantity' => 1], |
| 232 | ]); |
| 233 | |
| 234 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 235 | $wcOrder->add_serialcode_to_order($order->get_id()); |
| 236 | |
| 237 | $codes = $this->main->getCore()->getCodesByOrderId($order->get_id()); |
| 238 | $this->assertNotEmpty($codes); |
| 239 | |
| 240 | $codeObj = $codes[0]; |
| 241 | $metaObj = $this->main->getCore()->encodeMetaValuesAndFillObject($codeObj['meta'], $codeObj); |
| 242 | |
| 243 | $this->assertEquals($order->get_id(), $metaObj['woocommerce']['order_id']); |
| 244 | $this->assertEquals($ticket['product_id'], $metaObj['woocommerce']['product_id']); |
| 245 | $this->assertEquals(1, $metaObj['wc_ticket']['is_ticket']); |
| 246 | } |
| 247 | |
| 248 | // ── add_serialcode_to_order — edge cases ──────────────────── |
| 249 | |
| 250 | public function test_add_serialcode_skips_zero_order_id(): void { |
| 251 | $this->main->getWC()->getOrderManager()->add_serialcode_to_order(0); |
| 252 | $this->assertTrue(true); // No exception = pass |
| 253 | } |
| 254 | |
| 255 | public function test_add_serialcode_skips_nonexistent_order(): void { |
| 256 | $this->main->getWC()->getOrderManager()->add_serialcode_to_order(999999); |
| 257 | $this->assertTrue(true); // No exception = pass |
| 258 | } |
| 259 | |
| 260 | public function test_add_serialcode_skips_pending_order(): void { |
| 261 | $ticket = $this->createTicketProduct(); |
| 262 | $order = $this->createOrder([ |
| 263 | ['product' => $ticket['product'], 'quantity' => 1], |
| 264 | ], 'pending'); |
| 265 | |
| 266 | $this->main->getWC()->getOrderManager()->add_serialcode_to_order($order->get_id()); |
| 267 | |
| 268 | $codes = $this->main->getCore()->getCodesByOrderId($order->get_id()); |
| 269 | $this->assertEmpty($codes, 'No codes should be generated for pending (unpaid) order'); |
| 270 | } |
| 271 | |
| 272 | // ── woocommerce_order_status_changed — refund path ────────── |
| 273 | |
| 274 | public function test_status_change_to_refunded_does_not_generate_tickets(): void { |
| 275 | $ticket = $this->createTicketProduct(); |
| 276 | $order = $this->createOrder([ |
| 277 | ['product' => $ticket['product']], |
| 278 | ], 'pending'); |
| 279 | |
| 280 | $this->main->getWC()->getOrderManager()->woocommerce_order_status_changed( |
| 281 | $order->get_id(), 'completed', 'refunded' |
| 282 | ); |
| 283 | |
| 284 | $codes = $this->main->getCore()->getCodesByOrderId($order->get_id()); |
| 285 | $this->assertEmpty($codes); |
| 286 | } |
| 287 | |
| 288 | public function test_status_change_fires_action(): void { |
| 289 | $ticket = $this->createTicketProduct(); |
| 290 | $order = $this->createOrder([ |
| 291 | ['product' => $ticket['product']], |
| 292 | ]); |
| 293 | |
| 294 | $fired = false; |
| 295 | $callback = function () use (&$fired) { |
| 296 | $fired = true; |
| 297 | }; |
| 298 | add_action($this->main->_do_action_prefix . 'woocommerce-hooks_woocommerce_order_status_changed', $callback); |
| 299 | |
| 300 | $this->main->getWC()->getOrderManager()->woocommerce_order_status_changed( |
| 301 | $order->get_id(), 'pending', 'completed' |
| 302 | ); |
| 303 | |
| 304 | $this->assertTrue($fired); |
| 305 | remove_action($this->main->_do_action_prefix . 'woocommerce-hooks_woocommerce_order_status_changed', $callback); |
| 306 | } |
| 307 | |
| 308 | // ── Order item meta correctness ───────────────────────────── |
| 309 | |
| 310 | public function test_ticket_is_marked_in_order_item_meta(): void { |
| 311 | $ticket = $this->createTicketProduct(); |
| 312 | $order = $this->createOrder([ |
| 313 | ['product' => $ticket['product']], |
| 314 | ]); |
| 315 | |
| 316 | $this->main->getWC()->getOrderManager()->add_serialcode_to_order($order->get_id()); |
| 317 | |
| 318 | $order = wc_get_order($order->get_id()); |
| 319 | foreach ($order->get_items() as $item_id => $item) { |
| 320 | $isTicket = wc_get_order_item_meta($item_id, '_saso_eventtickets_is_ticket', true); |
| 321 | $pid = $item->get_product_id(); |
| 322 | if (get_post_meta($pid, 'saso_eventtickets_is_ticket', true) == 'yes') { |
| 323 | // Order item stores 1 (truthy), not 'yes' |
| 324 | $this->assertNotEmpty($isTicket); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | public function test_code_list_stored_in_order_item_meta(): void { |
| 330 | $ticket = $this->createTicketProduct(); |
| 331 | $order = $this->createOrder([ |
| 332 | ['product' => $ticket['product']], |
| 333 | ]); |
| 334 | |
| 335 | $this->main->getWC()->getOrderManager()->add_serialcode_to_order($order->get_id()); |
| 336 | |
| 337 | $order = wc_get_order($order->get_id()); |
| 338 | foreach ($order->get_items() as $item_id => $item) { |
| 339 | $codeList = wc_get_order_item_meta($item_id, '_saso_eventticket_code_list', true); |
| 340 | $pid = $item->get_product_id(); |
| 341 | if (get_post_meta($pid, 'saso_eventtickets_is_ticket', true) == 'yes') { |
| 342 | $this->assertEquals($ticket['list_id'], intval($codeList)); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | public function test_public_ticket_ids_stored_in_order_item_meta(): void { |
| 348 | $ticket = $this->createTicketProduct(); |
| 349 | $order = $this->createOrder([ |
| 350 | ['product' => $ticket['product'], 'quantity' => 2], |
| 351 | ]); |
| 352 | |
| 353 | $this->main->getWC()->getOrderManager()->add_serialcode_to_order($order->get_id()); |
| 354 | |
| 355 | $order = wc_get_order($order->get_id()); |
| 356 | foreach ($order->get_items() as $item_id => $item) { |
| 357 | $publicIds = wc_get_order_item_meta($item_id, '_saso_eventtickets_public_ticket_ids', true); |
| 358 | $pid = $item->get_product_id(); |
| 359 | if (get_post_meta($pid, 'saso_eventtickets_is_ticket', true) == 'yes') { |
| 360 | $this->assertNotEmpty($publicIds, 'Public ticket IDs should be stored'); |
| 361 | $idsArr = explode(',', $publicIds); |
| 362 | $this->assertCount(2, $idsArr, 'Should have 2 public ticket IDs for qty 2'); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // ── woocommerce_delete_order_item ─────────────────────────── |
| 368 | |
| 369 | public function test_delete_order_item_clears_wc_meta_from_code(): void { |
| 370 | update_option('sasoEventticketswcRestrictFreeCodeByOrderRefund', '1'); |
| 371 | $this->main->getOptions()->initOptions(); |
| 372 | |
| 373 | $ticket = $this->createTicketProduct(); |
| 374 | $order = $this->createOrder([ |
| 375 | ['product' => $ticket['product']], |
| 376 | ]); |
| 377 | |
| 378 | $this->main->getWC()->getOrderManager()->add_serialcode_to_order($order->get_id()); |
| 379 | |
| 380 | $order = wc_get_order($order->get_id()); |
| 381 | $codeStr = ''; |
| 382 | $targetItemId = 0; |
| 383 | foreach ($order->get_items() as $item_id => $item) { |
| 384 | $codes = wc_get_order_item_meta($item_id, '_saso_eventtickets_product_code', true); |
| 385 | if (!empty($codes)) { |
| 386 | $codeStr = trim(explode(',', $codes)[0]); |
| 387 | $targetItemId = $item_id; |
| 388 | } |
| 389 | } |
| 390 | $this->assertNotEmpty($codeStr); |
| 391 | |
| 392 | // Before deletion, code has WC order info |
| 393 | $codeObjBefore = $this->main->getCore()->retrieveCodeByCode($codeStr); |
| 394 | $metaBefore = json_decode($codeObjBefore['meta'], true); |
| 395 | $this->assertNotEmpty($metaBefore['woocommerce']['order_id'] ?? 0); |
| 396 | |
| 397 | $this->main->getWC()->getOrderManager()->woocommerce_delete_order_item($targetItemId); |
| 398 | |
| 399 | // After deletion, WC info should be cleared from code meta |
| 400 | $codeObjAfter = $this->main->getCore()->retrieveCodeByCode($codeStr); |
| 401 | $metaAfter = json_decode($codeObjAfter['meta'], true); |
| 402 | $this->assertEmpty($metaAfter['woocommerce']['order_id'] ?? 0, 'WC order info should be cleared after item deletion'); |
| 403 | } |
| 404 | |
| 405 | public function test_delete_order_item_fires_action(): void { |
| 406 | $ticket = $this->createTicketProduct(); |
| 407 | $order = $this->createOrder([ |
| 408 | ['product' => $ticket['product']], |
| 409 | ]); |
| 410 | |
| 411 | $this->main->getWC()->getOrderManager()->add_serialcode_to_order($order->get_id()); |
| 412 | |
| 413 | $order = wc_get_order($order->get_id()); |
| 414 | $targetItemId = 0; |
| 415 | foreach ($order->get_items() as $item_id => $item) { |
| 416 | $targetItemId = $item_id; |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | $fired = false; |
| 421 | $callback = function () use (&$fired) { |
| 422 | $fired = true; |
| 423 | }; |
| 424 | add_action($this->main->_do_action_prefix . 'woocommerce-hooks_woocommerce_delete_order_item', $callback); |
| 425 | |
| 426 | $this->main->getWC()->getOrderManager()->woocommerce_delete_order_item($targetItemId); |
| 427 | |
| 428 | $this->assertTrue($fired); |
| 429 | remove_action($this->main->_do_action_prefix . 'woocommerce-hooks_woocommerce_delete_order_item', $callback); |
| 430 | } |
| 431 | } |
| 432 |