Index: src/main/java/com/xpn/xwiki/api/XWiki.java =================================================================== --- src/main/java/com/xpn/xwiki/api/XWiki.java (revision 3959) +++ src/main/java/com/xpn/xwiki/api/XWiki.java (working copy) @@ -1132,10 +1132,7 @@ */ public boolean copyDocument(String docname, String targetdocname) throws XWikiException { - if (hasProgrammingRights()) - return xwiki.copyDocument(docname, targetdocname, null, null, null, false, - getXWikiContext()); - return false; + return this.copyDocument(docname, targetdocname, null, null, null, false, false); } /** @@ -1150,10 +1147,7 @@ public boolean copyDocument(String docname, String targetdocname, String wikilanguage) throws XWikiException { - if (hasProgrammingRights()) - return xwiki.copyDocument(docname, targetdocname, null, null, wikilanguage, false, - getXWikiContext()); - return false; + return this.copyDocument(docname, targetdocname, null, null, wikilanguage, false, false); } /** @@ -1170,10 +1164,8 @@ public boolean copyDocument(String docname, String sourceWiki, String targetWiki, String wikilanguage) throws XWikiException { - if (hasProgrammingRights()) - return xwiki.copyDocument(docname, docname, sourceWiki, targetWiki, wikilanguage, - true, getXWikiContext()); - return false; + return this.copyDocument(docname, docname, sourceWiki, targetWiki, wikilanguage, true, + false); } /** @@ -1191,10 +1183,8 @@ public boolean copyDocument(String docname, String targetdocname, String sourceWiki, String targetWiki, String wikilanguage, boolean reset) throws XWikiException { - if (hasProgrammingRights()) - return xwiki.copyDocument(docname, targetdocname, sourceWiki, targetWiki, - wikilanguage, reset, getXWikiContext()); - return false; + return this.copyDocument(docname, targetdocname, sourceWiki, targetWiki, wikilanguage, + reset, false); } /** @@ -1214,9 +1204,19 @@ String targetWiki, String wikilanguage, boolean reset, boolean force) throws XWikiException { - if (hasProgrammingRights()) - return xwiki.copyDocument(docname, targetdocname, sourceWiki, targetWiki, - wikilanguage, reset, force, getXWikiContext()); + if (hasProgrammingRights() + && xwiki.copyDocument(docname, targetdocname, sourceWiki, targetWiki, wikilanguage, + reset, force, getXWikiContext())) { + String currentWiki = getXWikiContext().getDatabase(); + if (targetWiki != null) { + getXWikiContext().setDatabase(targetWiki); + } + XWikiDocument tdoc = xwiki.getDocument(targetdocname, getXWikiContext()); + tdoc.setCreator(getXWikiContext().getUser()); + tdoc.setAuthor(getXWikiContext().getUser()); + getXWikiContext().setDatabase(currentWiki); + return true; + } return false; } Index: src/main/java/com/xpn/xwiki/XWiki.java =================================================================== --- src/main/java/com/xpn/xwiki/XWiki.java (revision 3959) +++ src/main/java/com/xpn/xwiki/XWiki.java (working copy) @@ -3263,7 +3263,7 @@ String targetWiki, String wikilanguage, boolean reset, XWikiContext context) throws XWikiException { - return copyDocument(docname, targetdocname, sourceWiki, targetWiki, wikilanguage, true, + return copyDocument(docname, targetdocname, sourceWiki, targetWiki, wikilanguage, reset, false, context); } Index: src/test/java/com/xpn/xwiki/api/XWikiTest.java =================================================================== --- src/test/java/com/xpn/xwiki/api/XWikiTest.java (revision 0) +++ src/test/java/com/xpn/xwiki/api/XWikiTest.java (revision 0) @@ -0,0 +1,153 @@ +/* + * Copyright 2007, XpertNet SARL, and individual contributors as indicated + * by the contributors.txt. + * + * 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 com.xpn.xwiki.api; + +import java.util.Calendar; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +import org.jmock.Mock; +import org.jmock.cglib.MockObjectTestCase; +import org.jmock.core.Invocation; +import org.jmock.core.stub.CustomStub; + +import com.xpn.xwiki.XWikiConfig; +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.XWikiException; +import com.xpn.xwiki.doc.XWikiDocument; +import com.xpn.xwiki.doc.XWikiDocumentArchive; +import com.xpn.xwiki.store.XWikiHibernateStore; +import com.xpn.xwiki.store.XWikiHibernateVersioningStore; +import com.xpn.xwiki.store.XWikiStoreInterface; +import com.xpn.xwiki.store.XWikiVersioningStoreInterface; +import com.xpn.xwiki.user.api.XWikiRightService; +import com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl; + +/** + * Unit tests for {@link com.xpn.xwiki.api.XWiki}. + * + * @version $Id: $ + */ +public class XWikiTest extends MockObjectTestCase +{ + public static final Random rand = new Random(Calendar.getInstance().getTimeInMillis()); + + private XWikiContext context; + + private com.xpn.xwiki.XWiki xwiki; + + private Document apiDocument; + + private XWiki apiXWiki; + + private Mock mockXWikiStore; + + private Mock mockXWikiVersioningStore; + + private Mock mockXWikiRightService; + + private Map docs = new HashMap(); + + protected void setUp() throws XWikiException + { + this.context = new XWikiContext(); + this.xwiki = new com.xpn.xwiki.XWiki(new XWikiConfig(), this.context); + this.apiXWiki = new XWiki(this.xwiki, this.context); + + this.mockXWikiStore = + mock(XWikiHibernateStore.class, new java.lang.Class[] {com.xpn.xwiki.XWiki.class, + XWikiContext.class}, new java.lang.Object[] {this.xwiki, this.context}); + this.mockXWikiStore.stubs().method("loadXWikiDoc").will( + new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") + { + public java.lang.Object invoke(Invocation invocation) throws Throwable + { + XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0); + if (docs.containsKey(shallowDoc.getName())) { + return (XWikiDocument) docs.get(shallowDoc.getName()); + } else { + return shallowDoc; + } + } + }); + this.mockXWikiStore.stubs().method("saveXWikiDoc").will( + new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc") + { + public java.lang.Object invoke(Invocation invocation) throws Throwable + { + XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0); + document.setNew(false); + document.setStore((XWikiStoreInterface) mockXWikiStore.proxy()); + document.setId(rand.nextLong()); + docs.put(document.getName(), document); + return null; + } + }); + this.mockXWikiStore.stubs().method("getTranslationList").will( + returnValue(Collections.EMPTY_LIST)); + + this.mockXWikiVersioningStore = + mock(XWikiHibernateVersioningStore.class, new java.lang.Class[] { + com.xpn.xwiki.XWiki.class, XWikiContext.class}, new java.lang.Object[] {this.xwiki, + this.context}); + this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will( + returnValue(new XWikiDocumentArchive())); + this.mockXWikiVersioningStore.stubs().method("saveXWikiDocArchive").will( + returnValue(null)); + + this.mockXWikiRightService = + mock(XWikiRightServiceImpl.class, new java.lang.Class[] {}, new java.lang.Object[] {}); + this.mockXWikiRightService.stubs().method("hasAccessLevel").will(returnValue(true)); + this.mockXWikiRightService.stubs().method("hasProgrammingRights").will(returnValue(true)); + + this.xwiki.setStore((XWikiStoreInterface) mockXWikiStore.proxy()); + this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) mockXWikiVersioningStore + .proxy()); + this.xwiki.setRightService((XWikiRightService) mockXWikiRightService.proxy()); + + this.context.setUser("Redtail"); + this.apiDocument = new Document(new XWikiDocument("MilkyWay", "Fidis"), this.context); + this.apiDocument.getDocument().setCreator("c" + this.context.getUser()); + this.apiDocument.getDocument().setAuthor("a" + this.context.getUser()); + this.apiDocument.save(); + } + + public void testAuthorAfterDocumentCopy() throws XWikiException + { + String copyName = "Lyre"; + String currentUser = this.context.getUser(); + this.apiXWiki.copyDocument(this.apiDocument.getName(), copyName); + Document copy = this.apiXWiki.getDocument(copyName); + + assertTrue(currentUser.equals(copy.getAuthor())); + } + + public void testCreatorAfterDocumentCopy() throws XWikiException + { + String copyName = "Sirius"; + String currentUser = this.context.getUser(); + this.apiXWiki.copyDocument(this.apiDocument.getName(), copyName); + Document copy = this.apiXWiki.getDocument(copyName); + + assertTrue(currentUser.equals(copy.getCreator())); + } +} Index: src/test/java/com/xpn/xwiki/doc/XWikiDocumentTest.java =================================================================== --- src/test/java/com/xpn/xwiki/doc/XWikiDocumentTest.java (revision 3959) +++ src/test/java/com/xpn/xwiki/doc/XWikiDocumentTest.java (working copy) @@ -24,7 +24,9 @@ import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.XWiki; import com.xpn.xwiki.XWikiConfig; +import com.xpn.xwiki.XWikiException; import com.xpn.xwiki.render.XWikiRenderingEngine; +import com.xpn.xwiki.store.XWikiHibernateVersioningStore; /** * Unit tests for {@link XWikiDocument}. @@ -37,6 +39,7 @@ private XWikiDocument document; private Mock mockXWiki; private Mock mockXWikiRenderingEngine; + private Mock mockXWikiVersioningStore; protected void setUp() { @@ -45,11 +48,20 @@ this.mockXWiki = mock(XWiki.class, new Class[] {XWikiConfig.class, XWikiContext.class}, new Object[] {new XWikiConfig(), this.context}); + this.mockXWiki.stubs().method("Param").will(returnValue(null)); this.mockXWikiRenderingEngine = mock(XWikiRenderingEngine.class); + + this.mockXWikiVersioningStore = + mock(XWikiHibernateVersioningStore.class, new Class[] {XWiki.class, + XWikiContext.class}, new Object[] {this.mockXWiki.proxy(), this.context}); + this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will( + returnValue(null)); this.mockXWiki.stubs().method("getRenderingEngine").will(returnValue( this.mockXWikiRenderingEngine.proxy())); + this.mockXWiki.stubs().method("getVersioningStore").will( + returnValue(this.mockXWikiVersioningStore.proxy())); this.context.setWiki((XWiki) this.mockXWiki.proxy()); } @@ -79,4 +91,24 @@ assertEquals("Title", this.document.getDisplayTitle(this.context)); } + + public void testAuthorAfterDocumentCopy() throws XWikiException + { + String author = "Albatross"; + this.document.setAuthor(author); + XWikiDocument copy = + this.document.copyDocument(this.document.getName() + " Copy", this.context); + + assertTrue(author.equals(copy.getAuthor())); + } + + public void testCreatorAfterDocumentCopy() throws XWikiException + { + String creator = "Condor"; + this.document.setCreator(creator); + XWikiDocument copy = + this.document.copyDocument(this.document.getName() + " Copy", this.context); + + assertTrue(creator.equals(copy.getCreator())); + } } Index: src/test/java/com/xpn/xwiki/XWikiTest.java =================================================================== --- src/test/java/com/xpn/xwiki/XWikiTest.java (revision 0) +++ src/test/java/com/xpn/xwiki/XWikiTest.java (revision 0) @@ -0,0 +1,130 @@ +/* + * Copyright 2007, XpertNet SARL, and individual contributors as indicated + * by the contributors.txt. + * + * 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 com.xpn.xwiki; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.jmock.Mock; +import org.jmock.cglib.MockObjectTestCase; +import org.jmock.core.Invocation; +import org.jmock.core.stub.CustomStub; + +import com.xpn.xwiki.doc.XWikiDocument; +import com.xpn.xwiki.store.XWikiHibernateStore; +import com.xpn.xwiki.store.XWikiHibernateVersioningStore; +import com.xpn.xwiki.store.XWikiStoreInterface; +import com.xpn.xwiki.store.XWikiVersioningStoreInterface; + +/** + * Unit tests for {@link com.xpn.xwiki.XWiki}. + * + * @version $Id: $ + */ +public class XWikiTest extends MockObjectTestCase +{ + private XWikiContext context; + + private XWikiDocument document; + + private XWiki xwiki; + + private Mock mockXWikiStore; + + private Mock mockXWikiVersioningStore; + + private Map docs = new HashMap(); + + protected void setUp() throws XWikiException + { + this.context = new XWikiContext(); + this.document = new XWikiDocument("MilkyWay", "Fidis"); + this.xwiki = new XWiki(new XWikiConfig(), this.context); + + this.mockXWikiStore = + mock(XWikiHibernateStore.class, new Class[] {XWiki.class, XWikiContext.class}, + new Object[] {this.xwiki, this.context}); + this.mockXWikiStore.stubs().method("loadXWikiDoc").will( + new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") + { + public Object invoke(Invocation invocation) throws Throwable + { + XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0); + if (docs.containsKey(shallowDoc.getName())) { + return (XWikiDocument) docs.get(shallowDoc.getName()); + } else { + return shallowDoc; + } + } + }); + this.mockXWikiStore.stubs().method("saveXWikiDoc").will( + new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc") + { + public Object invoke(Invocation invocation) throws Throwable + { + XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0); + document.setNew(false); + document.setStore((XWikiStoreInterface) mockXWikiStore.proxy()); + docs.put(document.getName(), document); + return null; + } + }); + this.mockXWikiStore.stubs().method("getTranslationList").will( + returnValue(Collections.EMPTY_LIST)); + + this.mockXWikiVersioningStore = + mock(XWikiHibernateVersioningStore.class, new Class[] {XWiki.class, + XWikiContext.class}, new Object[] {this.xwiki, this.context}); + this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will( + returnValue(null)); + + this.xwiki.setStore((XWikiStoreInterface) mockXWikiStore.proxy()); + this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) mockXWikiVersioningStore + .proxy()); + this.xwiki.saveDocument(this.document, this.context); + + this.document.setCreator("Condor"); + this.document.setAuthor("Albatross"); + this.xwiki.saveDocument(this.document, this.context); + } + + public void testAuthorAfterDocumentCopy() throws XWikiException + { + String copyName = "Lyre"; + String author = this.document.getAuthor(); + this.xwiki.copyDocument(this.document.getName(), this.document.getSpace() + "." + + copyName, this.context); + XWikiDocument copy = this.xwiki.getDocument(copyName, context); + + assertTrue(author.equals(copy.getAuthor())); + } + + public void testCreatorAfterDocumentCopy() throws XWikiException + { + String copyName = "Sirius"; + String creator = this.document.getCreator(); + this.xwiki.copyDocument(this.document.getName(), this.document.getSpace() + "." + + copyName, this.context); + XWikiDocument copy = this.xwiki.getDocument(copyName, context); + + assertTrue(creator.equals(copy.getCreator())); + } +}