1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File UndeleteActionTest.java

 

Code metrics

0
60
6
1
226
120
6
0.1
10
6
1

Classes

Class Line # Actions
UndeleteActionTest 51 60 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 5 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 com.xpn.xwiki.web;
21   
22    import java.util.Locale;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.mockito.Mockito;
28    import org.xwiki.csrf.CSRFToken;
29    import org.xwiki.localization.LocaleUtils;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.test.mockito.MockitoComponentManagerRule;
32   
33    import com.xpn.xwiki.XWiki;
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.doc.XWikiDeletedDocument;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.store.XWikiRecycleBinStoreInterface;
38   
39    import static org.junit.Assert.assertFalse;
40    import static org.mockito.ArgumentMatchers.any;
41    import static org.mockito.ArgumentMatchers.anyInt;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.verify;
44    import static org.mockito.Mockito.when;
45   
46    /**
47    * Unit tests for {@link UndeleteAction}.
48    *
49    * @version $Id: 3d9d10588adc116c1fb06340f1e686a567241fea $
50    */
 
51    public class UndeleteActionTest
52    {
53    /**
54    * A component manager that allows us to register mock components.
55    */
56    @Rule
57    public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
58   
59    /**
60    * The object being tested.
61    */
62    private UndeleteAction undeleteAction = new UndeleteAction();
63   
64    /**
65    * A mock {@link XWikiContext};
66    */
67    private XWikiContext context = mock(XWikiContext.class);
68   
69    /**
70    * A mock {@link XWiki};
71    */
72    private XWiki xwiki = mock(XWiki.class);
73   
74    /**
75    * A mock {@link XWikiDocument};
76    */
77    private XWikiDocument document = mock(XWikiDocument.class);
78   
 
79  5 toggle @Before
80    public void setUp() throws Exception
81    {
82  5 mocker.registerMockComponent(CSRFToken.class);
83  5 Utils.setComponentManager(mocker);
84   
85  5 when(context.getRequest()).thenReturn(mock(XWikiRequest.class));
86   
87  5 when(context.getWiki()).thenReturn(xwiki);
88   
89  5 when(context.getDoc()).thenReturn(document);
90  5 when(document.getDocumentReference()).thenReturn(new DocumentReference("xwiki", "Main", "DeletedDocument"));
91    }
92   
 
93  1 toggle @Test
94    public void missingCSRFToken() throws Exception
95    {
96  1 assertFalse(undeleteAction.action(context));
97   
98  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
99  1 verify(csrfToken).isTokenValid(null);
100    }
101   
102    /**
103    * @see "XWIKI-9421: Attachment version is incremented when a document is restored from recycle bin"
104    */
 
105  1 toggle @Test
106    public void restore() throws Exception
107    {
108  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
109  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
110   
111  1 when(xwiki.hasRecycleBin(context)).thenReturn(true);
112   
113  1 when(context.getRequest().getParameter("id")).thenReturn("13");
114   
115  1 XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
116  1 when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
117   
118  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
119  1 when(xwiki.getDeletedDocument(any(), any(), anyInt(), any(XWikiContext.class))).thenReturn(
120    deletedDocument);
121   
122  1 when(deletedDocument.getLanguage()).thenReturn("");
123   
124  1 when(xwiki.exists(any(DocumentReference.class), any(XWikiContext.class))).thenReturn(false);
125   
126  1 assertFalse(undeleteAction.action(context));
127   
128  1 verify(xwiki).restoreFromRecycleBin(document, 13, "Restored from recycle bin", context);
129    }
130   
131    /**
132    * When the recycle bin is disabled, the document should not be restored.
133    */
 
134  1 toggle @Test
135    public void testRecycleBinDisabled() throws Exception
136    {
137  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
138  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
139   
140  1 when(xwiki.hasRecycleBin(context)).thenReturn(false);
141   
142  1 when(context.getRequest().getParameter("id")).thenReturn("13");
143   
144  1 XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
145  1 when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
146   
147  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
148  1 when(xwiki.getDeletedDocument(any(), any(), anyInt(), any(XWikiContext.class))).thenReturn(
149    deletedDocument);
150   
151  1 when(deletedDocument.getLanguage()).thenReturn("");
152   
153  1 when(xwiki.exists(any(DocumentReference.class), any(XWikiContext.class))).thenReturn(false);
154   
155  1 assertFalse(undeleteAction.action(context));
156   
157  1 verify(xwiki, Mockito.never()).restoreFromRecycleBin(document, 13, "Restored from recycle bin", context);
158    }
159   
160    /**
161    * When the location where to restore the document already exists, don`t override.
162    */
 
163  1 toggle @Test
164    public void testDocumentAlreadyExists() throws Exception
165    {
166  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
167  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
168   
169  1 when(xwiki.hasRecycleBin(context)).thenReturn(true);
170   
171  1 when(context.getRequest().getParameter("id")).thenReturn("13");
172   
173  1 XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
174  1 when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
175   
176  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
177  1 when(xwiki.getDeletedDocument(any(), any(), anyInt(), any(XWikiContext.class))).thenReturn(
178    deletedDocument);
179   
180  1 when(deletedDocument.getLanguage()).thenReturn("");
181   
182  1 when(xwiki.exists(any(DocumentReference.class), any(XWikiContext.class))).thenReturn(true);
183   
184  1 assertFalse(undeleteAction.action(context));
185   
186  1 verify(xwiki, Mockito.never()).restoreFromRecycleBin(document, 13, "Restored from recycle bin", context);
187    }
188   
189    /**
190    * @see "XWIKI-9567: Cannot restore document translations from recycle bin"
191    */
 
192  1 toggle @Test
193    public void testRestoringTranslation() throws Exception
194    {
195  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
196  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
197   
198  1 when(xwiki.hasRecycleBin(context)).thenReturn(true);
199   
200  1 when(context.getRequest().getParameter("id")).thenReturn("13");
201   
202  1 XWikiRecycleBinStoreInterface recycleBin = mock(XWikiRecycleBinStoreInterface.class);
203  1 when(xwiki.getRecycleBinStore()).thenReturn(recycleBin);
204   
205  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
206  1 when(xwiki.getDeletedDocument(any(), any(), anyInt(), any(XWikiContext.class))).thenReturn(
207    deletedDocument);
208   
209    // Document to restore is a translation.
210  1 when(deletedDocument.getLanguage()).thenReturn("ro");
211   
212  1 DocumentReference translationDocumentReference =
213    new DocumentReference(document.getDocumentReference(), LocaleUtils.toLocale(deletedDocument.getLanguage(),
214    Locale.ROOT));
215  1 when(xwiki.exists(translationDocumentReference, context)).thenReturn(false);
216   
217  1 assertFalse(undeleteAction.action(context));
218   
219    // Make sure that the main document is not checked for existence, but the translated document which we actually
220    // want to restore.
221  1 verify(xwiki, Mockito.never()).exists(document.getDocumentReference(), context);
222  1 verify(xwiki).exists(translationDocumentReference, context);
223   
224  1 verify(xwiki).restoreFromRecycleBin(document, 13, "Restored from recycle bin", context);
225    }
226    }