I'm doing something in a visualforce controller for every attachment and chatter contentpost that is related to a particular case. Hoping to open-source it (pending employer ok), but for now I'll just post a few snippets from it.
for( CaseFeed fi : [
SELECT
ContentFileName,
ContentType,
ContentSize,
RelatedRecordId,
CreatedDate
FROM CaseFeed
WHERE ParentId =: ctrl.getId()
AND Type =: 'ContentPost'
LIMIT 500
]
){
//Do something with it
}
Parentid is set to the id of the case in question. This query works well with "with sharing" set on the controller. Changing the query from looking at CaseFeed to FeedItem requires "without sharing" to be set on the controller which is not cool.
The problem I'm facing is in unit testing this, you can't apparently insert a CaseFeed record directly, you're supposed to insert a FeedItem like so:
FeedItem chatterCrap = new FeedItem(
ParentId=caze.Id,
ContentFileName='kittens.jpg',
ContentData=EncodingUtil.base64Decode('fluffykittens')
);
insert chatterCrap;
The problem with this is that the query in the controller that's supposed to pick out all the ContentPosts (chatter with files on them) doesn't see any CaseFeed when only the FeedItem has been inserted. If the controller looks at FeedItems it works, but that's not acceptable due to violating sharing-control.
In the actual SF use, i.e. not test cases, inserting a FeedItem seems to automatically create a CaseFeed record, and the visualforce controller works flawlessly for what I want it to do - just not it's test cases.
My experience with chatter is limited so I'm hoping someone can point out where I'm perhaps doing something wrong, the documentation is utterly useless (much like chatter itself, but that's just my personal opinion).
Edit (addition of a minimal test case that fails in our org):
@isTest(seeAllData=false)
static void test(){
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' limit 1];
User u = new User(
Username='[email protected]',
Email='[email protected]',
Lastname='Lastname',
Alias='fl',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
TimezoneSidKey='America/Los_Angeles',
EmailEncodingKey='UTF-8',
ProfileId=p.Id
);
insert u;
Case caze = new Case();
insert caze;
System.runAs(u){
//Chatter post on case
FeedItem chatterCrap = new FeedItem(
ParentId=caze.Id,
ContentFileName='kittens.jpg',
ContentData=EncodingUtil.base64Decode('fluffykittens')
);
insert chatterCrap;
Integer howManyAreThere=0;
for( CaseFeed fi : [
SELECT
ContentFileName,
ContentType,
ContentSize,
RelatedRecordId,
CreatedDate
FROM CaseFeed
WHERE ParentId =: caze.Id
AND Type =: 'ContentPost'
LIMIT 500
]
){
howManyAreThere++;
}
System.assertEquals(1,howManyAreThere);
}
}