Search in sources :

Example 6 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class DefaultWikiComponentBuilderEventListener method installOrUpdateComponentRequirementXClass.

/**
 * Verify that the {@link #DEPENDENCY_CLASS} exists and is up-to-date (act if not).
 *
 * @throws XWikiException on failure
 */
private void installOrUpdateComponentRequirementXClass() throws XWikiException {
    XWikiContext xcontext = getXWikiContext();
    XWikiDocument doc = xcontext.getWiki().getDocument(DEPENDENCY_CLASS_REFERENCE, xcontext);
    BaseClass bclass = doc.getXClass();
    bclass.setDocumentReference(doc.getDocumentReference());
    boolean needsUpdate = false;
    needsUpdate |= this.initializeXClassDocumentMetadata(doc, "Wiki Component Dependency XWiki Class");
    needsUpdate |= bclass.addTextField(COMPONENT_ROLE_TYPE_FIELD, "Dependency Role Type", 30);
    needsUpdate |= bclass.addTextField(COMPONENT_ROLE_HINT_FIELD, "Dependency Role Hint", 30);
    needsUpdate |= bclass.addTextField(DEPENDENCY_BINDING_NAME_FIELD, "Binding name", 30);
    if (needsUpdate) {
        this.update(doc);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 7 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class XarExtensionHandlerTest method testInstallOnWikiWithoutAuthor.

@Test
public void testInstallOnWikiWithoutAuthor() throws Throwable {
    XWikiDocument existingDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
    BaseObject object = new BaseObject();
    object.setXClassReference(new DocumentReference("wiki", "space", "class"));
    existingDocument.addXObject(object);
    existingDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
    this.oldcore.getSpyXWiki().saveDocument(existingDocument, "", true, getXWikiContext());
    // install
    install(this.localXarExtensiontId1, "wiki", null);
    // validate
    DocumentReference xarAuthorReference = new DocumentReference("wiki", "XWiki", "author");
    DocumentReference xarCreatorReference = new DocumentReference("wiki", "XWiki", "creator");
    DocumentReference xarContentAuthorReference = new DocumentReference("wiki", "XWiki", "contentAuthor");
    // space.page
    XWikiDocument page = this.oldcore.getSpyXWiki().getDocument(existingDocument.getDocumentReference(), getXWikiContext());
    Assert.assertFalse("Document wiki:space.page has not been saved in the database", page.isNew());
    Assert.assertNull(page.getXObject(object.getXClassReference()));
    Assert.assertEquals("Wrong content", "content", page.getContent());
    Assert.assertEquals("Wrong creator", new DocumentReference("wiki", "space", "existingcreator"), page.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, page.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, page.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "2.1", page.getVersion());
    Assert.assertEquals("Wrong version", Locale.ROOT, page.getLocale());
    Assert.assertFalse("Document is hidden", page.isHidden());
    BaseClass baseClass = page.getXClass();
    Assert.assertNotNull(baseClass.getField("property"));
    Assert.assertEquals("property", baseClass.getField("property").getName());
    Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
    // space.pagewithattachment
    XWikiDocument pagewithattachment = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "pagewithattachment"), getXWikiContext());
    Assert.assertFalse(pagewithattachment.isNew());
    Assert.assertEquals("Wrong version", "1.1", pagewithattachment.getVersion());
    Assert.assertEquals("Wrong creator", xarCreatorReference, pagewithattachment.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, pagewithattachment.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, pagewithattachment.getContentAuthorReference());
    XWikiAttachment attachment = pagewithattachment.getAttachment("attachment.txt");
    Assert.assertNotNull(attachment);
    Assert.assertEquals("attachment.txt", attachment.getFilename());
    Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
    Assert.assertEquals("attachment content", IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "attachmentauthor"), attachment.getAuthorReference());
    // space1.page1
    XWikiDocument page1 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
    Assert.assertFalse("Document wiki:space1.page1 has not been saved in the database", page1.isNew());
    // translated.translated
    DocumentReference translatedReference = new DocumentReference("wiki", "translated", "translated");
    XWikiDocument defaultTranslated = this.oldcore.getSpyXWiki().getDocument(translatedReference, getXWikiContext());
    Assert.assertNotNull("Document wiki:translated.translated has not been saved in the database", defaultTranslated);
    Assert.assertFalse("Document wiki:translated.translated has not been saved in the database", defaultTranslated.isNew());
    Assert.assertEquals("Wrong content", "default content", defaultTranslated.getContent());
    Assert.assertEquals("Wrong creator", xarCreatorReference, defaultTranslated.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, defaultTranslated.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, defaultTranslated.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", defaultTranslated.getVersion());
    // translated.translated.tr
    XWikiDocument translated = this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("tr")));
    Assert.assertNotNull("Document wiki:translated.translated in langauge tr has not been saved in the database", translated);
    Assert.assertFalse("Document wiki:translated.translated in langauge tr has not been saved in the database", translated.isNew());
    Assert.assertEquals("Wrong content", "tr content", translated.getContent());
    Assert.assertEquals("Wrong creator", xarCreatorReference, translated.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, translated.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, translated.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", translated.getVersion());
    // translated.translated.fr
    XWikiDocument translated2 = this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("fr")));
    Assert.assertNotNull("Document wiki:translated.translated in language fr has not been saved in the database", translated2);
    Assert.assertFalse("Document wiki:translated.translated in langauge fr has not been saved in the database", translated2.isNew());
    Assert.assertEquals("Wrong content", "fr content", translated2.getContent());
    Assert.assertEquals("Wrong creator", xarCreatorReference, translated2.getCreatorReference());
    Assert.assertEquals("Wrong author", xarAuthorReference, translated2.getAuthorReference());
    Assert.assertEquals("Wrong content author", xarContentAuthorReference, translated2.getContentAuthorReference());
    Assert.assertEquals("Wrong version", "1.1", translated2.getVersion());
    // space.hiddenpage
    XWikiDocument hiddenpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "hiddenpage"), getXWikiContext());
    Assert.assertNotNull("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage);
    Assert.assertFalse("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage.isNew());
    Assert.assertTrue("Document is not hidden", hiddenpage.isHidden());
}
Also used : Locale(java.util.Locale) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 8 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class XarExtensionHandlerTest method testUpgradeOnRoot.

@Test
public void testUpgradeOnRoot() throws Throwable {
    doReturn(Arrays.asList("wiki1", "wiki2")).when(this.oldcore.getWikiDescriptorManager()).getAllIds();
    install(this.localXarExtensiontId1, null, this.contextUser);
    verifyHasAdminRight(2);
    // Do some local modifications
    XWikiDocument deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "deletedpage"), getXWikiContext());
    this.oldcore.getSpyXWiki().deleteDocument(deletedpage, getXWikiContext());
    XWikiDocument modifieddeletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "modifieddeletedpage"), getXWikiContext());
    this.oldcore.getSpyXWiki().deleteDocument(modifieddeletedpage, getXWikiContext());
    XWikiDocument pagewithobject = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "pagewithobject"), getXWikiContext());
    pagewithobject.removeXObjects(new LocalDocumentReference("XWiki", "XWikiGroups"));
    this.oldcore.getSpyXWiki().saveDocument(pagewithobject, getXWikiContext());
    XWikiDocument deletedpagewithmodifications = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space1", "modified"), getXWikiContext());
    deletedpagewithmodifications.setContent("modified content");
    // upgrade
    install(this.localXarExtensiontId2, null, this.contextUser);
    verifyHasAdminRight(3);
    // validate
    // samespace.samepage
    XWikiDocument samepage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "samespace", "samepage"), getXWikiContext());
    Assert.assertEquals("Wrong versions", "1.1", samepage.getVersion());
    // space.page
    XWikiDocument modifiedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "page"), getXWikiContext());
    Assert.assertFalse("Document wiki.space.page has not been saved in the database", modifiedpage.isNew());
    Assert.assertEquals("Wrong content", "content 2", modifiedpage.getContent());
    Assert.assertEquals("Wrong author", this.contextUser, modifiedpage.getAuthorReference());
    Assert.assertEquals("Wrong versions", "2.1", modifiedpage.getVersion());
    Assert.assertEquals("Wrong version", Locale.ROOT, modifiedpage.getLocale());
    Assert.assertEquals("Wrong customclass", "customclass2", modifiedpage.getCustomClass());
    Assert.assertEquals("Wrong defaultTemplate", "defaultTemplate2", modifiedpage.getDefaultTemplate());
    Assert.assertEquals("Wrong hidden", true, modifiedpage.isHidden());
    Assert.assertEquals("Wrong ValidationScript", "validationScript2", modifiedpage.getValidationScript());
    BaseClass baseClass = modifiedpage.getXClass();
    Assert.assertNotNull(baseClass.getField("property"));
    Assert.assertEquals("property", baseClass.getField("property").getName());
    Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
    XWikiAttachment attachment = modifiedpage.getAttachment("attachment.txt");
    Assert.assertNotNull(attachment);
    Assert.assertEquals("attachment.txt", attachment.getFilename());
    Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
    Assert.assertEquals("attachment content", IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
    // space2.page2
    XWikiDocument newPage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space2", "page2"), getXWikiContext());
    Assert.assertFalse("Document wiki:space2.page2 has not been saved in the database", newPage.isNew());
    // space1.page1
    XWikiDocument removedPage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space1", "page1"), getXWikiContext());
    Assert.assertTrue("Document wiki:space1.page1 has not been removed from the database", removedPage.isNew());
    // space.deletedpage
    deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "deletedpage"), getXWikiContext());
    Assert.assertTrue("Document wiki:space.deleted has been restored", deletedpage.isNew());
    // space.modifieddeletedpage
    modifieddeletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "modifieddeletedpage"), getXWikiContext());
    Assert.assertTrue("Document wiki:space.modifieddeletedpage has been restored", modifieddeletedpage.isNew());
    // space.pagewithobject
    pagewithobject = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "pagewithobject"), getXWikiContext());
    Assert.assertNull("Document wiki:space.pagewithobject does not contain an XWiki.XWikiGroups object", pagewithobject.getXObject(new LocalDocumentReference("XWiki", "XWikiGroups")));
    // space1.modified
    XWikiDocument space1modified = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space1", "modified"), getXWikiContext());
    Assert.assertFalse("Document wiki:space1.modified has been removed from the database", space1modified.isNew());
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class XarExtensionHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    // mock
    this.contextUser = new DocumentReference(getXWikiContext().getWikiId(), "XWiki", "ExtensionUser");
    this.localXarExtensiontId1 = new ExtensionId("test", "1.0");
    this.localXarExtensiontId2 = new ExtensionId("test", "2.0");
    this.collisionextension1 = new ExtensionId("collisionextension1", "version");
    this.collisionextension2 = new ExtensionId("collisionextension2", "version");
    // classes
    BaseClass styleSheetClass = new BaseClass();
    this.classes.put("StyleSheetExtension", styleSheetClass);
    // checking
    doReturn(true).when(this.oldcore.getSpyXWiki()).hasAttachmentRecycleBin(any(XWikiContext.class));
    getXWikiContext().setUserReference(this.contextUser);
    ((XWikiStubContextProvider) this.componentManager.getInstance(XWikiStubContextProvider.class)).initialize(getXWikiContext());
    CoreConfiguration coreConfiguration = this.componentManager.getInstance(CoreConfiguration.class);
    doReturn(Syntax.PLAIN_1_0).when(coreConfiguration).getDefaultDocumentSyntax();
    // lookup
    this.jobExecutor = this.componentManager.getInstance(JobExecutor.class);
    this.xarExtensionRepository = this.componentManager.getInstance(InstalledExtensionRepository.class, XarExtensionHandler.TYPE);
    this.observation = this.repositoryUtil.getComponentManager().getInstance(ObservationManager.class);
    // Get rid of wiki macro listener
    this.componentManager.<ObservationManager>getInstance(ObservationManager.class).removeListener("RegisterMacrosOnImportListener");
    this.installedExtensionRepository = this.componentManager.getInstance(InstalledExtensionRepository.class, "xar");
    // Programming right is not required for XAR extensions
    doThrow(AccessDeniedException.class).when(this.oldcore.getMockAuthorizationManager()).checkAccess(eq(Right.PROGRAM), any(), any());
}
Also used : XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) JobExecutor(org.xwiki.job.JobExecutor) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) ExtensionId(org.xwiki.extension.ExtensionId) ObservationManager(org.xwiki.observation.ObservationManager) CoreConfiguration(com.xpn.xwiki.CoreConfiguration) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XarInstalledExtensionRepository(org.xwiki.extension.xar.internal.repository.XarInstalledExtensionRepository) InstalledExtensionRepository(org.xwiki.extension.repository.InstalledExtensionRepository) Before(org.junit.Before)

Example 10 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project xwiki-platform by xwiki.

the class XarExtensionHandlerTest method testDowngradeOnWiki.

@Test
public void testDowngradeOnWiki() throws Throwable {
    install(this.localXarExtensiontId2, "wiki", this.contextUser);
    verifyHasAdminRight(1);
    // upgrade
    install(this.localXarExtensiontId1, "wiki", this.contextUser);
    verifyHasAdminRight(3);
    // validate
    // samespace.samepage
    XWikiDocument samepage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "samespace", "samepage"), getXWikiContext());
    Assert.assertEquals("Wrong versions", "1.1", samepage.getVersion());
    // space.page
    XWikiDocument modifiedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), getXWikiContext());
    Assert.assertFalse("Document wiki.space.page has not been saved in the database", modifiedpage.isNew());
    Assert.assertEquals("Wrong content", "content", modifiedpage.getContent());
    Assert.assertEquals("Wrong author", this.contextUser, modifiedpage.getAuthorReference());
    Assert.assertEquals("Wrong versions", "2.1", modifiedpage.getVersion());
    Assert.assertEquals("Wrong version", Locale.ROOT, modifiedpage.getLocale());
    BaseClass baseClass = modifiedpage.getXClass();
    Assert.assertNotNull(baseClass.getField("property"));
    Assert.assertEquals("property", baseClass.getField("property").getName());
    Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
    // The attachment does not exist in version 1.0
    Assert.assertNull(modifiedpage.getAttachment("attachment.txt"));
    // space2.page2
    XWikiDocument newPage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space2", "page2"), getXWikiContext());
    Assert.assertTrue("Document wiki.space2.page2 has not been removed from the database", newPage.isNew());
    // space1.page1
    XWikiDocument removedPage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
    Assert.assertFalse("Document wiki.space1.page1 has not been saved in the database", removedPage.isNew());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

BaseClass (com.xpn.xwiki.objects.classes.BaseClass)100 DocumentReference (org.xwiki.model.reference.DocumentReference)42 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)41 BaseObject (com.xpn.xwiki.objects.BaseObject)40 XWikiException (com.xpn.xwiki.XWikiException)26 XWikiContext (com.xpn.xwiki.XWikiContext)24 ArrayList (java.util.ArrayList)18 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)16 XWiki (com.xpn.xwiki.XWiki)15 Test (org.junit.Test)15 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)15 EntityReference (org.xwiki.model.reference.EntityReference)10 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)9 BaseProperty (com.xpn.xwiki.objects.BaseProperty)9 List (java.util.List)9 ToString (org.suigeneris.jrcs.util.ToString)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Before (org.junit.Before)6 TextAreaClass (com.xpn.xwiki.objects.classes.TextAreaClass)5 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)5