@isTest
public class TestDeleteAttachment {
@testSetup static void prepData()
{
Profile p1 = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
User testUser = new User(FirstName = 'Test', LastName = 'User', ProfileId = p1.Id,Email = '[email protected]', userName='[email protected]',alias='utest',
timezonesidkey='America/Chicago',localesidkey='en_US',emailencodingkey='UTF-8',languagelocalekey='en_US');
insert testUser;
//TO-DO: Create another user with a different profile in your org
//Create a dummy record
Account a1 = New Account (Name='ACME');
insert a1;
//Create Attachment
Attachment attach = New Attachment();
attach.Name = 'Test Attachment';
Blob bodyBlob=Blob.valueOf('Dummy Body');
attach.body=bodyBlob;
attach.parentId=a1.id;
insert attach;
}
static testMethod void testDeleteAttach1()
{
//First fetch the user you created in test setup
User u = [select Id from User where Username = '[email protected]'];
Attachment attach = [Select Id from Attachment where Name = 'Test Attachment' LIMIT 1];
System.runas(u)
{
//Delete the attachment to test the trigger
Test.startTest();
delete attach;
Test.stopTest();
}
}
}
}
static testMethod void testDeleteAttach2()
{
//To-DO: Use the 2nd user to test whether your trigger prevents the deletion as expected
//Also add methods to test the method 'isAllowed'
}
}
}