Why are no results returned when performing a SOSL query on ContentNote in a Test Method?

Following the example laid out in Adding SOSL Queries to Unit Tests, I created a test method that inserts a ContentNote record, sets the results using Test.setFixedSearchResults(), and then performs a SOSL query.

@isTest 
static void sampleTestMethod(){
    ContentNote n = New ContentNote(
        Title='test',
        Content = Blob.valueof('test')
    );

    insert n;

    Id [] fixedSearchResults= new Id[1];
    fixedSearchResults[0] = n.Id;
    Test.setFixedSearchResults(fixedSearchResults);
    List<List<SObject>> searchList = [FIND 'test' 
                                      IN ALL FIELDS RETURNING 
                                      ContentNote(id, title)];

    system.assertEquals(1,searchList[0].size());
}

I expected that there would be one result record, however, the system.assertEquals fails, indicating that no results were returned.

This pattern for performing SOSL queries in test methods works as expected with other objects, but not with ContentNote.

In order to reproduce this enhanced notes must be enabled in your org.

Answer

SOSL queries are tested similar to callouts using something like a Mock called fixedSearchResults(). See Adding SOSL Queries to Unit Tests in the Apex Docs. Here’s an example from the docs on one way it can be used:

@isTest
private class SoslFixedResultsTest1 {

    public static testMethod void testSoslFixedResults() {
       Id [] fixedSearchResults= new Id[1];
       fixedSearchResults[0] = '001x0000003G89h';
       Test.setFixedSearchResults(fixedSearchResults);
       List<List<SObject>> searchList = [FIND 'test' 
                                         IN ALL FIELDS RETURNING 
                                            Account(id, name WHERE name = 'test' LIMIT 1)];
    }
}

Here’s what you’ll find in the Test Class docs on setFixedSearchResults(setSearchResults).

setFixedSearchResults(setSearchResults)

Defines a list of fixed search results to be returned by all
subsequent SOSL statements in a test method. Signature

public static Void setFixedSearchResults(ID[] setSearchResults)

Parameters

setSearchResults Type: ID[] The list of record IDs specified by
opt_set_search_results replaces the results that would normally be
returned by the SOSL queries if they were not subject to any WHERE or
LIMIT clauses. If these clauses exist in the SOSL queries, they are
applied to the list of fixed search results. Return Value

Type: Void

Usage

If opt_set_search_results is not specified, all subsequent SOSL
queries return no results.

For more information, see Adding SOSL Queries to Unit Tests.

EDIT:

Having looked at the Object Reference for Content Note, I suspect your issue may be in how you’re creating the record in your test method. If you create the record as follows, I suspect your issue will disappear.

ContentNote n = new ContentNote();
n.Title = 'test';
String body = 'Hello World. Before insert/update, escape special characters such as ", ', &, and other standard escape characters.';
n.Content = Blob.valueOf(body.escapeHTML4());
insert(n);

Alternatively, it could be created like this:

ContentNote n = new ContentNote();
n.Title = 'test';
String body = '<b>Hello World. Because this text is already formatted as HTML, it does not need to be escaped. 
Special characters such as &quot;, etc. must already use their HTML equivalents.</b>';
n.Content = body;
insert(n);

If you add a system.debug('content record id = ' + n.Id ) to your test class, you may discover that you’re not getting the record inserted properly as you might think.

Attribution
Source : Link , Question Author : bardsley , Answer Author : Bahman.A

Leave a Comment