I have the below test class:
@isTest
public static void batchManagementDraftTest() {
User u = TestDataFactory.createUserWithRoleLeader();
User u2 = TestDataFactory.createAnotherUserWithRoleLeader();
List<Knowledge__kav> queriedKAV;
List<Knowledge__kav> kavList;
System.runAs(u) {
List<Employee__c> listEmployee = TestDataFactory.listEmployeeSingle();
kavList = TestDataFactory.createKnowledgeInternalFAQ();
queriedKAV = [
SELECT KnowledgeArticleId, createdBy.UserRole.DeveloperName, LastModifiedBy.UserRole.DeveloperName, RecordType.DeveloperName
FROM Knowledge__kav
WHERE id =: kavList[0].id AND LastModifiedBy.UserRole.DeveloperName != 'Super_User'
AND ((LastModifiedBy.UserRole.DeveloperName= 'Leader' AND RecordType.DeveloperName != 'CC')
OR LastModifiedBy.UserRole.DeveloperName != 'Leader')
];
}
System.runAs(u2) {
update queriedKAV;
}
Test.startTest();
BatchManagement bm = new BatchManagement();
Database.executeBatch(bam);
Test.stopTest();
}
Inside the BatchManagement class, there is KbManagement.PublishingService.deleteDraftArticle(kav.KnowledgeArticleId);
. Unfortunately, when this line is executed I am getting You can't perform this action. Be sure the action is valid for the current state of the article, and that you have permission to perform it.
Which is strange because I am running as Admin and I have the UserPermissionsKnowledgeUser
set to true
.
I tried moving the below part
Test.startTest();
BatchManagement bm = new BatchManagement();
Database.executeBatch(bam);
Test.stopTest();
to inside of the System.runAs(u2)
like this:
System.runAs(u2) {
update queriedKAV;
Test.startTest();
BatchManagement bm = new BatchManagement();
Database.executeBatch(bam);
Test.stopTest();
}
Still it doesn't work. Any suggestions?
deleteDraftArticle
in the batch class. I'm guessing, but it's possible the error is because you're attempting to delete a draft but the article is not in a draft state?TestDataFactory
simply creates a knowledge_kav in draft. As for the code beforedeleteDraftArticle
there are just some bunch ofif else
statements that are used to populate some lists, but no modifications to theknowledge_kav
untill you reach thedeleteDraftArticle
line.TestDataFactory.createKnowledgeInternalFAQ()
followed theexecuteBatch
between theTest.startTest()
andTest.stopTest()
it works fine. But unforutnately, the query in Batch Start has conditions that required that aknowlegde
be created by users with particular profile andlastmodifedBy
by users with a particular value in a custom field etc. that forced me to use thesystem.runAs
with various usersif elses
.