File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/scratch/test_notification_flow.php
Back
<?php /** * Test Notification Center, Modal Details, and Mark All Read Flow */ require_once __DIR__ . '/../database/Database.php'; require_once __DIR__ . '/../app/Models/Notification.php'; require_once __DIR__ . '/../app/Services/NotificationService.php'; $db = Database::getInstance(); $notifModel = new Notification(); $service = new NotificationService(); echo "=== TEST 1: Create Test Notifications for Multiple Roles ===\n"; $testUserId = 1; // Super Admin $n1 = $service->notifyUser($testUserId, 'order_created', 'คำสั่งซื้อใหม่ของคุณ', 'คำสั่งซื้อ #TEST-001 ได้รับการบันทึกแล้ว', 'order', 101, '/account/orders/TEST-001', 'customer'); $n2 = $service->notifyUser($testUserId, 'payment_slip_submitted', 'มีสลิปโอนเงินรอตรวจ', 'คำสั่งซื้อ #TEST-002 มีสลิปใหม่', 'payment', 102, '/admin/payments/TEST-002', 'admin'); $n3 = $service->notifyUser($testUserId, 'low_stock', 'สินค้าใกล้หมดสต็อก', 'สินค้า SKU-99 เหลือ 1 ชิ้น', 'product', 103, '/seller/inventory', 'seller'); assert($n1 > 0 && $n2 > 0 && $n3 > 0, "All 3 notifications must be created successfully"); echo " [PASS] Created notifications IDs: {$n1}, {$n2}, {$n3}\n"; echo "\n=== TEST 2: Check Unread Counts ===\n"; $totalUnread = $notifModel->getUnreadCount($testUserId); $customerUnread = $notifModel->getUnreadCount($testUserId, 'customer'); $adminUnread = $notifModel->getUnreadCount($testUserId, 'admin'); $sellerUnread = $notifModel->getUnreadCount($testUserId, 'seller'); assert($totalUnread >= 3, "Total unread should include all roles"); assert($customerUnread >= 1, "Customer unread should be >= 1"); assert($adminUnread >= 1, "Admin unread should be >= 1"); assert($sellerUnread >= 1, "Seller unread should be >= 1"); echo " [PASS] Counts: Total={$totalUnread}, Cust={$customerUnread}, Admin={$adminUnread}, Seller={$sellerUnread}\n"; echo "\n=== TEST 3: Fetch Latest Notifications Preview ===\n"; $latest = $notifModel->getLatestNotifications($testUserId, 5); assert(count($latest) >= 3, "Latest preview should return recent notifications"); echo " [PASS] Retrieved " . count($latest) . " notifications in preview\n"; echo "\n=== TEST 4: Mark Single Notification as Read ===\n"; $ok = $service->markAsRead($n1, $testUserId); assert($ok === true, "Mark as read should succeed"); $checkN1 = $db->query("SELECT is_read FROM notifications WHERE id = {$n1}")->fetchColumn(); assert((int)$checkN1 === 1, "Notification {$n1} must have is_read = 1"); echo " [PASS] Notification #{$n1} marked as read successfully\n"; echo "\n=== TEST 5: Mark All as Read (All Roles) ===\n"; $okAll = $service->markAllAsRead($testUserId, null); assert($okAll === true, "Mark all as read should succeed"); $remainingUnread = $notifModel->getUnreadCount($testUserId); assert($remainingUnread === 0, "All notifications for user must now be read (remaining: {$remainingUnread})"); echo " [PASS] All notifications marked as read! Remaining unread: {$remainingUnread}\n"; // Cleanup test notifications $db->exec("DELETE FROM notifications WHERE id IN ({$n1}, {$n2}, {$n3})"); echo "\n=== CLEANUP: Removed test notifications ===\n"; echo "ALL NOTIFICATION TESTS PASSED SUCCESSFULLY! (5/5)\n";
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.26 |
proxy
|
phpinfo
|
Settings