File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/scratch/test_notification_system.php
Back
<?php /** * Comprehensive Functional Test for Notification System */ require_once __DIR__ . '/../database/Database.php'; require_once __DIR__ . '/../app/Models/User.php'; require_once __DIR__ . '/../app/Models/Store.php'; require_once __DIR__ . '/../app/Models/Notification.php'; require_once __DIR__ . '/../app/Services/NotificationService.php'; $db = Database::getInstance(); $notifModel = new Notification(); $notifService = new NotificationService(); echo "=== CARGOO NOTIFICATION SYSTEM FUNCTIONAL TEST ===" . PHP_EOL . PHP_EOL; // 1. Get mock user & store echo "[1] Fetching test user & store..." . PHP_EOL; $user = $db->query("SELECT id FROM users ORDER BY id ASC LIMIT 1")->fetch(); $userId = (int)$user['id']; $otherUser = $db->query("SELECT id FROM users WHERE id != {$userId} LIMIT 1")->fetch(); $otherUserId = $otherUser ? (int)$otherUser['id'] : $userId + 99; $store = $db->query("SELECT id, user_id FROM stores LIMIT 1")->fetch(); $storeId = $store ? (int)$store['id'] : 1; echo " ✓ Test User ID: {$userId}, Other User ID: {$otherUserId}, Store ID: {$storeId}" . PHP_EOL; // 2. Dispatch Direct User Notification echo PHP_EOL . "[2] Testing notifyUser()..." . PHP_EOL; $notifId = $notifService->notifyUser( $userId, 'order_status_changed', 'คำสั่งซื้อจัดส่งสำเร็จแล้ว', 'คำสั่งซื้อ #ORD-TEST-123 จัดส่งถึงปลายทางแล้ว', 'order', 123, '/account/orders/ORD-TEST-123', 'customer' ); assert($notifId > 0, "Notification must be created"); echo " ✓ Created notification ID: {$notifId}" . PHP_EOL; // 3. Test Unread Count & Latest Preview echo PHP_EOL . "[3] Testing Unread Count & Preview..." . PHP_EOL; $unreadCount = $notifService->getUnreadCount($userId, 'customer'); assert($unreadCount >= 1, "Unread count must be >= 1"); echo " ✓ Customer Unread Count: {$unreadCount}" . PHP_EOL; $latest = $notifService->getLatestPreview($userId, 5, 'customer'); assert(!empty($latest), "Latest preview must return records"); echo " ✓ Latest Preview count: " . count($latest) . ", First title: '{$latest[0]['title']}'" . PHP_EOL; // 4. Test Notify Store Owner (Seller) echo PHP_EOL . "[4] Testing notifyStoreOwner()..." . PHP_EOL; $sellerNotifId = $notifService->notifyStoreOwner( $storeId, 'order_new', 'มีคำสั่งซื้อใหม่!', 'มีคำสั่งซื้อใหม่ #ORD-SELLER-999 ยอดรวม ฿1,500.00', 'order', 999, '/seller/orders/ORD-SELLER-999' ); if ($sellerNotifId) { echo " ✓ Created Seller notification ID: {$sellerNotifId}" . PHP_EOL; } else { echo " ✓ Store owner not linked, skipped dispatch" . PHP_EOL; } // 5. Test Notify Admins echo PHP_EOL . "[5] Testing notifyAdmins()..." . PHP_EOL; $adminCount = $notifService->notifyAdmins( 'payment_slip_submitted', 'มีสลิปโอนเงินรอตรวจสอบ', 'คำสั่งซื้อ #ORD-VERIFY-001 รอการตรวจสอบสลิป', 'payment', 55, '/admin/payments/ORD-VERIFY-001' ); echo " ✓ Broadcasted notification to {$adminCount} admins" . PHP_EOL; // 6. Test Mark As Read echo PHP_EOL . "[6] Testing Mark As Read..." . PHP_EOL; $markSuccess = $notifService->markAsRead($notifId, $userId); assert($markSuccess === true, "Mark as read must succeed"); $checkNotif = $notifModel->fetchOne("SELECT is_read, read_at FROM notifications WHERE id = :id", [':id' => $notifId]); assert((int)$checkNotif['is_read'] === 1, "is_read must be 1"); assert(!empty($checkNotif['read_at']), "read_at must not be null"); echo " ✓ Notification #{$notifId} marked as read at: {$checkNotif['read_at']}" . PHP_EOL; // 7. Test IDOR & Security Isolation echo PHP_EOL . "[7] Testing Security & IDOR Isolation..." . PHP_EOL; // Other user should NOT be able to mark read or delete this notification $idorMark = $notifService->markAsRead($notifId, $otherUserId); assert($idorMark === false, "IDOR mark read must fail"); $idorDel = $notifService->deleteNotification($notifId, $otherUserId); assert($idorDel === false, "IDOR delete must fail"); echo " ✓ IDOR protection: PASS (Unauthorized user cannot mark or delete other user's notification)" . PHP_EOL; // 8. Test Mark All As Read echo PHP_EOL . "[8] Testing Mark All As Read..." . PHP_EOL; // Create another unread $notif2 = $notifService->notifyUser($userId, 'review_received', 'มีรีวิวใหม่', 'ลูกค้าให้ 5 ดาว', 'review', 1, '/seller/reviews', 'seller'); $notifService->markAllAsRead($userId, 'seller'); $unreadAfter = $notifService->getUnreadCount($userId, 'seller'); assert($unreadAfter === 0, "Seller unread count must be 0 after markAllAsRead"); echo " ✓ Mark All As Read for seller: PASS (Unread count = {$unreadAfter})" . PHP_EOL; // 9. Clean up test records echo PHP_EOL . "[9] Cleaning up test notifications..." . PHP_EOL; $db->query("DELETE FROM notifications WHERE id IN ({$notifId}, {$notif2})"); if ($sellerNotifId) { $db->query("DELETE FROM notifications WHERE id = {$sellerNotifId}"); } echo " ✓ Cleaned up test notification records." . PHP_EOL; echo PHP_EOL . "🎉 ALL 9 NOTIFICATION SYSTEM FUNCTIONAL TESTS PASSED! 🎉" . PHP_EOL;
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.27 |
proxy
|
phpinfo
|
Settings