Skip to content

Commit

Permalink
XWIKI-22002: Improve the required rights analysis of TextArea properties
Browse files Browse the repository at this point in the history
* Use wiki syntax as default content type like the TextAreaClass.
* Add a test to verify the default behavior.
  • Loading branch information
michitux committed Mar 19, 2024
1 parent 529c8fe commit 153dbfa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ protected List<RequiredRightAnalysisResult> analyzeTextAreaProperty(BaseObject o
String contentTypeString = textAreaClass.getContentType();
TextAreaClass.ContentType contentType =
TextAreaClass.ContentType.getByValue(contentTypeString);
if (contentType == null) {
// Default to wiki text like TextAreaClass does.
contentType = TextAreaClass.ContentType.WIKI_TEXT;
}
PropertyInterface field = object.getField(propertyName);

List<RequiredRightAnalysisResult> result = List.of();
Expand All @@ -162,7 +166,7 @@ protected List<RequiredRightAnalysisResult> analyzeTextAreaProperty(BaseObject o
if (!textAreaClass.isRestricted() && field instanceof BaseStringProperty) {
String value = ((BaseStringProperty) field).getValue();

if (contentType != null && StringUtils.isNotBlank(value)) {
if (StringUtils.isNotBlank(value)) {
switch (contentType) {
case VELOCITY_CODE:
result = analyzeVelocityScriptValue(value, field.getReference(),
Expand All @@ -176,10 +180,10 @@ protected List<RequiredRightAnalysisResult> analyzeTextAreaProperty(BaseObject o
result = analyzeWikiContent(object, value, field.getReference());
}
break;
case WIKI_TEXT:
result = analyzeWikiContent(object, value, field.getReference());
case PURE_TEXT:
break;
default:
result = analyzeWikiContent(object, value, field.getReference());
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,40 @@ void analyzeWithDefaultAnalyzer()
assertEquals(testObject.getField(velocityWikiFieldName).getReference(), results.get(2).getEntityReference());
}

@Test
void analyzeDefaultTextArea()
throws XWikiException, RequiredRightsException, MissingParserException, ParseException
{
DocumentReference classReference = new DocumentReference("wiki", "XWiki", "StandardClass");
XWikiDocument classDocument = new XWikiDocument(classReference);
BaseClass classObject = classDocument.getXClass();
String wikiFieldName = "wiki";
classObject.addTextAreaField(wikiFieldName, "Wiki", 80, 5, "---", "---", false);
this.oldcore.getSpyXWiki().saveDocument(classDocument, this.oldcore.getXWikiContext());

XWikiDocument testDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
Syntax testSyntax = mock();
testDocument.setSyntax(testSyntax);
BaseObject testObject = testDocument.newXObject(classReference, this.oldcore.getXWikiContext());
String wikiContent = "{{groovy}}{{/groovy}}";
testObject.setLargeStringValue(wikiFieldName, wikiContent);

XDOM wikiXDOM = new XDOM(List.of());
when(this.contentParser.parse(wikiContent, testSyntax, testObject.getDocumentReference())).thenReturn(wikiXDOM);

RequiredRightAnalysisResult wikiResult = mock();
when(this.xdomRequiredRightAnalyzer.analyze(wikiXDOM)).thenReturn(List.of(wikiResult));

List<RequiredRightAnalysisResult> results = this.analyzer.analyze(testObject);
verify(this.xdomRequiredRightAnalyzer).analyze(wikiXDOM);
verifyNoMoreInteractions(this.xdomRequiredRightAnalyzer);
assertEquals(testObject.getField(wikiFieldName).getReference(),
wikiXDOM.getMetaData().getMetaData().get(XDOMRequiredRightAnalyzer.ENTITY_REFERENCE_METADATA));

assertEquals(wikiResult, results.get(0));
}


@Test
void analyzeWithCustomAnalyzerThrowsException() throws XWikiException, RequiredRightsException
{
Expand Down

0 comments on commit 153dbfa

Please sign in to comment.