function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aidel BruckAidel Bruck 

How to test this trigger on contentDocument

I have the following trigger I wrote for ContentDocument
I want to send an email alert to various users when a file is uploaded 
This is the code and I would like to know how to test it
Also I need a way to get the account name the file is associated with

trigger NewFileAlert on ContentDocument (after insert) 
{
// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (ContentDocument newDoc : Trigger.new) 
  {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(list of all addresses)
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('myaddress');
      mail.setSenderDisplayName('Aidel Bruck');
    
      // (Optional) Set list of people who should be CC'ed
      //List<String> ccTo = new List<String>();
      //ccTo.add('business@bankofnigeria.com');
      //mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Document Added');
      String body = 'Please note: A new document has been added/n  ';
      body += 'File name: '+ newdoc.title+'/n' ;
      body += 'Created By: '+ newdoc.createdbyid+ '/n';
      body += 'Created Date: '+ newdoc.CreatedDate+ '/n';
      body += 'link to file: '+ System.URL.getSalesforceBaseUrl().getHost()+newdoc.id;
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}

Thanks!
Waqar Hussain SFWaqar Hussain SF
try below code
@isTest
public class ContentDocument_Test{
	public static testmethod void MyUnitTest(){
		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;
	}
}

 
abruckabruck
The line in bold is causing the error below:
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_ARGUMENT_TYPE, The argument is null or invalid.: []

@isTest
02public class ContentDocument_Test{
03    public static testmethod void MyUnitTest(){
04        Account acct = new Account(Name='TEST_ACCT');
05        insert acct;
06 
07        ContentVersion contentVersion = new ContentVersion(
08          Title = 'Penguins',
09          PathOnClient = 'Penguins.jpg',
10          VersionData = Blob.valueOf('Test Content'),
11          IsMajorVersion = true
12        );
13        insert contentVersion;