Skip to content

Commit

Permalink
XWIKI-21472: Directly write RSS feed content in Main.DatabaseSearch
Browse files Browse the repository at this point in the history
* Add a page test
  • Loading branch information
pjeanjean authored and michitux committed Nov 16, 2023
1 parent c4c8d61 commit 95bdd6c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,12 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-feed-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,21 @@
#set ($discard = $feed.setDescription($services.localization.render('search.rss', [$text])))
#set ($discard = $feed.setLanguage("$xcontext.locale"))
#set ($discard = $feed.setCopyright($xwiki.getXWikiPreference('copyright')))
#set ($feedOutput = $xwiki.feed.getFeedOutput($feed, $xwiki.getXWikiPreference('feed_type', 'rss_2.0')))

#set ($discard = $response.setContentType('application/rss+xml'))
{{{$xwiki.feed.getFeedOutput($feed, $xwiki.getXWikiPreference('feed_type', 'rss_2.0'))}}}
#set ($characterEncoding = 'utf-8')
## Make sure the Character Encoding response header matches the character encoding used to write the response and
## compute its length.
#set ($discard = $response.setCharacterEncoding($characterEncoding))
## We write the output directly to the response to avoid the execution of the Rendering Transformations.
#set ($discard = $response.writer.print($feedOutput))
## The content length is measured in bytes and one character can use more than one byte.
#set ($discard = $response.setContentLength($feedOutput.getBytes($characterEncoding).size()))
## Make sure the entire content is send back to the client.
#set ($discard = $response.flushBuffer())
## Make sure XWiki doesn't write any more content to the response.
#set ($discard = $xcontext.setFinished(true))
#else
{{include reference="XWiki.Results"/}}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.search.ui;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.rendering.RenderingScriptServiceComponentList;
import org.xwiki.rendering.internal.configuration.DefaultRenderingConfigurationComponentList;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.page.HTML50ComponentList;
import org.xwiki.test.page.PageTest;
import org.xwiki.test.page.TestNoScriptMacro;
import org.xwiki.test.page.XWikiSyntax21ComponentList;

import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.plugin.feed.FeedPlugin;
import com.xpn.xwiki.web.XWikiServletResponseStub;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Page test for {@code Main.DatabaseSearch}.
*
* @version $Id$
*/
@ComponentList({
TestNoScriptMacro.class
})
@RenderingScriptServiceComponentList
@DefaultRenderingConfigurationComponentList
@HTML50ComponentList
@XWikiSyntax21ComponentList
class DatabaseSearchPageTest extends PageTest
{
private static final String WIKI_NAME = "xwiki";

private static final String MAIN_SPACE = "Main";

private static final DocumentReference DATABASE_SEARCH_REFERENCE =
new DocumentReference(WIKI_NAME, MAIN_SPACE, "DatabaseSearch");

@BeforeEach
void setUp()
{
this.xwiki.initializeMandatoryDocuments(this.context);

this.xwiki.getPluginManager().addPlugin("feed", FeedPlugin.class.getName(), this.context);
}

@Test
void checkRSSFeedContent() throws Exception
{
String unescapedText = "<b>}}}{{noscript}}</b>";
String escapedText = "&lt;b&gt;}}}{{noscript}}&lt;/b&gt;";

this.request.put("text", unescapedText);
this.context.setAction("get");

XWikiDocument databaseSearchDocument = loadPage(DATABASE_SEARCH_REFERENCE);
this.context.setDoc(databaseSearchDocument);

// Get directly the writer to check the RSS feed.
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
this.response = new XWikiServletResponseStub() {
@Override
public PrintWriter getWriter()
{
return writer;
}
};
this.context.setResponse(this.response);

String rssFeed = databaseSearchDocument.displayDocument(Syntax.PLAIN_1_0, this.context);
assertTrue(rssFeed.isEmpty());

rssFeed = out.toString();
assertTrue(rssFeed.contains("<title>search.rss [" + escapedText + "]</title>"));
assertTrue(rssFeed.contains("<description>search.rss [" + escapedText + "]</description>"));
}
}

1 comment on commit 95bdd6c

@montyc123

This comment was marked as spam.

Please sign in to comment.