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
ShortcodeAndCodesDisplayTest.php
446 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Tests for shortcodes and code display methods: |
| 4 | * - replacingShortcodeTicketDetail: empty code, attr code, renders output |
| 5 | * - replacingShortcodeMyCode: dispatches to text/formatted, order_id security |
| 6 | * - getMyCodeText: shows prefix, download PDF button, empty for no codes |
| 7 | * - getMyCodeFormatted: JSON output with display filters |
| 8 | * - getCodesTextAsShortList: HTML table rendering with status labels |
| 9 | * - isUserAllowedToAccessAdminArea: permission checks |
| 10 | * - canUserAccessOrder: admin, owner, order key checks |
| 11 | */ |
| 12 | |
| 13 | class ShortcodeAndCodesDisplayTest extends WP_UnitTestCase { |
| 14 | |
| 15 | private $main; |
| 16 | |
| 17 | public function set_up(): void { |
| 18 | parent::set_up(); |
| 19 | $this->main = sasoEventtickets::Instance(); |
| 20 | } |
| 21 | |
| 22 | public function tear_down(): void { |
| 23 | wp_set_current_user(0); |
| 24 | parent::tear_down(); |
| 25 | } |
| 26 | |
| 27 | // ── replacingShortcodeTicketDetail ────────────────────────── |
| 28 | |
| 29 | public function test_ticket_detail_shortcode_empty_code_returns_message(): void { |
| 30 | $result = $this->main->replacingShortcodeTicketDetail([]); |
| 31 | |
| 32 | $this->assertStringContainsString('ticket', strtolower($result)); |
| 33 | } |
| 34 | |
| 35 | public function test_ticket_detail_shortcode_with_attr_code(): void { |
| 36 | // Create a code in DB |
| 37 | $listId = $this->main->getDB()->insert('lists', [ |
| 38 | 'name' => 'SC Detail ' . uniqid(), |
| 39 | 'aktiv' => 1, |
| 40 | 'meta' => '{}', |
| 41 | ]); |
| 42 | $code = 'SCDETAIL' . strtoupper(uniqid()); |
| 43 | $this->main->getDB()->insert('codes', [ |
| 44 | 'list_id' => $listId, |
| 45 | 'code' => $code, |
| 46 | 'aktiv' => 1, |
| 47 | 'cvv' => '', |
| 48 | 'order_id' => 0, |
| 49 | 'user_id' => 0, |
| 50 | 'meta' => '{}', |
| 51 | ]); |
| 52 | |
| 53 | $result = $this->main->replacingShortcodeTicketDetail(['code' => $code]); |
| 54 | |
| 55 | // Should return some HTML (ticket detail or error, but not the "no code" message) |
| 56 | $this->assertIsString($result); |
| 57 | } |
| 58 | |
| 59 | // ── replacingShortcodeMyCode ──────────────────────────────── |
| 60 | |
| 61 | public function test_my_code_shortcode_returns_string_for_guest(): void { |
| 62 | wp_set_current_user(0); |
| 63 | |
| 64 | $result = $this->main->replacingShortcodeMyCode([]); |
| 65 | |
| 66 | $this->assertIsString($result); |
| 67 | } |
| 68 | |
| 69 | public function test_my_code_shortcode_with_format_returns_json(): void { |
| 70 | // Create a user with registered codes |
| 71 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 72 | wp_set_current_user($userId); |
| 73 | |
| 74 | $result = $this->main->replacingShortcodeMyCode(['format' => 'json']); |
| 75 | |
| 76 | $this->assertIsString($result); |
| 77 | // Should be valid JSON |
| 78 | $decoded = json_decode($result, true); |
| 79 | $this->assertIsArray($decoded); |
| 80 | } |
| 81 | |
| 82 | public function test_my_code_shortcode_with_order_id_denies_unauthorized(): void { |
| 83 | if (!class_exists('WC_Product_Simple')) { |
| 84 | $this->markTestSkipped('WooCommerce not available'); |
| 85 | } |
| 86 | |
| 87 | // Create an order belonging to user 1 |
| 88 | $user1 = $this->factory->user->create(['role' => 'subscriber']); |
| 89 | $user2 = $this->factory->user->create(['role' => 'subscriber']); |
| 90 | |
| 91 | $order = wc_create_order(); |
| 92 | $order->set_customer_id($user1); |
| 93 | $order->save(); |
| 94 | |
| 95 | // Login as user 2 → should be denied |
| 96 | wp_set_current_user($user2); |
| 97 | |
| 98 | $result = $this->main->replacingShortcodeMyCode(['order_id' => $order->get_id()]); |
| 99 | |
| 100 | $this->assertStringContainsString('permission', strtolower($result)); |
| 101 | } |
| 102 | |
| 103 | // ── getMyCodeText ────────────────────────────────────────── |
| 104 | |
| 105 | public function test_getMyCodeText_empty_for_user_without_codes(): void { |
| 106 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 107 | |
| 108 | $result = $this->main->getMyCodeText($userId); |
| 109 | |
| 110 | $this->assertEmpty($result); |
| 111 | } |
| 112 | |
| 113 | public function test_getMyCodeText_shows_prefix_when_always_option(): void { |
| 114 | update_option('sasoEventticketsuserDisplayCodePrefix', 'Your Tickets:'); |
| 115 | update_option('sasoEventticketsuserDisplayCodePrefixAlways', '1'); |
| 116 | $this->main->getOptions()->initOptions(); |
| 117 | |
| 118 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 119 | |
| 120 | $result = $this->main->getMyCodeText($userId); |
| 121 | |
| 122 | $this->assertStringContainsString('Your Tickets:', $result); |
| 123 | } |
| 124 | |
| 125 | public function test_getMyCodeText_shows_table_with_codes(): void { |
| 126 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 127 | $codes = $this->createCodesForUser($userId, 2); |
| 128 | |
| 129 | $result = $this->main->getMyCodeText($userId, [], null, '', $codes); |
| 130 | |
| 131 | $this->assertStringContainsString('<table>', $result); |
| 132 | $this->assertStringContainsString('</table>', $result); |
| 133 | } |
| 134 | |
| 135 | public function test_getMyCodeText_download_pdf_button(): void { |
| 136 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 137 | wp_set_current_user($userId); |
| 138 | $codes = $this->createCodesForUser($userId, 2); |
| 139 | |
| 140 | $result = $this->main->getMyCodeText($userId, [ |
| 141 | 'download_all_pdf' => 'true', |
| 142 | 'download_all_pdf_label' => 'Download All', |
| 143 | ], null, '', $codes); |
| 144 | |
| 145 | $this->assertStringContainsString('Download All', $result); |
| 146 | $this->assertStringContainsString('(2)', $result); |
| 147 | } |
| 148 | |
| 149 | public function test_getMyCodeText_download_pdf_too_many_shows_warning(): void { |
| 150 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 151 | wp_set_current_user($userId); |
| 152 | $codes = $this->createCodesForUser($userId, 5); |
| 153 | |
| 154 | $result = $this->main->getMyCodeText($userId, [ |
| 155 | 'download_all_pdf' => 'true', |
| 156 | 'download_all_pdf_max' => '3', |
| 157 | ], null, '', $codes); |
| 158 | |
| 159 | $this->assertStringContainsString('Too many', $result); |
| 160 | } |
| 161 | |
| 162 | // ── getMyCodeFormatted ───────────────────────────────────── |
| 163 | |
| 164 | public function test_getMyCodeFormatted_returns_valid_json(): void { |
| 165 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 166 | $codes = $this->createCodesForUser($userId, 1); |
| 167 | |
| 168 | $result = $this->main->getMyCodeFormatted($userId, ['format' => 'json'], null, '', $codes); |
| 169 | |
| 170 | $decoded = json_decode($result, true); |
| 171 | $this->assertIsArray($decoded); |
| 172 | $this->assertArrayHasKey('codes', $decoded); |
| 173 | } |
| 174 | |
| 175 | public function test_getMyCodeFormatted_respects_display_filter(): void { |
| 176 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 177 | $codes = $this->createCodesForUser($userId, 1); |
| 178 | |
| 179 | $result = $this->main->getMyCodeFormatted($userId, [ |
| 180 | 'format' => 'json', |
| 181 | 'display' => 'codes,confirmedCount', |
| 182 | ], null, '', $codes); |
| 183 | |
| 184 | $decoded = json_decode($result, true); |
| 185 | $this->assertArrayHasKey('codes', $decoded); |
| 186 | $this->assertArrayHasKey('confirmedCount', $decoded); |
| 187 | } |
| 188 | |
| 189 | public function test_getMyCodeFormatted_strips_meta_from_codes(): void { |
| 190 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 191 | $codes = $this->createCodesForUser($userId, 1); |
| 192 | |
| 193 | $result = $this->main->getMyCodeFormatted($userId, ['format' => 'json'], null, '', $codes); |
| 194 | |
| 195 | $decoded = json_decode($result, true); |
| 196 | foreach ($decoded['codes'] as $code) { |
| 197 | $this->assertArrayNotHasKey('meta', $code, 'meta should be stripped from output'); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // ── getCodesTextAsShortList ───────────────────────────────── |
| 202 | |
| 203 | public function test_getCodesTextAsShortList_empty_for_no_codes(): void { |
| 204 | $result = $this->main->getCodesTextAsShortList([]); |
| 205 | |
| 206 | $this->assertEmpty($result); |
| 207 | } |
| 208 | |
| 209 | public function test_getCodesTextAsShortList_shows_expired_label(): void { |
| 210 | $listId = $this->main->getDB()->insert('lists', [ |
| 211 | 'name' => 'Expired List ' . uniqid(), |
| 212 | 'aktiv' => 1, |
| 213 | 'meta' => '{}', |
| 214 | ]); |
| 215 | $code = 'EXPIRED' . strtoupper(uniqid()); |
| 216 | $this->main->getDB()->insert('codes', [ |
| 217 | 'list_id' => $listId, |
| 218 | 'code' => $code, |
| 219 | 'aktiv' => 1, |
| 220 | 'cvv' => '', |
| 221 | 'order_id' => 0, |
| 222 | 'user_id' => 0, |
| 223 | 'meta' => json_encode(['expiration' => ['date' => '2020-01-01']]), |
| 224 | ]); |
| 225 | $codeObj = $this->main->getCore()->retrieveCodeByCode($code); |
| 226 | $codeObj['code_display'] = $code; |
| 227 | |
| 228 | $result = $this->main->getCodesTextAsShortList([$codeObj]); |
| 229 | |
| 230 | $this->assertStringContainsString('<table>', $result); |
| 231 | $this->assertStringContainsString('EXPIRED', $result); |
| 232 | } |
| 233 | |
| 234 | public function test_getCodesTextAsShortList_shows_stolen_label(): void { |
| 235 | $listId = $this->main->getDB()->insert('lists', [ |
| 236 | 'name' => 'Stolen List ' . uniqid(), |
| 237 | 'aktiv' => 1, |
| 238 | 'meta' => '{}', |
| 239 | ]); |
| 240 | $code = 'STOLEN' . strtoupper(uniqid()); |
| 241 | $this->main->getDB()->insert('codes', [ |
| 242 | 'list_id' => $listId, |
| 243 | 'code' => $code, |
| 244 | 'aktiv' => 2, |
| 245 | 'cvv' => '', |
| 246 | 'order_id' => 0, |
| 247 | 'user_id' => 0, |
| 248 | 'meta' => '{}', |
| 249 | ]); |
| 250 | $codeObj = $this->main->getCore()->retrieveCodeByCode($code); |
| 251 | $codeObj['code_display'] = $code; |
| 252 | |
| 253 | $result = $this->main->getCodesTextAsShortList([$codeObj]); |
| 254 | |
| 255 | $this->assertStringContainsString('STOLEN', $result); |
| 256 | } |
| 257 | |
| 258 | public function test_getCodesTextAsShortList_shows_disabled_label(): void { |
| 259 | $listId = $this->main->getDB()->insert('lists', [ |
| 260 | 'name' => 'Disabled List ' . uniqid(), |
| 261 | 'aktiv' => 1, |
| 262 | 'meta' => '{}', |
| 263 | ]); |
| 264 | $code = 'DISABLED' . strtoupper(uniqid()); |
| 265 | $this->main->getDB()->insert('codes', [ |
| 266 | 'list_id' => $listId, |
| 267 | 'code' => $code, |
| 268 | 'aktiv' => 0, |
| 269 | 'cvv' => '', |
| 270 | 'order_id' => 0, |
| 271 | 'user_id' => 0, |
| 272 | 'meta' => '{}', |
| 273 | ]); |
| 274 | $codeObj = $this->main->getCore()->retrieveCodeByCode($code); |
| 275 | $codeObj['code_display'] = $code; |
| 276 | |
| 277 | $result = $this->main->getCodesTextAsShortList([$codeObj]); |
| 278 | |
| 279 | $this->assertStringContainsString('DISABLED', $result); |
| 280 | } |
| 281 | |
| 282 | public function test_getCodesTextAsShortList_fires_filter(): void { |
| 283 | $filtered = false; |
| 284 | $callback = function ($ret) use (&$filtered) { |
| 285 | $filtered = true; |
| 286 | return $ret; |
| 287 | }; |
| 288 | add_filter($this->main->_add_filter_prefix . 'main_getCodesTextAsShortList', $callback); |
| 289 | |
| 290 | $this->main->getCodesTextAsShortList([]); |
| 291 | |
| 292 | $this->assertTrue($filtered); |
| 293 | remove_filter($this->main->_add_filter_prefix . 'main_getCodesTextAsShortList', $callback); |
| 294 | } |
| 295 | |
| 296 | // ── isUserAllowedToAccessAdminArea ────────────────────────── |
| 297 | |
| 298 | public function test_admin_user_is_allowed(): void { |
| 299 | $adminId = $this->factory->user->create(['role' => 'administrator']); |
| 300 | wp_set_current_user($adminId); |
| 301 | |
| 302 | // Reset cached value |
| 303 | $ref = new ReflectionProperty($this->main, 'isAllowedAccess'); |
| 304 | $ref->setAccessible(true); |
| 305 | $ref->setValue($this->main, null); |
| 306 | |
| 307 | $this->assertTrue($this->main->isUserAllowedToAccessAdminArea()); |
| 308 | } |
| 309 | |
| 310 | public function test_subscriber_is_not_allowed(): void { |
| 311 | $subId = $this->factory->user->create(['role' => 'subscriber']); |
| 312 | wp_set_current_user($subId); |
| 313 | |
| 314 | $ref = new ReflectionProperty($this->main, 'isAllowedAccess'); |
| 315 | $ref->setAccessible(true); |
| 316 | $ref->setValue($this->main, null); |
| 317 | |
| 318 | $result = $this->main->isUserAllowedToAccessAdminArea(); |
| 319 | $this->assertEmpty($result, 'Subscriber should not have admin access'); |
| 320 | } |
| 321 | |
| 322 | public function test_access_can_be_filtered(): void { |
| 323 | $subId = $this->factory->user->create(['role' => 'subscriber']); |
| 324 | wp_set_current_user($subId); |
| 325 | |
| 326 | $ref = new ReflectionProperty($this->main, 'isAllowedAccess'); |
| 327 | $ref->setAccessible(true); |
| 328 | $ref->setValue($this->main, null); |
| 329 | |
| 330 | // Override via filter |
| 331 | $callback = function () { |
| 332 | return true; |
| 333 | }; |
| 334 | add_filter($this->main->_add_filter_prefix . 'main_isUserAllowedToAccessAdminArea', $callback); |
| 335 | |
| 336 | $this->assertTrue($this->main->isUserAllowedToAccessAdminArea()); |
| 337 | |
| 338 | remove_filter($this->main->_add_filter_prefix . 'main_isUserAllowedToAccessAdminArea', $callback); |
| 339 | } |
| 340 | |
| 341 | // ── canUserAccessOrder ───────────────────────────────────── |
| 342 | |
| 343 | public function test_canUserAccessOrder_admin_can_access(): void { |
| 344 | if (!class_exists('WC_Product_Simple')) { |
| 345 | $this->markTestSkipped('WooCommerce not available'); |
| 346 | } |
| 347 | |
| 348 | $adminId = $this->factory->user->create(['role' => 'administrator']); |
| 349 | wp_set_current_user($adminId); |
| 350 | // Grant manage_woocommerce capability |
| 351 | $user = new WP_User($adminId); |
| 352 | $user->add_cap('manage_woocommerce'); |
| 353 | |
| 354 | $order = wc_create_order(); |
| 355 | $order->save(); |
| 356 | |
| 357 | $ref = new ReflectionMethod($this->main, 'canUserAccessOrder'); |
| 358 | $ref->setAccessible(true); |
| 359 | |
| 360 | $this->assertTrue($ref->invoke($this->main, $order->get_id())); |
| 361 | } |
| 362 | |
| 363 | public function test_canUserAccessOrder_owner_can_access(): void { |
| 364 | if (!class_exists('WC_Product_Simple')) { |
| 365 | $this->markTestSkipped('WooCommerce not available'); |
| 366 | } |
| 367 | |
| 368 | $userId = $this->factory->user->create(['role' => 'subscriber']); |
| 369 | wp_set_current_user($userId); |
| 370 | |
| 371 | $order = wc_create_order(); |
| 372 | $order->set_customer_id($userId); |
| 373 | $order->save(); |
| 374 | |
| 375 | $ref = new ReflectionMethod($this->main, 'canUserAccessOrder'); |
| 376 | $ref->setAccessible(true); |
| 377 | |
| 378 | $this->assertTrue($ref->invoke($this->main, $order->get_id())); |
| 379 | } |
| 380 | |
| 381 | public function test_canUserAccessOrder_other_user_denied(): void { |
| 382 | if (!class_exists('WC_Product_Simple')) { |
| 383 | $this->markTestSkipped('WooCommerce not available'); |
| 384 | } |
| 385 | |
| 386 | $user1 = $this->factory->user->create(['role' => 'subscriber']); |
| 387 | $user2 = $this->factory->user->create(['role' => 'subscriber']); |
| 388 | wp_set_current_user($user2); |
| 389 | |
| 390 | $order = wc_create_order(); |
| 391 | $order->set_customer_id($user1); |
| 392 | $order->save(); |
| 393 | |
| 394 | $ref = new ReflectionMethod($this->main, 'canUserAccessOrder'); |
| 395 | $ref->setAccessible(true); |
| 396 | |
| 397 | $this->assertFalse($ref->invoke($this->main, $order->get_id())); |
| 398 | } |
| 399 | |
| 400 | public function test_canUserAccessOrder_nonexistent_order_denied(): void { |
| 401 | $ref = new ReflectionMethod($this->main, 'canUserAccessOrder'); |
| 402 | $ref->setAccessible(true); |
| 403 | |
| 404 | $this->assertFalse($ref->invoke($this->main, 999999)); |
| 405 | } |
| 406 | |
| 407 | // ── Helper methods ───────────────────────────────────────── |
| 408 | |
| 409 | private function createCodesForUser(int $userId, int $count): array { |
| 410 | $listId = $this->main->getDB()->insert('lists', [ |
| 411 | 'name' => 'UserCodes List ' . uniqid(), |
| 412 | 'aktiv' => 1, |
| 413 | 'meta' => '{}', |
| 414 | ]); |
| 415 | |
| 416 | $codes = []; |
| 417 | for ($i = 0; $i < $count; $i++) { |
| 418 | $code = 'UCODE' . strtoupper(uniqid()); |
| 419 | $metaObj = [ |
| 420 | 'user' => [ |
| 421 | 'value' => 'TestUser', |
| 422 | 'reg_ip' => '127.0.0.1', |
| 423 | 'reg_approved' => 1, |
| 424 | 'reg_request' => '2026-01-01 12:00:00', |
| 425 | 'reg_request_tz' => 'UTC', |
| 426 | 'reg_userid' => $userId, |
| 427 | ], |
| 428 | 'confirmedCount' => 0, |
| 429 | ]; |
| 430 | $this->main->getDB()->insert('codes', [ |
| 431 | 'list_id' => $listId, |
| 432 | 'code' => $code, |
| 433 | 'aktiv' => 1, |
| 434 | 'cvv' => '', |
| 435 | 'order_id' => 0, |
| 436 | 'user_id' => $userId, |
| 437 | 'meta' => json_encode($metaObj), |
| 438 | ]); |
| 439 | $codeObj = $this->main->getCore()->retrieveCodeByCode($code); |
| 440 | $codeObj['code_display'] = $code; |
| 441 | $codes[] = $codeObj; |
| 442 | } |
| 443 | return $codes; |
| 444 | } |
| 445 | } |
| 446 |