Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPD-42706 #6133

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if (portletTitleBasedNavigation) {
<ul class="middle navbar-nav">
<li class="nav-item">
<span class="text-secondary">
<liferay-ui:message arguments="<%= new String[] {HtmlUtil.escape(entry.getUserName()), LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - entry.getModifiedDate().getTime(), true)} %>" key="x-modified-x-ago" translateArguments="<%= false %>" />
<liferay-ui:message arguments="<%= LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - entry.getModifiedDate().getTime(), true) %>" key="modified-x-ago" translateArguments="<%= false %>" />
</span>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ RepositoryBrowserTagDisplayContext repositoryBrowserTagDisplayContext = (Reposit
</c:choose>

<span class="text-default">
<liferay-ui:message arguments="<%= new String[] {repositoryEntry.getUserName(), repositoryBrowserTagDisplayContext.getRepositoryEntryModifiedDateDescription(repositoryEntry)} %>" key="x-modified-x-ago" />
<liferay-ui:message arguments="<%= repositoryBrowserTagDisplayContext.getRepositoryEntryModifiedDateDescription(repositoryEntry) %>" key="modified-x-ago" />
</span>
</liferay-ui:search-container-column-text>

Expand Down Expand Up @@ -117,7 +117,7 @@ RepositoryBrowserTagDisplayContext repositoryBrowserTagDisplayContext = (Reposit
cssClass="table-cell-expand-smaller table-cell-minw-150"
name="modified-date"
>
<liferay-ui:message arguments="<%= new String[] {repositoryEntry.getUserName(), repositoryBrowserTagDisplayContext.getRepositoryEntryModifiedDateDescription(repositoryEntry)} %>" key="x-modified-x-ago" />
<liferay-ui:message arguments="<%= repositoryBrowserTagDisplayContext.getRepositoryEntryModifiedDateDescription(repositoryEntry) %>" key="modified-x-ago" />
</liferay-ui:search-container-column-text>

<liferay-ui:search-container-column-text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Folder folder = (Folder)row.getObject();
folder = folder.toEscapedModel();

Date modifiedDate = folder.getModifiedDate();

String modifiedDateDescription = LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true);
%>

<h2 class="h5">
Expand All @@ -40,14 +38,7 @@ String modifiedDateDescription = LanguageUtil.getTimeDescription(request, System
</h2>

<span>
<c:choose>
<c:when test="<%= Validator.isNull(folder.getUserName()) %>">
<liferay-ui:message arguments="<%= modifiedDateDescription %>" key="modified-x-ago" />
</c:when>
<c:otherwise>
<liferay-ui:message arguments="<%= new String[] {folder.getUserName(), modifiedDateDescription} %>" key="x-modified-x-ago" />
</c:otherwise>
</c:choose>
<liferay-ui:message arguments="<%= LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true) %>" key="modified-x-ago" />
</span>
<span>
<%= DLUtil.getAbsolutePath(liferayPortletRequest, dlAdminDisplayContext.getRootFolderId(), folder.getParentFolderId()).replace(StringPool.RAQUO_CHAR, StringPool.GREATER_THAN) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ KBArticleViewDisplayContext kbArticleViewDisplayContext = new KBArticleViewDispl

<%
Date modifiedDate = kbFolder.getModifiedDate();

String modifiedDateDescription = LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true);
%>

<liferay-ui:message arguments="<%= new String[] {HtmlUtil.escape(kbFolder.getUserName()), modifiedDateDescription} %>" key="x-modified-x-ago" />
<liferay-ui:message arguments="<%= LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true) %>" key="modified-x-ago" />
</span>
</liferay-ui:search-container-column-text>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ViewKBTemplatesDisplayContext viewKBTemplatesDisplayContext = (ViewKBTemplatesDi
</h2>

<span class="text-default">
<liferay-ui:message arguments="<%= new String[] {HtmlUtil.escape(kbTemplate.getUserName()), viewKBTemplatesDisplayContext.getKBTemplateModifiedDateDescription(kbTemplate)} %>" key="x-modified-x-ago" />
<liferay-ui:message arguments="<%= viewKBTemplatesDisplayContext.getKBTemplateModifiedDateDescription(kbTemplate) %>" key="modified-x-ago" />
</span>
</liferay-ui:search-container-column-text>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.message.boards.web.internal.display.context;

import com.liferay.message.boards.model.MBMessage;
import com.liferay.portal.kernel.language.LanguageUtil;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

/**
* @author Jürgen KAppler
*/
public class MBDisplayContext {

public MBDisplayContext(HttpServletRequest httpServletRequest) {
_httpServletRequest = httpServletRequest;
}

public String getModifiedLabel(MBMessage message) {
String messageUserName = "anonymous";

if (!message.isAnonymous()) {
messageUserName = message.getStatusByUserName();
}

Date modifiedDate = message.getModifiedDate();

String modifiedDateDescription = LanguageUtil.getTimeDescription(
_httpServletRequest,
System.currentTimeMillis() - modifiedDate.getTime(), true);

return LanguageUtil.format(
_httpServletRequest, "x-modified-x-ago",
new String[] {messageUserName, modifiedDateDescription});
}

private final HttpServletRequest _httpServletRequest;

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ page import="com.liferay.message.boards.web.internal.dao.search.MBResultRowSplit
page import="com.liferay.message.boards.web.internal.display.MBCategoryDisplay" %><%@
page import="com.liferay.message.boards.web.internal.display.context.MBBannedUsersManagementToolbarDisplayContext" %><%@
page import="com.liferay.message.boards.web.internal.display.context.MBConfigurationDisplayContext" %><%@
page import="com.liferay.message.boards.web.internal.display.context.MBDisplayContext" %><%@
page import="com.liferay.message.boards.web.internal.display.context.MBEditMessageDisplayContext" %><%@
page import="com.liferay.message.boards.web.internal.display.context.MBEntriesManagementToolbarDisplayContext" %><%@
page import="com.liferay.message.boards.web.internal.display.context.MBNavigationDisplayContext" %><%@
Expand Down Expand Up @@ -231,6 +232,8 @@ Format dateFormat = FastDateFormatFactoryUtil.getDate(locale, timeZone);
Format dateTimeFormat = FastDateFormatFactoryUtil.getDateTime(locale, timeZone);

NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);

MBDisplayContext mbDisplayContext = new MBDisplayContext(request);
%>

<%@ include file="/message_boards/init-ext.jsp" %>
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,8 @@

<c:choose>
<c:when test="<%= (message != null) && (thread.getMessageCount() == 1) %>">

<%
String messageUserName = "anonymous";

if (!message.isAnonymous()) {
messageUserName = message.getUserName();
}

Date modifiedDate = message.getModifiedDate();

String modifiedDateDescription = LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true);
%>

<p class="list-group-subtext">
<liferay-ui:message arguments="<%= new String[] {messageUserName, modifiedDateDescription} %>" key="x-modified-x-ago" />
<%= HtmlUtil.escape(mbDisplayContext.getModifiedLabel(message)) %>
</p>
</c:when>
<c:otherwise>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,7 @@ User messageUser = UserLocalServiceUtil.fetchUser(message.getUserId());
>

<%
String messageUserName = "anonymous";

if (!message.isAnonymous()) {
messageUserName = message.getUserName();
}

Date modifiedDate = message.getModifiedDate();

String modifiedDateDescription = LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true);

String userDisplayText = LanguageUtil.format(request, "x-modified-x-ago", new Object[] {messageUserName, modifiedDateDescription});
String userDisplayText = mbDisplayContext.getModifiedLabel(message);
%>

<span class="message-user-display text-default" title="<%= HtmlUtil.escapeAttribute(userDisplayText) %>">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,8 @@ MBCategoryDisplay categoryDisplay = new MBCategoryDisplay(scopeGroupId, category

<c:choose>
<c:when test="<%= (message != null) && (thread.getMessageCount() == 1) %>">

<%
String messageUserName = "anonymous";

if (!message.isAnonymous()) {
messageUserName = message.getUserName();
}

Date modifiedDate = message.getModifiedDate();

String modifiedDateDescription = LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true);
%>

<div>
<liferay-ui:message arguments="<%= new String[] {messageUserName, modifiedDateDescription} %>" key="x-modified-x-ago" />
<%= HtmlUtil.escape(mbDisplayContext.getModifiedLabel(message)) %>
</div>
</c:when>
<c:otherwise>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.message.boards.web.internal.display.context;

import com.liferay.message.boards.model.MBMessage;
import com.liferay.portal.kernel.bean.BeanProperties;
import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
import com.liferay.portal.kernel.language.Language;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.test.rule.LiferayUnitTestRule;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import org.mockito.Mockito;

/**
* @author Jürgen Kappler
*/
public class MBDisplayContextTest {

@ClassRule
@Rule
public static final LiferayUnitTestRule liferayUnitTestRule =
LiferayUnitTestRule.INSTANCE;

@Before
public void setUp() {
BeanPropertiesUtil beanPropertiesUtil = new BeanPropertiesUtil();

beanPropertiesUtil.setBeanProperties(_beanProperties);

_setUpLanguageUtil();
}

@Test
public void testGetModifiedLabel() {
HttpServletRequest httpServletRequest = Mockito.mock(
HttpServletRequest.class);

Mockito.when(
httpServletRequest.getLocale()
).thenReturn(
LocaleUtil.US
);

MBDisplayContext mbDisplayContext = new MBDisplayContext(
httpServletRequest);

MBMessage mbMessage = Mockito.mock(MBMessage.class);

String userName = RandomTestUtil.randomString();
String statusByUserName = RandomTestUtil.randomString();

Mockito.when(
mbMessage.getModifiedDate()
).thenReturn(
new Date()
);

Mockito.when(
mbMessage.getUserName()
).thenReturn(
userName
);

Mockito.when(
mbMessage.getStatusByUserName()
).thenReturn(
statusByUserName
);

_testGetModifiedLabel(
false, statusByUserName, mbMessage, mbDisplayContext);
_testGetModifiedLabel(true, "anonymous", mbMessage, mbDisplayContext);
}

private void _setUpLanguageUtil() {
LanguageUtil languageUtil = new LanguageUtil();

Mockito.when(
_language.format(
Mockito.any(HttpServletRequest.class), Mockito.anyString(),
(String[])Mockito.any())
).thenAnswer(
invocation -> {
String[] values = invocation.getArgument(2);

return values[0];
}
);

Mockito.when(
_language.getTimeDescription(
Mockito.any(HttpServletRequest.class), Mockito.anyLong(),
Mockito.anyBoolean())
).thenReturn(
RandomTestUtil.randomString()
);

languageUtil.setLanguage(_language);
}

private void _testGetModifiedLabel(
boolean anonymous, String expectedModifiedLabel, MBMessage mbMessage,
MBDisplayContext mbDisplayContext) {

Mockito.when(
mbMessage.isAnonymous()
).thenReturn(
anonymous
);

Assert.assertEquals(
expectedModifiedLabel,
mbDisplayContext.getModifiedLabel(mbMessage));
}

private final BeanProperties _beanProperties = Mockito.mock(
BeanProperties.class);
private final Language _language = Mockito.mock(Language.class);

}