• Roman Poltavskiy
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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);
			
		}
	}                    
}

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);
			
		}
	}                    
}