Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public class MyController
  2. {
  3. public MyController() {}
  4.  
  5. public List<X__c> selectX()
  6. {
  7. if (X__c.SObjectType.getDescribe().isAccessible()) {
  8. if (X__c.Fld__c.getDescribe().isAccessible())
  9. {
  10. return [SELECT Id, Fld__c FROM X__c];
  11. }
  12. }
  13.  
  14. return null;
  15. }
  16. }
  17.  
  18. public class MyControllerTest
  19. {
  20. @isTest
  21. static void verifySelectX()
  22. {
  23. // Given
  24. X__c[] xRecords =
  25. (List<X__c>) TestFactory.create(new X__c(), 2);
  26.  
  27. // When
  28. xRecords = new MyController().selectX();
  29.  
  30. // Then
  31. System.assertNotEquals(null, xRecords);
  32. System.assertEquals(2, xRecords.size());
  33. }
  34.  
  35. @isTest
  36. static void verifySelectXNoAccess()
  37. {
  38. // Given
  39. X__c[] xRecords =
  40. (List<X__c>) TestFactory.create(new X__c(), 2);
  41.  
  42. // Create a user which will not have access to the test object type
  43. User testUser = createUser('Chatter External');
  44. if (testUser == null) {
  45. // Abort the test if unable to create a user with low enough acess
  46. return;
  47. }
  48.  
  49. // When
  50. System.runAs(testUser)
  51. {
  52. xRecords = new MyController().selectX();
  53.  
  54. // Then
  55. System.assertEquals(null, xRecords);
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement