Salesforce Test Class Data For ContentDocument

Sample Code:

//Create Document
ContentVersion cv = new ContentVersion();
cv.Title = 'Test Document';
cv.PathOnClient = 'TestDocument.pdf';
cv.VersionData = Blob.valueOf('Test Content');
cv.IsMajorVersion = true;
Insert cv;

//Get Content Version
List<ContentVersion> cvList = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id];
System.assertEquals(cvList.size(), 1);

//Get Content Documents
List<ContentDocument> cdList = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
System.assertEquals(cdList.size(), 1);

Salesforce Test Class Data For ContentDocumentLink

Sample Code:

//Create Document Parent Record
Account acc = new Account(Name='Test Account');
Insert acc;

//Create Document
ContentVersion cv = new ContentVersion();
cv.Title = 'Test Document';
cv.PathOnClient = 'TestDocument.pdf';
cv.VersionData = Blob.valueOf('Test Content');
cv.IsMajorVersion = true;
Insert cv;

//Get Content Documents
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;

//Create ContentDocumentLink 
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = acc.Id;
cdl.ContentDocumentId = conDocId;
cdl.shareType = 'V';
Insert cdl;

Component Events vs Application Events

Component Events:

  • Component Events can be handled by same component or a component that instantiates or contains the component.
  • The component events can only be registered in child component and handled by parent component.
  • We use attribute type="COMPONENT" in the aura:event tag for an component event.
  • While handling component events, we need to specify name attribute in aura:handler. The name attribute in aura:handler must match the name attribute in the aura:registerEvent tag in the Child Component that fires the event.
  • We use cmp.getEvent("evtName") in JavaScript to get an instance of the Component type event.

Application Events:

  • Application Events are handled by any component have handler defined for event.These events are essentially a traditional publish-subscribe model.
  • Application event can be used through out the application.
  • We use attribute type="APPLICATION" in the aura:event tag for an application event.
  • While handling application events, no need to specify name attribute in aura:handler.
  • We use $A.get("e.myNamespace:myAppEvent") in JavaScript to get an instance of the Application type event.

Application Events in Salesforce Lightning Framework

Lightning framework is based on event-driven architecture which allows to communicate between different events. Lightning events are fired from JavaScript controller actions that are triggered by a user interacting with the user interface.

The communication between components are handled by events. There are two types of custom events in the Lightning Framework:

  • Component Events
  • Application Events

Application Events are handled by any component have handler defined for event.These events are essentially a traditional publish-subscribe model.

Application Event Example:
In below example by using Application event, I’m passing the values from Component1 to a Component2 via event.

Sample Application Event:
Create a sample application type event. Use type=”APPLICATION” in the aura:event tag for an application event. The attribute type is the one that will differentiate Application event from Component event.

<!--SampleApplicationEvent.evt-->
<aura:event type="Application" description="Sample Application Event">
    <aura:attribute name="message" type="String" />
</aura:event>

Component1:

<!--Component1.cmp-->
<aura:component>
    <aura:registerEvent name="SampleApplicationEvent" type="c:SampleApplicationEvent"/>
    <lightning:button label="Click to fire the event" variant="brand" onclick="{!c.component1Event}"/>
</aura:component>

Component1 JS Controller:
Use $A.get(“e.myNamespace:myAppEvent”) in JavaScript to get an instance of the myAppEvent event in the myNamespace namespace.
To set the attribute values of event, call event.setParam() or event.setParams().

({
    component1Event : function(cmp, event,helper) { 
        //Get the event using event name. 
        var appEvent = $A.get("e.c:SampleApplicationEvent"); 
        //Set event attribute value
        appEvent.setParams({"message" : "Welcome "}); 
        appEvent.fire(); 
    }
})

Component2:
The application event handled by the Component2 that fired using aura:handler in the markup.
The action attribute of aura:handler sets the client-side controller action to handle the event.

<!--Component2.cmp-->
<aura:component>
    <aura:attribute name="eventMessage" type="String"/> 
    <aura:handler event="c:SampleApplicationEvent" action="{!c.component2Event}"/>
    <div class="slds-m-around_xx-large">
        <p>{!v.eventMessage}</p> 
    </div>
</aura:component>

Component2 JS Controller:

({
    component2Event : function(cmp, event) { 
        //Get the event message attribute
        var message = event.getParam("message"); 
        //Set the handler attributes based on event data 
        cmp.set("v.eventMessage", message + 'Biswajeet');         
    }
})

Lightning Application:

<aura:application extends="force:slds">
    <c:Component1/>
    <c:Component2/>
</aura:application>

Output:

Component Events in Salesforce Lightning Framework

Lightning framework is based on event-driven architecture which allows to communicate between different events. Lightning events are fired from JavaScript controller actions that are triggered by a user interacting with the user interface.

The communication between components are handled by events. There are two types of custom events in the Lightning Framework:

  • Component Events
  • Application Events

Component Events can be handled by same component or a component that instantiates or contains the component. The component events can only be registered in child component and handled by parent component.

Component Event Example:
In below example by using component event, I’m passing the values from a child component to a parent component via event.

Sample Component Event:
Create a sample component type event. Use type=”COMPONENT” in the aura:event tag for an component event. The attribute type is the one that will differentiate Component event from Application event.

<!--SampleComponentEvent.evt-->
<aura:event type="COMPONENT" description="Sample Component Event">
    <aura:attribute name="message" type="String" default="Hello World!!" />
</aura:event>

Child Component:
The event is registered in this component by using aura:registerEvent in its markup.

<!--Child.cmp-->
<aura:component >
    <aura:registerEvent name="sampleCmpEvent" type="c:SampleComponentEvent" />
    <lightning:button label="Click to fire the event" variant="brand" onclick="{!c.childComponentEvent}"/>
</aura:component>

Child Component JS Controller:
To set the attribute values of event, call event.setParam() or event.setParams().

({
    childComponentEvent : function(cmp, event,helper) { 
        //Get the event using registerEvent name. 
        var cmpEvent = cmp.getEvent("sampleCmpEvent"); 
        //Set event attribute value
        cmpEvent.setParams({"message" : "Welcome "}); 
        cmpEvent.fire(); 
    }
})

Parent Component:
The component event handled by the Parent Component that fired using aura:handler in the markup.
The name attribute in aura:handler must match the name attribute in the aura:registerEvent tag in the Child Component that fires the event.
The action attribute of aura:handler sets the client-side controller action to handle the event.

<!--Parent.cmp-->
<aura:component >
    <aura:attribute name="eventMessage" type="String"/> 
    <aura:handler name="sampleCmpEvent" event="c:SampleComponentEvent" action="{!c.parentComponentEvent}"/>
    <div class="slds-m-around_xx-large">
        <c:Child /> 
        <p>{!v.eventMessage}</p> 
    </div>
</aura:component>

Parent Component JS Controller:

({
    parentComponentEvent : function(cmp, event) { 
        //Get the event message attribute
        var message = event.getParam("message"); 
        //Set the handler attributes based on event data 
        cmp.set("v.eventMessage", message + 'Biswajeet');         
    } 
})

Output: