1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.user.internal

File WikiUserFromXEMMigrationTest.java

 

Code metrics

0
76
4
1
215
145
4
0.05
19
4
1

Classes

Class Line # Actions
WikiUserFromXEMMigrationTest 57 76 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.wiki.user.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Date;
24    import java.util.List;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.xwiki.context.Execution;
30    import org.xwiki.context.ExecutionContext;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.LocalDocumentReference;
33    import org.xwiki.test.mockito.MockitoComponentMockingRule;
34    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
35    import org.xwiki.wiki.user.MembershipType;
36    import org.xwiki.wiki.user.UserScope;
37    import org.xwiki.wiki.user.WikiUserConfiguration;
38   
39    import com.xpn.xwiki.XWiki;
40    import com.xpn.xwiki.XWikiContext;
41    import com.xpn.xwiki.doc.XWikiDocument;
42    import com.xpn.xwiki.objects.BaseObject;
43    import com.xpn.xwiki.store.migration.hibernate.HibernateDataMigration;
44   
45    import static org.mockito.ArgumentMatchers.any;
46    import static org.mockito.ArgumentMatchers.eq;
47    import static org.mockito.Mockito.mock;
48    import static org.mockito.Mockito.times;
49    import static org.mockito.Mockito.verify;
50    import static org.mockito.Mockito.when;
51   
52    /**
53    * Tests for {@link WikiUserFromXEMMigration}.
54    *
55    * @since 5.3RC1
56    */
 
57    public class WikiUserFromXEMMigrationTest
58    {
59    @Rule
60    public MockitoComponentMockingRule<WikiUserFromXEMMigration> mocker =
61    new MockitoComponentMockingRule(WikiUserFromXEMMigration.class, HibernateDataMigration.class,
62    "R530010WikiUserFromXEMMigration");
63   
64    private WikiDescriptorManager wikiDescriptorManager;
65   
66    private WikiUserConfigurationHelper wikiUserConfigurationHelper;
67   
68    private Execution execution;
69   
70    private XWikiContext xcontext;
71   
72    private XWiki xwiki;
73   
 
74  3 toggle @Before
75    public void setUp() throws Exception
76    {
77  3 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
78  3 wikiUserConfigurationHelper = mocker.getInstance(WikiUserConfigurationHelper.class);
79  3 execution = mock(Execution.class);
80  3 mocker.registerComponent(Execution.class, execution);
81  3 xcontext = mock(XWikiContext.class);
82  3 xwiki = mock(XWiki.class);
83   
84  3 ExecutionContext executionContext = mock(ExecutionContext.class);
85  3 when(execution.getContext()).thenReturn(executionContext);
86  3 when(executionContext.getProperty("xwikicontext")).thenReturn(xcontext);
87  3 when(xcontext.getWiki()).thenReturn(xwiki);
88  3 when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki");
89    }
90   
 
91  1 toggle @Test
92    public void upgradeRegularSubWiki() throws Exception
93    {
94    // Mocks
95  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("subwiki");
96  1 XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
97  1 when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE, "XWikiServerSubwiki")),
98    any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
99  1 when(oldDescriptorDocument.getXObject(eq(new DocumentReference("mainWiki", "WorkspaceManager", "WorkspaceClass")
100    ))).thenReturn(null);
101   
102    // Run
103  1 mocker.getComponentUnderTest().hibernateMigrate();
104   
105    // Verify
106  1 WikiUserConfiguration expectedConfiguration = new WikiUserConfiguration();
107  1 expectedConfiguration.setUserScope(UserScope.LOCAL_AND_GLOBAL);
108  1 expectedConfiguration.setMembershipType(MembershipType.INVITE);
109  1 verify(wikiUserConfigurationHelper).saveConfiguration(eq(expectedConfiguration), eq("subwiki"));
110    }
111   
 
112  1 toggle @Test
113    public void upgradeWorkspace() throws Exception
114    {
115    // Mocks about the descriptor
116  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("workspace");
117  1 XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
118  1 when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE, "XWikiServerWorkspace")),
119    any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
120   
121    // Mocks about the old workspace object
122  1 BaseObject oldObject = mock(BaseObject.class);
123  1 when(oldDescriptorDocument.getXObject(eq(new DocumentReference("mainWiki", "WorkspaceManager", "WorkspaceClass")
124    ))).thenReturn(oldObject);
125  1 when(oldObject.getStringValue("membershipType")).thenReturn("request");
126   
127    // Mocks about candidacies
128  1 DocumentReference memberGroupRef = new DocumentReference("workspace", XWiki.SYSTEM_SPACE, "XWikiAllGroup");
129  1 XWikiDocument memberGroupDoc = mock(XWikiDocument.class);
130  1 when(xwiki.getDocument(eq(memberGroupRef), any(XWikiContext.class))).thenReturn(memberGroupDoc);
131  1 DocumentReference candidacyOldClass = new DocumentReference("workspace", "XWiki",
132    "WorkspaceCandidateMemberClass");
133  1 List<BaseObject> oldCandidacies = new ArrayList<>();
134  1 BaseObject oldCandidacy = mock(BaseObject.class);
135  1 oldCandidacies.add(oldCandidacy);
136  1 when(memberGroupDoc.getXObjects(eq(candidacyOldClass))).thenReturn(oldCandidacies);
137  1 LocalDocumentReference newCandidacyClassRef = WikiCandidateMemberClassInitializer.REFERENCE;
138  1 BaseObject newCandidacyObject = mock(BaseObject.class);
139  1 when(memberGroupDoc.newXObject(eq(newCandidacyClassRef), any(XWikiContext.class)))
140    .thenReturn(newCandidacyObject);
141  1 when(oldCandidacy.getStringValue("type")).thenReturn("aa");
142  1 when(oldCandidacy.getStringValue("status")).thenReturn("bb");
143  1 when(oldCandidacy.getStringValue("userName")).thenReturn("cc");
144  1 when(oldCandidacy.getLargeStringValue("userComment")).thenReturn("dd");
145  1 when(oldCandidacy.getStringValue("reviewer")).thenReturn("ee");
146  1 when(oldCandidacy.getLargeStringValue("reviewerComment")).thenReturn("ff");
147  1 when(oldCandidacy.getLargeStringValue("reviewerPrivateComment")).thenReturn("gg");
148  1 when(oldCandidacy.getDateValue("date")).thenReturn(new Date(2000));
149  1 when(oldCandidacy.getDateValue("resolutionDate")).thenReturn(new Date(8000));
150   
151    // Run
152  1 mocker.getComponentUnderTest().hibernateMigrate();
153   
154    // Verify the user configuration is accurate
155  1 WikiUserConfiguration expectedConfiguration = new WikiUserConfiguration();
156  1 expectedConfiguration.setUserScope(UserScope.GLOBAL_ONLY);
157  1 expectedConfiguration.setMembershipType(MembershipType.REQUEST);
158  1 verify(wikiUserConfigurationHelper).saveConfiguration(eq(expectedConfiguration), eq("workspace"));
159   
160    // Verify the old workspace object has been removed and the descriptor saved
161  1 verify(oldDescriptorDocument).removeXObject(oldObject);
162  1 verify(xwiki, times(1)).saveDocument(oldDescriptorDocument, "[UPGRADE] Remove the old WorkspaceManager.WorkspaceClass" +
163    " object.", xcontext);
164   
165    // Verify the candidacy has been upgraded
166  1 verify(newCandidacyObject).setStringValue(WikiCandidateMemberClassInitializer.FIELD_TYPE, "aa");
167  1 verify(newCandidacyObject).setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS, "bb");
168  1 verify(newCandidacyObject).setStringValue(WikiCandidateMemberClassInitializer.FIELD_USER, "cc");
169  1 verify(newCandidacyObject).setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT, "dd");
170  1 verify(newCandidacyObject).setStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN, "ee");
171  1 verify(newCandidacyObject).setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT, "ff");
172  1 verify(newCandidacyObject).setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT,
173    "gg");
174  1 verify(newCandidacyObject).setDateValue(eq(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION),
175    eq(new Date(2000)));
176  1 verify(newCandidacyObject).setDateValue(eq(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE),
177    eq(new Date(8000)));
178   
179    // Verify the old candidacy has been removed and the document saved
180  1 verify(memberGroupDoc).removeXObject(oldCandidacy);
181  1 verify(xwiki, times(1)).saveDocument(memberGroupDoc, "Upgrade candidacies from the old Workspace Application" +
182    " to the new Wiki Application.", xcontext);
183   
184    }
185   
 
186  1 toggle @Test
187    public void upgradeOldWorkspaceTemplate() throws Exception
188    {
189    // Mocks about the descriptor
190  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("workspacetemplate");
191  1 XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
192  1 when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE, "XWikiServerWorkspacetemplate")),
193    any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
194   
195    // Mock about the workspace special page
196  1 when(xwiki.exists(eq(new DocumentReference("workspacetemplate", "XWiki", "ManageWorkspace")),
197    any(XWikiContext.class))).thenReturn(true);
198   
199    // Mocks about candidacies
200  1 DocumentReference memberGroupRef = new DocumentReference("workspacetemplate", XWiki.SYSTEM_SPACE,
201    "XWikiAllGroup");
202  1 XWikiDocument memberGroupDoc = mock(XWikiDocument.class);
203  1 when(xwiki.getDocument(eq(memberGroupRef), any(XWikiContext.class))).thenReturn(memberGroupDoc);
204   
205    // Run
206  1 mocker.getComponentUnderTest().hibernateMigrate();
207   
208    // Verify the user configuration is accurate
209  1 WikiUserConfiguration expectedConfiguration = new WikiUserConfiguration();
210  1 expectedConfiguration.setUserScope(UserScope.GLOBAL_ONLY);
211  1 expectedConfiguration.setMembershipType(MembershipType.INVITE);
212  1 verify(wikiUserConfigurationHelper).saveConfiguration(eq(expectedConfiguration), eq("workspacetemplate"));
213    }
214   
215    }