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
RefundTest.php
248 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Tests for WooCommerce refund handling and ticket removal. |
| 4 | */ |
| 5 | |
| 6 | class RefundTest extends WP_UnitTestCase { |
| 7 | |
| 8 | private $main; |
| 9 | |
| 10 | public function set_up(): void { |
| 11 | parent::set_up(); |
| 12 | $this->main = sasoEventtickets::Instance(); |
| 13 | |
| 14 | if (!class_exists('WC_Product_Simple')) { |
| 15 | $this->markTestSkipped('WooCommerce not available'); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Helper: create a ticket product with list. |
| 21 | */ |
| 22 | private function createTicketProduct(): array { |
| 23 | $listId = $this->main->getDB()->insert('lists', [ |
| 24 | 'name' => 'Refund Test List ' . uniqid(), |
| 25 | 'aktiv' => 1, |
| 26 | 'meta' => '{}', |
| 27 | ]); |
| 28 | |
| 29 | $product = new WC_Product_Simple(); |
| 30 | $product->set_name('Refund Test Product'); |
| 31 | $product->set_regular_price('10.00'); |
| 32 | $product->set_status('publish'); |
| 33 | $product->save(); |
| 34 | $productId = $product->get_id(); |
| 35 | |
| 36 | update_post_meta($productId, 'saso_eventtickets_is_ticket', 'yes'); |
| 37 | update_post_meta($productId, 'saso_eventtickets_list', $listId); |
| 38 | |
| 39 | return ['product' => $product, 'product_id' => $productId, 'list_id' => $listId]; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Helper: create an order with ticket product and generate codes. |
| 44 | */ |
| 45 | private function createOrderWithTickets(int $quantity = 2): array { |
| 46 | $ticket = $this->createTicketProduct(); |
| 47 | |
| 48 | $order = wc_create_order(); |
| 49 | $order->add_product($ticket['product'], $quantity); |
| 50 | $order->calculate_totals(); |
| 51 | $order->set_status('completed'); |
| 52 | $order->save(); |
| 53 | |
| 54 | // Generate tickets |
| 55 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 56 | $wcOrder->add_serialcode_to_order($order->get_id()); |
| 57 | |
| 58 | // Refresh order |
| 59 | $order = wc_get_order($order->get_id()); |
| 60 | |
| 61 | return [ |
| 62 | 'order' => $order, |
| 63 | 'ticket' => $ticket, |
| 64 | ]; |
| 65 | } |
| 66 | |
| 67 | // ── Ticket generation baseline ─────────────────────────────── |
| 68 | |
| 69 | public function test_order_generates_correct_number_of_codes(): void { |
| 70 | $data = $this->createOrderWithTickets(3); |
| 71 | $codes = $this->main->getCore()->getCodesByOrderId($data['order']->get_id()); |
| 72 | $this->assertCount(3, $codes); |
| 73 | } |
| 74 | |
| 75 | // ── Partial refund ─────────────────────────────────────────── |
| 76 | |
| 77 | public function test_partial_refund_removes_codes(): void { |
| 78 | // Enable refund option |
| 79 | $this->main->getOptions()->changeOption(['key' => 'wcassignmentOrderItemRefund', 'value' => 1]); |
| 80 | |
| 81 | $data = $this->createOrderWithTickets(3); |
| 82 | $order = $data['order']; |
| 83 | |
| 84 | // Get codes before refund |
| 85 | $codesBefore = $this->main->getCore()->getCodesByOrderId($order->get_id()); |
| 86 | $this->assertCount(3, $codesBefore); |
| 87 | |
| 88 | // Create a partial refund for 1 item |
| 89 | $items = $order->get_items(); |
| 90 | $itemId = array_key_first($items); |
| 91 | |
| 92 | $refund = wc_create_refund([ |
| 93 | 'order_id' => $order->get_id(), |
| 94 | 'amount' => '10.00', |
| 95 | 'line_items' => [ |
| 96 | $itemId => [ |
| 97 | 'qty' => 1, |
| 98 | 'refund_total' => 10.00, |
| 99 | ], |
| 100 | ], |
| 101 | ]); |
| 102 | |
| 103 | $this->assertNotWPError($refund); |
| 104 | |
| 105 | // Trigger the refund handler |
| 106 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 107 | $wcOrder->woocommerce_order_partially_refunded($order->get_id(), $refund->get_id()); |
| 108 | |
| 109 | // Get order item meta — should have fewer codes |
| 110 | $order = wc_get_order($order->get_id()); |
| 111 | $items = $order->get_items(); |
| 112 | $item = $items[$itemId]; |
| 113 | $codesStr = wc_get_order_item_meta($itemId, '_saso_eventtickets_product_code', true); |
| 114 | |
| 115 | if (!empty($codesStr)) { |
| 116 | $remainingCodes = explode(',', $codesStr); |
| 117 | $this->assertCount(2, $remainingCodes, 'Should have 2 codes after refunding 1 of 3'); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public function test_partial_refund_disabled_option_does_nothing(): void { |
| 122 | // Disable refund option |
| 123 | $this->main->getOptions()->changeOption(['key' => 'wcassignmentOrderItemRefund', 'value' => 0]); |
| 124 | |
| 125 | $data = $this->createOrderWithTickets(2); |
| 126 | $order = $data['order']; |
| 127 | |
| 128 | $items = $order->get_items(); |
| 129 | $itemId = array_key_first($items); |
| 130 | |
| 131 | $codesStrBefore = wc_get_order_item_meta($itemId, '_saso_eventtickets_product_code', true); |
| 132 | |
| 133 | // Manually call the handler (don't use wc_create_refund which triggers hooks) |
| 134 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 135 | $wcOrder->woocommerce_order_partially_refunded($order->get_id(), 0); |
| 136 | |
| 137 | // Codes should be unchanged when option is disabled |
| 138 | $codesStrAfter = wc_get_order_item_meta($itemId, '_saso_eventtickets_product_code', true); |
| 139 | $this->assertEquals($codesStrBefore, $codesStrAfter); |
| 140 | } |
| 141 | |
| 142 | // ── Full refund (all items) ────────────────────────────────── |
| 143 | |
| 144 | public function test_full_refund_removes_all_codes(): void { |
| 145 | $this->main->getOptions()->changeOption(['key' => 'wcassignmentOrderItemRefund', 'value' => 1]); |
| 146 | |
| 147 | $data = $this->createOrderWithTickets(2); |
| 148 | $order = $data['order']; |
| 149 | |
| 150 | $items = $order->get_items(); |
| 151 | $itemId = array_key_first($items); |
| 152 | |
| 153 | // Refund all items |
| 154 | $refund = wc_create_refund([ |
| 155 | 'order_id' => $order->get_id(), |
| 156 | 'amount' => '20.00', |
| 157 | 'line_items' => [ |
| 158 | $itemId => [ |
| 159 | 'qty' => 2, |
| 160 | 'refund_total' => 20.00, |
| 161 | ], |
| 162 | ], |
| 163 | ]); |
| 164 | |
| 165 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 166 | $wcOrder->woocommerce_order_partially_refunded($order->get_id(), $refund->get_id()); |
| 167 | |
| 168 | // Should have 0 codes remaining |
| 169 | $codesStr = wc_get_order_item_meta($itemId, '_saso_eventtickets_product_code', true); |
| 170 | if (!empty($codesStr)) { |
| 171 | $remainingCodes = array_filter(explode(',', $codesStr)); |
| 172 | $this->assertCount(0, $remainingCodes, 'Should have 0 codes after full refund'); |
| 173 | } else { |
| 174 | $this->assertEmpty($codesStr); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // ── Order notes on refund ──────────────────────────────────── |
| 179 | |
| 180 | public function test_refund_adds_order_note(): void { |
| 181 | $this->main->getOptions()->changeOption(['key' => 'wcassignmentOrderItemRefund', 'value' => 1]); |
| 182 | |
| 183 | $data = $this->createOrderWithTickets(2); |
| 184 | $order = $data['order']; |
| 185 | |
| 186 | $items = $order->get_items(); |
| 187 | $itemId = array_key_first($items); |
| 188 | |
| 189 | $refund = wc_create_refund([ |
| 190 | 'order_id' => $order->get_id(), |
| 191 | 'amount' => '10.00', |
| 192 | 'line_items' => [ |
| 193 | $itemId => [ |
| 194 | 'qty' => 1, |
| 195 | 'refund_total' => 10.00, |
| 196 | ], |
| 197 | ], |
| 198 | ]); |
| 199 | |
| 200 | $wcOrder = $this->main->getWC()->getOrderManager(); |
| 201 | $wcOrder->woocommerce_order_partially_refunded($order->get_id(), $refund->get_id()); |
| 202 | |
| 203 | // Check that order notes mention refunded ticket |
| 204 | $notes = wc_get_order_notes(['order_id' => $order->get_id()]); |
| 205 | $noteTexts = array_map(function ($n) { return $n->content; }, $notes); |
| 206 | $combined = implode(' ', $noteTexts); |
| 207 | |
| 208 | $this->assertStringContainsString('Refunded ticket', $combined); |
| 209 | } |
| 210 | |
| 211 | // ── releaseSeatsByOrderId ──────────────────────────────────── |
| 212 | |
| 213 | public function test_releaseSeatsByOrderId_clears_blocks(): void { |
| 214 | $planManager = $this->main->getSeating()->getPlanManager(); |
| 215 | $seatManager = $this->main->getSeating()->getSeatManager(); |
| 216 | $blockManager = $this->main->getSeating()->getBlockManager(); |
| 217 | |
| 218 | // Clean seating tables |
| 219 | global $wpdb; |
| 220 | $wpdb->query("DELETE FROM {$wpdb->prefix}saso_eventtickets_seat_blocks"); |
| 221 | $wpdb->query("DELETE FROM {$wpdb->prefix}saso_eventtickets_seats"); |
| 222 | $wpdb->query("DELETE FROM {$wpdb->prefix}saso_eventtickets_seatingplans"); |
| 223 | |
| 224 | $planId = $planManager->create(['name' => 'Refund Seat ' . uniqid()]); |
| 225 | $seatId = $seatManager->create($planId, ['seat_identifier' => 'R1']); |
| 226 | |
| 227 | $product = new WC_Product_Simple(); |
| 228 | $product->set_name('Seat Refund Product'); |
| 229 | $product->set_regular_price('25.00'); |
| 230 | $product->set_status('publish'); |
| 231 | $product->save(); |
| 232 | $productId = $product->get_id(); |
| 233 | |
| 234 | $sessionId = 'refund_seat_' . uniqid(); |
| 235 | $result = $blockManager->blockSeat($seatId, $productId, $sessionId); |
| 236 | $this->assertTrue($result['success'], 'blockSeat should succeed'); |
| 237 | |
| 238 | $blockManager->confirmBlock($result['block_id'], 1, 1, 1); |
| 239 | |
| 240 | // Release by order ID |
| 241 | $blockManager->releaseSeatsByOrderId(1); |
| 242 | |
| 243 | // Seat should be available |
| 244 | $available = $blockManager->isSeatAvailable($seatId, $productId); |
| 245 | $this->assertTrue($available); |
| 246 | } |
| 247 | } |
| 248 |