|
| 1 | +package org.xwiki.search.ui; |
| 2 | + |
| 3 | +import org.jsoup.nodes.Document; |
| 4 | +import org.jsoup.select.Elements; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.xwiki.model.reference.DocumentReference; |
| 8 | +import org.xwiki.rendering.RenderingScriptServiceComponentList; |
| 9 | +import org.xwiki.rendering.internal.configuration.DefaultRenderingConfigurationComponentList; |
| 10 | +import org.xwiki.test.annotation.ComponentList; |
| 11 | +import org.xwiki.test.page.HTML50ComponentList; |
| 12 | +import org.xwiki.test.page.PageTest; |
| 13 | +import org.xwiki.test.page.TestNoScriptMacro; |
| 14 | +import org.xwiki.test.page.XWikiSyntax21ComponentList; |
| 15 | +import org.xwiki.uiextension.script.UIExtensionScriptServiceComponentList; |
| 16 | + |
| 17 | +import com.xpn.xwiki.doc.XWikiDocument; |
| 18 | +import com.xpn.xwiki.objects.BaseObject; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +/** |
| 24 | + * Page test for {@code XWiki.SearchSuggestSourceSheet}. |
| 25 | + * |
| 26 | + * @version $Id$ |
| 27 | + */ |
| 28 | +@ComponentList({ |
| 29 | + TestNoScriptMacro.class |
| 30 | +}) |
| 31 | +@UIExtensionScriptServiceComponentList |
| 32 | +@RenderingScriptServiceComponentList |
| 33 | +@DefaultRenderingConfigurationComponentList |
| 34 | +@HTML50ComponentList |
| 35 | +@XWikiSyntax21ComponentList |
| 36 | +class SearchSuggestSourceSheetPageTest extends PageTest |
| 37 | +{ |
| 38 | + private static final String WIKI_NAME = "xwiki"; |
| 39 | + |
| 40 | + private static final String XWIKI_SPACE = "XWiki"; |
| 41 | + |
| 42 | + private static final DocumentReference SEARCH_SUGGEST_SOURCE_SHEET = |
| 43 | + new DocumentReference(WIKI_NAME, XWIKI_SPACE, "SearchSuggestSourceSheet"); |
| 44 | + |
| 45 | + private static final DocumentReference SEARCH_SUGGEST_SOURCE_CLASS = |
| 46 | + new DocumentReference(WIKI_NAME, XWIKI_SPACE, "SearchSuggestSourceClass"); |
| 47 | + |
| 48 | + private XWikiDocument searchSuggestSourceSheetDocument; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUp() throws Exception |
| 52 | + { |
| 53 | + this.xwiki.initializeMandatoryDocuments(this.context); |
| 54 | + |
| 55 | + this.loadPage(SEARCH_SUGGEST_SOURCE_CLASS); |
| 56 | + this.searchSuggestSourceSheetDocument = this.loadPage(SEARCH_SUGGEST_SOURCE_SHEET); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void checkPropertiesEscaping() throws Exception |
| 61 | + { |
| 62 | + // Create an instance of XWiki.SearchSuggestSourceClass with properties that require escaping. |
| 63 | + String[] properties = new String[]{"name", "engine", "url", "query", "resultsNumber", "icon"}; |
| 64 | + String unescapedProperty = "{{/html}}}}}{{noscript}}"; |
| 65 | + BaseObject searchSuggestSource = |
| 66 | + this.searchSuggestSourceSheetDocument.newXObject(SEARCH_SUGGEST_SOURCE_CLASS, this.context); |
| 67 | + for (String property : properties) { |
| 68 | + searchSuggestSource.set(property, unescapedProperty, this.context); |
| 69 | + } |
| 70 | + this.xwiki.saveDocument(this.searchSuggestSourceSheetDocument, this.context); |
| 71 | + |
| 72 | + this.context.setDoc(this.searchSuggestSourceSheetDocument); |
| 73 | + Document document = renderHTMLPage(this.searchSuggestSourceSheetDocument); |
| 74 | + Elements labels = document.getElementsByTag("label"); |
| 75 | + Elements values = document.getElementsByTag("dd"); |
| 76 | + |
| 77 | + // Check that the value of the property has not been evaluated for each label that we know of. |
| 78 | + for (String property : properties) { |
| 79 | + int iLabel = -1; |
| 80 | + for (int i = 0; i < labels.size(); i++) { |
| 81 | + if (labels.get(i).text().replaceAll("^.*_", "").equals(property)) { |
| 82 | + iLabel = i; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + assertTrue(iLabel >= 0, "Could not find property " + property + " in rendered document."); |
| 87 | + assertEquals(unescapedProperty, values.get(iLabel).text()); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments