1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
48 |
|
|
49 |
|
@version |
50 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (66) |
Complexity: 6 |
Complexity Density: 0.1 |
|
51 |
|
public class UndeleteActionTest |
52 |
|
{ |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
@Rule |
57 |
|
public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule(); |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private UndeleteAction undeleteAction = new UndeleteAction(); |
63 |
|
|
64 |
|
|
65 |
|
@link |
66 |
|
|
67 |
|
private XWikiContext context = mock(XWikiContext.class); |
68 |
|
|
69 |
|
|
70 |
|
@link |
71 |
|
|
72 |
|
private XWiki xwiki = mock(XWiki.class); |
73 |
|
|
74 |
|
|
75 |
|
@link |
76 |
|
|
77 |
|
private XWikiDocument document = mock(XWikiDocument.class); |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
79 |
5 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
93 |
1 |
@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 |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
105 |
1 |
@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 |
|
|
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
134 |
1 |
@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 |
|
|
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
163 |
1 |
@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 |
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
192 |
1 |
@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 |
|
|
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 |
|
|
220 |
|
|
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 |
|
} |