1

We have a use case where we expose knowledge articles to customer community users and I'm trying to write test code for that.

The problem I am having is that whenever I query for articles using system.runas(myUser) it does not return any results. This code is working perfectly when executed from the community (eg. the feature works) I am just failing to write unit tests for it.

Here is my test setup code

@testsetup
public static void createEvents() {
    List<Events__kav> newEvents = new List<Events__kav>();
    List<Events__DataCategorySelection> newCategorizations = new List<Events__DataCategorySelection>();
    for (Integer i = 0; i < 20; i++) {
        Events__kav a = new Events__kav();
        a.Language = 'fr';
        a.Title = 'TestEvent' + i;
        a.UrlName = 'TestEvent' + i;
        a.Visible_on_App__c = true;
        a.Visible_on_Portal__c = true;
        newEvents.add(a);
    }
    insert newEvents;

    List<Events__kav> evts = [SELECT Id, KnowledgeArticleId FROM Events__kav WHERE Id IN :newEvents];
    List<Events__viewstat> evtsVS = new List<Events__viewstat>();
    for (Events__kav a : evts) {
        Events__DataCategorySelection dataCategory = new Events__DataCategorySelection();
        dataCategory.ParentId = a.Id;
        dataCategory.DataCategoryGroupName = 'Brand';
        dataCategory.DataCategoryName = 'Volkswagen';
        newCategorizations.add(dataCategory);
        KbManagement.PublishingService.publishArticle(a.KnowledgeArticleId, true); //PublishStatus = online
    }
    insert newCategorizations;
}

And this is the method that is not returning any results when called from the test class...

public static List<Events__kav> getAllEventDetails(String brandName, String language, Boolean visibleOnApp, Boolean visibleOnPortal) {
    String categoryQuery = 'SELECT 
      Id,Title,Summary,Full_Content__c,External_Link__c,
      External_Link_Label__c,Start_Date__c,End_Date__c,Location_Name__c,
      Location_Street__c,Location_Street_Number__c,Location_City__c,
      ArticleNumber FROM Events__kav 
         WHERE PublishStatus=\'online\' AND 
               Language = \'' + language + '\'';
    if (visibleOnApp && !visibleOnPortal) {
        categoryQuery = categoryQuery + ' AND Visible_On_App__c = ' + visibleOnApp;
    } else if (!visibleOnApp && visibleOnPortal) {
        categoryQuery = categoryQuery + ' AND Visible_On_Portal__c = ' + visibleOnPortal;
    } else { //if above clauses fail then both app and portal have the same value.
        categoryQuery = categoryQuery + ' AND Visible_On_App__c = ' + visibleOnApp + ' AND Visible_On_Portal__c = ' + visibleOnPortal;
    }
    categoryQuery = categoryQuery + ' AND Start_date__c >= THIS_MONTH WITH DATA CATEGORY Brand__c BELOW ' + brandName + '__c';
    //categoryQuery = categoryQuery + ' ORDER BY Start_Date__c ASC';
    List<Events__kav> events = (List<Events__kav>) Database.query(categoryQuery);
    return events;
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.