diff --git a/CHANGES.md b/CHANGES.md index a750467c8..d3e50f336 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ ### Behaviour changes worth reading before upgrading +- **Templates see only published entries when they look one up by anchor.** + `$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. - **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/WeblogWrapper.java b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogWrapper.java index d8341c9ba..390ecdaff 100644 --- a/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogWrapper.java +++ b/app/src/main/java/org/apache/roller/weblogger/pojos/wrapper/WeblogWrapper.java @@ -31,6 +31,7 @@ import java.util.stream.Collectors; import org.apache.roller.weblogger.pojos.ThemeTemplate.ComponentType; import org.apache.roller.weblogger.pojos.Weblog; +import org.apache.roller.weblogger.pojos.WeblogEntry; /** @@ -273,8 +274,16 @@ public String getAbsoluteURL() { } + /** + * The published entry with this anchor, or null. Templates never see + * draft, pending or scheduled entries through this lookup. + */ public WeblogEntryWrapper getWeblogEntry(String anchor) { - return WeblogEntryWrapper.wrap(this.pojo.getWeblogEntry(anchor), urlStrategy); + WeblogEntry entry = this.pojo.getWeblogEntry(anchor); + if (entry == null || !entry.isPublished()) { + return null; + } + return WeblogEntryWrapper.wrap(entry, urlStrategy); } diff --git a/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogWrapperEntryLookupTest.java b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogWrapperEntryLookupTest.java new file mode 100644 index 000000000..43567c2eb --- /dev/null +++ b/app/src/test/java/org/apache/roller/weblogger/pojos/wrapper/WeblogWrapperEntryLookupTest.java @@ -0,0 +1,65 @@ +/* + * 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 org.apache.roller.weblogger.pojos.Weblog; +import org.apache.roller.weblogger.pojos.WeblogEntry; +import org.apache.roller.weblogger.pojos.WeblogEntry.PubStatus; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Templates look up entries by anchor through the weblog wrapper. Only + * published entries come back. + */ +public class WeblogWrapperEntryLookupTest { + + private static WeblogWrapper weblogWithEntry(PubStatus status) { + WeblogEntry entry = new WeblogEntry(); + entry.setAnchor("the-anchor"); + entry.setStatus(status); + Weblog weblog = mock(Weblog.class); + when(weblog.getWeblogEntry("the-anchor")).thenReturn(entry); + return WeblogWrapper.wrap(weblog, null); + } + + @Test + public void publishedEntryIsReturned() { + WeblogEntryWrapper found = weblogWithEntry(PubStatus.PUBLISHED).getWeblogEntry("the-anchor"); + assertNotNull(found); + assertEquals("the-anchor", found.getAnchor()); + } + + @Test + public void unpublishedEntriesAreNotReturned() { + for (PubStatus status : new PubStatus[] {PubStatus.DRAFT, PubStatus.PENDING, PubStatus.SCHEDULED}) { + assertNull(weblogWithEntry(status).getWeblogEntry("the-anchor"), status.name()); + } + } + + @Test + public void missingEntryIsNull() { + assertNull(weblogWithEntry(PubStatus.PUBLISHED).getWeblogEntry("other")); + } +}