Skip to main content Stream TDX Bengaluru on Salesforce+. Start learning the critical skills you need to build and deploy trusted autonomous agents with Agentforce. Register for free.

Please help me with writing a test for this code!

public with sharing class AttClass {

Id PageId;

Id contentDocId;

public Id contentId{get; set;}

public Blob Body {get;set;}

public String Name {get;set;}

id cand;

public ContentDocument imgExist(Id c){

try{

contentDocId = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =: c LIMIT 1].ContentDocumentId;

if(contentDocId == null){return null;}

ContentDocument cdRec = [SELECT Id, LatestPublishedVersionId FROM ContentDocument WHERE Id =: contentDocId];

System.debug('LatestPublishedVersionId:' + cdRec.LatestPublishedVersionId);

if(cdRec == null){return null;}

return cdRec;

} catch (Exception e){

return null;

}

}

public AttClass() {

/*cand = [SELECT Id, Name FROM Candidate__c

WHERE Id = :ApexPages.currentPage().getParameters().get('id')];*/

//Name = 'Candidate ' + cand.Name + ' Photo';

cand = ApexPages.currentPage().getParameters().get('id');

ContentDocument cd = imgExist(cand);

if(cd != null){

contentId = cd.LatestPublishedVersionId;

System.debug('LatestPublVersionId: ' + contentId);

}

}

public PageReference save() {

if(contentId == null){

ContentVersion ConVer = createContentVersion(Name, Body);

contentId = ConVer.Id;

createContentLink(contentId, cand);

return null;

}

else {

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'You have one photo');

ApexPages.addMessage(myMsg);

return null;

}

}

public static ContentVersion createContentVersion(String Name, Blob Body){

ContentVersion ConVer = new ContentVersion();

ConVer.VersionData = Body;

ConVer.Title = Name;

ConVer.PathOnClient = Name;

try {

insert ConVer;

return ConVer;

} catch(DMLException e) {

System.debug(e);

return null;

}

}

public static void createContentLink(String contentVersionId, String recordId){

ContentDocumentLink cdLink = new ContentDocumentLink();

cdLink.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion

WHERE Id =: contentVersionId].ContentDocumentId;

cdLink.LinkedEntityId = recordId;

cdLink.ShareType = 'I';

cdLink.Visibility = 'AllUsers';

try{

insert cdLink;

} catch(DMLException e){

System.debug(e);

}

}

}

4 respuestas
  1. 26 sep 2018, 7:51 p.m.
    Change your VF page name and here is the code with 91 %

     

    @isTest

    public class AttClassTest

    {

    static testMethod void testEx1()

    {

    Account testAccount = new Account();

    testAccount.Name='Test Account' ;

    insert testAccount;

    Contact cont = new Contact ();

    cont.FirstName = 'FirstName';

    cont.LastName = 'LastName';

    cont.Email='email@email.com';

    cont.phone='12345678';

    insert cont;

    Account acct = new Account(Name='TEST_ACCT');

    insert acct;

    ContentVersion contentVersion = new ContentVersion(

    Title = 'Penguins',

    PathOnClient = 'Penguins.jpg',

    VersionData = Blob.valueOf('Test Content'),

    IsMajorVersion = true

    );

    insert contentVersion;

    List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

    //create ContentDocumentLink record

    ContentDocumentLink cdl = New ContentDocumentLink();

    cdl.LinkedEntityId = acct.id;

    cdl.ContentDocumentId = documents[0].Id;

    cdl.shareType = 'V';

    insert cdl;

    System.Test.StartTest();

    PageReference pageRef = Page.aaaaaaaaaa; // Add your VF page Name here

    pageRef.getParameters().put('Id', String.valueOf(acct.Id));

    System.Test.setCurrentPage(pageRef);

    AttClass cls = new AttClass();

    cls.Body =Blob.valueOf('Test Content') ;

    cls.Name='Test' ;

    cls.save();

    AttClass.createContentVersion('Test',Blob.valueOf('Test Content'));

    AttClass.createContentLink(contentVersion.Id , acct.Id);

    cls.contentId =null ;

    cls.save();

    System.Test.StopTest();

    }

    }

     
Cargando
0/9000