1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| @isTest private class CaseHandlerTest { @TestSetup static void setup() { // 创建测试队列 Group queue = new Group(Name='High_Risk_Support_Queue', Type='Queue'); insert queue;
// 创建测试 Account 和 Contact Account acc = new Account(Name='Test Account'); insert acc;
Contact con = new Contact(LastName='Doe', AccountId=acc.Id); insert con;
// 创建 Owner User(确保不是当前运行用户) Profile prof = [SELECT Id FROM Profile WHERE Name='Standard User' LIMIT 1]; User user = new User( Username='testuser@example.com.unique', Email='testuser@example.com', FirstName='Test', LastName='User', Alias='tuser', CommunityNickname='tuser123', ProfileId=prof.Id, TimeZoneSidKey='America/Los_Angeles', LocaleSidKey='en_US', EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US' ); insert user; acc.OwnerId = user.Id; update acc; }
//测试投诉功能升级 @isTest static void testMultipleComplaintsTriggersEscalation() { Test.startTest();
//任意找一个联系人,以这个联系人的身份写投诉工单(其实应该用测试联系人Doe,随便吧) Contact con = [SELECT Id FROM Contact LIMIT 1];
// Step 1: 创建前两次投诉(模拟历史数据)//case List<Case> historicalCases = new List<Case>(); for (Integer i = 0; i < 2; i++) { historicalCases.add(new Case( Subject = 'Old Complaint ' + i, Type = 'Complaint', ContactId = con.Id )); } insert historicalCases;
// 更新 Contact 统计(手动模拟之前逻辑)//contact Contact c = [SELECT Id, Number_of_Complaints__c FROM Contact WHERE Id = :con.Id]; c.Number_of_Complaints__c = 2; c.Last_Complaint_Date__c = System.today().addDays(-50); update c;
// Step 2: 插入两条新的 Complaint Case(应触发升级)//第3条、第4条投诉工单 List<Case> newCases = new List<Case>(); for (Integer i = 0; i < 2; i++) { newCases.add(new Case( Subject = 'New Complaint ' + i, Type = 'Complaint', ContactId = con.Id, Priority = 'Normal', Status = 'New' )); } insert newCases;
// 查询结果验证 List<Case> insertedCases = [ SELECT Id, Priority, Status, Owner.Type, Owner.Name FROM Case WHERE Id IN :newCases ];
System.debug('JSON格式: ' + JSON.serializePretty(insertedCases)); //比较插入的是否是高优先级,3和4肯定是高优先级 for (Case cInserted : insertedCases) { System.assertEquals('High', cInserted.Priority, 'Priority should be High'); System.assertEquals('Escalated', cInserted.Status, 'Status should be Escalated'); //System.assertEquals('Queue', cInserted.Owner.Type, 'Owner should be a queue'); }
// 验证 Contact 计数已增加(照理来说到4) Contact updatedContact = [SELECT Number_of_Complaints__c, Last_Complaint_Date__c FROM Contact WHERE Id = :con.Id]; system.debug(updatedContact.Number_of_Complaints__c); System.assertEquals(4, updatedContact.Number_of_Complaints__c, 'Complaint count should be 4'); system.debug(updatedContact.Last_Complaint_Date__c); System.assertEquals(System.today(), updatedContact.Last_Complaint_Date__c, 'Last complaint date should be today');
// 验证任务是否创建(照理有2个被加到高优先处理级别) List<Task> tasks = [SELECT Id, Subject, WhatId FROM Task WHERE Subject = 'High risk customer follow-up']; System.assertEquals(2, tasks.size(), 'Two follow-up tasks should be created');
Test.stopTest(); } //测试批处理(直接调用批处理) @isTest static void testBatchResetsInactiveContacts() { Test.startTest();
//随机来一个联系人用来测试 Contact con = [SELECT Id FROM Contact LIMIT 1];
// 创建一个很久以前的投诉 Case Case oldCase = new Case( Subject = 'Very Old Complaint', Type = 'Complaint', ContactId = con.Id ); insert oldCase;
// 手动设置投诉计数 Contact c = [SELECT Id, Number_of_Complaints__c FROM Contact WHERE Id = :con.Id]; c.Number_of_Complaints__c = 5; update c;
// 运行批处理(测试环境) ResetComplaintCounterBatch batchJob = new ResetComplaintCounterBatch(); Database.executeBatch(batchJob, 10);//每次处理10条
Test.stopTest();
// 验证计数被清零 Contact refreshed = [SELECT Number_of_Complaints__c FROM Contact WHERE Id = :con.Id]; System.assertEquals(0, refreshed.Number_of_Complaints__c, 'Complaint count should be reset to 0'); }
}
|