Search in sources :

Example 86 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class CurrentEntityReferenceProvider method getDefaultReference.

@Override
public EntityReference getDefaultReference(EntityType type) {
    EntityReference result = null;
    XWikiContext xcontext = this.xcontextProvider.get();
    if (xcontext != null) {
        if (type == EntityType.WIKI) {
            result = xcontext.getWikiReference();
        } else if (type == EntityType.SPACE) {
            XWikiDocument currentDoc = xcontext.getDoc();
            if (currentDoc != null) {
                SpaceReference spaceReference = currentDoc.getDocumentReference().getLastSpaceReference();
                // Keep only the spaces part
                result = spaceReference.removeParent(spaceReference.getWikiReference());
            }
        } else if (type == EntityType.DOCUMENT) {
            XWikiDocument currentDoc = xcontext.getDoc();
            if (currentDoc != null) {
                DocumentReference documentReference = currentDoc.getDocumentReference();
                // Keep only the document part
                result = documentReference.removeParent(documentReference.getLastSpaceReference());
            }
        }
    }
    if (result == null) {
        result = super.getDefaultReference(type);
    }
    return result;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 87 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class XWikiMockitoTest method getSpacePreference.

@Test
public void getSpacePreference() throws Exception {
    this.mocker.registerMockComponent(ConfigurationSource.class, "wiki");
    ConfigurationSource spaceConfiguration = this.mocker.registerMockComponent(ConfigurationSource.class, "space");
    when(this.xwikiCfgConfigurationSource.getProperty(any(), anyString())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArgument(1);
        }
    });
    WikiReference wikiReference = new WikiReference("wiki");
    SpaceReference space1Reference = new SpaceReference("space1", wikiReference);
    SpaceReference space2Reference = new SpaceReference("space2", space1Reference);
    // Without preferences and current doc
    assertEquals("", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals("defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals("defaultvalue", this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));
    // Without preferences but with current doc
    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));
    assertEquals("", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals("defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals("defaultvalue", this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));
    // With preferences
    final Map<String, Map<String, String>> spacesPreferences = new HashMap<>();
    Map<String, String> space1Preferences = new HashMap<>();
    space1Preferences.put("pref", "prefvalue1");
    space1Preferences.put("pref1", "pref1value1");
    Map<String, String> space2Preferences = new HashMap<>();
    space2Preferences.put("pref", "prefvalue2");
    space2Preferences.put("pref2", "pref2value2");
    spacesPreferences.put(space1Reference.getName(), space1Preferences);
    spacesPreferences.put(space2Reference.getName(), space2Preferences);
    when(spaceConfiguration.getProperty(any(), same(String.class))).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            if (context.getDoc() != null) {
                Map<String, String> spacePreferences = spacesPreferences.get(context.getDoc().getDocumentReference().getParent().getName());
                if (spacePreferences != null) {
                    return spacePreferences.get(invocation.getArgument(0));
                }
            }
            return null;
        }
    });
    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space1Reference)));
    assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref2", this.context));
    this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));
    assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", this.context));
    assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
    assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context));
    assertEquals("pref2value2", this.xwiki.getSpacePreference("pref2", this.context));
    assertEquals("", this.xwiki.getSpacePreference("nopref", space1Reference, this.context));
    assertEquals("defaultvalue", this.xwiki.getSpacePreference("nopref", space1Reference, "defaultvalue", this.context));
    assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", space1Reference, this.context));
    assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", space1Reference, "defaultvalue", this.context));
    assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", space1Reference, this.context));
    assertEquals("", this.xwiki.getSpacePreference("pref2", space1Reference, this.context));
    assertEquals("", this.xwiki.getSpacePreference("nopref", space2Reference, this.context));
    assertEquals("defaultvalue", this.xwiki.getSpacePreference("nopref", space2Reference, "defaultvalue", this.context));
    assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
    assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));
    assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", space2Reference, this.context));
    assertEquals("pref2value2", this.xwiki.getSpacePreference("pref2", space2Reference, this.context));
}
Also used : XWikiCfgConfigurationSource(com.xpn.xwiki.internal.XWikiCfgConfigurationSource) ConfigurationSource(org.xwiki.configuration.ConfigurationSource) HashMap(java.util.HashMap) SpaceReference(org.xwiki.model.reference.SpaceReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) InvocationOnMock(org.mockito.invocation.InvocationOnMock) WikiReference(org.xwiki.model.reference.WikiReference) Map(java.util.Map) HashMap(java.util.HashMap) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 88 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DatabaseDocumentIterator method next.

@Override
public Pair<DocumentReference, String> next() {
    Object[] result = getResults().get(index++);
    String localSpaceReference = (String) result[0];
    String name = (String) result[1];
    String locale = (String) result[2];
    String version = (String) result[3];
    SpaceReference spaceReference = new SpaceReference(this.explicitEntityReferenceResolver.resolve(localSpaceReference, EntityType.SPACE, new WikiReference(wiki)));
    DocumentReference documentReference = new DocumentReference(name, spaceReference);
    if (!StringUtils.isEmpty(locale)) {
        documentReference = new DocumentReference(documentReference, LocaleUtils.toLocale(locale));
    }
    return new ImmutablePair<DocumentReference, String>(documentReference, version);
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) SpaceReference(org.xwiki.model.reference.SpaceReference) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 89 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class CurrentAttachmentReferenceResolverTest method resolveTest.

@Test
public void resolveTest() throws Exception {
    // Mocks
    EntityReference entityReference = new EntityReference("hello.txt", EntityType.ATTACHMENT);
    AttachmentReference attachmentReference = new AttachmentReference("hello.txt", new DocumentReference("Document", new SpaceReference("Space", new WikiReference("wiki"))));
    when(entityReferenceResolver.resolve(entityReference, EntityType.ATTACHMENT)).thenReturn(attachmentReference);
    // Test
    AttachmentReference result = mocker.getComponentUnderTest().resolve(entityReference);
    // Verify
    assertEquals(attachmentReference, result);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 90 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class SolrIndexScriptServiceTest method openrationsOnMultipleReferencesOnTheSameWikiChecksRightsOnlyOnceForThatWiki.

@Test
public void openrationsOnMultipleReferencesOnTheSameWikiChecksRightsOnlyOnceForThatWiki() throws Exception {
    // References from the same wiki
    WikiReference wikiReference = new WikiReference("wiki");
    SpaceReference spaceReference = new SpaceReference("space", wikiReference);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "name");
    DocumentReference documentReference2 = new DocumentReference("wiki", "space", "name2");
    // Call
    this.service.index(Arrays.asList(wikiReference, spaceReference, documentReference, documentReference2));
    // Assert and verify
    // Actual rights check
    verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
    verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

SpaceReference (org.xwiki.model.reference.SpaceReference)142 DocumentReference (org.xwiki.model.reference.DocumentReference)96 Test (org.junit.Test)83 WikiReference (org.xwiki.model.reference.WikiReference)58 EntityReference (org.xwiki.model.reference.EntityReference)24 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)21 ArrayList (java.util.ArrayList)11 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)11 XWikiException (com.xpn.xwiki.XWikiException)9 QueryRestrictionGroup (com.celements.search.lucene.query.QueryRestrictionGroup)8 Expectations (org.jmock.Expectations)8 XWikiContext (com.xpn.xwiki.XWikiContext)7 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)7 ComponentManager (org.xwiki.component.manager.ComponentManager)7 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)7 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)7 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)7