1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.ui

File LanguageTest.java

 
 

Code metrics

4
67
12
1
243
149
14
0.21
5.58
12
1.17

Classes

Class Line # Actions
LanguageTest 51 67 0% 14 29
0.650602465.1%
 

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 org.xwiki.test.ui;
21   
22    import java.util.Arrays;
23    import java.util.HashSet;
24    import java.util.List;
25    import java.util.Locale;
26   
27    import org.junit.AfterClass;
28    import org.junit.Assert;
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.openqa.selenium.By;
33    import org.openqa.selenium.WebElement;
34    import org.xwiki.administration.test.po.AdministrationPage;
35    import org.xwiki.administration.test.po.LocalizationAdministrationSectionPage;
36    import org.xwiki.model.reference.LocalDocumentReference;
37    import org.xwiki.rest.model.jaxb.Page;
38    import org.xwiki.test.ui.browser.IgnoreBrowser;
39    import org.xwiki.test.ui.po.ViewPage;
40    import org.xwiki.test.ui.po.editor.WikiEditPage;
41   
42    import static org.junit.Assert.assertEquals;
43    import static org.junit.Assert.assertTrue;
44   
45    /**
46    * Verify the ability to change the language.
47    *
48    * @version $Id: f895218f1b30c7541ee4321248aefacf8eb86961 $
49    * @since 2.4RC1
50    */
 
51    public class LanguageTest extends AbstractTest
52    {
53    @Rule
54    public AdminAuthenticationRule adminAuthenticationRule = new AdminAuthenticationRule(getUtil());
55   
56    /**
57    * Make sure we set back the language to monolingual and english for other tests that come thereafter
58    */
 
59  1 toggle @AfterClass
60    public static void afterClass()
61    {
62  1 setLanguageSettings(false, "en");
63    }
64   
65    /**
66    * Ensure the default language is English and that the wiki is in monolingual mode
67    */
 
68  5 toggle @Before
69    public void before() throws Exception
70    {
71  5 setLanguageSettings(false, "en");
72    }
73   
 
74  1 toggle @Test
75    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146")
76    public void testChangeLanguageInMonolingualModeUsingTheAdministrationPreference()
77    {
78  1 WikiEditPage edit = WikiEditPage.gotoPage("Test", "LanguageTest");
79  1 edit.setContent("{{velocity}}context = ($xcontext.language), doc = ($doc.language), "
80    + "default = ($doc.defaultLanguage), tdoc = ($tdoc.language), "
81    + "tdocdefault = ($tdoc.defaultLanguage){{/velocity}}");
82  1 ViewPage vp = edit.clickSaveAndView();
83   
84    // Current language must be "en"
85  1 Assert.assertEquals("Invalid content", vp.getContent(),
86    "context = (en), doc = (), default = (en), tdoc = (), tdocdefault = (en)");
87   
88    // Change default language to "fr"
89  1 AdministrationPage adminPage = AdministrationPage.gotoPage();
90  1 LocalizationAdministrationSectionPage sectionPage = adminPage.clickLocalizationSection();
91  1 sectionPage.setDefaultLanguage("fr");
92  1 sectionPage.clickSave();
93   
94    // Now language must be "fr"
95  1 vp = getUtil().gotoPage("Test", "LanguageTest");
96  1 Assert.assertTrue("Page not in French!", isPageInFrench());
97  1 Assert.assertEquals("Invalid content", vp.getContent(),
98    "context = (fr), doc = (), default = (en), tdoc = (), tdocdefault = (en)");
99    }
100   
 
101  1 toggle @Test
102    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146")
103    public void testPassingLanguageInRequestHasNoEffectInMonoligualMode()
104    {
105  1 getUtil().gotoPage("Main", "WebHome", "view", "language=fr");
106  1 Assert.assertTrue("Page not in English!", isPageInEnglish());
107    }
108   
 
109  1 toggle @Test
110    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146")
111    public void testChangeLanguageInMultilingualModeUsingTheLanguageRequestParameter()
112    {
113  1 setLanguageSettings(true, "en");
114   
115  1 getUtil().gotoPage("Main", "WebHome", "view", "language=fr");
116  1 Assert.assertTrue("Page not in French!", isPageInFrench());
117    }
118   
 
119  1 toggle @Test
120    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146")
121    public void testHeaderCorrectLanguage()
122    {
123  1 setLanguageSettings(true, "en");
124   
125    // if we don't use language=default, the value stored in cookies is used, which might be wrong
126  1 getUtil().gotoPage("Main", "Test", "view", "language=default");
127  1 checkLanguageTagsArePresent("en");
128   
129  1 getUtil().gotoPage("Main", "Test", "view", "language=fr");
130  1 checkLanguageTagsArePresent("fr");
131    }
132   
 
133  0 toggle @Test
134    public void testTranslateDocument() throws Exception
135    {
136  0 LocalDocumentReference referenceDEFAULT = new LocalDocumentReference("LanguageTest", "Page");
137  0 LocalDocumentReference referenceFR = new LocalDocumentReference(referenceDEFAULT, Locale.FRENCH);
138   
139    // Cleanup
140  0 getUtil().rest().delete(referenceFR);
141  0 getUtil().rest().delete(referenceDEFAULT);
142   
143    // Set 2 locales
144  0 setLanguageSettings(true, "en", Arrays.asList("en", "fr"));
145   
146    // Create default version
147  0 ViewPage viewPage = getUtil().createPage("LanguageTest", "Page", "en content", "en title");
148   
149    // Edit the page
150  0 WikiEditPage editPage = viewPage.editWiki();
151   
152    // Make sure current translation is the right one
153  0 Test failure here assertTrue(getDriver().hasElement(By.xpath("//strong[text()='You are editing the original page (en).']")));
154   
155  0 assertEquals(Arrays.asList(Locale.FRENCH), editPage.getNotExistingLocales());
156  0 assertEquals(Arrays.asList(), editPage.getExistingLocales());
157   
158    // Translated to French
159  0 editPage = editPage.clickTranslate("fr");
160  0 editPage.setTitle("titre fr");
161  0 editPage.setContent("contenu fr");
162   
163  0 viewPage = editPage.clickSaveAndView();
164   
165    // Make sure both have the right content
166  0 Page page = getUtil().rest().get(referenceFR);
167  0 assertEquals("titre fr", page.getTitle());
168  0 assertEquals("contenu fr", page.getContent());
169  0 page = getUtil().rest().get(referenceDEFAULT);
170  0 assertEquals("en title", page.getTitle());
171  0 assertEquals("en content", page.getContent());
172   
173    // Make sure two locales are listed for this page in the UI
174  0 assertEquals(new HashSet<>(Arrays.asList(Locale.ENGLISH, Locale.FRENCH)), new HashSet<>(viewPage.getLocales()));
175   
176    // Switch to en
177  0 viewPage.clickLocale(Locale.ENGLISH);
178   
179    // Verify edit mode informations
180  0 editPage = viewPage.editWiki();
181   
182  0 assertEquals(Arrays.asList(), editPage.getNotExistingLocales());
183  0 assertEquals(Arrays.asList(Locale.FRENCH), editPage.getExistingLocales());
184    }
185   
186    /**
187    * Assert that the given <code>language</code> is present in various attributes and tags on the page
188    *
189    * @param language the language to use, should be a valid language, e.g. "en"
190    */
 
191  2 toggle private void checkLanguageTagsArePresent(String language)
192    {
193  2 WebElement html = getDriver().findElement(By.tagName("html"));
194  2 Assert.assertEquals(language, html.getAttribute("lang"));
195  2 Assert.assertEquals(language, html.getAttribute("xml:lang"));
196   
197  2 ViewPage vp = new ViewPage();
198  2 Assert.assertEquals("locale=" + language, vp.getHTMLMetaDataValue("gwt:property"));
199    // For retro-compatibility only
200  2 Assert.assertEquals(language, vp.getHTMLMetaDataValue("language"));
201   
202  2 String content = getDriver().getPageSource();
203  2 Assert.assertTrue(content.contains("language=" + language));
204    }
205   
206    /**
207    * Check if the currently displayed page is in English, by looking at the "Log-Out" link
208    */
 
209  1 toggle private boolean isPageInEnglish()
210    {
211  1 return getDriver().findElement(By.className("xdocLastModification")).getText().toLowerCase()
212    .contains("last modified by");
213    }
214   
215    /**
216    * Check if the currently displayed page is in French, by looking at the "Log-Out" link
217    */
 
218  2 toggle private boolean isPageInFrench()
219    {
220  2 return getDriver().findElement(By.className("xdocLastModification")).getText().toLowerCase()
221    .contains("modifié par");
222    }
223   
 
224  8 toggle private static void setLanguageSettings(boolean isMultiLingual, String defaultLanguage)
225    {
226  8 setLanguageSettings(isMultiLingual, defaultLanguage, null);
227    }
228   
 
229  9 toggle private static void setLanguageSettings(boolean isMultiLingual, String defaultLanguage,
230    List<String> supportedLanguages)
231    {
232  9 AdministrationPage adminPage = AdministrationPage.gotoPage();
233  9 LocalizationAdministrationSectionPage sectionPage = adminPage.clickLocalizationSection();
234  9 sectionPage.setMultiLingual(isMultiLingual);
235  9 if (defaultLanguage != null) {
236  9 sectionPage.setDefaultLanguage(defaultLanguage);
237    }
238  8 if (supportedLanguages != null) {
239  0 sectionPage.setSupportedLanguages(supportedLanguages);
240    }
241  9 sectionPage.clickSave();
242    }
243    }