1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.lesscss.internal.colortheme; |
21 |
|
|
22 |
|
import javax.inject.Provider; |
23 |
|
|
24 |
|
import org.junit.Before; |
25 |
|
import org.junit.Rule; |
26 |
|
import org.junit.Test; |
27 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
28 |
|
import org.xwiki.model.reference.DocumentReference; |
29 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
30 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
31 |
|
import org.xwiki.model.reference.WikiReference; |
32 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
33 |
|
import org.xwiki.security.authorization.Right; |
34 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
35 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
36 |
|
|
37 |
|
import com.xpn.xwiki.XWiki; |
38 |
|
import com.xpn.xwiki.XWikiContext; |
39 |
|
import com.xpn.xwiki.web.XWikiRequest; |
40 |
|
|
41 |
|
import static org.junit.Assert.assertEquals; |
42 |
|
import static org.mockito.ArgumentMatchers.any; |
43 |
|
import static org.mockito.ArgumentMatchers.eq; |
44 |
|
import static org.mockito.Mockito.mock; |
45 |
|
import static org.mockito.Mockito.when; |
46 |
|
|
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
@since |
51 |
|
@version |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (37) |
Complexity: 5 |
Complexity Density: 0.16 |
|
53 |
|
public class CurrentColorThemeGetterTest |
54 |
|
{ |
55 |
|
@Rule |
56 |
|
public MockitoComponentMockingRule<CurrentColorThemeGetter> mocker = |
57 |
|
new MockitoComponentMockingRule<>(CurrentColorThemeGetter.class); |
58 |
|
|
59 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
60 |
|
|
61 |
|
private EntityReferenceSerializer<String> entityReferenceSerializer; |
62 |
|
|
63 |
|
private Provider<XWikiContext> xcontextProvider; |
64 |
|
|
65 |
|
private WikiDescriptorManager wikiDescriptorManager; |
66 |
|
|
67 |
|
private AuthorizationManager authorizationManager; |
68 |
|
|
69 |
|
private XWikiContext xcontext; |
70 |
|
|
71 |
|
private XWiki xwiki; |
72 |
|
|
73 |
|
private XWikiRequest request; |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 1 |
Complexity Density: 0.05 |
|
75 |
4 |
@Before... |
76 |
|
public void setUp() throws Exception |
77 |
|
{ |
78 |
4 |
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class); |
79 |
4 |
authorizationManager = mocker.getInstance(AuthorizationManager.class); |
80 |
4 |
documentReferenceResolver = mocker.getInstance(new DefaultParameterizedType(null, DocumentReferenceResolver.class, |
81 |
|
String.class)); |
82 |
4 |
entityReferenceSerializer = mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceSerializer.class, |
83 |
|
String.class)); |
84 |
4 |
xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
85 |
4 |
xcontext = mock(XWikiContext.class); |
86 |
4 |
when(xcontextProvider.get()).thenReturn(xcontext); |
87 |
4 |
xwiki = mock(XWiki.class); |
88 |
4 |
when(xcontext.getWiki()).thenReturn(xwiki); |
89 |
|
|
90 |
4 |
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wikiId"); |
91 |
4 |
request = mock(XWikiRequest.class); |
92 |
4 |
when(xcontext.getRequest()).thenReturn(request); |
93 |
4 |
DocumentReference colorThemeReference = new DocumentReference("wikiId", "XWiki", "MyColorTheme"); |
94 |
4 |
WikiReference mainWikiReference = new WikiReference("wikiId"); |
95 |
4 |
when(documentReferenceResolver.resolve(eq("myColorTheme"), eq(mainWikiReference))).thenReturn(colorThemeReference); |
96 |
4 |
when(entityReferenceSerializer.serialize(colorThemeReference)).thenReturn("wikiId:ColorTheme.MyColorTheme"); |
97 |
4 |
when(xwiki.exists(colorThemeReference, xcontext)).thenReturn(true); |
98 |
4 |
DocumentReference currentUser = new DocumentReference("xwiki", "XWiki", "CurrentUser"); |
99 |
4 |
when(xcontext.getUserReference()).thenReturn(currentUser); |
100 |
4 |
when(authorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class), |
101 |
|
any(DocumentReference.class))).thenReturn(true); |
102 |
|
} |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
104 |
1 |
@Test... |
105 |
|
public void getCurrentColorThemeTestWhenRequestParameter() throws Exception |
106 |
|
{ |
107 |
1 |
when(request.getParameter("colorTheme")).thenReturn("myColorTheme"); |
108 |
1 |
assertEquals("wikiId:ColorTheme.MyColorTheme", mocker.getComponentUnderTest().getCurrentColorTheme("default")); |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
111 |
1 |
@Test... |
112 |
|
public void getCurrentColorThemeTestWhenNoRequestParameter() throws Exception |
113 |
|
{ |
114 |
1 |
when(xwiki.getUserPreference(eq("colorTheme"), any(XWikiContext.class))).thenReturn("myColorTheme"); |
115 |
1 |
assertEquals("wikiId:ColorTheme.MyColorTheme", mocker.getComponentUnderTest().getCurrentColorTheme("default")); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
118 |
1 |
@Test... |
119 |
|
public void getCurrentColorThemeFallbackTest() throws Exception |
120 |
|
{ |
121 |
1 |
when(request.getParameter("colorTheme")).thenReturn("myColorTheme"); |
122 |
1 |
when(xwiki.exists(any(DocumentReference.class), eq(xcontext))).thenReturn(false); |
123 |
1 |
assertEquals("fallback", mocker.getComponentUnderTest().getCurrentColorTheme("fallback")); |
124 |
1 |
assertEquals("error", mocker.getComponentUnderTest().getCurrentColorTheme("error")); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
127 |
1 |
@Test... |
128 |
|
public void getCurrentColorWithAndWithoutRight() throws Exception |
129 |
|
{ |
130 |
1 |
when(request.getParameter("colorTheme")).thenReturn("myColorTheme"); |
131 |
1 |
when(authorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class), any(DocumentReference.class))) |
132 |
|
.thenReturn(false); |
133 |
1 |
assertEquals("fallback", mocker.getComponentUnderTest().getCurrentColorTheme(true, "fallback")); |
134 |
1 |
assertEquals("wikiId:ColorTheme.MyColorTheme", |
135 |
|
mocker.getComponentUnderTest().getCurrentColorTheme(false, "fallback")); |
136 |
|
} |
137 |
|
} |