0

Written a testclass on feeditem, but no luck(its 0%) - not sure about the mistake in my code. Please do help me out and de needful.

Trigger :

trigger RestrictFilesOnFeedItems on FeedItem (after insert) {
    Map<Id, FeedItem> contentversionIdToFeedItemMap = new Map<Id, FeedItem>();
    for(FeedItem fi : trigger.new){
        if(fi.type == 'ContentPost')
            contentversionIdToFeedItemMap.put(fi.RelatedRecordId, fi);
    }
    for(ContentVersion cv : [SELECT Id, FileExtension,FileType FROM ContentVersion where Id IN :contentversionIdToFeedItemMap.keySet()]){
    if(cv.FileType =='exe' || cv.FileType=='html' || cv.FileType=='edi'|| cv.FileType=='dll'|| cv.FileType=='bin'|| cv.FileType=='cmd'|| cv.FileType=='vbscript'|| cv.FileType=='wsf'|| cv.FileType=='java'|| cv.FileType=='vbs'|| cv.FileType=='vb')
    {
        contentversionIdToFeedItemMap.get(cv.Id).addError('Sorry..!! You cannot attach this file format');
    }

Test Class:

@isTest(seeAlldata = true)
private class RestrictFilesOnFeedItems_Test {
    static testMethod void unitTestInsert() {
        id abc = 'EXE' ;
    Case cse = new case (COMPLAINANT__C = 'Self', STATUS = 'New', ORIGIN = 'Web', TYPE='Claims', SR_SUB_TYPE__C = 'Report Claim', SUBJECT = 'Test', DESCRIPTION= 'Testing');
        Insert cse;
        /*
        Profile p = [select id from profile where Name = :'System Administrator' limit 1];
        User pu = new User(profileId = p.id, username = 'abc@abc.com', email = 'abc@abc.com',
        emailencodingkey = 'UTF-8', localesidkey = 'en_US',
        languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles',
        alias='cspu', lastname='lastname');
        insert pu;
        */
        ContentVersion  cv = new ContentVersion ();
        insert cv;
        Test.startTest();
        FeedItem feed = new FeedItem (
            parentid = cse.id,
            type = 'ContentPost',
            RelatedRecordId = cv.id,
            Body = 'Hello'
        );
        try {
            insert feed;
        } catch (exception e) {
            System.assert(e.getMessage().contains('Sorry..!! You cannot attach this file format'));
        }
        Test.stopTest();
        List<FeedItem > lstfeed = [select id from FeedItem where parent.id = :cse.id];
        System.assertEquals(1, lstfeed.size());
        System.assertNotEquals(0, lstfeed.size());
    }
}
0

I have modified the code, please consider below :

private class RestrictFilesOnFeedItems_Test {
static testMethod void unitTestInsert() {
    //id abc = 'EXE' ;
    Case cse = new case (STATUS = 'New', ORIGIN = 'Web', TYPE='Claims',  SUBJECT = 'Test', DESCRIPTION= 'Testing');
    Insert cse;       
    Test.startTest();

    ContentVersion contentVersion_1 = new ContentVersion(
        Title = 'Content',
        PathOnClient = 'Content.html',
        VersionData = Blob.valueOf('Test Content'),
        IsMajorVersion = true
    );
    insert contentVersion_1;//create the the content version record 

    ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];//query the contentdocumentID
    List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

    **System.assertEquals(documents.size(), 1);//expected document of size 1
    System.assertEquals(documents[0].Id, contentVersion_2.ContentDocumentId); // matches the id of the document
    System.assertEquals(documents[0].LatestPublishedVersionId, contentVersion_2.Id);  //matches the Latest Published Version Id
    System.assertEquals(documents[0].Title, contentVersion_2.Title);  // matches the title**


    ContentDocumentLink newcontent = new ContentDocumentLink();
    newcontent.ContentDocumentId = contentVersion_2.ContentDocumentId;
    newcontent.LinkedEntityId = cse.Id;
    newcontent.ShareType =  'v';

    insert newcontent;//create ContentDocumentLink record

    FeedItem feed = new FeedItem (
        parentid = cse.id,
        type = 'ContentPost',
        RelatedRecordId = contentVersion_1.id,
        Body = 'Hello'
    );
    //then create feedItem record
    try {
        insert feed;    
    } catch (exception e) {
        System.assert(e.getMessage().contains('Sorry..!! You cannot attach this file format'));
    }
    Test.stopTest();      
}

}

It will work properly and it is covering 100% of your trigger.

0

Your test doesn't actually ensure that any validation takes place. If you look at your ContentVersion record, you don't even specify a FileType. I prefer this style of assertion:

// additional setup
FeedItem record = new FeedItem(/*data*/);
DmlException expectedException;

Test.startTest();
    try
    {
        insert record;
    }
    catch (DmlException d)
    {
        expectedException = d;
    }
Test.stopTest();

system.assertNotEquals(null, expectedException, 'A validation should occur');
// additional assertions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.