diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractReferenceEntityReferenceResolver.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractReferenceEntityReferenceResolver.java index 0ed295203a8b..1e7d5f235926 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractReferenceEntityReferenceResolver.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractReferenceEntityReferenceResolver.java @@ -102,7 +102,7 @@ && isParentTypeAndAllowedTypeNotMatching(types, reference.getParentType())) { EntityReference newReference = resolveDefaultReference(reference.getParentType(), parameters); normalizedReference = normalizedReference.appendParent(newReference); reference = newReference; - } else if (reference.getParent() != null && (types.isEmpty() || reference.getParentType() == null)) { + } else if (reference.getParent() != null && types.isEmpty()) { // There's a parent but no one is allowed throw new InvalidEntityReferenceException(); } else { diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractStringEntityReferenceResolver.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractStringEntityReferenceResolver.java index 1ace1b006f94..3220691e2cce 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractStringEntityReferenceResolver.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractStringEntityReferenceResolver.java @@ -406,6 +406,13 @@ private EntityReference appendNewReference(EntityReference reference, EntityRefe } else { return newReference; } + } else if (reference != null) { + EntityReference root = reference.getRoot(); + if (root.getType() == EntityType.SPACE) { + EntityReference newRoot = + new EntityReference(root, Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.SPACE)); + return (reference == root) ? newRoot : reference.replaceParent(root, newRoot); + } } return reference; diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/RelativeStringEntityReferenceResolverTest.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/RelativeStringEntityReferenceResolverTest.java index 92c918597397..03a475192c2d 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/RelativeStringEntityReferenceResolverTest.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/RelativeStringEntityReferenceResolverTest.java @@ -39,20 +39,22 @@ @ComponentList({ DefaultSymbolScheme.class }) -public class RelativeStringEntityReferenceResolverTest +class RelativeStringEntityReferenceResolverTest { @InjectMockComponents private RelativeStringEntityReferenceResolver resolver; @Test - public void resolveDocumentReference() + void resolveDocumentReference() { EntityReference reference = this.resolver.resolve("", EntityType.DOCUMENT); assertNull(reference); reference = this.resolver.resolve("space.page", EntityType.DOCUMENT); assertNull(reference.extractReference(EntityType.WIKI)); - assertEquals("space", reference.extractReference(EntityType.SPACE).getName()); + EntityReference spaceReference = reference.extractReference(EntityType.SPACE); + assertEquals("space", spaceReference.getName()); + assertEquals(EntityType.SPACE, spaceReference.getParentType()); assertEquals("page", reference.getName()); reference = this.resolver.resolve("wiki:space.page", EntityType.DOCUMENT); @@ -62,7 +64,7 @@ public void resolveDocumentReference() } @Test - public void resolveDocumentReferenceWithBaseReference() + void resolveDocumentReferenceWithBaseReference() { EntityReference reference = this.resolver.resolve("", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE)); diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/converter/EntityReferenceConverterTest.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/converter/EntityReferenceConverterTest.java index b2ddbc075157..745b399621da 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/converter/EntityReferenceConverterTest.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/converter/EntityReferenceConverterTest.java @@ -21,6 +21,8 @@ import javax.inject.Inject; +import java.util.Map; + import org.junit.jupiter.api.Test; import org.xwiki.model.EntityType; import org.xwiki.model.reference.EntityReference; @@ -57,7 +59,9 @@ void convertDocumentFromString() new EntityReference("wiki", EntityType.WIKI))); assertEquals(reference, this.converterManager.convert(EntityReference.class, "document:wiki:space.page")); - reference = new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE)); + reference = new EntityReference("page", EntityType.DOCUMENT, + new EntityReference("space", EntityType.SPACE, + Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.SPACE))); assertEquals(reference, this.converterManager.convert(EntityReference.class, "document:space.page")); assertEquals(reference, this.converterManager.convert(EntityReference.class, "space.page")); @@ -74,7 +78,8 @@ void convertSpaceFromString() reference = new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)); assertEquals(reference, this.converterManager.convert(EntityReference.class, "space:wiki:space")); - reference = new EntityReference("space", EntityType.SPACE); + reference = new EntityReference("space", EntityType.SPACE, Map.of(EntityReference.PARENT_TYPE_PARAMETER, + EntityType.SPACE)); assertEquals(reference, this.converterManager.convert(EntityReference.class, "space:space")); } diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/reference/EntityReferenceSetTest.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/reference/EntityReferenceSetTest.java index 6ebdad43794e..d4887e1e0805 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/reference/EntityReferenceSetTest.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/reference/EntityReferenceSetTest.java @@ -21,16 +21,16 @@ import java.util.Locale; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.xwiki.model.EntityType; import org.xwiki.model.internal.reference.DefaultSymbolScheme; import org.xwiki.model.internal.reference.RelativeStringEntityReferenceResolver; import org.xwiki.test.annotation.ComponentList; -import org.xwiki.test.mockito.MockitoComponentMockingRule; +import org.xwiki.test.junit5.mockito.ComponentTest; +import org.xwiki.test.junit5.mockito.InjectMockComponents; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Validate {@link EntityReferenceSet}. @@ -40,11 +40,11 @@ @ComponentList({ DefaultSymbolScheme.class }) -public class EntityReferenceSetTest +@ComponentTest +class EntityReferenceSetTest { - @Rule - public MockitoComponentMockingRule resolverMocker = - new MockitoComponentMockingRule<>(RelativeStringEntityReferenceResolver.class); + @InjectMockComponents + private RelativeStringEntityReferenceResolver resolver; private EntityReferenceSet set = new EntityReferenceSet(); @@ -60,12 +60,12 @@ private void assertNotMatches(EntityReference reference) private void assertMatches(String reference, EntityType type) throws Exception { - assertMatches(this.resolverMocker.getComponentUnderTest().resolve(reference, type)); + assertMatches(this.resolver.resolve(reference, type)); } private void assertNotMatches(String reference, EntityType type) throws Exception { - assertNotMatches(this.resolverMocker.getComponentUnderTest().resolve(reference, type)); + assertNotMatches(this.resolver.resolve(reference, type)); } private void assertMatchesWiki(String reference) throws Exception @@ -107,7 +107,7 @@ private void includes(EntityReference reference) private void includes(String reference, EntityType type) throws Exception { - includes(this.resolverMocker.getComponentUnderTest().resolve(reference, type)); + includes(this.resolver.resolve(reference, type)); } private void includesWiki(String reference) throws Exception @@ -134,7 +134,7 @@ private void excludes(EntityReference reference) private void excludes(String reference, EntityType type) throws Exception { - excludes(this.resolverMocker.getComponentUnderTest().resolve(reference, type)); + excludes(this.resolver.resolve(reference, type)); } private void excludesWiki(String reference) throws Exception @@ -155,7 +155,7 @@ private void excludesDocument(String reference) throws Exception // Tests @Test - public void includeWiki() throws Exception + void includeWiki() throws Exception { includesWiki("wiki"); @@ -175,7 +175,7 @@ public void includeWiki() throws Exception } @Test - public void includeSpace() throws Exception + void includeSpace() throws Exception { includesSpace("wiki:space"); @@ -194,7 +194,7 @@ public void includeSpace() throws Exception } @Test - public void includeNestedSpace() throws Exception + void includeNestedSpace() throws Exception { includesSpace("wiki:space.nested"); @@ -218,7 +218,7 @@ public void includeNestedSpace() throws Exception } @Test - public void includePartialOnlySpace() throws Exception + void includePartialOnlySpace() throws Exception { includesSpace("space"); @@ -230,7 +230,7 @@ public void includePartialOnlySpace() throws Exception } @Test - public void includeDocument() throws Exception + void includeDocument() throws Exception { includesDocument("wiki:space.document"); @@ -242,7 +242,7 @@ public void includeDocument() throws Exception } @Test - public void includeLocalDocumentLocale() + void includeLocalDocumentLocale() { includes(new LocalDocumentReference("space", "document", Locale.ROOT)); @@ -261,7 +261,7 @@ public void includeLocalDocumentLocale() } @Test - public void includeDocumentInNestedSpace() throws Exception + void includeDocumentInNestedSpace() throws Exception { includesDocument("wiki:space.nestedspace.document"); @@ -276,7 +276,7 @@ public void includeDocumentInNestedSpace() throws Exception } @Test - public void includeDocumentsInNestedSpacesWithShortAfterLong() throws Exception + void includeDocumentsInNestedSpacesWithShortAfterLong() throws Exception { includesDocument("wiki:space.nestedspace.document"); includesDocument("wiki:space.document"); @@ -292,7 +292,7 @@ public void includeDocumentsInNestedSpacesWithShortAfterLong() throws Exception } @Test - public void includeDocumentsInNestedSpacesWithLongAfterShort() throws Exception + void includeDocumentsInNestedSpacesWithLongAfterShort() throws Exception { includesDocument("wiki:space.document"); includesDocument("wiki:space.nestedspace.document"); @@ -308,7 +308,7 @@ public void includeDocumentsInNestedSpacesWithLongAfterShort() throws Exception } @Test - public void excludeWiki() throws Exception + void excludeWiki() throws Exception { excludesWiki("wiki"); @@ -328,7 +328,7 @@ public void excludeWiki() throws Exception } @Test - public void excludeSpace() throws Exception + void excludeSpace() throws Exception { excludesSpace("wiki:space"); @@ -340,7 +340,7 @@ public void excludeSpace() throws Exception } @Test - public void excludeNestedSpace() throws Exception + void excludeNestedSpace() throws Exception { excludesSpace("wiki:space.nested"); @@ -355,10 +355,11 @@ public void excludeNestedSpace() throws Exception } @Test - public void excludePartial() throws Exception + void excludePartial() throws Exception { excludesSpace("space"); + // FIXME: Assertion is false because the excluded space contains a parentType parameter since it's relative assertNotMatches(new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI))); assertNotMatches(new EntityReference("space", EntityType.SPACE)); @@ -367,7 +368,7 @@ public void excludePartial() throws Exception } @Test - public void includeLocale() + void includeLocale() { includes(new DocumentReference("wiki", "space", "document", Locale.ENGLISH)); @@ -387,7 +388,7 @@ public void includeLocale() } @Test - public void excludeLocale() + void excludeLocale() { excludes(new DocumentReference("wiki", "space", "document", Locale.ENGLISH));