forked from jongpie/NebulaQueryAndSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for using System.AccessLevel (jongpie#36)
- Loading branch information
1 parent
00d196d
commit 82c6d68
Showing
8 changed files
with
226 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,52 @@ private class AggregateQuery_Tests { | |
System.Assert.areEqual(expectedResults, returnedResults); | ||
} | ||
|
||
@IsTest | ||
static void it_should_run_with_system_mode() { | ||
String expectedQueryString = 'SELECT COUNT(Id) COUNT__Id FROM Opportunity'; | ||
|
||
AggregateQuery aggregateQuery = new AggregateQuery(Schema.Opportunity.SObjectType) | ||
.addAggregate(SOQL.Aggregate.COUNT, Opportunity.Id) | ||
.withAccessLevel(System.AccessLevel.SYSTEM_MODE); | ||
|
||
System.Assert.areEqual(expectedQueryString, aggregateQuery.getQuery()); | ||
List<Schema.AggregateResult> expectedResults = Database.query(expectedQueryString); | ||
List<Schema.AggregateResult> returnedResults; | ||
Exception caughtException; | ||
System.runAs(minimumAccessUser()) { | ||
try { | ||
returnedResults = aggregateQuery.getResults(); | ||
} catch (Exception e) { | ||
caughtException = e; | ||
} | ||
} | ||
System.Assert.isNull(caughtException, 'Query should not throw exception when run in System Mode'); | ||
System.Assert.areEqual(expectedResults, returnedResults); | ||
} | ||
|
||
@IsTest | ||
static void it_should_run_with_user_mode() { | ||
String expectedQueryString = 'SELECT COUNT(Id) COUNT__Id FROM Opportunity'; | ||
|
||
AggregateQuery aggregateQuery = new AggregateQuery(Schema.Opportunity.SObjectType) | ||
.addAggregate(SOQL.Aggregate.COUNT, Opportunity.Id) | ||
.withAccessLevel(System.AccessLevel.USER_MODE); | ||
|
||
System.Assert.areEqual(expectedQueryString, aggregateQuery.getQuery()); | ||
List<Schema.AggregateResult> expectedResults = Database.query(expectedQueryString); | ||
List<Schema.AggregateResult> returnedResults; | ||
Exception caughtException; | ||
System.runAs(minimumAccessUser()) { | ||
try { | ||
returnedResults = aggregateQuery.getResults(); | ||
} catch (Exception e) { | ||
caughtException = e; | ||
} | ||
} | ||
System.Assert.isInstanceOfType(caughtException, System.QueryException.class, 'Query should throw exception when run in User Mode'); | ||
System.Assert.isTrue(caughtException.getMessage().contains('sObject type \'Opportunity\' is not supported'), 'Query should throw exception when run in User Mode'); | ||
} | ||
|
||
@IsTest | ||
static void it_should_build_a_ridiculous_query_string() { | ||
String expectedQueryString = | ||
|
@@ -168,4 +214,18 @@ private class AggregateQuery_Tests { | |
List<Schema.AggregateResult> returnedResults = aggregateQuery.getResults(); | ||
System.Assert.areEqual(expectedResults, returnedResults); | ||
} | ||
|
||
static User minimumAccessUser() { | ||
return new User( | ||
Alias = 'newUser', | ||
Email = '[email protected]', | ||
EmailEncodingKey = 'UTF-8', | ||
LastName = 'Testing', | ||
LanguageLocaleKey = 'en_US', | ||
LocaleSidKey = 'en_US', | ||
ProfileId = [SELECT Id FROM Profile WHERE Name = 'Minimum Access - Salesforce'].Id, | ||
TimeZoneSidKey = 'GMT', | ||
UserName = '[email protected]' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,6 +193,52 @@ private class Query_Tests { | |
List<Account> accounts = accountQuery.getResults(); | ||
} | ||
|
||
@IsTest | ||
static void it_should_run_with_system_mode() { | ||
String expectedQueryString = 'SELECT Id, Name FROM Account LIMIT 1'; | ||
|
||
Query accountQuery = new Query(Schema.Account.SObjectType) | ||
.limitTo(1) | ||
.withAccessLevel(System.AccessLevel.SYSTEM_MODE); | ||
|
||
System.Assert.areEqual(expectedQueryString, accountQuery.getQuery()); | ||
List<Account> expectedResults = Database.query(expectedQueryString); | ||
List<Account> returnedResults; | ||
Exception caughtException; | ||
System.runAs(minimumAccessUser()) { | ||
try { | ||
returnedResults = accountQuery.getResults(); | ||
} catch (Exception e) { | ||
caughtException = e; | ||
} | ||
} | ||
System.Assert.isNull(caughtException, 'Query should not throw exception when run in System Mode'); | ||
System.Assert.areEqual(expectedResults, returnedResults); | ||
} | ||
|
||
@IsTest | ||
static void it_should_run_with_user_mode() { | ||
String expectedQueryString = 'SELECT Id, Name FROM Account LIMIT 1'; | ||
|
||
Query accountQuery = new Query(Schema.Account.SObjectType) | ||
.limitTo(1) | ||
.withAccessLevel(System.AccessLevel.USER_MODE); | ||
|
||
System.Assert.areEqual(expectedQueryString, accountQuery.getQuery()); | ||
List<Account> expectedResults = Database.query(expectedQueryString); | ||
List<Account> returnedResults; | ||
Exception caughtException; | ||
System.runAs(minimumAccessUser()) { | ||
try { | ||
returnedResults = accountQuery.getResults(); | ||
} catch (Exception e) { | ||
caughtException = e; | ||
} | ||
} | ||
System.Assert.isInstanceOfType(caughtException, System.QueryException.class, 'Query should throw exception when run in User Mode'); | ||
System.Assert.isTrue(caughtException.getMessage().contains('sObject type \'Account\' is not supported'), 'Query should throw exception when run in User Mode'); | ||
} | ||
|
||
@IsTest | ||
static void it_includes_order_by_statement_for_single_field() { | ||
String expectedQueryString = 'SELECT Id, Name FROM Lead ORDER BY CreatedDate ASC NULLS FIRST'; | ||
|
@@ -277,4 +323,18 @@ private class Query_Tests { | |
|
||
System.Test.stopTest(); | ||
} | ||
|
||
static User minimumAccessUser() { | ||
return new User( | ||
Alias = 'newUser', | ||
Email = '[email protected]', | ||
EmailEncodingKey = 'UTF-8', | ||
LastName = 'Testing', | ||
LanguageLocaleKey = 'en_US', | ||
LocaleSidKey = 'en_US', | ||
ProfileId = [SELECT Id FROM Profile WHERE Name = 'Minimum Access - Salesforce'].Id, | ||
TimeZoneSidKey = 'GMT', | ||
UserName = '[email protected]' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,6 +127,56 @@ private class RecordSearch_Tests { | |
List<User> userSearchResults = userSearch.getFirstResults(); | ||
} | ||
|
||
@IsTest | ||
static void it_should_run_with_system_mode() { | ||
String expectedSearchQueryString = 'FIND \'' + System.UserInfo.getUserEmail() + '\' IN ALL FIELDS RETURNING Contact(Id, Name)'; | ||
|
||
Query contactQuery = new Query(Schema.Contact.SObjectType); | ||
RecordSearch contactSearch = new RecordSearch(System.UserInfo.getUserEmail(), contactQuery) | ||
.withAccessLevel(System.AccessLevel.SYSTEM_MODE); | ||
|
||
System.assertEquals(expectedSearchQueryString, contactSearch.getSearch()); | ||
List<Contact> contactSearchResults = contactSearch.getFirstResults(); | ||
|
||
List<List<SObject>> expectedResults = Search.query(expectedSearchQueryString); | ||
List<List<SObject>> returnedResults; | ||
Exception caughtException; | ||
System.runAs(minimumAccessUser()) { | ||
try { | ||
returnedResults = contactSearch.getResults(); | ||
} catch (Exception e) { | ||
caughtException = e; | ||
} | ||
} | ||
System.Assert.isNull(caughtException, 'Search should not throw exception when run in System Mode'); | ||
System.Assert.areEqual(expectedResults, returnedResults); | ||
} | ||
|
||
@IsTest | ||
static void it_should_run_with_user_mode() { | ||
String expectedSearchQueryString = 'FIND \'' + System.UserInfo.getUserEmail() + '\' IN ALL FIELDS RETURNING Contact(Id, Name)'; | ||
|
||
Query contactQuery = new Query(Schema.Contact.SObjectType); | ||
RecordSearch contactSearch = new RecordSearch(System.UserInfo.getUserEmail(), contactQuery) | ||
.withAccessLevel(System.AccessLevel.USER_MODE); | ||
|
||
System.assertEquals(expectedSearchQueryString, contactSearch.getSearch()); | ||
List<Contact> contactSearchResults = contactSearch.getFirstResults(); | ||
|
||
List<List<SObject>> expectedResults = Search.query(expectedSearchQueryString); | ||
List<List<SObject>> returnedResults; | ||
Exception caughtException; | ||
System.runAs(minimumAccessUser()) { | ||
try { | ||
returnedResults = contactSearch.getResults(); | ||
} catch (Exception e) { | ||
caughtException = e; | ||
} | ||
} | ||
System.Assert.isInstanceOfType(caughtException, System.QueryException.class, 'Search should throw exception when run in User Mode'); | ||
System.Assert.isTrue(caughtException.getMessage().contains('sObject type \'contact\' is not supported'), 'Search should throw exception when run in User Mode'); | ||
} | ||
|
||
@IsTest | ||
static void it_should_cache_search_results_when_enabled() { | ||
Integer loops = 4; | ||
|
@@ -150,4 +200,18 @@ private class RecordSearch_Tests { | |
|
||
System.Test.stopTest(); | ||
} | ||
|
||
static User minimumAccessUser() { | ||
return new User( | ||
Alias = 'newUser', | ||
Email = '[email protected]', | ||
EmailEncodingKey = 'UTF-8', | ||
LastName = 'Testing', | ||
LanguageLocaleKey = 'en_US', | ||
LocaleSidKey = 'en_US', | ||
ProfileId = [SELECT Id FROM Profile WHERE Name = 'Minimum Access - Salesforce'].Id, | ||
TimeZoneSidKey = 'GMT', | ||
UserName = '[email protected]' | ||
); | ||
} | ||
} |