This is an automated email from the ASF dual-hosted git repository. snoopdave pushed a commit to branch template-lookups-published-only-6.1.x in repository https://gitbox.apache.org/repos/asf/roller.git
commit e72d2ed29c6a622f974a8b8236cca41185631c7b Author: David M. Johnson <[email protected]> AuthorDate: Tue Nov 24 21:13:21 2026 -0500 Give templates only published entries from a category and only approved comments from an entry A category's retrieveWeblogEntries and an entry's getComments return the same as their default forms whatever a template passes, as the anchor lookup already does. --- CHANGES.md | 6 +- .../pojos/wrapper/WeblogCategoryWrapper.java | 6 +- .../pojos/wrapper/WeblogEntryWrapper.java | 7 ++- .../wrapper/TemplateLookupsPublishedOnlyTest.java | 68 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d3e50f336..2f1b70c7e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,10 +4,12 @@ ### Behaviour changes worth reading before upgrading -- **Templates see only published entries when they look one up by anchor.** +- **Templates see only published entries and approved comments.** `$weblog.getWeblogEntry(anchor)` in a custom template, and the same call on a weblog reached through `$site`, now returns null for draft, pending and - scheduled entries. + scheduled entries. A category's `retrieveWeblogEntries(...)` returns only + published entries, and an entry's `getComments(...)` only approved comments, + whatever arguments the template passes. - **Only global admins manage Admin on the aggregated front-page weblog.** With `site.frontpage.weblog.aggregated=true`, that weblog's templates can read the whole site, so granting, removing or inviting an Admin there now diff --git a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogCategoryWrapper.java b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogCategoryWrapper.java index 4f99ad57f..48c4be22e 100644 --- a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogCategoryWrapper.java +++ b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogCategoryWrapper.java @@ -77,8 +77,12 @@ public final class WeblogCategoryWrapper { return WeblogWrapper.wrap(this.pojo.getWeblog(), urlStrategy); } + /** + * The category's published entries. Templates never see drafts, pending + * or scheduled entries, so the argument is kept only for compatibility. + */ public List<WeblogEntryWrapper> retrieveWeblogEntries(boolean publishedOnly) throws WebloggerException { - return this.pojo.retrieveWeblogEntries(publishedOnly).stream() + return this.pojo.retrieveWeblogEntries(true).stream() .map(entry -> WeblogEntryWrapper.wrap(entry, urlStrategy)) .collect(Collectors.toList()); } diff --git a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryWrapper.java b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryWrapper.java index 026001a64..212fa652e 100644 --- a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryWrapper.java +++ b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogEntryWrapper.java @@ -215,8 +215,13 @@ public final class WeblogEntryWrapper { } + /** + * The entry's approved comments, the same as {@link #getComments()}. + * Templates never see pending or spam comments, so the arguments are kept + * only for compatibility. + */ public List<WeblogEntryCommentWrapper> getComments(boolean ignoreSpam, boolean approvedOnly) { - return this.pojo.getComments(ignoreSpam, approvedOnly).stream() + return this.pojo.getComments(true, true).stream() .map(comment -> WeblogEntryCommentWrapper.wrap(comment, urlStrategy)) .collect(Collectors.toList()); } diff --git a/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/TemplateLookupsPublishedOnlyTest.java b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/TemplateLookupsPublishedOnlyTest.java new file mode 100644 index 000000000..2deb6dec3 --- /dev/null +++ b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/TemplateLookupsPublishedOnlyTest.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. The ASF licenses this file to You + * under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. For additional information regarding + * copyright in this work, please see the NOTICE file in the top level + * directory of this distribution. + */ + +package org.apache.roller.weblogger.pojos.wrapper; + +import java.util.Collections; +import org.apache.roller.weblogger.pojos.WeblogCategory; +import org.apache.roller.weblogger.pojos.WeblogEntry; +import org.apache.roller.weblogger.pojos.WeblogEntryComment; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Templates list a category's entries and an entry's comments through the + * wrappers. Whatever a template asks for, it gets only published entries and + * approved comments. + */ +public class TemplateLookupsPublishedOnlyTest { + + @Test + public void categoryEntriesArePublishedOnlyWhateverTheTemplateAsks() throws Exception { + WeblogCategory category = mock(WeblogCategory.class); + WeblogEntry published = new WeblogEntry(); + published.setAnchor("published"); + when(category.retrieveWeblogEntries(true)).thenReturn(Collections.singletonList(published)); + + WeblogCategoryWrapper wrapper = WeblogCategoryWrapper.wrap(category, null); + assertEquals(1, wrapper.retrieveWeblogEntries(false).size()); + assertEquals(1, wrapper.retrieveWeblogEntries(true).size()); + verify(category, never()).retrieveWeblogEntries(false); + } + + @Test + public void entryCommentsAreApprovedOnlyWhateverTheTemplateAsks() { + WeblogEntry entry = mock(WeblogEntry.class); + WeblogEntryComment approved = new WeblogEntryComment(); + when(entry.getComments(true, true)).thenReturn(Collections.singletonList(approved)); + + WeblogEntryWrapper wrapper = WeblogEntryWrapper.wrap(entry, null); + assertEquals(1, wrapper.getComments(false, false).size()); + assertEquals(1, wrapper.getComments(true, false).size()); + assertEquals(1, wrapper.getComments(false, true).size()); + verify(entry, never()).getComments(anyBoolean(), eq(false)); + verify(entry, never()).getComments(eq(false), anyBoolean()); + } +}
