@Test public void getIdLanguageNotAvailable() throws Exception { DocumentSolrMetadataExtractor extractor = (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest(); // Mock // Empty string returned as language. The default "en" will be used. when(mockDocument.getRealLanguage()).thenReturn(""); // Call String id = extractor.getId(documentReference); // Assert and verify Assert.assertEquals("wiki:space.name_en", id); verify(mockDab, times(1)).getDocument(documentReference); verify(mockDocument, times(1)).getRealLanguage(); }
public String handleMatch(MatchResult result, FilterContext context) { String id = null; String title = result.group(0); String level = result.group(1); int level_i = (level.length() + 1) / 2; String hlevel = (level_i <= 6 ? level_i : 6) + ""; String text = result.group(3); String numbering = ""; RenderContext rcontext = context.getRenderContext(); XWikiContext xcontext = ((XWikiRadeoxRenderEngine) rcontext.getRenderEngine()).getXWikiContext(); VelocityContext vcontext = (VelocityContext) xcontext.get("vcontext"); XWikiDocument doc = xcontext.getDoc(); LOGGER.debug("Processing '" + text + "'"); // generate unique ID of the heading IdGenerator idGenerator = (IdGenerator) xcontext.get("headingsIdGenerator"); if (idGenerator == null) { idGenerator = new IdGenerator(); xcontext.put("headingsIdGenerator", idGenerator); } id = idGenerator.generateUniqueId("H", text); LOGGER.debug("Generated heading id '" + id + "'"); // add numbering if the flag is set if (xcontext.containsKey(TOC_NUMBERED) && ((Boolean) xcontext.get(TOC_NUMBERED)).booleanValue()) { // This is the old place where the data was placed, but this requires programming // rights. Instead, we now use vcontext. if (xcontext.containsKey(TOC_DATA)) { Map tocEntry = (Map) ((Map) xcontext.get(TOC_DATA)).get(id); if (tocEntry != null) { numbering = (String) tocEntry.get(TOCGenerator.TOC_DATA_NUMBERING) + " "; } } else if (vcontext != null && vcontext.containsKey(TOC_DATA)) { Map tocEntry = (Map) ((Map) vcontext.get(TOC_DATA)).get(id); if (tocEntry != null) { numbering = (String) tocEntry.get(TOCGenerator.TOC_DATA_NUMBERING) + " "; } } } String heading = formatter.format(new Object[] {id, numbering, text, hlevel}); // Only show the section edit button for view action and when the user has edit rights on // the current document boolean showEditButton = false; if (xcontext.getWiki().hasSectionEdit(xcontext) && ("view".equals(xcontext.getAction()))) { try { // TODO: The user should always be set and never be null when this code gets // executed. Unfortunately this is currently happening. It should be set to XWiki // Guest immediatly in the initialization phase. // TODO: Similarly the current document should never be null when this code gets // executed as it would mean we're trying to render the headings for a null // document and that doesn't make sense... if ((doc != null) && ((xcontext.getUser() != null) && xcontext .getWiki() .getRightService() .hasAccessLevel("edit", xcontext.getUser(), doc.getFullName(), xcontext))) { showEditButton = true; } } catch (XWikiException e) { // TODO: Remove this try/catch block by removing the throw exception on // hasAccessLevel() as it never throws any exception... } } Object beforeAction = xcontext.get("action"); if (showEditButton) { if (beforeAction != null) { if (!beforeAction.toString().equals("HeadingFilter")) { xcontext.put("action", "HeadingFilter"); sectionNumber = 0; } } if (level.equals("1") || level.equals("1.1")) { // This check is needed so that only the document content generates sectionedit // links. // TODO: Find a better way to make this check, as this prevents generating links for // titles that are transformed by velocity (1.1 about $doc.fullName) or by radeox // (1.1 This is *important*). if (doc != null && doc.getContent().indexOf(title.trim()) != -1) { // TODO: This is unstable, meaning that it works in the current skin, but it might // fail if there are other headings processed before the document content. sectionNumber++; StringBuffer editparams = new StringBuffer(); if (xcontext.getWiki().getEditorPreference(xcontext).equals("wysiwyg")) { editparams.append("xpage=wysiwyg&section=").append(sectionNumber); } else { editparams.append("section=").append(sectionNumber); } try { if ((xcontext.getWiki().isMultiLingual(xcontext)) && (doc.getRealLanguage(xcontext) != null)) { editparams.append("&language=").append(doc.getRealLanguage(xcontext)); } } catch (XWikiException e) { } String url = doc.getURL("edit", editparams.toString(), xcontext); return heading + "<span class='edit_section'>[" + "<a style='text-decoration: none;' title='Edit section: " + text.replaceAll("'", "'") + "' href='" + url + "'>" + "edit" + "</a>]</span>"; } } } return heading; }
@Before public void setUp() throws Exception { EntityReferenceSerializer<String> localSerializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local"); EntityReferenceSerializer<String> serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "default"); // No locale provided. documentReference = new DocumentReference("wiki", "space", "name"); documentReferenceString = serializer.serialize(documentReference); documentReferenceLocalString = localSerializer.serialize(documentReference); language = "en"; renderedContent = "content"; title = "title"; version = "1.1"; hidden = false; date = new Date(); creationDate = new Date(); authorReference = new DocumentReference("wiki", "space", "author"); authorString = serializer.serialize(authorReference); authorDisplay = "Au Thor"; creatorReference = new DocumentReference("wiki", "space", "Creator"); creatorString = serializer.serialize(creatorReference); creatorDisplay = "Crea Tor"; // Mock mockContext = mock(XWikiContext.class); Execution mockExecution = mocker.getInstance(Execution.class); ExecutionContext mockExecutionContext = new ExecutionContext(); mockExecutionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, mockContext); when(mockExecution.getContext()).thenReturn(mockExecutionContext); mockXWiki = mock(XWiki.class); mockDocument = mock(XWikiDocument.class); when(mockContext.getWiki()).thenReturn(mockXWiki); when(mockXWiki.getDocument(documentReference, mockContext)).thenReturn(mockDocument); when(mockDocument.getRealLanguage()).thenReturn(language); when(mockDocument.getTranslatedDocument(any(String.class), eq(mockContext))) .thenReturn(mockDocument); mockDab = mocker.getInstance(DocumentAccessBridge.class); when(mockDab.getDocument(documentReference)).thenReturn(mockDocument); BlockRenderer mockPlainRenderer = mocker.getInstance(BlockRenderer.class, "plain/1.0"); doAnswer( new Answer<Object>() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); WikiPrinter printer = (WikiPrinter) args[1]; printer.print(renderedContent); return null; } }) .when(mockPlainRenderer) .render(any(Block.class), any(WikiPrinter.class)); when(mockDocument.getRenderedTitle(any(Syntax.class), eq(mockContext))).thenReturn(title); when(mockDocument.getVersion()).thenReturn(version); when(mockDocument.getAuthorReference()).thenReturn(authorReference); when(mockXWiki.getUserName(authorString, null, false, mockContext)).thenReturn(authorDisplay); when(mockDocument.getCreatorReference()).thenReturn(creatorReference); when(mockXWiki.getUserName(creatorString, null, false, mockContext)).thenReturn(creatorDisplay); when(mockDocument.getCreationDate()).thenReturn(creationDate); when(mockDocument.getContentUpdateDate()).thenReturn(date); when(mockDocument.isHidden()).thenReturn(hidden); }