1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.test.ui

File LanguageTest.java

 

Code metrics

4
68
13
1
249
153
15
0.22
5.23
13
1.15

Classes

Class Line # Actions
LanguageTest 51 68 0% 15 1
0.988235398.8%
 

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: 056b9a1ffd2dea404088d82c9bb1ec11bbc51f08 $
49    * @since 2.4RC1
50    */
 
51    public class LanguageTest extends AbstractTest
52    {
53    @Rule
54    public AdminAuthenticationRule adminAuthenticationRule = new AdminAuthenticationRule(true, 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 reset();
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 reset();
72    }
73   
 
74  6 toggle private static void reset()
75    {
76    // Reset default language configuration
77  6 setLanguageSettings(false, "en");
78    // Reset current language
79  6 getDriver().manage().deleteCookieNamed("language");
80    }
81   
 
82  1 toggle @Test
83    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See https://jira.xwiki.org/browse/XE-1146")
84    public void testChangeLanguageInMonolingualModeUsingTheAdministrationPreference()
85    {
86  1 WikiEditPage edit = WikiEditPage.gotoPage("Test", "LanguageTest");
87  1 edit.setContent("{{velocity}}context = ($xcontext.language), doc = ($doc.language), "
88    + "default = ($doc.defaultLanguage), tdoc = ($tdoc.language), "
89    + "tdocdefault = ($tdoc.defaultLanguage){{/velocity}}");
90  1 ViewPage vp = edit.clickSaveAndView();
91   
92    // Current language must be "en"
93  1 Assert.assertEquals("Invalid content", vp.getContent(),
94    "context = (en), doc = (), default = (en), tdoc = (), tdocdefault = (en)");
95   
96    // Change default language to "fr"
97  1 AdministrationPage adminPage = AdministrationPage.gotoPage();
98  1 LocalizationAdministrationSectionPage sectionPage = adminPage.clickLocalizationSection();
99  1 sectionPage.setDefaultLanguage("fr");
100  1 sectionPage.clickSave();
101   
102    // Now language must be "fr"
103  1 vp = getUtil().gotoPage("Test", "LanguageTest");
104  1 Assert.assertTrue("Page not in French!", isPageInFrench());
105  1 Assert.assertEquals("Invalid content", vp.getContent(),
106    "context = (fr), doc = (), default = (en), tdoc = (), tdocdefault = (en)");
107    }
108   
 
109  1 toggle @Test
110    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See https://jira.xwiki.org/browse/XE-1146")
111    public void testPassingLanguageInRequestHasNoEffectInMonoligualMode()
112    {
113  1 getUtil().gotoPage("Main", "WebHome", "view", "language=fr");
114  1 Assert.assertTrue("Page not in English!", isPageInEnglish());
115    }
116   
 
117  1 toggle @Test
118    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See https://jira.xwiki.org/browse/XE-1146")
119    public void testChangeLanguageInMultilingualModeUsingTheLanguageRequestParameter()
120    {
121  1 setLanguageSettings(true, "en");
122   
123  1 getUtil().gotoPage("Main", "WebHome", "view", "language=fr");
124  1 Assert.assertTrue("Page not in French!", isPageInFrench());
125    }
126   
 
127  1 toggle @Test
128    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See https://jira.xwiki.org/browse/XE-1146")
129    public void testHeaderCorrectLanguage()
130    {
131  1 setLanguageSettings(true, "en");
132   
133  1 getUtil().gotoPage("Main", "Test", "view");
134  1 checkLanguageTagsArePresent("en");
135   
136  1 getUtil().gotoPage("Main", "Test", "view", "language=fr");
137  1 checkLanguageTagsArePresent("fr");
138    }
139   
 
140  1 toggle @Test
141    public void testTranslateDocument() throws Exception
142    {
143  1 LocalDocumentReference referenceDEFAULT = new LocalDocumentReference("LanguageTest", "Page");
144  1 LocalDocumentReference referenceFR = new LocalDocumentReference(referenceDEFAULT, Locale.FRENCH);
145   
146    // Cleanup
147  1 getUtil().rest().delete(referenceFR);
148  1 getUtil().rest().delete(referenceDEFAULT);
149   
150    // Set 2 locales
151  1 setLanguageSettings(true, "en", Arrays.asList("en", "fr"));
152   
153    // Create default version
154  1 ViewPage viewPage = getUtil().createPage("LanguageTest", "Page", "en content", "en title");
155   
156    // Edit the page
157  1 WikiEditPage editPage = viewPage.editWiki();
158   
159    // Make sure current translation is the right one
160  1 assertTrue(getDriver().hasElement(By.xpath("//strong[text()='You are editing the original page (en).']")));
161   
162  1 assertEquals(Arrays.asList(Locale.FRENCH), editPage.getNotExistingLocales());
163  1 assertEquals(Arrays.asList(), editPage.getExistingLocales());
164   
165    // Translated to French
166  1 editPage = editPage.clickTranslate("fr");
167  1 editPage.setTitle("titre fr");
168  1 editPage.setContent("contenu fr");
169   
170  1 viewPage = editPage.clickSaveAndView();
171   
172    // Make sure both have the right content
173  1 Page page = getUtil().rest().get(referenceFR);
174  1 assertEquals("titre fr", page.getTitle());
175  1 assertEquals("contenu fr", page.getContent());
176  1 page = getUtil().rest().get(referenceDEFAULT);
177  1 assertEquals("en title", page.getTitle());
178  1 assertEquals("en content", page.getContent());
179   
180    // Make sure two locales are listed for this page in the UI
181  1 assertEquals(new HashSet<>(Arrays.asList(Locale.ENGLISH, Locale.FRENCH)), new HashSet<>(viewPage.getLocales()));
182   
183    // Switch to en
184  1 viewPage.clickLocale(Locale.ENGLISH);
185   
186    // Verify edit mode informations
187  1 editPage = viewPage.editWiki();
188   
189  1 assertEquals(Arrays.asList(), editPage.getNotExistingLocales());
190  1 assertEquals(Arrays.asList(Locale.FRENCH), editPage.getExistingLocales());
191    }
192   
193    /**
194    * Assert that the given <code>language</code> is present in various attributes and tags on the page
195    *
196    * @param language the language to use, should be a valid language, e.g. "en"
197    */
 
198  2 toggle private void checkLanguageTagsArePresent(String language)
199    {
200  2 WebElement html = getDriver().findElement(By.tagName("html"));
201  2 Assert.assertEquals(language, html.getAttribute("lang"));
202  2 Assert.assertEquals(language, html.getAttribute("xml:lang"));
203   
204  2 ViewPage vp = new ViewPage();
205    // For retro-compatibility only
206  2 Assert.assertEquals(language, vp.getHTMLMetaDataValue("language"));
207   
208  2 String content = getDriver().getPageSource();
209  2 Assert.assertTrue(content.contains("language=" + language));
210    }
211   
212    /**
213    * Check if the currently displayed page is in English, by looking at the "Log-Out" link
214    */
 
215  1 toggle private boolean isPageInEnglish()
216    {
217  1 return getDriver().findElement(By.className("xdocLastModification")).getText().toLowerCase()
218    .contains("last modified by");
219    }
220   
221    /**
222    * Check if the currently displayed page is in French, by looking at the "Log-Out" link
223    */
 
224  2 toggle private boolean isPageInFrench()
225    {
226  2 return getDriver().findElement(By.className("xdocLastModification")).getText().toLowerCase()
227    .contains("modifié par");
228    }
229   
 
230  8 toggle private static void setLanguageSettings(boolean isMultiLingual, String defaultLanguage)
231    {
232  8 setLanguageSettings(isMultiLingual, defaultLanguage, null);
233    }
234   
 
235  9 toggle private static void setLanguageSettings(boolean isMultiLingual, String defaultLanguage,
236    List<String> supportedLanguages)
237    {
238  9 AdministrationPage adminPage = AdministrationPage.gotoPage();
239  9 LocalizationAdministrationSectionPage sectionPage = adminPage.clickLocalizationSection();
240  9 sectionPage.setMultiLingual(isMultiLingual);
241  9 if (defaultLanguage != null) {
242  9 sectionPage.setDefaultLanguage(defaultLanguage);
243    }
244  9 if (supportedLanguages != null) {
245  1 sectionPage.setSupportedLanguages(supportedLanguages);
246    }
247  9 sectionPage.clickSave();
248    }
249    }