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
WCOrderMethodsTest.php
268 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Batch 43 — WC Order methods: |
| 4 | * - hasTicketsInOrder: checks order for ticket products |
| 5 | * - hasTicketsInOrderWithTicketnumber: checks for assigned ticket numbers |
| 6 | * - getTicketsFromOrder: extracts ticket data from order |
| 7 | * - woocommerce_order_status_changed: status change handler |
| 8 | * - add_serialcode_to_order: ticket generation for completed orders |
| 9 | * - woocommerce_order_item_display_meta_key: meta key display filter |
| 10 | * - woocommerce_order_item_display_meta_value: meta value display filter |
| 11 | */ |
| 12 | |
| 13 | class WCOrderMethodsTest extends WP_UnitTestCase { |
| 14 | |
| 15 | private $main; |
| 16 | private $orderMgr; |
| 17 | |
| 18 | public function set_up(): void { |
| 19 | parent::set_up(); |
| 20 | $this->main = sasoEventtickets::Instance(); |
| 21 | |
| 22 | if (!class_exists('WC_Product_Simple')) { |
| 23 | $this->markTestSkipped('WooCommerce not available'); |
| 24 | } |
| 25 | |
| 26 | $this->orderMgr = $this->main->getWC()->getOrderManager(); |
| 27 | } |
| 28 | |
| 29 | private function createTicketProduct(): array { |
| 30 | $listId = $this->main->getDB()->insert('lists', [ |
| 31 | 'name' => 'WC Order List ' . uniqid(), |
| 32 | 'aktiv' => 1, |
| 33 | 'meta' => '{}', |
| 34 | ]); |
| 35 | |
| 36 | $product = new WC_Product_Simple(); |
| 37 | $product->set_name('Order Test Product ' . uniqid()); |
| 38 | $product->set_regular_price('15.00'); |
| 39 | $product->set_status('publish'); |
| 40 | $product->save(); |
| 41 | $pid = $product->get_id(); |
| 42 | |
| 43 | update_post_meta($pid, 'saso_eventtickets_is_ticket', 'yes'); |
| 44 | update_post_meta($pid, 'saso_eventtickets_list', $listId); |
| 45 | |
| 46 | return ['product' => $product, 'product_id' => $pid, 'list_id' => $listId]; |
| 47 | } |
| 48 | |
| 49 | private function createRegularProduct(): WC_Product_Simple { |
| 50 | $product = new WC_Product_Simple(); |
| 51 | $product->set_name('Regular Product ' . uniqid()); |
| 52 | $product->set_regular_price('5.00'); |
| 53 | $product->set_status('publish'); |
| 54 | $product->save(); |
| 55 | return $product; |
| 56 | } |
| 57 | |
| 58 | private function createOrderWithProduct(int $productId, int $qty = 1): WC_Order { |
| 59 | $order = wc_create_order(); |
| 60 | $product = wc_get_product($productId); |
| 61 | $order->add_product($product, $qty); |
| 62 | $order->calculate_totals(); |
| 63 | $order->save(); |
| 64 | return $order; |
| 65 | } |
| 66 | |
| 67 | // ── hasTicketsInOrder ──────────────────────────────────── |
| 68 | |
| 69 | public function test_hasTicketsInOrder_true_for_ticket_product(): void { |
| 70 | $tp = $this->createTicketProduct(); |
| 71 | $order = $this->createOrderWithProduct($tp['product_id']); |
| 72 | $this->assertTrue($this->orderMgr->hasTicketsInOrder($order)); |
| 73 | } |
| 74 | |
| 75 | public function test_hasTicketsInOrder_false_for_regular_product(): void { |
| 76 | $product = $this->createRegularProduct(); |
| 77 | $order = $this->createOrderWithProduct($product->get_id()); |
| 78 | $this->assertFalse($this->orderMgr->hasTicketsInOrder($order)); |
| 79 | } |
| 80 | |
| 81 | public function test_hasTicketsInOrder_true_for_mixed_order(): void { |
| 82 | $tp = $this->createTicketProduct(); |
| 83 | $regular = $this->createRegularProduct(); |
| 84 | |
| 85 | $order = wc_create_order(); |
| 86 | $order->add_product(wc_get_product($tp['product_id'])); |
| 87 | $order->add_product($regular); |
| 88 | $order->save(); |
| 89 | |
| 90 | $this->assertTrue($this->orderMgr->hasTicketsInOrder($order)); |
| 91 | } |
| 92 | |
| 93 | public function test_hasTicketsInOrder_false_for_empty_order(): void { |
| 94 | $order = wc_create_order(); |
| 95 | $order->save(); |
| 96 | $this->assertFalse($this->orderMgr->hasTicketsInOrder($order)); |
| 97 | } |
| 98 | |
| 99 | // ── hasTicketsInOrderWithTicketnumber ───────────────────── |
| 100 | |
| 101 | public function test_hasTicketsInOrderWithTicketnumber_false_without_codes(): void { |
| 102 | $tp = $this->createTicketProduct(); |
| 103 | $order = $this->createOrderWithProduct($tp['product_id']); |
| 104 | // No codes assigned yet |
| 105 | $this->assertFalse($this->orderMgr->hasTicketsInOrderWithTicketnumber($order)); |
| 106 | } |
| 107 | |
| 108 | public function test_hasTicketsInOrderWithTicketnumber_true_with_codes(): void { |
| 109 | $tp = $this->createTicketProduct(); |
| 110 | $order = $this->createOrderWithProduct($tp['product_id']); |
| 111 | |
| 112 | // Manually add a code to the order item |
| 113 | $items = $order->get_items(); |
| 114 | foreach ($items as $item_id => $item) { |
| 115 | wc_update_order_item_meta($item_id, '_saso_eventtickets_product_code', 'TESTCODE123'); |
| 116 | } |
| 117 | |
| 118 | $this->assertTrue($this->orderMgr->hasTicketsInOrderWithTicketnumber($order)); |
| 119 | } |
| 120 | |
| 121 | // ── getTicketsFromOrder ────────────────────────────────── |
| 122 | |
| 123 | public function test_getTicketsFromOrder_returns_ticket_items(): void { |
| 124 | $tp = $this->createTicketProduct(); |
| 125 | $order = $this->createOrderWithProduct($tp['product_id'], 2); |
| 126 | |
| 127 | $tickets = $this->orderMgr->getTicketsFromOrder($order); |
| 128 | $this->assertIsArray($tickets); |
| 129 | $this->assertCount(1, $tickets); // 1 ticket product |
| 130 | |
| 131 | $ticket = reset($tickets); |
| 132 | $this->assertEquals(2, $ticket['quantity']); |
| 133 | $this->assertEquals($tp['product_id'], $ticket['product_id']); |
| 134 | } |
| 135 | |
| 136 | public function test_getTicketsFromOrder_empty_for_regular_products(): void { |
| 137 | $product = $this->createRegularProduct(); |
| 138 | $order = $this->createOrderWithProduct($product->get_id()); |
| 139 | |
| 140 | $tickets = $this->orderMgr->getTicketsFromOrder($order); |
| 141 | $this->assertIsArray($tickets); |
| 142 | $this->assertEmpty($tickets); |
| 143 | } |
| 144 | |
| 145 | public function test_getTicketsFromOrder_only_ticket_items_from_mixed(): void { |
| 146 | $tp = $this->createTicketProduct(); |
| 147 | $regular = $this->createRegularProduct(); |
| 148 | |
| 149 | $order = wc_create_order(); |
| 150 | $order->add_product(wc_get_product($tp['product_id']), 3); |
| 151 | $order->add_product($regular, 1); |
| 152 | $order->save(); |
| 153 | |
| 154 | $tickets = $this->orderMgr->getTicketsFromOrder($order); |
| 155 | $this->assertCount(1, $tickets); // Only the ticket product |
| 156 | $ticket = reset($tickets); |
| 157 | $this->assertEquals(3, $ticket['quantity']); |
| 158 | } |
| 159 | |
| 160 | // ── add_serialcode_to_order ────────────────────────────── |
| 161 | |
| 162 | public function test_add_serialcode_generates_codes_for_completed_order(): void { |
| 163 | $tp = $this->createTicketProduct(); |
| 164 | |
| 165 | // Pre-generate codes in the list |
| 166 | for ($i = 0; $i < 3; $i++) { |
| 167 | $metaObj = $this->main->getCore()->getMetaObject(); |
| 168 | $metaJson = $this->main->getCore()->json_encode_with_error_handling($metaObj); |
| 169 | $this->main->getDB()->insert('codes', [ |
| 170 | 'list_id' => $tp['list_id'], |
| 171 | 'code' => 'ORDAUTO' . strtoupper(uniqid()), |
| 172 | 'aktiv' => 1, |
| 173 | 'cvv' => '', |
| 174 | 'order_id' => 0, |
| 175 | 'user_id' => 0, |
| 176 | 'meta' => $metaJson, |
| 177 | ]); |
| 178 | } |
| 179 | |
| 180 | $order = $this->createOrderWithProduct($tp['product_id'], 2); |
| 181 | $order->set_status('completed'); |
| 182 | $order->save(); |
| 183 | |
| 184 | // Trigger ticket generation |
| 185 | $this->orderMgr->add_serialcode_to_order($order->get_id()); |
| 186 | |
| 187 | // Check that codes were assigned |
| 188 | $items = $order->get_items(); |
| 189 | $codesAssigned = false; |
| 190 | foreach ($items as $item_id => $item) { |
| 191 | $codes = wc_get_order_item_meta($item_id, '_saso_eventtickets_product_code', true); |
| 192 | if (!empty($codes)) { |
| 193 | $codesAssigned = true; |
| 194 | } |
| 195 | } |
| 196 | $this->assertTrue($codesAssigned, 'Codes should be assigned to completed order'); |
| 197 | } |
| 198 | |
| 199 | public function test_add_serialcode_skips_zero_order_id(): void { |
| 200 | // Should not throw |
| 201 | $this->orderMgr->add_serialcode_to_order(0); |
| 202 | $this->assertTrue(true); |
| 203 | } |
| 204 | |
| 205 | public function test_add_serialcode_skips_nonexistent_order(): void { |
| 206 | // Should not throw |
| 207 | $this->orderMgr->add_serialcode_to_order(999999); |
| 208 | $this->assertTrue(true); |
| 209 | } |
| 210 | |
| 211 | // ── woocommerce_order_status_changed ───────────────────── |
| 212 | |
| 213 | public function test_order_status_changed_does_not_throw(): void { |
| 214 | $tp = $this->createTicketProduct(); |
| 215 | $order = $this->createOrderWithProduct($tp['product_id']); |
| 216 | |
| 217 | // Should not throw for any status change |
| 218 | $this->orderMgr->woocommerce_order_status_changed( |
| 219 | $order->get_id(), 'pending', 'processing' |
| 220 | ); |
| 221 | $this->assertTrue(true); |
| 222 | } |
| 223 | |
| 224 | public function test_order_status_changed_cancelled_releases_seats(): void { |
| 225 | $tp = $this->createTicketProduct(); |
| 226 | $order = $this->createOrderWithProduct($tp['product_id']); |
| 227 | |
| 228 | // Status change to cancelled should not throw |
| 229 | $this->orderMgr->woocommerce_order_status_changed( |
| 230 | $order->get_id(), 'processing', 'cancelled' |
| 231 | ); |
| 232 | $this->assertTrue(true); |
| 233 | } |
| 234 | |
| 235 | // ── woocommerce_order_item_display_meta_key ────────────── |
| 236 | |
| 237 | public function test_display_meta_key_transforms_ticket_keys(): void { |
| 238 | // Create a mock meta object |
| 239 | $meta = new stdClass(); |
| 240 | $meta->key = '_saso_eventtickets_product_code'; |
| 241 | |
| 242 | $result = $this->orderMgr->woocommerce_order_item_display_meta_key( |
| 243 | $meta->key, $meta, null |
| 244 | ); |
| 245 | // Should transform the internal key to a display label |
| 246 | $this->assertIsString($result); |
| 247 | } |
| 248 | |
| 249 | public function test_display_meta_key_passes_unknown_keys(): void { |
| 250 | $meta = new stdClass(); |
| 251 | $meta->key = '_some_other_meta'; |
| 252 | |
| 253 | $result = $this->orderMgr->woocommerce_order_item_display_meta_key( |
| 254 | '_some_other_meta', $meta, null |
| 255 | ); |
| 256 | $this->assertEquals('_some_other_meta', $result); |
| 257 | } |
| 258 | |
| 259 | // ── woocommerce_new_order ──────────────────────────────── |
| 260 | |
| 261 | public function test_new_order_does_not_throw(): void { |
| 262 | $order = wc_create_order(); |
| 263 | $order->save(); |
| 264 | $this->orderMgr->woocommerce_new_order($order->get_id()); |
| 265 | $this->assertTrue(true); |
| 266 | } |
| 267 | } |
| 268 |