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

Cherrypick xwiki22681 #3743

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
<version>${commons.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-model-api</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-tool-test-jmock</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.model.script;

import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReferenceValueProvider;

/**
* Legacy version of {@link ModelScriptService}, holding deprecated methods.
*
* @version $Id$
* @since 17.0.0RC1
*/
@Deprecated(since = "17.0.0RC1")
@Component
@Named("model")
@Singleton
public class LegacyModelScriptService extends ModelScriptService
{
/**
* Get the value configured for a specific entity type, like the space name or wiki name. This doesn't return a
* proper entity reference, but just the string value that should be used for that type of entity.
*
* @param type the target entity type; from Velocity it's enough to use a string with the uppercase name of the
* entity, like {@code 'SPACE'}
* @param hint the hint of the value provider to use (valid hints are for example "default", "current" and
* "currentmixed")
* @return the configured value for the requested entity type, for example "Main" for the default space or "WebHome"
* for the default space homepage
* @since 4.3M1
* @deprecated since 7.2M1, use {@link #getEntityReference(EntityType, String)}
*/
@Deprecated
public String getEntityReferenceValue(EntityType type, String hint)
{
if (type == null) {
return null;
}

try {
EntityReferenceValueProvider provider =
this.componentManager.getInstance(EntityReferenceValueProvider.class, hint);
return provider.getDefaultValue(type);
} catch (ComponentLookupException ex) {
return null;
}
}

/**
* Get the current value for a specific entity type, like the current space or wiki name. This doesn't return a
* proper entity reference, but just the string value that should be used for that type of entity.
*
* @param type the target entity type; from Velocity it's enough to use a string with the uppercase name of the
* entity, like {@code 'SPACE'}
* @return the current value for the requested entity type
* @since 4.3M1
* @deprecated since 7.4.1/8.0M1, use {@link #getEntityReference(EntityType)}
*/
@Deprecated
public String getEntityReferenceValue(EntityType type)
{
return getEntityReferenceValue(type, DEFAULT_RESOLVER_HINT);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.xwiki.model.internal.reference.DefaultEntityReferenceValueProvider
org.xwiki.model.internal.reference.DeprecatedDefaultReferenceDocumentReferenceResolver
org.xwiki.model.internal.reference.DeprecatedDefaultReferenceDocumentReferenceResolver2
org.xwiki.model.internal.reference.DeprecatedDefaultReferenceEntityReferenceResolver
Expand All @@ -19,3 +20,4 @@ org.xwiki.model.internal.reference.DeprecatedLocalReferenceEntityReferenceSerial
org.xwiki.model.internal.reference.DeprecatedLocalReferenceEntityReferenceSerializer2
org.xwiki.model.internal.reference.DeprecatedLocalStringEntityReferenceSerializer
org.xwiki.model.internal.reference.DeprecatedRelativeStringEntityReferenceResolver
500:org.xwiki.model.script.LegacyModelScriptService
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,24 @@ public class DefaultEntityReferenceValueProviderTest implements TestConstants
@BeforeEach
public void beforeEach()
{
when(this.configuration.getDefaultReferenceValue(EntityType.SPACE)).thenReturn(DEFAULT_SPACE);
when(this.configuration.getDefaultReferenceValue(EntityType.WIKI)).thenReturn(DEFAULT_WIKI);
when(this.configuration.getDefaultReferenceValue(EntityType.DOCUMENT)).thenReturn(DEFAULT_DOCUMENT);
when(this.configuration.getDefaultReferenceValue(EntityType.ATTACHMENT)).thenReturn(DEFAULT_ATTACHMENT);
when(this.configuration.getDefaultReferenceValue(EntityType.PAGE)).thenReturn(DEFAULT_PAGE);
when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_ATTACHMENT)).thenReturn(DEFAULT_ATTACHMENT);
when(this.configuration.getDefaultReferenceValue(EntityType.SPACE)).thenReturn(TestConstants.DEFAULT_SPACE);
when(this.configuration.getDefaultReferenceValue(EntityType.WIKI)).thenReturn(TestConstants.DEFAULT_WIKI);
when(this.configuration.getDefaultReferenceValue(EntityType.DOCUMENT)).thenReturn(TestConstants.DEFAULT_DOCUMENT);
when(this.configuration.getDefaultReferenceValue(EntityType.ATTACHMENT)).thenReturn(
TestConstants.DEFAULT_ATTACHMENT);
when(this.configuration.getDefaultReferenceValue(EntityType.PAGE)).thenReturn(TestConstants.DEFAULT_PAGE);
when(this.configuration.getDefaultReferenceValue(EntityType.PAGE_ATTACHMENT)).thenReturn(
TestConstants.DEFAULT_ATTACHMENT);
}

@Test
public void testGetDefaultValue()
{
assertEquals(DEFAULT_WIKI, this.provider.getDefaultValue(EntityType.WIKI));
assertEquals(DEFAULT_SPACE, this.provider.getDefaultValue(EntityType.SPACE));
assertEquals(DEFAULT_DOCUMENT, this.provider.getDefaultValue(EntityType.DOCUMENT));
assertEquals(DEFAULT_ATTACHMENT, this.provider.getDefaultValue(EntityType.ATTACHMENT));
assertEquals(DEFAULT_PAGE, this.provider.getDefaultValue(EntityType.PAGE));
assertEquals(DEFAULT_ATTACHMENT, this.provider.getDefaultValue(EntityType.PAGE_ATTACHMENT));
assertEquals(TestConstants.DEFAULT_WIKI, this.provider.getDefaultValue(EntityType.WIKI));
assertEquals(TestConstants.DEFAULT_SPACE, this.provider.getDefaultValue(EntityType.SPACE));
assertEquals(TestConstants.DEFAULT_DOCUMENT, this.provider.getDefaultValue(EntityType.DOCUMENT));
assertEquals(TestConstants.DEFAULT_ATTACHMENT, this.provider.getDefaultValue(EntityType.ATTACHMENT));
assertEquals(TestConstants.DEFAULT_PAGE, this.provider.getDefaultValue(EntityType.PAGE));
assertEquals(TestConstants.DEFAULT_ATTACHMENT, this.provider.getDefaultValue(EntityType.PAGE_ATTACHMENT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.model.script;

import javax.inject.Named;

import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReferenceValueProvider;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

@ComponentTest
class LegacyModelScriptServiceTest
{
@InjectMockComponents
private LegacyModelScriptService service;

@MockComponent
@Named("context")
private ComponentManager componentManager;

@Mock
private EntityReferenceValueProvider valueProvider;

@Test
void getEntityReferenceValue() throws Exception
{
when(this.componentManager.getInstance(EntityReferenceValueProvider.class, "current"))
.thenReturn(this.valueProvider);
when(this.valueProvider.getDefaultValue(EntityType.WIKI)).thenReturn("somewiki");

assertEquals("somewiki", this.service.getEntityReferenceValue(EntityType.WIKI));
}

@Test
void getEntityReferenceValueWithInvalidHint() throws Exception
{
when(this.componentManager.getInstance(EntityReferenceValueProvider.class, "invalid"))
.thenThrow(new ComponentLookupException("error"));

assertNull(this.service.getEntityReferenceValue(EntityType.WIKI, "invalid"));
}

@Test
void getEntityReferenceValueWithNullType() throws Exception
{
assertNull(this.service.getEntityReferenceValue(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
</exclusion>
</exclusions>
</dependency>
<!-- Legacy oldcore needs some legacy classes from legacy model -->
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-legacy-model-api</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Legacy oldcore needs some legacy classes from legacy rendering -->
<dependency>
Expand All @@ -78,6 +84,7 @@
<version>${rendering.version}</version>
</dependency>


<!-- Build tools -->
<!-- Needed for backward compatibility Aspects -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
org.xwiki.legacy.internal.oldcore.notification.LegacyNotificationDispatcher
com.xpn.xwiki.internal.mandatory.SheetClassDocumentInitializer
com.xpn.xwiki.internal.model.reference.CurrentEntityReferenceValueProvider
com.xpn.xwiki.internal.model.reference.CurrentMixedEntityReferenceValueProvider
com.xpn.xwiki.internal.model.reference.DeprecatedCompactStringEntityReferenceSerializer
com.xpn.xwiki.internal.model.reference.DeprecatedCompactWikiStringEntityReferenceSerializer
com.xpn.xwiki.internal.model.reference.DeprecatedCurrentMixedReferenceDocumentReferenceResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<xwiki.jacoco.instructionRatio>0.79</xwiki.jacoco.instructionRatio>
<!-- Define the old name of this module for extensions using it -->
<xwiki.extension.features>org.xwiki.platform:xwiki-platform-model</xwiki.extension.features>
<!-- revapi call skipped since there's a legacy module -->
<xwiki.revapi.skip>true</xwiki.revapi.skip>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -99,6 +101,23 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>**/TestConstants.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ private EntityReference normalizeReference(EntityReference referenceToResolve, O
EntityReference reference = normalizedReference;
while (reference != null) {
List<EntityType> types = reference.getType().getAllowedParents();
if (reference.getParent() != null && !types.isEmpty() && !types.contains(reference.getParent().getType())) {
if (reference.getParent() != null
&& isParentTypeAndAllowedTypeNotMatching(types, reference.getParentType())) {
// The parent reference isn't the allowed parent: insert an allowed reference
EntityReference newReference =
resolveDefaultReference(types.get(0), parameters).appendParent(reference.getParent());
normalizedReference = normalizedReference.replaceParent(reference.getParent(), newReference);
reference = newReference;
} else if (reference.getParent() == null && !types.isEmpty()) {
} else if (reference.getParent() == null && reference.getParentType() != null) {
// The top reference isn't the allowed top level reference, add a parent reference
EntityReference newReference = resolveDefaultReference(types.get(0), parameters);
EntityReference newReference = resolveDefaultReference(reference.getParentType(), parameters);
normalizedReference = normalizedReference.appendParent(newReference);
reference = newReference;
} else if (reference.getParent() != null && types.isEmpty()) {
Expand All @@ -112,4 +113,9 @@ private EntityReference normalizeReference(EntityReference referenceToResolve, O

return normalizedReference;
}

private boolean isParentTypeAndAllowedTypeNotMatching(List<EntityType> allowedTypes, EntityType parentType)
{
return !allowedTypes.isEmpty() && parentType != null && !allowedTypes.contains(parentType);
}
}
Loading
Loading