locale.test 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289
  1. <?php
  2. /**
  3. * @file
  4. * Tests for locale.module.
  5. *
  6. * The test file includes:
  7. * - a functional test for the language configuration forms;
  8. * - functional tests for the translation functionalities, including searching;
  9. * - a functional test for the PO files import feature, including validation;
  10. * - functional tests for translations and templates export feature;
  11. * - functional tests for the uninstall process;
  12. * - a functional test for the language switching feature;
  13. * - a functional test for a user's ability to change their default language;
  14. * - a functional test for configuring a different path alias per language;
  15. * - a functional test for configuring a different path alias per language;
  16. * - a functional test for multilingual support by content type and on nodes.
  17. * - a functional test for multilingual fields.
  18. * - a functional test for comment language.
  19. * - a functional test fot language types/negotiation info.
  20. */
  21. /**
  22. * Functional tests for the language configuration forms.
  23. */
  24. class LocaleConfigurationTest extends DrupalWebTestCase {
  25. public static function getInfo() {
  26. return array(
  27. 'name' => 'Language configuration',
  28. 'description' => 'Adds a new locale and tests changing its status and the default language.',
  29. 'group' => 'Locale',
  30. );
  31. }
  32. function setUp() {
  33. parent::setUp('locale');
  34. }
  35. /**
  36. * Functional tests for adding, editing and deleting languages.
  37. */
  38. function testLanguageConfiguration() {
  39. global $base_url;
  40. // User to add and remove language.
  41. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  42. $this->drupalLogin($admin_user);
  43. // Add predefined language.
  44. $edit = array(
  45. 'langcode' => 'fr',
  46. );
  47. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  48. $this->assertText('fr', 'Language added successfully.');
  49. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  50. // Add custom language.
  51. // Code for the language.
  52. $langcode = 'xx';
  53. // The English name for the language.
  54. $name = $this->randomName(16);
  55. // The native name for the language.
  56. $native = $this->randomName(16);
  57. // The domain prefix.
  58. $prefix = $langcode;
  59. $edit = array(
  60. 'langcode' => $langcode,
  61. 'name' => $name,
  62. 'native' => $native,
  63. 'prefix' => $prefix,
  64. 'direction' => '0',
  65. );
  66. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  67. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  68. $this->assertText($langcode, 'Language code found.');
  69. $this->assertText($name, 'Name found.');
  70. $this->assertText($native, 'Native found.');
  71. $this->assertText($native, 'Test language added.');
  72. // Check if we can change the default language.
  73. $path = 'admin/config/regional/language';
  74. $this->drupalGet($path);
  75. $this->assertFieldChecked('edit-site-default-en', 'English is the default language.');
  76. // Change the default language.
  77. $edit = array(
  78. 'site_default' => $langcode,
  79. );
  80. $this->drupalPost(NULL, $edit, t('Save configuration'));
  81. $this->assertNoFieldChecked('edit-site-default-en', 'Default language updated.');
  82. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  83. // Check if a valid language prefix is added after changing the default
  84. // language.
  85. $this->drupalGet('admin/config/regional/language/edit/en');
  86. $this->assertFieldByXPath('//input[@name="prefix"]', 'en', 'A valid path prefix has been added to the previous default language.');
  87. // Ensure we can't delete the default language.
  88. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  89. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  90. $this->assertText(t('The default language cannot be deleted.'), 'Failed to delete the default language.');
  91. // Check if we can disable a language.
  92. $edit = array(
  93. 'enabled[en]' => FALSE,
  94. );
  95. $this->drupalPost($path, $edit, t('Save configuration'));
  96. $this->assertNoFieldChecked('edit-enabled-en', 'Language disabled.');
  97. // Set disabled language to be the default and ensure it is re-enabled.
  98. $edit = array(
  99. 'site_default' => 'en',
  100. );
  101. $this->drupalPost(NULL, $edit, t('Save configuration'));
  102. $this->assertFieldChecked('edit-enabled-en', 'Default language re-enabled.');
  103. // Ensure 'edit' link works.
  104. $this->clickLink(t('edit'));
  105. $this->assertTitle(t('Edit language | Drupal'), 'Page title is "Edit language".');
  106. // Edit a language.
  107. $name = $this->randomName(16);
  108. $edit = array(
  109. 'name' => $name,
  110. );
  111. $this->drupalPost('admin/config/regional/language/edit/' . $langcode, $edit, t('Save language'));
  112. $this->assertRaw($name, 'The language has been updated.');
  113. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  114. // Ensure 'delete' link works.
  115. $this->drupalGet('admin/config/regional/language');
  116. $this->clickLink(t('delete'));
  117. $this->assertText(t('Are you sure you want to delete the language'), '"delete" link is correct.');
  118. // Delete an enabled language.
  119. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  120. // First test the 'cancel' link.
  121. $this->clickLink(t('Cancel'));
  122. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  123. $this->assertRaw($name, 'The language was not deleted.');
  124. // Delete the language for real. This a confirm form, we do not need any
  125. // fields changed.
  126. $this->drupalPost('admin/config/regional/language/delete/' . $langcode, array(), t('Delete'));
  127. // We need raw here because %locale will add HTML.
  128. $this->assertRaw(t('The language %locale has been removed.', array('%locale' => $name)), 'The test language has been removed.');
  129. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  130. // Verify that language is no longer found.
  131. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  132. $this->assertResponse(404, 'Language no longer found.');
  133. // Make sure the "language_count" variable has been updated correctly.
  134. drupal_static_reset('language_list');
  135. $enabled = language_list('enabled');
  136. $this->assertEqual(variable_get('language_count', 1), count($enabled[1]), 'Language count is correct.');
  137. // Delete a disabled language.
  138. // Disable an enabled language.
  139. $edit = array(
  140. 'enabled[fr]' => FALSE,
  141. );
  142. $this->drupalPost($path, $edit, t('Save configuration'));
  143. $this->assertNoFieldChecked('edit-enabled-fr', 'French language disabled.');
  144. // Get the count of enabled languages.
  145. drupal_static_reset('language_list');
  146. $enabled = language_list('enabled');
  147. // Delete the disabled language.
  148. $this->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete'));
  149. // We need raw here because %locale will add HTML.
  150. $this->assertRaw(t('The language %locale has been removed.', array('%locale' => 'French')), 'Disabled language has been removed.');
  151. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  152. // Verify that language is no longer found.
  153. $this->drupalGet('admin/config/regional/language/delete/fr');
  154. $this->assertResponse(404, 'Language no longer found.');
  155. // Make sure the "language_count" variable has not changed.
  156. $this->assertEqual(variable_get('language_count', 1), count($enabled[1]), 'Language count is correct.');
  157. // Ensure we can't delete the English language.
  158. $this->drupalGet('admin/config/regional/language/delete/en');
  159. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  160. $this->assertText(t('The English language cannot be deleted.'), 'Failed to delete English language.');
  161. }
  162. }
  163. /**
  164. * Tests localization of the JavaScript libraries.
  165. *
  166. * Currently, only the jQuery datepicker is localized using Drupal translations.
  167. */
  168. class LocaleLibraryInfoAlterTest extends DrupalWebTestCase {
  169. public static function getInfo() {
  170. return array(
  171. 'name' => 'Javascript library localisation',
  172. 'description' => 'Tests the localisation of JavaScript libraries.',
  173. 'group' => 'Locale',
  174. );
  175. }
  176. function setUp() {
  177. parent::setUp('locale', 'locale_test');
  178. }
  179. /**
  180. * Verifies that the datepicker can be localized.
  181. *
  182. * @see locale_library_info_alter()
  183. */
  184. public function testLibraryInfoAlter() {
  185. drupal_add_library('system', 'ui.datepicker');
  186. $scripts = drupal_get_js();
  187. $this->assertTrue(strpos($scripts, 'locale.datepicker.js'), 'locale.datepicker.js added to scripts.');
  188. }
  189. }
  190. /**
  191. * Functional tests for JavaScript parsing for translatable strings.
  192. */
  193. class LocaleJavascriptTranslationTest extends DrupalWebTestCase {
  194. public static function getInfo() {
  195. return array(
  196. 'name' => 'Javascript translation',
  197. 'description' => 'Tests parsing js files for translatable strings',
  198. 'group' => 'Locale',
  199. );
  200. }
  201. function setUp() {
  202. parent::setUp('locale', 'locale_test');
  203. }
  204. function testFileParsing() {
  205. $filename = drupal_get_path('module', 'locale_test') . '/locale_test.js';
  206. // Parse the file to look for source strings.
  207. _locale_parse_js_file($filename);
  208. // Get all of the source strings that were found.
  209. $source_strings = db_select('locales_source', 's')
  210. ->fields('s', array('source', 'context'))
  211. ->condition('s.location', $filename)
  212. ->execute()
  213. ->fetchAllKeyed();
  214. // List of all strings that should be in the file.
  215. $test_strings = array(
  216. "Standard Call t" => '',
  217. "Whitespace Call t" => '',
  218. "Single Quote t" => '',
  219. "Single Quote \\'Escaped\\' t" => '',
  220. "Single Quote Concat strings t" => '',
  221. "Double Quote t" => '',
  222. "Double Quote \\\"Escaped\\\" t" => '',
  223. "Double Quote Concat strings t" => '',
  224. "Context !key Args t" => "Context string",
  225. "Context Unquoted t" => "Context string unquoted",
  226. "Context Single Quoted t" => "Context string single quoted",
  227. "Context Double Quoted t" => "Context string double quoted",
  228. "Standard Call plural" => '',
  229. "Standard Call @count plural" => '',
  230. "Whitespace Call plural" => '',
  231. "Whitespace Call @count plural" => '',
  232. "Single Quote plural" => '',
  233. "Single Quote @count plural" => '',
  234. "Single Quote \\'Escaped\\' plural" => '',
  235. "Single Quote \\'Escaped\\' @count plural" => '',
  236. "Double Quote plural" => '',
  237. "Double Quote @count plural" => '',
  238. "Double Quote \\\"Escaped\\\" plural" => '',
  239. "Double Quote \\\"Escaped\\\" @count plural" => '',
  240. "Context !key Args plural" => "Context string",
  241. "Context !key Args @count plural" => "Context string",
  242. "Context Unquoted plural" => "Context string unquoted",
  243. "Context Unquoted @count plural" => "Context string unquoted",
  244. "Context Single Quoted plural" => "Context string single quoted",
  245. "Context Single Quoted @count plural" => "Context string single quoted",
  246. "Context Double Quoted plural" => "Context string double quoted",
  247. "Context Double Quoted @count plural" => "Context string double quoted",
  248. );
  249. // Assert that all strings were found properly.
  250. foreach ($test_strings as $str => $context) {
  251. $args = array('%source' => $str, '%context' => $context);
  252. // Make sure that the string was found in the file.
  253. $this->assertTrue(isset($source_strings[$str]), format_string('Found source string: %source', $args));
  254. // Make sure that the proper context was matched.
  255. $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, strlen($context) > 0 ? format_string('Context for %source is %context', $args) : format_string('Context for %source is blank', $args));
  256. }
  257. $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.');
  258. }
  259. }
  260. /**
  261. * Functional test for string translation and validation.
  262. */
  263. class LocaleTranslationFunctionalTest extends DrupalWebTestCase {
  264. public static function getInfo() {
  265. return array(
  266. 'name' => 'String translate, search and validate',
  267. 'description' => 'Adds a new locale and translates its name. Checks the validation of translation strings and search results.',
  268. 'group' => 'Locale',
  269. );
  270. }
  271. function setUp() {
  272. parent::setUp('locale');
  273. }
  274. /**
  275. * Adds a language and tests string translation by users with the appropriate permissions.
  276. */
  277. function testStringTranslation() {
  278. global $base_url;
  279. // User to add and remove language.
  280. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  281. // User to translate and delete string.
  282. $translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
  283. // Code for the language.
  284. $langcode = 'xx';
  285. // The English name for the language. This will be translated.
  286. $name = $this->randomName(16);
  287. // The native name for the language.
  288. $native = $this->randomName(16);
  289. // The domain prefix.
  290. $prefix = $langcode;
  291. // This is the language indicator on the translation search screen for
  292. // untranslated strings. Copied straight from locale.inc.
  293. $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  294. // This will be the translation of $name.
  295. $translation = $this->randomName(16);
  296. // Add custom language.
  297. $this->drupalLogin($admin_user);
  298. $edit = array(
  299. 'langcode' => $langcode,
  300. 'name' => $name,
  301. 'native' => $native,
  302. 'prefix' => $prefix,
  303. 'direction' => '0',
  304. );
  305. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  306. // Add string.
  307. t($name, array(), array('langcode' => $langcode));
  308. // Reset locale cache.
  309. locale_reset();
  310. $this->assertText($langcode, 'Language code found.');
  311. $this->assertText($name, 'Name found.');
  312. $this->assertText($native, 'Native found.');
  313. // No t() here, we do not want to add this string to the database and it's
  314. // surely not translated yet.
  315. $this->assertText($native, 'Test language added.');
  316. $this->drupalLogout();
  317. // Search for the name and translate it.
  318. $this->drupalLogin($translate_user);
  319. $search = array(
  320. 'string' => $name,
  321. 'language' => 'all',
  322. 'translation' => 'all',
  323. 'group' => 'all',
  324. );
  325. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  326. // assertText() seems to remove the input field where $name always could be
  327. // found, so this is not a false assert. See how assertNoText succeeds
  328. // later.
  329. $this->assertText($name, 'Search found the name.');
  330. $this->assertRaw($language_indicator, 'Name is untranslated.');
  331. // Assume this is the only result, given the random name.
  332. $this->clickLink(t('edit'));
  333. // We save the lid from the path.
  334. $matches = array();
  335. preg_match('!admin/config/regional/translate/edit/(\d+)!', $this->getUrl(), $matches);
  336. $lid = $matches[1];
  337. // No t() here, it's surely not translated yet.
  338. $this->assertText($name, 'name found on edit screen.');
  339. $edit = array(
  340. "translations[$langcode]" => $translation,
  341. );
  342. $this->drupalPost(NULL, $edit, t('Save translations'));
  343. $this->assertText(t('The string has been saved.'), 'The string has been saved.');
  344. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  345. $this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, 't() works.');
  346. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  347. // The indicator should not be here.
  348. $this->assertNoRaw($language_indicator, 'String is translated.');
  349. // Verify that a translation set which has an empty target string can be
  350. // updated without any database error.
  351. db_update('locales_target')
  352. ->fields(array('translation' => ''))
  353. ->condition('language', $langcode, '=')
  354. ->condition('lid', $lid, '=')
  355. ->execute();
  356. $this->drupalPost('admin/config/regional/translate/edit/' . $lid, $edit, t('Save translations'));
  357. $this->assertText(t('The string has been saved.'), 'The string has been saved.');
  358. // Try to edit a non-existent string and ensure we're redirected correctly.
  359. // Assuming we don't have 999,999 strings already.
  360. $random_lid = 999999;
  361. $this->drupalGet('admin/config/regional/translate/edit/' . $random_lid);
  362. $this->assertText(t('String not found'), 'String not found.');
  363. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  364. $this->drupalLogout();
  365. // Delete the language.
  366. $this->drupalLogin($admin_user);
  367. $path = 'admin/config/regional/language/delete/' . $langcode;
  368. // This a confirm form, we do not need any fields changed.
  369. $this->drupalPost($path, array(), t('Delete'));
  370. // We need raw here because %locale will add HTML.
  371. $this->assertRaw(t('The language %locale has been removed.', array('%locale' => $name)), 'The test language has been removed.');
  372. // Reload to remove $name.
  373. $this->drupalGet($path);
  374. // Verify that language is no longer found.
  375. $this->assertResponse(404, 'Language no longer found.');
  376. $this->drupalLogout();
  377. // Delete the string.
  378. $this->drupalLogin($translate_user);
  379. $search = array(
  380. 'string' => $name,
  381. 'language' => 'all',
  382. 'translation' => 'all',
  383. 'group' => 'all',
  384. );
  385. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  386. // Assume this is the only result, given the random name.
  387. $this->clickLink(t('delete'));
  388. $this->assertText(t('Are you sure you want to delete the string'), '"delete" link is correct.');
  389. // Delete the string.
  390. $path = 'admin/config/regional/translate/delete/' . $lid;
  391. $this->drupalGet($path);
  392. // First test the 'cancel' link.
  393. $this->clickLink(t('Cancel'));
  394. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  395. $this->assertRaw($name, 'The string was not deleted.');
  396. // Delete the name string.
  397. $this->drupalPost('admin/config/regional/translate/delete/' . $lid, array(), t('Delete'));
  398. $this->assertText(t('The string has been removed.'), 'The string has been removed message.');
  399. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  400. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  401. $this->assertNoText($name, 'Search now can not find the name.');
  402. }
  403. /*
  404. * Adds a language and checks that the JavaScript translation files are
  405. * properly created and rebuilt on deletion.
  406. */
  407. function testJavaScriptTranslation() {
  408. $user = $this->drupalCreateUser(array('translate interface', 'administer languages', 'access administration pages'));
  409. $this->drupalLogin($user);
  410. $langcode = 'xx';
  411. // The English name for the language. This will be translated.
  412. $name = $this->randomName(16);
  413. // The native name for the language.
  414. $native = $this->randomName(16);
  415. // The domain prefix.
  416. $prefix = $langcode;
  417. // Add custom language.
  418. $edit = array(
  419. 'langcode' => $langcode,
  420. 'name' => $name,
  421. 'native' => $native,
  422. 'prefix' => $prefix,
  423. 'direction' => '0',
  424. );
  425. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  426. drupal_static_reset('language_list');
  427. // Build the JavaScript translation file.
  428. $this->drupalGet('admin/config/regional/translate/translate');
  429. // Retrieve the id of the first string available in the {locales_source}
  430. // table and translate it.
  431. $query = db_select('locales_source', 'l');
  432. $query->addExpression('min(l.lid)', 'lid');
  433. $result = $query->condition('l.location', '%.js%', 'LIKE')
  434. ->condition('l.textgroup', 'default')
  435. ->execute();
  436. $url = 'admin/config/regional/translate/edit/' . $result->fetchObject()->lid;
  437. $edit = array('translations['. $langcode .']' => $this->randomName());
  438. $this->drupalPost($url, $edit, t('Save translations'));
  439. // Trigger JavaScript translation parsing and building.
  440. require_once DRUPAL_ROOT . '/includes/locale.inc';
  441. _locale_rebuild_js($langcode);
  442. // Retrieve the JavaScript translation hash code for the custom language to
  443. // check that the translation file has been properly built.
  444. $file = db_select('languages', 'l')
  445. ->fields('l', array('javascript'))
  446. ->condition('language', $langcode)
  447. ->execute()
  448. ->fetchObject();
  449. $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/' . $langcode . '_' . $file->javascript . '.js';
  450. $this->assertTrue($result = file_exists($js_file), format_string('JavaScript file created: %file', array('%file' => $result ? $js_file : 'not found')));
  451. // Test JavaScript translation rebuilding.
  452. file_unmanaged_delete($js_file);
  453. $this->assertTrue($result = !file_exists($js_file), format_string('JavaScript file deleted: %file', array('%file' => $result ? $js_file : 'found')));
  454. cache_clear_all();
  455. _locale_rebuild_js($langcode);
  456. $this->assertTrue($result = file_exists($js_file), format_string('JavaScript file rebuilt: %file', array('%file' => $result ? $js_file : 'not found')));
  457. }
  458. /**
  459. * Tests the validation of the translation input.
  460. */
  461. function testStringValidation() {
  462. global $base_url;
  463. // User to add language and strings.
  464. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface'));
  465. $this->drupalLogin($admin_user);
  466. $langcode = 'xx';
  467. // The English name for the language. This will be translated.
  468. $name = $this->randomName(16);
  469. // The native name for the language.
  470. $native = $this->randomName(16);
  471. // The domain prefix.
  472. $prefix = $langcode;
  473. // This is the language indicator on the translation search screen for
  474. // untranslated strings. Copied straight from locale.inc.
  475. $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  476. // These will be the invalid translations of $name.
  477. $key = $this->randomName(16);
  478. $bad_translations[$key] = "<script>alert('xss');</script>" . $key;
  479. $key = $this->randomName(16);
  480. $bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key;
  481. $key = $this->randomName(16);
  482. $bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key;
  483. $key = $this->randomName(16);
  484. $bad_translations[$key] ="<BODY ONLOAD=alert('xss')>" . $key;
  485. // Add custom language.
  486. $edit = array(
  487. 'langcode' => $langcode,
  488. 'name' => $name,
  489. 'native' => $native,
  490. 'prefix' => $prefix,
  491. 'direction' => '0',
  492. );
  493. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  494. // Add string.
  495. t($name, array(), array('langcode' => $langcode));
  496. // Reset locale cache.
  497. $search = array(
  498. 'string' => $name,
  499. 'language' => 'all',
  500. 'translation' => 'all',
  501. 'group' => 'all',
  502. );
  503. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  504. // Find the edit path.
  505. $content = $this->drupalGetContent();
  506. $this->assertTrue(preg_match('@(admin/config/regional/translate/edit/[0-9]+)@', $content, $matches), 'Found the edit path.');
  507. $path = $matches[0];
  508. foreach ($bad_translations as $key => $translation) {
  509. $edit = array(
  510. "translations[$langcode]" => $translation,
  511. );
  512. $this->drupalPost($path, $edit, t('Save translations'));
  513. // Check for a form error on the textarea.
  514. $form_class = $this->xpath('//form[@id="locale-translate-edit-form"]//textarea/@class');
  515. $this->assertNotIdentical(FALSE, strpos($form_class[0], 'error'), 'The string was rejected as unsafe.');
  516. $this->assertNoText(t('The string has been saved.'), 'The string was not saved.');
  517. }
  518. }
  519. /**
  520. * Tests translation search form.
  521. */
  522. function testStringSearch() {
  523. global $base_url;
  524. // User to add and remove language.
  525. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  526. // User to translate and delete string.
  527. $translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
  528. // Code for the language.
  529. $langcode = 'xx';
  530. // The English name for the language. This will be translated.
  531. $name = $this->randomName(16);
  532. // The native name for the language.
  533. $native = $this->randomName(16);
  534. // The domain prefix.
  535. $prefix = $langcode;
  536. // This is the language indicator on the translation search screen for
  537. // untranslated strings. Copied straight from locale.inc.
  538. $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  539. // This will be the translation of $name.
  540. $translation = $this->randomName(16);
  541. // Add custom language.
  542. $this->drupalLogin($admin_user);
  543. $edit = array(
  544. 'langcode' => $langcode,
  545. 'name' => $name,
  546. 'native' => $native,
  547. 'prefix' => $prefix,
  548. 'direction' => '0',
  549. );
  550. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  551. // Add string.
  552. t($name, array(), array('langcode' => $langcode));
  553. // Reset locale cache.
  554. locale_reset();
  555. $this->drupalLogout();
  556. // Search for the name.
  557. $this->drupalLogin($translate_user);
  558. $search = array(
  559. 'string' => $name,
  560. 'language' => 'all',
  561. 'translation' => 'all',
  562. 'group' => 'all',
  563. );
  564. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  565. // assertText() seems to remove the input field where $name always could be
  566. // found, so this is not a false assert. See how assertNoText succeeds
  567. // later.
  568. $this->assertText($name, 'Search found the string.');
  569. // Ensure untranslated string doesn't appear if searching on 'only
  570. // translated strings'.
  571. $search = array(
  572. 'string' => $name,
  573. 'language' => 'all',
  574. 'translation' => 'translated',
  575. 'group' => 'all',
  576. );
  577. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  578. $this->assertText(t('No strings available.'), "Search didn't find the string.");
  579. // Ensure untranslated string appears if searching on 'only untranslated
  580. // strings' in "all" (hasn't been translated to any language).
  581. $search = array(
  582. 'string' => $name,
  583. 'language' => 'all',
  584. 'translation' => 'untranslated',
  585. 'group' => 'all',
  586. );
  587. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  588. $this->assertNoText(t('No strings available.'), 'Search found the string.');
  589. // Ensure untranslated string appears if searching on 'only untranslated
  590. // strings' in the custom language (hasn't been translated to that specific language).
  591. $search = array(
  592. 'string' => $name,
  593. 'language' => $langcode,
  594. 'translation' => 'untranslated',
  595. 'group' => 'all',
  596. );
  597. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  598. $this->assertNoText(t('No strings available.'), 'Search found the string.');
  599. // Add translation.
  600. // Assume this is the only result, given the random name.
  601. $this->clickLink(t('edit'));
  602. // We save the lid from the path.
  603. $matches = array();
  604. preg_match('!admin/config/regional/translate/edit/(\d)+!', $this->getUrl(), $matches);
  605. $lid = $matches[1];
  606. $edit = array(
  607. "translations[$langcode]" => $translation,
  608. );
  609. $this->drupalPost(NULL, $edit, t('Save translations'));
  610. // Ensure translated string does appear if searching on 'only
  611. // translated strings'.
  612. $search = array(
  613. 'string' => $translation,
  614. 'language' => 'all',
  615. 'translation' => 'translated',
  616. 'group' => 'all',
  617. );
  618. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  619. $this->assertNoText(t('No strings available.'), 'Search found the translation.');
  620. // Ensure translated source string doesn't appear if searching on 'only
  621. // untranslated strings'.
  622. $search = array(
  623. 'string' => $name,
  624. 'language' => 'all',
  625. 'translation' => 'untranslated',
  626. 'group' => 'all',
  627. );
  628. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  629. $this->assertText(t('No strings available.'), "Search didn't find the source string.");
  630. // Ensure translated string doesn't appear if searching on 'only
  631. // untranslated strings'.
  632. $search = array(
  633. 'string' => $translation,
  634. 'language' => 'all',
  635. 'translation' => 'untranslated',
  636. 'group' => 'all',
  637. );
  638. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  639. $this->assertText(t('No strings available.'), "Search didn't find the translation.");
  640. // Ensure translated string does appear if searching on the custom language.
  641. $search = array(
  642. 'string' => $translation,
  643. 'language' => $langcode,
  644. 'translation' => 'all',
  645. 'group' => 'all',
  646. );
  647. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  648. $this->assertNoText(t('No strings available.'), 'Search found the translation.');
  649. // Ensure translated string doesn't appear if searching on English.
  650. $search = array(
  651. 'string' => $translation,
  652. 'language' => 'en',
  653. 'translation' => 'all',
  654. 'group' => 'all',
  655. );
  656. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  657. $this->assertText(t('No strings available.'), "Search didn't find the translation.");
  658. // Search for a string that isn't in the system.
  659. $unavailable_string = $this->randomName(16);
  660. $search = array(
  661. 'string' => $unavailable_string,
  662. 'language' => 'all',
  663. 'translation' => 'all',
  664. 'group' => 'all',
  665. );
  666. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  667. $this->assertText(t('No strings available.'), "Search didn't find the invalid string.");
  668. }
  669. }
  670. /**
  671. * Tests plural index computation functionality.
  672. */
  673. class LocalePluralFormatTest extends DrupalWebTestCase {
  674. public static function getInfo() {
  675. return array(
  676. 'name' => 'Plural formula evaluation',
  677. 'description' => 'Tests plural formula evaluation for various languages.',
  678. 'group' => 'Locale',
  679. );
  680. }
  681. function setUp() {
  682. parent::setUp('locale', 'locale_test');
  683. $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
  684. $this->drupalLogin($admin_user);
  685. // Import some .po files with formulas to set up the environment.
  686. // These will also add the languages to the system and enable them.
  687. $this->importPoFile($this->getPoFileWithSimplePlural(), array(
  688. 'langcode' => 'fr',
  689. ));
  690. $this->importPoFile($this->getPoFileWithComplexPlural(), array(
  691. 'langcode' => 'hr',
  692. ));
  693. }
  694. /**
  695. * Tests locale_get_plural() functionality.
  696. */
  697. function testGetPluralFormat() {
  698. $this->drupalGet('locale_test_plural_format_page');
  699. $tests = _locale_test_plural_format_tests();
  700. $result = array();
  701. foreach ($tests as $test) {
  702. $this->assertPluralFormat($test['count'], $test['language'], $test['expected-result']);
  703. }
  704. }
  705. /**
  706. * Helper assert to test locale_get_plural page.
  707. *
  708. * @param $count
  709. * Number for testing.
  710. * @param $lang
  711. * Language for testing
  712. * @param $expected_result
  713. * Expected result.
  714. * @param $message
  715. */
  716. function assertPluralFormat($count, $lang, $expected_result) {
  717. $message_param = array(
  718. '@lang' => $lang,
  719. '@count' => $count,
  720. '@expected_result' => $expected_result,
  721. );
  722. $message = t("Computed plural index for '@lang' with count @count is @expected_result.", $message_param);
  723. $message_param = array(
  724. '@lang' => $lang,
  725. '@expected_result' => $expected_result,
  726. );
  727. $this->assertText(format_string('Language: @lang, locale_get_plural: @expected_result.', $message_param, $message));
  728. }
  729. /**
  730. * Imports a standalone .po file in a given language.
  731. *
  732. * @param $contents
  733. * Contents of the .po file to import.
  734. * @param $options
  735. * Additional options to pass to the translation import form.
  736. */
  737. function importPoFile($contents, array $options = array()) {
  738. $name = tempnam('temporary://', "po_") . '.po';
  739. file_put_contents($name, $contents);
  740. $options['files[file]'] = $name;
  741. $this->drupalPost('admin/config/regional/translate/import', $options, t('Import'));
  742. drupal_unlink($name);
  743. }
  744. /**
  745. * Returns a .po file with a simple plural formula.
  746. */
  747. function getPoFileWithSimplePlural() {
  748. return <<< EOF
  749. msgid ""
  750. msgstr ""
  751. "Project-Id-Version: Drupal 7\\n"
  752. "MIME-Version: 1.0\\n"
  753. "Content-Type: text/plain; charset=UTF-8\\n"
  754. "Content-Transfer-Encoding: 8bit\\n"
  755. "Plural-Forms: nplurals=2; plural=(n!=1);\\n"
  756. msgid "One sheep"
  757. msgid_plural "@count sheep"
  758. msgstr[0] "un mouton"
  759. msgstr[1] "@count moutons"
  760. msgid "Monday"
  761. msgstr "lundi"
  762. EOF;
  763. }
  764. /**
  765. * Returns a .po file with a complex plural formula.
  766. */
  767. function getPoFileWithComplexPlural() {
  768. return <<< EOF
  769. msgid ""
  770. msgstr ""
  771. "Project-Id-Version: Drupal 7\\n"
  772. "MIME-Version: 1.0\\n"
  773. "Content-Type: text/plain; charset=UTF-8\\n"
  774. "Content-Transfer-Encoding: 8bit\\n"
  775. "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n"
  776. msgid "1 hour"
  777. msgid_plural "@count hours"
  778. msgstr[0] "@count sat"
  779. msgstr[1] "@count sata"
  780. msgstr[2] "@count sati"
  781. msgid "Monday"
  782. msgstr "Ponedjeljak"
  783. EOF;
  784. }
  785. }
  786. /**
  787. * Functional tests for the import of translation files.
  788. */
  789. class LocaleImportFunctionalTest extends DrupalWebTestCase {
  790. public static function getInfo() {
  791. return array(
  792. 'name' => 'Translation import',
  793. 'description' => 'Tests the import of locale files.',
  794. 'group' => 'Locale',
  795. );
  796. }
  797. /**
  798. * A user able to create languages and import translations.
  799. */
  800. protected $admin_user = NULL;
  801. function setUp() {
  802. parent::setUp('locale', 'locale_test');
  803. $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
  804. $this->drupalLogin($this->admin_user);
  805. }
  806. /**
  807. * Test import of standalone .po files.
  808. */
  809. function testStandalonePoFile() {
  810. // Try importing a .po file.
  811. $this->importPoFile($this->getPoFile(), array(
  812. 'langcode' => 'fr',
  813. ));
  814. // The import should automatically create the corresponding language.
  815. $this->assertRaw(t('The language %language has been created.', array('%language' => 'French')), 'The language has been automatically created.');
  816. // The import should have created 7 strings.
  817. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 9, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.');
  818. // This import should have saved plural forms to have 2 variants.
  819. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, 'Plural number initialized.');
  820. // Ensure we were redirected correctly.
  821. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  822. // Try importing a .po file with invalid tags in the default text group.
  823. $this->importPoFile($this->getBadPoFile(), array(
  824. 'langcode' => 'fr',
  825. ));
  826. // The import should have created 1 string and rejected 2.
  827. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.');
  828. $skip_message = format_plural(2, 'One translation string was skipped because it contains disallowed HTML.', '@count translation strings were skipped because they contain disallowed HTML.');
  829. $this->assertRaw($skip_message, 'Unsafe strings were skipped.');
  830. // Try importing a .po file with invalid tags in a non default text group.
  831. $this->importPoFile($this->getBadPoFile(), array(
  832. 'langcode' => 'fr',
  833. 'group' => 'custom',
  834. ));
  835. // The import should have created 3 strings.
  836. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 3, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.');
  837. // Try importing a .po file which doesn't exist.
  838. $name = $this->randomName(16);
  839. $this->drupalPost('admin/config/regional/translate/import', array(
  840. 'langcode' => 'fr',
  841. 'files[file]' => $name,
  842. 'group' => 'custom',
  843. ), t('Import'));
  844. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/import', array('absolute' => TRUE)), 'Correct page redirection.');
  845. $this->assertText(t('File to import not found.'), 'File to import not found message.');
  846. // Try importing a .po file with overriding strings, and ensure existing
  847. // strings are kept.
  848. $this->importPoFile($this->getOverwritePoFile(), array(
  849. 'langcode' => 'fr',
  850. 'mode' => 1, // Existing strings are kept, only new strings are added.
  851. ));
  852. // The import should have created 1 string.
  853. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.');
  854. // Ensure string wasn't overwritten.
  855. $search = array(
  856. 'string' => 'Montag',
  857. 'language' => 'fr',
  858. 'translation' => 'translated',
  859. 'group' => 'all',
  860. );
  861. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  862. $this->assertText(t('No strings available.'), 'String not overwritten by imported string.');
  863. // This import should not have changed number of plural forms.
  864. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, 'Plural numbers untouched.');
  865. $this->importPoFile($this->getPoFileWithBrokenPlural(), array(
  866. 'langcode' => 'fr',
  867. 'mode' => 1, // Existing strings are kept, only new strings are added.
  868. ));
  869. // Attempt to import broken .po file as well to prove that this
  870. // will not overwrite the proper plural formula imported above.
  871. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, 'Broken plurals: plural numbers untouched.');
  872. $this->importPoFile($this->getPoFileWithMissingPlural(), array(
  873. 'langcode' => 'fr',
  874. 'mode' => 1, // Existing strings are kept, only new strings are added.
  875. ));
  876. // Attempt to import .po file which has no plurals and prove that this
  877. // will not overwrite the proper plural formula imported above.
  878. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 2, 'No plurals: plural numbers untouched.');
  879. // Try importing a .po file with overriding strings, and ensure existing
  880. // strings are overwritten.
  881. $this->importPoFile($this->getOverwritePoFile(), array(
  882. 'langcode' => 'fr',
  883. 'mode' => 0, // Strings in the uploaded file replace existing ones, new ones are added.
  884. ));
  885. // The import should have updated 2 strings.
  886. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 0, '%update' => 2, '%delete' => 0)), 'The translation file was successfully imported.');
  887. // Ensure string was overwritten.
  888. $search = array(
  889. 'string' => 'Montag',
  890. 'language' => 'fr',
  891. 'translation' => 'translated',
  892. 'group' => 'all',
  893. );
  894. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  895. $this->assertNoText(t('No strings available.'), 'String overwritten by imported string.');
  896. // This import should have changed number of plural forms.
  897. $this->assert(db_query("SELECT plurals FROM {languages} WHERE language = 'fr'")->fetchField() == 3, 'Plural numbers changed.');
  898. }
  899. /**
  900. * Test automatic import of a module's translation files when a language is
  901. * enabled.
  902. */
  903. function testAutomaticModuleTranslationImportLanguageEnable() {
  904. // Code for the language - manually set to match the test translation file.
  905. $langcode = 'xx';
  906. // The English name for the language.
  907. $name = $this->randomName(16);
  908. // The native name for the language.
  909. $native = $this->randomName(16);
  910. // The domain prefix.
  911. $prefix = $langcode;
  912. // Create a custom language.
  913. $edit = array(
  914. 'langcode' => $langcode,
  915. 'name' => $name,
  916. 'native' => $native,
  917. 'prefix' => $prefix,
  918. 'direction' => '0',
  919. );
  920. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  921. // Ensure the translation file was automatically imported when language was
  922. // added.
  923. $this->assertText(t('One translation file imported for the enabled modules.'), 'Language file automatically imported.');
  924. // Ensure strings were successfully imported.
  925. $search = array(
  926. 'string' => 'lundi',
  927. 'language' => $langcode,
  928. 'translation' => 'translated',
  929. 'group' => 'all',
  930. );
  931. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  932. $this->assertNoText(t('No strings available.'), 'String successfully imported.');
  933. }
  934. /**
  935. * Test msgctxt context support.
  936. */
  937. function testLanguageContext() {
  938. // Try importing a .po file.
  939. $this->importPoFile($this->getPoFileWithContext(), array(
  940. 'langcode' => 'hr',
  941. ));
  942. $this->assertIdentical(t('May', array(), array('langcode' => 'hr', 'context' => 'Long month name')), 'Svibanj', 'Long month name context is working.');
  943. $this->assertIdentical(t('May', array(), array('langcode' => 'hr')), 'Svi.', 'Default context is working.');
  944. }
  945. /**
  946. * Test empty msgstr at end of .po file see #611786.
  947. */
  948. function testEmptyMsgstr() {
  949. $langcode = 'hu';
  950. // Try importing a .po file.
  951. $this->importPoFile($this->getPoFileWithMsgstr(), array(
  952. 'langcode' => $langcode,
  953. ));
  954. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.');
  955. $this->assertIdentical(t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', 'String imported and translated.');
  956. // Try importing a .po file.
  957. $this->importPoFile($this->getPoFileWithEmptyMsgstr(), array(
  958. 'langcode' => $langcode,
  959. 'mode' => 0,
  960. ));
  961. $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 0, '%update' => 0, '%delete' => 1)), 'The translation file was successfully imported.');
  962. // This is the language indicator on the translation search screen for
  963. // untranslated strings. Copied straight from locale.inc.
  964. $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  965. $str = "Operations";
  966. $search = array(
  967. 'string' => $str,
  968. 'language' => 'all',
  969. 'translation' => 'all',
  970. 'group' => 'all',
  971. );
  972. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  973. // assertText() seems to remove the input field where $str always could be
  974. // found, so this is not a false assert.
  975. $this->assertText($str, 'Search found the string.');
  976. $this->assertRaw($language_indicator, 'String is untranslated again.');
  977. }
  978. /**
  979. * Helper function: import a standalone .po file in a given language.
  980. *
  981. * @param $contents
  982. * Contents of the .po file to import.
  983. * @param $options
  984. * Additional options to pass to the translation import form.
  985. */
  986. function importPoFile($contents, array $options = array()) {
  987. $name = tempnam('temporary://', "po_") . '.po';
  988. file_put_contents($name, $contents);
  989. $options['files[file]'] = $name;
  990. $this->drupalPost('admin/config/regional/translate/import', $options, t('Import'));
  991. drupal_unlink($name);
  992. }
  993. /**
  994. * Helper function that returns a proper .po file.
  995. */
  996. function getPoFile() {
  997. return <<< EOF
  998. msgid ""
  999. msgstr ""
  1000. "Project-Id-Version: Drupal 7\\n"
  1001. "MIME-Version: 1.0\\n"
  1002. "Content-Type: text/plain; charset=UTF-8\\n"
  1003. "Content-Transfer-Encoding: 8bit\\n"
  1004. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  1005. msgid "One sheep"
  1006. msgid_plural "@count sheep"
  1007. msgstr[0] "un mouton"
  1008. msgstr[1] "@count moutons"
  1009. msgid "Monday"
  1010. msgstr "lundi"
  1011. msgid "Tuesday"
  1012. msgstr "mardi"
  1013. msgid "Wednesday"
  1014. msgstr "mercredi"
  1015. msgid "Thursday"
  1016. msgstr "jeudi"
  1017. msgid "Friday"
  1018. msgstr "vendredi"
  1019. msgid "Saturday"
  1020. msgstr "samedi"
  1021. msgid "Sunday"
  1022. msgstr "dimanche"
  1023. EOF;
  1024. }
  1025. /**
  1026. * Helper function that returns a bad .po file.
  1027. */
  1028. function getBadPoFile() {
  1029. return <<< EOF
  1030. msgid ""
  1031. msgstr ""
  1032. "Project-Id-Version: Drupal 7\\n"
  1033. "MIME-Version: 1.0\\n"
  1034. "Content-Type: text/plain; charset=UTF-8\\n"
  1035. "Content-Transfer-Encoding: 8bit\\n"
  1036. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  1037. msgid "Save configuration"
  1038. msgstr "Enregistrer la configuration"
  1039. msgid "edit"
  1040. msgstr "modifier<img SRC="javascript:alert(\'xss\');">"
  1041. msgid "delete"
  1042. msgstr "supprimer<script>alert('xss');</script>"
  1043. EOF;
  1044. }
  1045. /**
  1046. * Helper function that returns a proper .po file, for testing overwriting
  1047. * existing translations.
  1048. */
  1049. function getOverwritePoFile() {
  1050. return <<< EOF
  1051. msgid ""
  1052. msgstr ""
  1053. "Project-Id-Version: Drupal 7\\n"
  1054. "MIME-Version: 1.0\\n"
  1055. "Content-Type: text/plain; charset=UTF-8\\n"
  1056. "Content-Transfer-Encoding: 8bit\\n"
  1057. "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n"
  1058. msgid "Monday"
  1059. msgstr "Montag"
  1060. msgid "Day"
  1061. msgstr "Jour"
  1062. EOF;
  1063. }
  1064. /**
  1065. * Helper function that returns a .po file with context.
  1066. */
  1067. function getPoFileWithContext() {
  1068. // Croatian (code hr) is one of the languages that have a different
  1069. // form for the full name and the abbreviated name for the month May.
  1070. return <<< EOF
  1071. msgid ""
  1072. msgstr ""
  1073. "Project-Id-Version: Drupal 7\\n"
  1074. "MIME-Version: 1.0\\n"
  1075. "Content-Type: text/plain; charset=UTF-8\\n"
  1076. "Content-Transfer-Encoding: 8bit\\n"
  1077. "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n"
  1078. msgctxt "Long month name"
  1079. msgid "May"
  1080. msgstr "Svibanj"
  1081. msgid "May"
  1082. msgstr "Svi."
  1083. EOF;
  1084. }
  1085. /**
  1086. * Helper function that returns a .po file with an empty last item.
  1087. */
  1088. function getPoFileWithEmptyMsgstr() {
  1089. return <<< EOF
  1090. msgid ""
  1091. msgstr ""
  1092. "Project-Id-Version: Drupal 7\\n"
  1093. "MIME-Version: 1.0\\n"
  1094. "Content-Type: text/plain; charset=UTF-8\\n"
  1095. "Content-Transfer-Encoding: 8bit\\n"
  1096. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  1097. msgid "Operations"
  1098. msgstr ""
  1099. EOF;
  1100. }
  1101. /**
  1102. * Helper function that returns a .po file with an empty last item.
  1103. */
  1104. function getPoFileWithMsgstr() {
  1105. return <<< EOF
  1106. msgid ""
  1107. msgstr ""
  1108. "Project-Id-Version: Drupal 7\\n"
  1109. "MIME-Version: 1.0\\n"
  1110. "Content-Type: text/plain; charset=UTF-8\\n"
  1111. "Content-Transfer-Encoding: 8bit\\n"
  1112. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  1113. msgid "Operations"
  1114. msgstr "Műveletek"
  1115. msgid "Will not appear in Drupal core, so we can ensure the test passes"
  1116. msgstr ""
  1117. EOF;
  1118. }
  1119. /**
  1120. * Returns a .po file with a missing plural formula.
  1121. */
  1122. function getPoFileWithMissingPlural() {
  1123. return <<< EOF
  1124. msgid ""
  1125. msgstr ""
  1126. "Project-Id-Version: Drupal 7\\n"
  1127. "MIME-Version: 1.0\\n"
  1128. "Content-Type: text/plain; charset=UTF-8\\n"
  1129. "Content-Transfer-Encoding: 8bit\\n"
  1130. msgid "Monday"
  1131. msgstr "Ponedjeljak"
  1132. EOF;
  1133. }
  1134. /**
  1135. * Returns a .po file with a broken plural formula.
  1136. */
  1137. function getPoFileWithBrokenPlural() {
  1138. return <<< EOF
  1139. msgid ""
  1140. msgstr ""
  1141. "Project-Id-Version: Drupal 7\\n"
  1142. "MIME-Version: 1.0\\n"
  1143. "Content-Type: text/plain; charset=UTF-8\\n"
  1144. "Content-Transfer-Encoding: 8bit\\n"
  1145. "Plural-Forms: broken, will not parse\\n"
  1146. msgid "Monday"
  1147. msgstr "lundi"
  1148. EOF;
  1149. }
  1150. }
  1151. /**
  1152. * Functional tests for the export of translation files.
  1153. */
  1154. class LocaleExportFunctionalTest extends DrupalWebTestCase {
  1155. public static function getInfo() {
  1156. return array(
  1157. 'name' => 'Translation export',
  1158. 'description' => 'Tests the exportation of locale files.',
  1159. 'group' => 'Locale',
  1160. );
  1161. }
  1162. /**
  1163. * A user able to create languages and export translations.
  1164. */
  1165. protected $admin_user = NULL;
  1166. function setUp() {
  1167. parent::setUp('locale', 'locale_test');
  1168. $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
  1169. $this->drupalLogin($this->admin_user);
  1170. }
  1171. /**
  1172. * Test exportation of translations.
  1173. */
  1174. function testExportTranslation() {
  1175. // First import some known translations.
  1176. // This will also automatically enable the 'fr' language.
  1177. $name = tempnam('temporary://', "po_") . '.po';
  1178. file_put_contents($name, $this->getPoFile());
  1179. $this->drupalPost('admin/config/regional/translate/import', array(
  1180. 'langcode' => 'fr',
  1181. 'files[file]' => $name,
  1182. ), t('Import'));
  1183. drupal_unlink($name);
  1184. // Get the French translations.
  1185. $this->drupalPost('admin/config/regional/translate/export', array(
  1186. 'langcode' => 'fr',
  1187. ), t('Export'));
  1188. // Ensure we have a translation file.
  1189. $this->assertRaw('# French translation of Drupal', 'Exported French translation file.');
  1190. // Ensure our imported translations exist in the file.
  1191. $this->assertRaw('msgstr "lundi"', 'French translations present in exported file.');
  1192. }
  1193. /**
  1194. * Test exportation of translation template file.
  1195. */
  1196. function testExportTranslationTemplateFile() {
  1197. // Get the translation template file.
  1198. // There are two 'Export' buttons on this page, but it somehow works. It'd
  1199. // be better if we could use the submit button id like documented but that
  1200. // doesn't work.
  1201. $this->drupalPost('admin/config/regional/translate/export', array(), t('Export'));
  1202. // Ensure we have a translation file.
  1203. $this->assertRaw('# LANGUAGE translation of PROJECT', 'Exported translation template file.');
  1204. }
  1205. /**
  1206. * Helper function that returns a proper .po file.
  1207. */
  1208. function getPoFile() {
  1209. return <<< EOF
  1210. msgid ""
  1211. msgstr ""
  1212. "Project-Id-Version: Drupal 6\\n"
  1213. "MIME-Version: 1.0\\n"
  1214. "Content-Type: text/plain; charset=UTF-8\\n"
  1215. "Content-Transfer-Encoding: 8bit\\n"
  1216. "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
  1217. msgid "Monday"
  1218. msgstr "lundi"
  1219. EOF;
  1220. }
  1221. }
  1222. /**
  1223. * Tests for the st() function.
  1224. */
  1225. class LocaleInstallTest extends DrupalWebTestCase {
  1226. public static function getInfo() {
  1227. return array(
  1228. 'name' => 'String translation using st()',
  1229. 'description' => 'Tests that st() works like t().',
  1230. 'group' => 'Locale',
  1231. );
  1232. }
  1233. function setUp() {
  1234. parent::setUp('locale');
  1235. // st() lives in install.inc, so ensure that it is loaded for all tests.
  1236. require_once DRUPAL_ROOT . '/includes/install.inc';
  1237. }
  1238. /**
  1239. * Verify that function signatures of t() and st() are equal.
  1240. */
  1241. function testFunctionSignatures() {
  1242. $reflector_t = new ReflectionFunction('t');
  1243. $reflector_st = new ReflectionFunction('st');
  1244. $this->assertEqual($reflector_t->getParameters(), $reflector_st->getParameters(), 'Function signatures of t() and st() are equal.');
  1245. }
  1246. }
  1247. /**
  1248. * Locale uninstall with English UI functional test.
  1249. */
  1250. class LocaleUninstallFunctionalTest extends DrupalWebTestCase {
  1251. public static function getInfo() {
  1252. return array(
  1253. 'name' => 'Locale uninstall (EN)',
  1254. 'description' => 'Tests the uninstall process using the built-in UI language.',
  1255. 'group' => 'Locale',
  1256. );
  1257. }
  1258. /**
  1259. * The default language set for the UI before uninstall.
  1260. */
  1261. protected $language;
  1262. function setUp() {
  1263. parent::setUp('locale');
  1264. $this->language = 'en';
  1265. }
  1266. /**
  1267. * Check if the values of the Locale variables are correct after uninstall.
  1268. */
  1269. function testUninstallProcess() {
  1270. $locale_module = array('locale');
  1271. // Add a new language and optionally set it as default.
  1272. require_once DRUPAL_ROOT . '/includes/locale.inc';
  1273. locale_add_language('fr', 'French', 'Français', LANGUAGE_LTR, '', '', TRUE, $this->language == 'fr');
  1274. // Check the UI language.
  1275. drupal_language_initialize();
  1276. global $language;
  1277. $this->assertEqual($language->language, $this->language, format_string('Current language: %lang', array('%lang' => $language->language)));
  1278. // Enable multilingual workflow option for articles.
  1279. variable_set('language_content_type_article', 1);
  1280. // Change JavaScript translations directory.
  1281. variable_set('locale_js_directory', 'js_translations');
  1282. // Build the JavaScript translation file for French.
  1283. $user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
  1284. $this->drupalLogin($user);
  1285. $this->drupalGet('admin/config/regional/translate/translate');
  1286. $string = db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE :location AND textgroup = :textgroup', array(
  1287. ':location' => '%.js%',
  1288. ':textgroup' => 'default',
  1289. ))->fetchObject();
  1290. $edit = array('translations[fr]' => 'french translation');
  1291. $this->drupalPost('admin/config/regional/translate/edit/' . $string->lid, $edit, t('Save translations'));
  1292. _locale_rebuild_js('fr');
  1293. $file = db_query('SELECT javascript FROM {languages} WHERE language = :language', array(':language' => 'fr'))->fetchObject();
  1294. $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/fr_' . $file->javascript . '.js';
  1295. $this->assertTrue($result = file_exists($js_file), format_string('JavaScript file created: %file', array('%file' => $result ? $js_file : 'none')));
  1296. // Disable string caching.
  1297. variable_set('locale_cache_strings', 0);
  1298. // Change language negotiation options.
  1299. drupal_load('module', 'locale');
  1300. variable_set('language_types', drupal_language_types() + array('language_custom' => TRUE));
  1301. variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, locale_language_negotiation_info());
  1302. variable_set('language_negotiation_' . LANGUAGE_TYPE_CONTENT, locale_language_negotiation_info());
  1303. variable_set('language_negotiation_' . LANGUAGE_TYPE_URL, locale_language_negotiation_info());
  1304. // Change language providers settings.
  1305. variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX);
  1306. variable_set('locale_language_negotiation_session_param', TRUE);
  1307. // Uninstall Locale.
  1308. module_disable($locale_module);
  1309. drupal_uninstall_modules($locale_module);
  1310. // Visit the front page.
  1311. $this->drupalGet('');
  1312. // Check the init language logic.
  1313. drupal_language_initialize();
  1314. $this->assertEqual($language->language, 'en', format_string('Language after uninstall: %lang', array('%lang' => $language->language)));
  1315. // Check JavaScript files deletion.
  1316. $this->assertTrue($result = !file_exists($js_file), format_string('JavaScript file deleted: %file', array('%file' => $result ? $js_file : 'found')));
  1317. // Check language count.
  1318. $language_count = variable_get('language_count', 1);
  1319. $this->assertEqual($language_count, 1, format_string('Language count: %count', array('%count' => $language_count)));
  1320. // Check language negotiation.
  1321. require_once DRUPAL_ROOT . '/includes/language.inc';
  1322. $this->assertTrue(count(language_types()) == count(drupal_language_types()), 'Language types reset');
  1323. $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_INTERFACE) == LANGUAGE_NEGOTIATION_DEFAULT;
  1324. $this->assertTrue($language_negotiation, format_string('Interface language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set')));
  1325. $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_CONTENT) == LANGUAGE_NEGOTIATION_DEFAULT;
  1326. $this->assertTrue($language_negotiation, format_string('Content language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set')));
  1327. $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_URL) == LANGUAGE_NEGOTIATION_DEFAULT;
  1328. $this->assertTrue($language_negotiation, format_string('URL language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set')));
  1329. // Check language providers settings.
  1330. $this->assertFalse(variable_get('locale_language_negotiation_url_part', FALSE), 'URL language provider indicator settings cleared.');
  1331. $this->assertFalse(variable_get('locale_language_negotiation_session_param', FALSE), 'Visit language provider settings cleared.');
  1332. // Check JavaScript parsed.
  1333. $javascript_parsed_count = count(variable_get('javascript_parsed', array()));
  1334. $this->assertEqual($javascript_parsed_count, 0, format_string('JavaScript parsed count: %count', array('%count' => $javascript_parsed_count)));
  1335. // Check multilingual workflow option for articles.
  1336. $multilingual = variable_get('language_content_type_article', 0);
  1337. $this->assertEqual($multilingual, 0, format_string('Multilingual workflow option: %status', array('%status' => $multilingual ? 'enabled': 'disabled')));
  1338. // Check JavaScript translations directory.
  1339. $locale_js_directory = variable_get('locale_js_directory', 'languages');
  1340. $this->assertEqual($locale_js_directory, 'languages', format_string('JavaScript translations directory: %dir', array('%dir' => $locale_js_directory)));
  1341. // Check string caching.
  1342. $locale_cache_strings = variable_get('locale_cache_strings', 1);
  1343. $this->assertEqual($locale_cache_strings, 1, format_string('String caching: %status', array('%status' => $locale_cache_strings ? 'enabled': 'disabled')));
  1344. }
  1345. }
  1346. /**
  1347. * Locale uninstall with French UI functional test.
  1348. *
  1349. * Because this class extends LocaleUninstallFunctionalTest, it doesn't require a new
  1350. * test of its own. Rather, it switches the default UI language in setUp and then
  1351. * runs the testUninstallProcess (which it inherits from LocaleUninstallFunctionalTest)
  1352. * to test with this new language.
  1353. */
  1354. class LocaleUninstallFrenchFunctionalTest extends LocaleUninstallFunctionalTest {
  1355. public static function getInfo() {
  1356. return array(
  1357. 'name' => 'Locale uninstall (FR)',
  1358. 'description' => 'Tests the uninstall process using French as interface language.',
  1359. 'group' => 'Locale',
  1360. );
  1361. }
  1362. function setUp() {
  1363. parent::setUp();
  1364. $this->language = 'fr';
  1365. }
  1366. }
  1367. /**
  1368. * Functional tests for the language switching feature.
  1369. */
  1370. class LocaleLanguageSwitchingFunctionalTest extends DrupalWebTestCase {
  1371. public static function getInfo() {
  1372. return array(
  1373. 'name' => 'Language switching',
  1374. 'description' => 'Tests for the language switching feature.',
  1375. 'group' => 'Locale',
  1376. );
  1377. }
  1378. function setUp() {
  1379. parent::setUp('locale');
  1380. // Create and login user.
  1381. $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'translate interface', 'access administration pages'));
  1382. $this->drupalLogin($admin_user);
  1383. }
  1384. /**
  1385. * Functional tests for the language switcher block.
  1386. */
  1387. function testLanguageBlock() {
  1388. // Enable the language switching block.
  1389. $language_type = LANGUAGE_TYPE_INTERFACE;
  1390. $edit = array(
  1391. "blocks[locale_{$language_type}][region]" => 'sidebar_first',
  1392. );
  1393. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  1394. // Add language.
  1395. $edit = array(
  1396. 'langcode' => 'fr',
  1397. );
  1398. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1399. // Enable URL language detection and selection.
  1400. $edit = array('language[enabled][locale-url]' => '1');
  1401. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1402. // Assert that the language switching block is displayed on the frontpage.
  1403. $this->drupalGet('');
  1404. $this->assertText(t('Languages'), 'Language switcher block found.');
  1405. // Assert that only the current language is marked as active.
  1406. list($language_switcher) = $this->xpath('//div[@id=:id]/div[@class="content"]', array(':id' => 'block-locale-' . $language_type));
  1407. $links = array(
  1408. 'active' => array(),
  1409. 'inactive' => array(),
  1410. );
  1411. $anchors = array(
  1412. 'active' => array(),
  1413. 'inactive' => array(),
  1414. );
  1415. foreach ($language_switcher->ul->li as $link) {
  1416. $classes = explode(" ", (string) $link['class']);
  1417. list($language) = array_intersect($classes, array('en', 'fr'));
  1418. if (in_array('active', $classes)) {
  1419. $links['active'][] = $language;
  1420. }
  1421. else {
  1422. $links['inactive'][] = $language;
  1423. }
  1424. $anchor_classes = explode(" ", (string) $link->a['class']);
  1425. if (in_array('active', $anchor_classes)) {
  1426. $anchors['active'][] = $language;
  1427. }
  1428. else {
  1429. $anchors['inactive'][] = $language;
  1430. }
  1431. }
  1432. $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language list item is marked as active on the language switcher block.');
  1433. $this->assertIdentical($anchors, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language anchor is marked as active on the language switcher block.');
  1434. }
  1435. }
  1436. /**
  1437. * Test browser language detection.
  1438. */
  1439. class LocaleBrowserDetectionTest extends DrupalUnitTestCase {
  1440. public static function getInfo() {
  1441. return array(
  1442. 'name' => 'Browser language detection',
  1443. 'description' => 'Tests for the browser language detection.',
  1444. 'group' => 'Locale',
  1445. );
  1446. }
  1447. /**
  1448. * Unit tests for the locale_language_from_browser() function.
  1449. */
  1450. function testLanguageFromBrowser() {
  1451. // Load the required functions.
  1452. require_once DRUPAL_ROOT . '/includes/locale.inc';
  1453. $languages = array(
  1454. // In our test case, 'en' has priority over 'en-US'.
  1455. 'en' => (object) array(
  1456. 'language' => 'en',
  1457. ),
  1458. 'en-US' => (object) array(
  1459. 'language' => 'en-US',
  1460. ),
  1461. // But 'fr-CA' has priority over 'fr'.
  1462. 'fr-CA' => (object) array(
  1463. 'language' => 'fr-CA',
  1464. ),
  1465. 'fr' => (object) array(
  1466. 'language' => 'fr',
  1467. ),
  1468. // 'es-MX' is alone.
  1469. 'es-MX' => (object) array(
  1470. 'language' => 'es-MX',
  1471. ),
  1472. // 'pt' is alone.
  1473. 'pt' => (object) array(
  1474. 'language' => 'pt',
  1475. ),
  1476. // Language codes with more then one dash are actually valid.
  1477. // eh-oh-laa-laa is the official language code of the Teletubbies.
  1478. 'eh-oh-laa-laa' => (object) array(
  1479. 'language' => 'eh-oh-laa-laa',
  1480. ),
  1481. );
  1482. $test_cases = array(
  1483. // Equal qvalue for each language, choose the site preferred one.
  1484. 'en,en-US,fr-CA,fr,es-MX' => 'en',
  1485. 'en-US,en,fr-CA,fr,es-MX' => 'en',
  1486. 'fr,en' => 'en',
  1487. 'en,fr' => 'en',
  1488. 'en-US,fr' => 'en',
  1489. 'fr,en-US' => 'en',
  1490. 'fr,fr-CA' => 'fr-CA',
  1491. 'fr-CA,fr' => 'fr-CA',
  1492. 'fr' => 'fr-CA',
  1493. 'fr;q=1' => 'fr-CA',
  1494. 'fr,es-MX' => 'fr-CA',
  1495. 'fr,es' => 'fr-CA',
  1496. 'es,fr' => 'fr-CA',
  1497. 'es-MX,de' => 'es-MX',
  1498. 'de,es-MX' => 'es-MX',
  1499. // Different cases and whitespace.
  1500. 'en' => 'en',
  1501. 'En' => 'en',
  1502. 'EN' => 'en',
  1503. ' en' => 'en',
  1504. 'en ' => 'en',
  1505. 'en, fr' => 'en',
  1506. // A less specific language from the browser matches a more specific one
  1507. // from the website, and the other way around for compatibility with
  1508. // some versions of Internet Explorer.
  1509. 'es' => 'es-MX',
  1510. 'es-MX' => 'es-MX',
  1511. 'pt' => 'pt',
  1512. 'pt-PT' => 'pt',
  1513. 'pt-PT;q=0.5,pt-BR;q=1,en;q=0.7' => 'en',
  1514. 'pt-PT;q=1,pt-BR;q=0.5,en;q=0.7' => 'en',
  1515. 'pt-PT;q=0.4,pt-BR;q=0.1,en;q=0.7' => 'en',
  1516. 'pt-PT;q=0.1,pt-BR;q=0.4,en;q=0.7' => 'en',
  1517. // Language code with several dashes are valid. The less specific language
  1518. // from the browser matches the more specific one from the website.
  1519. 'eh-oh-laa-laa' => 'eh-oh-laa-laa',
  1520. 'eh-oh-laa' => 'eh-oh-laa-laa',
  1521. 'eh-oh' => 'eh-oh-laa-laa',
  1522. 'eh' => 'eh-oh-laa-laa',
  1523. // Different qvalues.
  1524. 'fr,en;q=0.5' => 'fr-CA',
  1525. 'fr,en;q=0.5,fr-CA;q=0.25' => 'fr',
  1526. // Silly wildcards are also valid.
  1527. '*,fr-CA;q=0.5' => 'en',
  1528. '*,en;q=0.25' => 'fr-CA',
  1529. 'en,en-US;q=0.5,fr;q=0.25' => 'en',
  1530. 'en-US,en;q=0.5,fr;q=0.25' => 'en-US',
  1531. // Unresolvable cases.
  1532. '' => FALSE,
  1533. 'de,pl' => FALSE,
  1534. 'iecRswK4eh' => FALSE,
  1535. $this->randomName(10) => FALSE,
  1536. );
  1537. foreach ($test_cases as $accept_language => $expected_result) {
  1538. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $accept_language;
  1539. $result = locale_language_from_browser($languages);
  1540. $this->assertIdentical($result, $expected_result, format_string("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none')));
  1541. }
  1542. }
  1543. }
  1544. /**
  1545. * Functional tests for a user's ability to change their default language.
  1546. */
  1547. class LocaleUserLanguageFunctionalTest extends DrupalWebTestCase {
  1548. public static function getInfo() {
  1549. return array(
  1550. 'name' => 'User language settings',
  1551. 'description' => "Tests user's ability to change their default language.",
  1552. 'group' => 'Locale',
  1553. );
  1554. }
  1555. function setUp() {
  1556. parent::setUp('locale');
  1557. }
  1558. /**
  1559. * Test if user can change their default language.
  1560. */
  1561. function testUserLanguageConfiguration() {
  1562. global $base_url;
  1563. // User to add and remove language.
  1564. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  1565. // User to change their default language.
  1566. $web_user = $this->drupalCreateUser();
  1567. // Add custom language.
  1568. $this->drupalLogin($admin_user);
  1569. // Code for the language.
  1570. $langcode = 'xx';
  1571. // The English name for the language.
  1572. $name = $this->randomName(16);
  1573. // The native name for the language.
  1574. $native = $this->randomName(16);
  1575. // The domain prefix.
  1576. $prefix = 'xx';
  1577. $edit = array(
  1578. 'langcode' => $langcode,
  1579. 'name' => $name,
  1580. 'native' => $native,
  1581. 'prefix' => $prefix,
  1582. 'direction' => '0',
  1583. );
  1584. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1585. // Add custom language and disable it.
  1586. // Code for the language.
  1587. $langcode_disabled = 'xx-yy';
  1588. // The English name for the language. This will be translated.
  1589. $name_disabled = $this->randomName(16);
  1590. // The native name for the language.
  1591. $native_disabled = $this->randomName(16);
  1592. // The domain prefix.
  1593. $prefix_disabled = $langcode_disabled;
  1594. $edit = array(
  1595. 'langcode' => $langcode_disabled,
  1596. 'name' => $name_disabled,
  1597. 'native' => $native_disabled,
  1598. 'prefix' => $prefix_disabled,
  1599. 'direction' => '0',
  1600. );
  1601. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1602. // Disable the language.
  1603. $edit = array(
  1604. 'enabled[' . $langcode_disabled . ']' => FALSE,
  1605. );
  1606. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  1607. $this->drupalLogout();
  1608. // Login as normal user and edit account settings.
  1609. $this->drupalLogin($web_user);
  1610. $path = 'user/' . $web_user->uid . '/edit';
  1611. $this->drupalGet($path);
  1612. // Ensure language settings fieldset is available.
  1613. $this->assertText(t('Language settings'), 'Language settings available.');
  1614. // Ensure custom language is present.
  1615. $this->assertText($name, 'Language present on form.');
  1616. // Ensure disabled language isn't present.
  1617. $this->assertNoText($name_disabled, 'Disabled language not present on form.');
  1618. // Switch to our custom language.
  1619. $edit = array(
  1620. 'language' => $langcode,
  1621. );
  1622. $this->drupalPost($path, $edit, t('Save'));
  1623. // Ensure form was submitted successfully.
  1624. $this->assertText(t('The changes have been saved.'), 'Changes were saved.');
  1625. // Check if language was changed.
  1626. $elements = $this->xpath('//input[@id=:id]', array(':id' => 'edit-language-' . $langcode));
  1627. $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), 'Default language successfully updated.');
  1628. $this->drupalLogout();
  1629. }
  1630. }
  1631. /**
  1632. * Functional test for language handling during user creation.
  1633. */
  1634. class LocaleUserCreationTest extends DrupalWebTestCase {
  1635. public static function getInfo() {
  1636. return array(
  1637. 'name' => 'User creation',
  1638. 'description' => 'Tests whether proper language is stored for new users and access to language selector.',
  1639. 'group' => 'Locale',
  1640. );
  1641. }
  1642. function setUp() {
  1643. parent::setUp('locale');
  1644. variable_set('user_register', USER_REGISTER_VISITORS);
  1645. }
  1646. /**
  1647. * Functional test for language handling during user creation.
  1648. */
  1649. function testLocalUserCreation() {
  1650. // User to add and remove language and create new users.
  1651. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer users'));
  1652. $this->drupalLogin($admin_user);
  1653. // Add predefined language.
  1654. $langcode = 'fr';
  1655. $edit = array(
  1656. 'langcode' => 'fr',
  1657. );
  1658. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1659. $this->assertText($langcode, 'Language added successfully.');
  1660. $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
  1661. // Set language negotiation.
  1662. $edit = array(
  1663. 'language[enabled][locale-url]' => TRUE,
  1664. );
  1665. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1666. $this->assertText(t('Language negotiation configuration saved.'), 'Set language negotiation.');
  1667. // Check if the language selector is available on admin/people/create and
  1668. // set to the currently active language.
  1669. $this->drupalGet($langcode . '/admin/people/create');
  1670. $this->assertFieldChecked("edit-language-$langcode", 'Global language set in the language selector.');
  1671. // Create a user with the admin/people/create form and check if the correct
  1672. // language is set.
  1673. $username = $this->randomName(10);
  1674. $edit = array(
  1675. 'name' => $username,
  1676. 'mail' => $this->randomName(4) . '@example.com',
  1677. 'pass[pass1]' => $username,
  1678. 'pass[pass2]' => $username,
  1679. );
  1680. $this->drupalPost($langcode . '/admin/people/create', $edit, t('Create new account'));
  1681. $user = user_load_by_name($username);
  1682. $this->assertEqual($user->language, $langcode, 'New user has correct language set.');
  1683. // Register a new user and check if the language selector is hidden.
  1684. $this->drupalLogout();
  1685. $this->drupalGet($langcode . '/user/register');
  1686. $this->assertNoFieldByName('language[fr]', 'Language selector is not accessible.');
  1687. $username = $this->randomName(10);
  1688. $edit = array(
  1689. 'name' => $username,
  1690. 'mail' => $this->randomName(4) . '@example.com',
  1691. );
  1692. $this->drupalPost($langcode . '/user/register', $edit, t('Create new account'));
  1693. $user = user_load_by_name($username);
  1694. $this->assertEqual($user->language, $langcode, 'New user has correct language set.');
  1695. // Test if the admin can use the language selector and if the
  1696. // correct language is was saved.
  1697. $user_edit = $langcode . '/user/' . $user->uid . '/edit';
  1698. $this->drupalLogin($admin_user);
  1699. $this->drupalGet($user_edit);
  1700. $this->assertFieldChecked("edit-language-$langcode", 'Language selector is accessible and correct language is selected.');
  1701. // Set pass_raw so we can login the new user.
  1702. $user->pass_raw = $this->randomName(10);
  1703. $edit = array(
  1704. 'pass[pass1]' => $user->pass_raw,
  1705. 'pass[pass2]' => $user->pass_raw,
  1706. );
  1707. $this->drupalPost($user_edit, $edit, t('Save'));
  1708. $this->drupalLogin($user);
  1709. $this->drupalGet($user_edit);
  1710. $this->assertFieldChecked("edit-language-$langcode", 'Language selector is accessible and correct language is selected.');
  1711. }
  1712. }
  1713. /**
  1714. * Functional tests for configuring a different path alias per language.
  1715. */
  1716. class LocalePathFunctionalTest extends DrupalWebTestCase {
  1717. public static function getInfo() {
  1718. return array(
  1719. 'name' => 'Path language settings',
  1720. 'description' => 'Checks you can configure a language for individual URL aliases.',
  1721. 'group' => 'Locale',
  1722. );
  1723. }
  1724. function setUp() {
  1725. parent::setUp('locale', 'path');
  1726. }
  1727. /**
  1728. * Test if a language can be associated with a path alias.
  1729. */
  1730. function testPathLanguageConfiguration() {
  1731. global $base_url;
  1732. // User to add and remove language.
  1733. $admin_user = $this->drupalCreateUser(array('administer languages', 'create page content', 'administer url aliases', 'create url aliases', 'access administration pages'));
  1734. // Add custom language.
  1735. $this->drupalLogin($admin_user);
  1736. // Code for the language.
  1737. $langcode = 'xx';
  1738. // The English name for the language.
  1739. $name = $this->randomName(16);
  1740. // The native name for the language.
  1741. $native = $this->randomName(16);
  1742. // The domain prefix.
  1743. $prefix = $langcode;
  1744. $edit = array(
  1745. 'langcode' => $langcode,
  1746. 'name' => $name,
  1747. 'native' => $native,
  1748. 'prefix' => $prefix,
  1749. 'direction' => '0',
  1750. );
  1751. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1752. // Check that the "xx" front page is not available when path prefixes are
  1753. // not enabled yet.
  1754. $this->drupalPost('admin/config/regional/language/configure', array(), t('Save settings'));
  1755. $this->drupalGet($prefix);
  1756. $this->assertResponse(404, 'The "xx" front page is not available yet.');
  1757. // Enable URL language detection and selection.
  1758. $edit = array('language[enabled][locale-url]' => 1);
  1759. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  1760. // Create a node.
  1761. $node = $this->drupalCreateNode(array('type' => 'page'));
  1762. // Create a path alias in default language (English).
  1763. $path = 'admin/config/search/path/add';
  1764. $english_path = $this->randomName(8);
  1765. $edit = array(
  1766. 'source' => 'node/' . $node->nid,
  1767. 'alias' => $english_path,
  1768. 'language' => 'en',
  1769. );
  1770. $this->drupalPost($path, $edit, t('Save'));
  1771. // Create a path alias in new custom language.
  1772. $custom_language_path = $this->randomName(8);
  1773. $edit = array(
  1774. 'source' => 'node/' . $node->nid,
  1775. 'alias' => $custom_language_path,
  1776. 'language' => $langcode,
  1777. );
  1778. $this->drupalPost($path, $edit, t('Save'));
  1779. // Confirm English language path alias works.
  1780. $this->drupalGet($english_path);
  1781. $this->assertText($node->title, 'English alias works.');
  1782. // Confirm custom language path alias works.
  1783. $this->drupalGet($prefix . '/' . $custom_language_path);
  1784. $this->assertText($node->title, 'Custom language alias works.');
  1785. // Create a custom path.
  1786. $custom_path = $this->randomName(8);
  1787. // Check priority of language for alias by source path.
  1788. $edit = array(
  1789. 'source' => 'node/' . $node->nid,
  1790. 'alias' => $custom_path,
  1791. 'language' => LANGUAGE_NONE,
  1792. );
  1793. path_save($edit);
  1794. $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, 'en');
  1795. $this->assertEqual($english_path, $lookup_path, 'English language alias has priority.');
  1796. // Same check for language 'xx'.
  1797. $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, $prefix);
  1798. $this->assertEqual($custom_language_path, $lookup_path, 'Custom language alias has priority.');
  1799. path_delete($edit);
  1800. // Create language nodes to check priority of aliases.
  1801. $first_node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  1802. $second_node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  1803. // Assign a custom path alias to the first node with the English language.
  1804. $edit = array(
  1805. 'source' => 'node/' . $first_node->nid,
  1806. 'alias' => $custom_path,
  1807. 'language' => 'en',
  1808. );
  1809. path_save($edit);
  1810. // Assign a custom path alias to second node with LANGUAGE_NONE.
  1811. $edit = array(
  1812. 'source' => 'node/' . $second_node->nid,
  1813. 'alias' => $custom_path,
  1814. 'language' => LANGUAGE_NONE,
  1815. );
  1816. path_save($edit);
  1817. // Test that both node titles link to our path alias.
  1818. $this->drupalGet('<front>');
  1819. $custom_path_url = base_path() . (variable_get('clean_url', 0) ? $custom_path : '?q=' . $custom_path);
  1820. $elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $first_node->title));
  1821. $this->assertTrue(!empty($elements), 'First node links to the path alias.');
  1822. $elements = $this->xpath('//a[@href=:href and .=:title]', array(':href' => $custom_path_url, ':title' => $second_node->title));
  1823. $this->assertTrue(!empty($elements), 'Second node links to the path alias.');
  1824. // Confirm that the custom path leads to the first node.
  1825. $this->drupalGet($custom_path);
  1826. $this->assertText($first_node->title, 'Custom alias returns first node.');
  1827. // Confirm that the custom path with prefix leads to the second node.
  1828. $this->drupalGet($prefix . '/' . $custom_path);
  1829. $this->assertText($second_node->title, 'Custom alias with prefix returns second node.');
  1830. }
  1831. }
  1832. /**
  1833. * Functional tests for multilingual support on nodes.
  1834. */
  1835. class LocaleContentFunctionalTest extends DrupalWebTestCase {
  1836. public static function getInfo() {
  1837. return array(
  1838. 'name' => 'Content language settings',
  1839. 'description' => 'Checks you can enable multilingual support on content types and configure a language for a node.',
  1840. 'group' => 'Locale',
  1841. );
  1842. }
  1843. function setUp() {
  1844. parent::setUp('locale');
  1845. }
  1846. /**
  1847. * Verifies that machine name fields are always LTR.
  1848. */
  1849. function testMachineNameLTR() {
  1850. // User to add and remove language.
  1851. $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
  1852. // Log in as admin.
  1853. $this->drupalLogin($admin_user);
  1854. // Verify that the machine name field is LTR for a new content type.
  1855. $this->drupalGet('admin/structure/types/add');
  1856. $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when no additional language is configured.');
  1857. // Install the Arabic language (which is RTL) and configure as the default.
  1858. $edit = array();
  1859. $edit['langcode'] = 'ar';
  1860. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1861. $edit = array();
  1862. $edit['site_default'] = 'ar';
  1863. $this->drupalPost(NULL, $edit, t('Save configuration'));
  1864. // Verify that the machine name field is still LTR for a new content type.
  1865. $this->drupalGet('admin/structure/types/add');
  1866. $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when the default language is RTL.');
  1867. }
  1868. /**
  1869. * Test if a content type can be set to multilingual and language setting is
  1870. * present on node add and edit forms.
  1871. */
  1872. function testContentTypeLanguageConfiguration() {
  1873. global $base_url;
  1874. // User to add and remove language.
  1875. $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
  1876. // User to create a node.
  1877. $web_user = $this->drupalCreateUser(array('create article content', 'create page content', 'edit any page content'));
  1878. // Add custom language.
  1879. $this->drupalLogin($admin_user);
  1880. // Code for the language.
  1881. $langcode = 'xx';
  1882. // The English name for the language.
  1883. $name = $this->randomName(16);
  1884. // The native name for the language.
  1885. $native = $this->randomName(16);
  1886. // The domain prefix.
  1887. $prefix = $langcode;
  1888. $edit = array(
  1889. 'langcode' => $langcode,
  1890. 'name' => $name,
  1891. 'native' => $native,
  1892. 'prefix' => $prefix,
  1893. 'direction' => '0',
  1894. );
  1895. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1896. // Add disabled custom language.
  1897. // Code for the language.
  1898. $langcode_disabled = 'xx-yy';
  1899. // The English name for the language.
  1900. $name_disabled = $this->randomName(16);
  1901. // The native name for the language.
  1902. $native_disabled = $this->randomName(16);
  1903. // The domain prefix.
  1904. $prefix_disabled = $langcode_disabled;
  1905. $edit = array(
  1906. 'langcode' => $langcode_disabled,
  1907. 'name' => $name_disabled,
  1908. 'native' => $native_disabled,
  1909. 'prefix' => $prefix_disabled,
  1910. 'direction' => '0',
  1911. );
  1912. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  1913. // Disable second custom language.
  1914. $path = 'admin/config/regional/language';
  1915. $edit = array(
  1916. 'enabled[' . $langcode_disabled . ']' => FALSE,
  1917. );
  1918. $this->drupalPost($path, $edit, t('Save configuration'));
  1919. // Set "Basic page" content type to use multilingual support.
  1920. $this->drupalGet('admin/structure/types/manage/page');
  1921. $this->assertText(t('Multilingual support'), 'Multilingual support fieldset present on content type configuration form.');
  1922. $edit = array(
  1923. 'language_content_type' => 1,
  1924. );
  1925. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  1926. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), 'Basic page content type has been updated.');
  1927. $this->drupalLogout();
  1928. // Verify language selection is not present on add article form.
  1929. $this->drupalLogin($web_user);
  1930. $this->drupalGet('node/add/article');
  1931. // Verify language select list is not present.
  1932. $this->assertNoFieldByName('language', NULL, 'Language select not present on add article form.');
  1933. // Verify language selection appears on add "Basic page" form.
  1934. $this->drupalGet('node/add/page');
  1935. // Verify language select list is present.
  1936. $this->assertFieldByName('language', NULL, 'Language select present on add Basic page form.');
  1937. // Ensure enabled language appears.
  1938. $this->assertText($name, 'Enabled language present.');
  1939. // Ensure disabled language doesn't appear.
  1940. $this->assertNoText($name_disabled, 'Disabled language not present.');
  1941. // Create "Basic page" content.
  1942. $node_title = $this->randomName();
  1943. $node_body = $this->randomName();
  1944. $edit = array(
  1945. 'type' => 'page',
  1946. 'title' => $node_title,
  1947. 'body' => array($langcode => array(array('value' => $node_body))),
  1948. 'language' => $langcode,
  1949. );
  1950. $node = $this->drupalCreateNode($edit);
  1951. // Edit the content and ensure correct language is selected.
  1952. $path = 'node/' . $node->nid . '/edit';
  1953. $this->drupalGet($path);
  1954. $this->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', 'Correct language selected.');
  1955. // Ensure we can change the node language.
  1956. $edit = array(
  1957. 'language' => 'en',
  1958. );
  1959. $this->drupalPost($path, $edit, t('Save'));
  1960. $this->assertRaw(t('%title has been updated.', array('%title' => $node_title)), 'Basic page content updated.');
  1961. $this->drupalLogout();
  1962. }
  1963. /**
  1964. * Verifies that nodes may be created with different languages.
  1965. */
  1966. function testNodeCreationWithLanguage() {
  1967. // Create an admin user and log them in.
  1968. $perms = array(
  1969. // Standard node permissions.
  1970. 'create page content',
  1971. 'administer content types',
  1972. 'administer nodes',
  1973. 'bypass node access',
  1974. // Locale.
  1975. 'administer languages',
  1976. );
  1977. $web_user = $this->drupalCreateUser($perms);
  1978. $this->drupalLogin($web_user);
  1979. // Create some test nodes using different langcodes.
  1980. foreach (array(LANGUAGE_NONE, 'en', 'fr') as $langcode) {
  1981. $node_args = array(
  1982. 'type' => 'page',
  1983. 'promote' => 1,
  1984. 'language' => $langcode,
  1985. );
  1986. $node = $this->drupalCreateNode($node_args);
  1987. $node_reloaded = node_load($node->nid, NULL, TRUE);
  1988. $this->assertEqual($node_reloaded->language, $langcode, format_string('The language code of the node was successfully set to @langcode.', array('@langcode' => $langcode)));
  1989. }
  1990. }
  1991. }
  1992. /**
  1993. * Test UI language negotiation
  1994. * 1. URL (PATH) > DEFAULT
  1995. * UI Language base on URL prefix, browser language preference has no
  1996. * influence:
  1997. * admin/config
  1998. * UI in site default language
  1999. * zh-hans/admin/config
  2000. * UI in Chinese
  2001. * blah-blah/admin/config
  2002. * 404
  2003. * 2. URL (PATH) > BROWSER > DEFAULT
  2004. * admin/config
  2005. * UI in user's browser language preference if the site has that
  2006. * language enabled, if not, the default language
  2007. * zh-hans/admin/config
  2008. * UI in Chinese
  2009. * blah-blah/admin/config
  2010. * 404
  2011. * 3. URL (DOMAIN) > DEFAULT
  2012. * http://example.com/admin/config
  2013. * UI language in site default
  2014. * http://example.cn/admin/config
  2015. * UI language in Chinese
  2016. */
  2017. class LocaleUILanguageNegotiationTest extends DrupalWebTestCase {
  2018. public static function getInfo() {
  2019. return array(
  2020. 'name' => 'UI language negotiation',
  2021. 'description' => 'Test UI language switching by URL path prefix and domain.',
  2022. 'group' => 'Locale',
  2023. );
  2024. }
  2025. function setUp() {
  2026. parent::setUp('locale', 'locale_test');
  2027. require_once DRUPAL_ROOT . '/includes/language.inc';
  2028. drupal_load('module', 'locale');
  2029. $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages', 'administer blocks'));
  2030. $this->drupalLogin($admin_user);
  2031. }
  2032. /**
  2033. * Tests for language switching by URL path.
  2034. */
  2035. function testUILanguageNegotiation() {
  2036. // A few languages to switch to.
  2037. // This one is unknown, should get the default lang version.
  2038. $language_unknown = 'blah-blah';
  2039. // For testing browser lang preference.
  2040. $language_browser_fallback = 'vi';
  2041. // For testing path prefix.
  2042. $language = 'zh-hans';
  2043. // For setting browser language preference to 'vi'.
  2044. $http_header_browser_fallback = array("Accept-Language: $language_browser_fallback;q=1");
  2045. // For setting browser language preference to some unknown.
  2046. $http_header_blah = array("Accept-Language: blah;q=1");
  2047. // This domain should switch the UI to Chinese.
  2048. $language_domain = 'example.cn';
  2049. // Setup the site languages by installing two languages.
  2050. require_once DRUPAL_ROOT . '/includes/locale.inc';
  2051. locale_add_language($language_browser_fallback);
  2052. locale_add_language($language);
  2053. // We will look for this string in the admin/config screen to see if the
  2054. // corresponding translated string is shown.
  2055. $default_string = 'Configure languages for content and the user interface';
  2056. // Set the default language in order for the translated string to be registered
  2057. // into database when seen by t(). Without doing this, our target string
  2058. // is for some reason not found when doing translate search. This might
  2059. // be some bug.
  2060. drupal_static_reset('language_list');
  2061. $languages = language_list('enabled');
  2062. variable_set('language_default', $languages[1]['vi']);
  2063. // First visit this page to make sure our target string is searchable.
  2064. $this->drupalGet('admin/config');
  2065. // Now the t()'ed string is in db so switch the language back to default.
  2066. variable_del('language_default');
  2067. // Translate the string.
  2068. $language_browser_fallback_string = "In $language_browser_fallback In $language_browser_fallback In $language_browser_fallback";
  2069. $language_string = "In $language In $language In $language";
  2070. // Do a translate search of our target string.
  2071. $edit = array( 'string' => $default_string);
  2072. $this->drupalPost('admin/config/regional/translate/translate', $edit, t('Filter'));
  2073. // Should find the string and now click edit to post translated string.
  2074. $this->clickLink('edit');
  2075. $edit = array(
  2076. "translations[$language_browser_fallback]" => $language_browser_fallback_string,
  2077. "translations[$language]" => $language_string,
  2078. );
  2079. $this->drupalPost(NULL, $edit, t('Save translations'));
  2080. // Configure URL language rewrite.
  2081. variable_set('locale_language_negotiation_url_type', LANGUAGE_TYPE_INTERFACE);
  2082. $tests = array(
  2083. // Default, browser preference should have no influence.
  2084. array(
  2085. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
  2086. 'path' => 'admin/config',
  2087. 'expect' => $default_string,
  2088. 'expected_provider' => LANGUAGE_NEGOTIATION_DEFAULT,
  2089. 'http_header' => $http_header_browser_fallback,
  2090. 'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.',
  2091. ),
  2092. // Language prefix.
  2093. array(
  2094. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
  2095. 'path' => "$language/admin/config",
  2096. 'expect' => $language_string,
  2097. 'expected_provider' => LOCALE_LANGUAGE_NEGOTIATION_URL,
  2098. 'http_header' => $http_header_browser_fallback,
  2099. 'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix',
  2100. ),
  2101. // Default, go by browser preference.
  2102. array(
  2103. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LOCALE_LANGUAGE_NEGOTIATION_BROWSER),
  2104. 'path' => 'admin/config',
  2105. 'expect' => $language_browser_fallback_string,
  2106. 'expected_provider' => LOCALE_LANGUAGE_NEGOTIATION_BROWSER,
  2107. 'http_header' => $http_header_browser_fallback,
  2108. 'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference',
  2109. ),
  2110. // Prefix, switch to the language.
  2111. array(
  2112. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LOCALE_LANGUAGE_NEGOTIATION_BROWSER),
  2113. 'path' => "$language/admin/config",
  2114. 'expect' => $language_string,
  2115. 'expected_provider' => LOCALE_LANGUAGE_NEGOTIATION_URL,
  2116. 'http_header' => $http_header_browser_fallback,
  2117. 'message' => 'URL (PATH) > BROWSER: with langage prefix, UI language is based on path prefix',
  2118. ),
  2119. // Default, browser language preference is not one of site's lang.
  2120. array(
  2121. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LOCALE_LANGUAGE_NEGOTIATION_BROWSER, LANGUAGE_NEGOTIATION_DEFAULT),
  2122. 'path' => 'admin/config',
  2123. 'expect' => $default_string,
  2124. 'expected_provider' => LANGUAGE_NEGOTIATION_DEFAULT,
  2125. 'http_header' => $http_header_blah,
  2126. 'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language',
  2127. ),
  2128. );
  2129. foreach ($tests as $test) {
  2130. $this->runTest($test);
  2131. }
  2132. // Unknown language prefix should return 404.
  2133. variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, locale_language_negotiation_info());
  2134. $this->drupalGet("$language_unknown/admin/config", array(), $http_header_browser_fallback);
  2135. $this->assertResponse(404, "Unknown language path prefix should return 404");
  2136. // Setup for domain negotiation, first configure the language to have domain
  2137. // URL. We use HTTPS and a port to make sure that only the domain name is used.
  2138. $edit = array('prefix' => '', 'domain' => "https://$language_domain:99");
  2139. $this->drupalPost("admin/config/regional/language/edit/$language", $edit, t('Save language'));
  2140. // Set the site to use domain language negotiation.
  2141. $tests = array(
  2142. // Default domain, browser preference should have no influence.
  2143. array(
  2144. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
  2145. 'locale_language_negotiation_url_part' => LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN,
  2146. 'path' => 'admin/config',
  2147. 'expect' => $default_string,
  2148. 'expected_provider' => LANGUAGE_NEGOTIATION_DEFAULT,
  2149. 'http_header' => $http_header_browser_fallback,
  2150. 'message' => 'URL (DOMAIN) > DEFAULT: default domain should get default language',
  2151. ),
  2152. // Language domain specific URL, we set the $_SERVER['HTTP_HOST'] in
  2153. // locale_test.module hook_boot() to simulate this.
  2154. array(
  2155. 'language_negotiation' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
  2156. 'locale_language_negotiation_url_part' => LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN,
  2157. 'locale_test_domain' => $language_domain . ':88',
  2158. 'path' => 'admin/config',
  2159. 'expect' => $language_string,
  2160. 'expected_provider' => LOCALE_LANGUAGE_NEGOTIATION_URL,
  2161. 'http_header' => $http_header_browser_fallback,
  2162. 'message' => 'URL (DOMAIN) > DEFAULT: domain example.cn should switch to Chinese',
  2163. ),
  2164. );
  2165. foreach ($tests as $test) {
  2166. $this->runTest($test);
  2167. }
  2168. }
  2169. private function runTest($test) {
  2170. if (!empty($test['language_negotiation'])) {
  2171. $negotiation = array_flip($test['language_negotiation']);
  2172. language_negotiation_set(LANGUAGE_TYPE_INTERFACE, $negotiation);
  2173. }
  2174. if (!empty($test['locale_language_negotiation_url_part'])) {
  2175. variable_set('locale_language_negotiation_url_part', $test['locale_language_negotiation_url_part']);
  2176. }
  2177. if (!empty($test['locale_test_domain'])) {
  2178. variable_set('locale_test_domain', $test['locale_test_domain']);
  2179. }
  2180. $this->drupalGet($test['path'], array(), $test['http_header']);
  2181. $this->assertText($test['expect'], $test['message']);
  2182. $this->assertText(t('Language negotiation provider: @name', array('@name' => $test['expected_provider'])));
  2183. }
  2184. /**
  2185. * Test URL language detection when the requested URL has no language.
  2186. */
  2187. function testUrlLanguageFallback() {
  2188. // Add the Italian language.
  2189. $language_browser_fallback = 'it';
  2190. locale_add_language($language_browser_fallback);
  2191. $languages = language_list();
  2192. // Enable the path prefix for the default language: this way any unprefixed
  2193. // URL must have a valid fallback value.
  2194. $edit = array('prefix' => 'en');
  2195. $this->drupalPost('admin/config/regional/language/edit/en', $edit, t('Save language'));
  2196. // Enable browser and URL language detection.
  2197. $edit = array(
  2198. 'language[enabled][locale-browser]' => TRUE,
  2199. 'language[enabled][locale-url]' => TRUE,
  2200. 'language[weight][locale-browser]' => -8,
  2201. 'language[weight][locale-url]' => -10,
  2202. );
  2203. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  2204. $this->drupalGet('admin/config/regional/language/configure');
  2205. // Enable the language switcher block.
  2206. $edit = array('blocks[locale_language][region]' => 'sidebar_first');
  2207. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  2208. // Access the front page without specifying any valid URL language prefix
  2209. // and having as browser language preference a non-default language.
  2210. $http_header = array("Accept-Language: $language_browser_fallback;q=1");
  2211. $this->drupalGet('', array(), $http_header);
  2212. // Check that the language switcher active link matches the given browser
  2213. // language.
  2214. $args = array(':url' => base_path() . (!empty($GLOBALS['conf']['clean_url']) ? $language_browser_fallback : "?q=$language_browser_fallback"));
  2215. $fields = $this->xpath('//div[@id="block-locale-language"]//a[@class="language-link active" and @href=:url]', $args);
  2216. $this->assertTrue($fields[0] == $languages[$language_browser_fallback]->native, 'The browser language is the URL active language');
  2217. // Check that URLs are rewritten using the given browser language.
  2218. $fields = $this->xpath('//div[@id="site-name"]//a[@rel="home" and @href=:url]//span', $args);
  2219. $this->assertTrue($fields[0] == 'Drupal', 'URLs are rewritten using the browser language.');
  2220. }
  2221. /**
  2222. * Tests url() when separate domains are used for multiple languages.
  2223. */
  2224. function testLanguageDomain() {
  2225. // Add the Italian language, without protocol.
  2226. $langcode = 'it';
  2227. locale_add_language($langcode, 'Italian', 'Italian', LANGUAGE_LTR, 'it.example.com', '', TRUE, FALSE);
  2228. // Add the French language, with protocol.
  2229. $langcode = 'fr';
  2230. locale_add_language($langcode, 'French', 'French', LANGUAGE_LTR, 'http://fr.example.com', '', TRUE, FALSE);
  2231. // Enable language URL detection.
  2232. $negotiation = array_flip(array(LOCALE_LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT));
  2233. language_negotiation_set(LANGUAGE_TYPE_INTERFACE, $negotiation);
  2234. variable_set('locale_language_negotiation_url_part', 1);
  2235. global $is_https;
  2236. $languages = language_list();
  2237. foreach (array('it', 'fr') as $langcode) {
  2238. // Build the link we're going to test based on the clean URL setting.
  2239. $link = (!empty($GLOBALS['conf']['clean_url'])) ? $langcode . '.example.com/admin' : $langcode . '.example.com/?q=admin';
  2240. // Test URL in another language.
  2241. // Base path gives problems on the testbot, so $correct_link is hard-coded.
  2242. // @see UrlAlterFunctionalTest::assertUrlOutboundAlter (path.test).
  2243. $url = url('admin', array('language' => $languages[$langcode]));
  2244. $url_scheme = ($is_https) ? 'https://' : 'http://';
  2245. $correct_link = $url_scheme . $link;
  2246. $this->assertTrue($url == $correct_link, format_string('The url() function returns the right url (@url) in accordance with the chosen language', array('@url' => $url . " == " . $correct_link)));
  2247. // Test HTTPS via options.
  2248. variable_set('https', TRUE);
  2249. $url = url('admin', array('https' => TRUE, 'language' => $languages[$langcode]));
  2250. $correct_link = 'https://' . $link;
  2251. $this->assertTrue($url == $correct_link, format_string('The url() function returns the right https url (via options) (@url) in accordance with the chosen language', array('@url' => $url . " == " . $correct_link)));
  2252. variable_set('https', FALSE);
  2253. // Test HTTPS via current URL scheme.
  2254. $temp_https = $is_https;
  2255. $is_https = TRUE;
  2256. $url = url('admin', array('language' => $languages[$langcode]));
  2257. $correct_link = 'https://' . $link;
  2258. $this->assertTrue($url == $correct_link, format_string('The url() function returns the right url (via current url scheme) (@url) in accordance with the chosen language', array('@url' => $url . " == " . $correct_link)));
  2259. $is_https = $temp_https;
  2260. }
  2261. }
  2262. }
  2263. /**
  2264. * Test that URL rewriting works as expected.
  2265. */
  2266. class LocaleUrlRewritingTest extends DrupalWebTestCase {
  2267. public static function getInfo() {
  2268. return array(
  2269. 'name' => 'URL rewriting',
  2270. 'description' => 'Test that URL rewriting works as expected.',
  2271. 'group' => 'Locale',
  2272. );
  2273. }
  2274. function setUp() {
  2275. parent::setUp('locale');
  2276. // Create and login user.
  2277. $this->web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
  2278. $this->drupalLogin($this->web_user);
  2279. // Install French language.
  2280. $edit = array();
  2281. $edit['langcode'] = 'fr';
  2282. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  2283. // Install Italian language.
  2284. $edit = array();
  2285. $edit['langcode'] = 'it';
  2286. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  2287. // Disable Italian language.
  2288. $edit = array('enabled[it]' => FALSE);
  2289. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  2290. // Enable URL language detection and selection.
  2291. $edit = array('language[enabled][locale-url]' => 1);
  2292. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  2293. // Reset static caching.
  2294. drupal_static_reset('language_list');
  2295. drupal_static_reset('locale_url_outbound_alter');
  2296. drupal_static_reset('locale_language_url_rewrite_url');
  2297. }
  2298. /**
  2299. * Check that disabled or non-installed languages are not considered.
  2300. */
  2301. function testUrlRewritingEdgeCases() {
  2302. // Check URL rewriting with a disabled language.
  2303. $languages = language_list();
  2304. $this->checkUrl($languages['it'], 'Path language is ignored if language is disabled.', 'URL language negotiation does not work with disabled languages');
  2305. // Check URL rewriting with a non-installed language.
  2306. $non_existing = language_default();
  2307. $non_existing->language = $this->randomName();
  2308. $non_existing->prefix = $this->randomName();
  2309. $this->checkUrl($non_existing, 'Path language is ignored if language is not installed.', 'URL language negotiation does not work with non-installed languages');
  2310. }
  2311. /**
  2312. * Check URL rewriting for the given language.
  2313. *
  2314. * The test is performed with a fixed URL (the default front page) to simply
  2315. * check that language prefixes are not added to it and that the prefixed URL
  2316. * is actually not working.
  2317. *
  2318. * @param string $language
  2319. * The language prefix, e.g. 'es'.
  2320. * @param string $message1
  2321. * Message to display in assertion that language prefixes are not added.
  2322. * @param string $message2
  2323. * The message to display confirming prefixed URL is not working.
  2324. */
  2325. private function checkUrl($language, $message1, $message2) {
  2326. $options = array('language' => $language);
  2327. $base_path = trim(base_path(), '/');
  2328. $rewritten_path = trim(str_replace(array('?q=', $base_path), '', url('node', $options)), '/');
  2329. $segments = explode('/', $rewritten_path, 2);
  2330. $prefix = $segments[0];
  2331. $path = isset($segments[1]) ? $segments[1] : $prefix;
  2332. // If the rewritten URL has not a language prefix we pick the right one from
  2333. // the language object so we can always check the prefixed URL.
  2334. if ($this->assertNotEqual($language->prefix, $prefix, $message1)) {
  2335. $prefix = $language->prefix;
  2336. }
  2337. $this->drupalGet("$prefix/$path");
  2338. $this->assertResponse(404, $message2);
  2339. }
  2340. /**
  2341. * Check URL rewriting when using a domain name and a non-standard port.
  2342. */
  2343. function testDomainNameNegotiationPort() {
  2344. $language_domain = 'example.fr';
  2345. $edit = array(
  2346. 'locale_language_negotiation_url_part' => 1,
  2347. );
  2348. $this->drupalPost('admin/config/regional/language/configure/url', $edit, t('Save configuration'));
  2349. $edit = array(
  2350. 'prefix' => '',
  2351. 'domain' => $language_domain
  2352. );
  2353. $this->drupalPost('admin/config/regional/language/edit/fr', $edit, t('Save language'));
  2354. // Enable domain configuration.
  2355. variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN);
  2356. // Reset static caching.
  2357. drupal_static_reset('language_list');
  2358. drupal_static_reset('language_url_outbound_alter');
  2359. drupal_static_reset('language_url_rewrite_url');
  2360. // In case index.php is part of the URLs, we need to adapt the asserted
  2361. // URLs as well.
  2362. $index_php = strpos(url('', array('absolute' => TRUE)), 'index.php') !== FALSE;
  2363. // Remember current HTTP_HOST.
  2364. $http_host = $_SERVER['HTTP_HOST'];
  2365. // Fake a different port.
  2366. $_SERVER['HTTP_HOST'] .= ':88';
  2367. // Create an absolute French link.
  2368. $languages = language_list();
  2369. $language = $languages['fr'];
  2370. $url = url('', array(
  2371. 'absolute' => TRUE,
  2372. 'language' => $language
  2373. ));
  2374. $expected = 'http://example.fr:88/';
  2375. $expected .= $index_php ? 'index.php/' : '';
  2376. $this->assertEqual($url, $expected, 'The right port is used.');
  2377. // If we set the port explicitly in url(), it should not be overriden.
  2378. $url = url('', array(
  2379. 'absolute' => TRUE,
  2380. 'language' => $language,
  2381. 'base_url' => $GLOBALS['base_url'] . ':90',
  2382. ));
  2383. $expected = 'http://example.fr:90/';
  2384. $expected .= $index_php ? 'index.php/' : '';
  2385. $this->assertEqual($url, $expected, 'A given port is not overriden.');
  2386. // Restore HTTP_HOST.
  2387. $_SERVER['HTTP_HOST'] = $http_host;
  2388. }
  2389. }
  2390. /**
  2391. * Functional test for multilingual fields.
  2392. */
  2393. class LocaleMultilingualFieldsFunctionalTest extends DrupalWebTestCase {
  2394. public static function getInfo() {
  2395. return array(
  2396. 'name' => 'Multilingual fields',
  2397. 'description' => 'Test multilingual support for fields.',
  2398. 'group' => 'Locale',
  2399. );
  2400. }
  2401. function setUp() {
  2402. parent::setUp('locale');
  2403. // Setup users.
  2404. $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages', 'create page content', 'edit own page content'));
  2405. $this->drupalLogin($admin_user);
  2406. // Add a new language.
  2407. require_once DRUPAL_ROOT . '/includes/locale.inc';
  2408. locale_add_language('it', 'Italian', 'Italiano', LANGUAGE_LTR, '', '', TRUE, FALSE);
  2409. // Enable URL language detection and selection.
  2410. $edit = array('language[enabled][locale-url]' => '1');
  2411. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  2412. // Set "Basic page" content type to use multilingual support.
  2413. $edit = array(
  2414. 'language_content_type' => 1,
  2415. );
  2416. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  2417. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), 'Basic page content type has been updated.');
  2418. // Make node body translatable.
  2419. $field = field_info_field('body');
  2420. $field['translatable'] = TRUE;
  2421. field_update_field($field);
  2422. }
  2423. /**
  2424. * Test if field languages are correctly set through the node form.
  2425. */
  2426. function testMultilingualNodeForm() {
  2427. // Create "Basic page" content.
  2428. $langcode = LANGUAGE_NONE;
  2429. $title_key = "title";
  2430. $title_value = $this->randomName(8);
  2431. $body_key = "body[$langcode][0][value]";
  2432. $body_value = $this->randomName(16);
  2433. // Create node to edit.
  2434. $edit = array();
  2435. $edit[$title_key] = $title_value;
  2436. $edit[$body_key] = $body_value;
  2437. $edit['language'] = 'en';
  2438. $this->drupalPost('node/add/page', $edit, t('Save'));
  2439. // Check that the node exists in the database.
  2440. $node = $this->drupalGetNodeByTitle($edit[$title_key]);
  2441. $this->assertTrue($node, 'Node found in database.');
  2442. $assert = isset($node->body['en']) && !isset($node->body[LANGUAGE_NONE]) && $node->body['en'][0]['value'] == $body_value;
  2443. $this->assertTrue($assert, 'Field language correctly set.');
  2444. // Change node language.
  2445. $this->drupalGet("node/$node->nid/edit");
  2446. $edit = array(
  2447. $title_key => $this->randomName(8),
  2448. 'language' => 'it'
  2449. );
  2450. $this->drupalPost(NULL, $edit, t('Save'));
  2451. $node = $this->drupalGetNodeByTitle($edit[$title_key]);
  2452. $this->assertTrue($node, 'Node found in database.');
  2453. $assert = isset($node->body['it']) && !isset($node->body['en']) && $node->body['it'][0]['value'] == $body_value;
  2454. $this->assertTrue($assert, 'Field language correctly changed.');
  2455. // Enable content language URL detection.
  2456. language_negotiation_set(LANGUAGE_TYPE_CONTENT, array(LOCALE_LANGUAGE_NEGOTIATION_URL => 0));
  2457. // Test multilingual field language fallback logic.
  2458. $this->drupalGet("it/node/$node->nid");
  2459. $this->assertRaw($body_value, 'Body correctly displayed using Italian as requested language');
  2460. $this->drupalGet("node/$node->nid");
  2461. $this->assertRaw($body_value, 'Body correctly displayed using English as requested language');
  2462. }
  2463. /*
  2464. * Test multilingual field display settings.
  2465. */
  2466. function testMultilingualDisplaySettings() {
  2467. // Create "Basic page" content.
  2468. $langcode = LANGUAGE_NONE;
  2469. $title_key = "title";
  2470. $title_value = $this->randomName(8);
  2471. $body_key = "body[$langcode][0][value]";
  2472. $body_value = $this->randomName(16);
  2473. // Create node to edit.
  2474. $edit = array();
  2475. $edit[$title_key] = $title_value;
  2476. $edit[$body_key] = $body_value;
  2477. $edit['language'] = 'en';
  2478. $this->drupalPost('node/add/page', $edit, t('Save'));
  2479. // Check that the node exists in the database.
  2480. $node = $this->drupalGetNodeByTitle($edit[$title_key]);
  2481. $this->assertTrue($node, 'Node found in database.');
  2482. // Check if node body is showed.
  2483. $this->drupalGet("node/$node->nid");
  2484. $body = $this->xpath('//div[@id=:id]//div[@property="content:encoded"]/p', array(':id' => 'node-' . $node->nid));
  2485. $this->assertEqual(current($body), $node->body['en'][0]['value'], 'Node body is correctly showed.');
  2486. }
  2487. }
  2488. /**
  2489. * Functional tests for comment language.
  2490. */
  2491. class LocaleCommentLanguageFunctionalTest extends DrupalWebTestCase {
  2492. public static function getInfo() {
  2493. return array(
  2494. 'name' => 'Comment language',
  2495. 'description' => 'Tests for comment language.',
  2496. 'group' => 'Locale',
  2497. );
  2498. }
  2499. function setUp() {
  2500. parent::setUp('locale', 'locale_test');
  2501. // Create and login user.
  2502. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'administer comments', 'create article content'));
  2503. $this->drupalLogin($admin_user);
  2504. // Add language.
  2505. $edit = array('langcode' => 'fr');
  2506. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  2507. // Set "Article" content type to use multilingual support.
  2508. $edit = array('language_content_type' => 1);
  2509. $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
  2510. // Enable content language negotiation UI.
  2511. variable_set('locale_test_content_language_type', TRUE);
  2512. // Set interface language detection to user and content language detection
  2513. // to URL. Disable inheritance from interface language to ensure content
  2514. // language will fall back to the default language if no URL language can be
  2515. // detected.
  2516. $edit = array(
  2517. 'language[enabled][locale-user]' => TRUE,
  2518. 'language_content[enabled][locale-url]' => TRUE,
  2519. 'language_content[enabled][locale-interface]' => FALSE,
  2520. );
  2521. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  2522. // Change user language preference, this way interface language is always
  2523. // French no matter what path prefix the URLs have.
  2524. $edit = array('language' => 'fr');
  2525. $this->drupalPost("user/{$admin_user->uid}/edit", $edit, t('Save'));
  2526. // Make comment body translatable.
  2527. $field = field_info_field('comment_body');
  2528. $field['translatable'] = TRUE;
  2529. field_update_field($field);
  2530. $this->assertTrue(field_is_translatable('comment', $field), 'Comment body is translatable.');
  2531. }
  2532. /**
  2533. * Test that comment language is properly set.
  2534. */
  2535. function testCommentLanguage() {
  2536. drupal_static_reset('language_list');
  2537. // Create two nodes, one for english and one for french, and comment each
  2538. // node using both english and french as content language by changing URL
  2539. // language prefixes. Meanwhile interface language is always French, which
  2540. // is the user language preference. This way we can ensure that node
  2541. // language and interface language do not influence comment language, as
  2542. // only content language has to.
  2543. foreach (language_list() as $node_langcode => $node_language) {
  2544. $language_none = LANGUAGE_NONE;
  2545. // Create "Article" content.
  2546. $title = $this->randomName();
  2547. $edit = array(
  2548. "title" => $title,
  2549. "body[$language_none][0][value]" => $this->randomName(),
  2550. "language" => $node_langcode,
  2551. );
  2552. $this->drupalPost("node/add/article", $edit, t('Save'));
  2553. $node = $this->drupalGetNodeByTitle($title);
  2554. foreach (language_list() as $langcode => $language) {
  2555. // Post a comment with content language $langcode.
  2556. $prefix = empty($language->prefix) ? '' : $language->prefix . '/';
  2557. $comment_values[$node_langcode][$langcode] = $this->randomName();
  2558. // Initially field form widgets have no language.
  2559. $edit = array(
  2560. 'subject' => $this->randomName(),
  2561. "comment_body[$language_none][0][value]" => $comment_values[$node_langcode][$langcode],
  2562. );
  2563. $this->drupalPost("{$prefix}node/{$node->nid}", $edit, t('Preview'));
  2564. // After the first submit the submitted entity language is taken into
  2565. // account.
  2566. $edit = array(
  2567. 'subject' => $edit['subject'],
  2568. "comment_body[$langcode][0][value]" => $comment_values[$node_langcode][$langcode],
  2569. );
  2570. $this->drupalPost(NULL, $edit, t('Save'));
  2571. // Check that comment language matches the current content language.
  2572. $cid = db_select('comment', 'c')
  2573. ->fields('c', array('cid'))
  2574. ->condition('nid', $node->nid)
  2575. ->orderBy('cid', 'DESC')
  2576. ->range(0, 1)
  2577. ->execute()
  2578. ->fetchField();
  2579. $comment = comment_load($cid);
  2580. $comment_langcode = entity_language('comment', $comment);
  2581. $args = array('%node_language' => $node_langcode, '%comment_language' => $comment_langcode, '%langcode' => $langcode);
  2582. $this->assertEqual($comment_langcode, $langcode, format_string('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args));
  2583. $this->assertEqual($comment->comment_body[$langcode][0]['value'], $comment_values[$node_langcode][$langcode], 'Comment body correctly stored.');
  2584. }
  2585. }
  2586. // Check that comment bodies appear in the administration UI.
  2587. $this->drupalGet('admin/content/comment');
  2588. foreach ($comment_values as $node_values) {
  2589. foreach ($node_values as $value) {
  2590. $this->assertRaw($value);
  2591. }
  2592. }
  2593. }
  2594. }
  2595. /**
  2596. * Functional tests for localizing date formats.
  2597. */
  2598. class LocaleDateFormatsFunctionalTest extends DrupalWebTestCase {
  2599. public static function getInfo() {
  2600. return array(
  2601. 'name' => 'Localize date formats',
  2602. 'description' => 'Tests for the localization of date formats.',
  2603. 'group' => 'Locale',
  2604. );
  2605. }
  2606. function setUp() {
  2607. parent::setUp('locale');
  2608. // Create and login user.
  2609. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'create article content'));
  2610. $this->drupalLogin($admin_user);
  2611. }
  2612. /**
  2613. * Functional tests for localizing date formats.
  2614. */
  2615. function testLocalizeDateFormats() {
  2616. // Add language.
  2617. $edit = array(
  2618. 'langcode' => 'fr',
  2619. );
  2620. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  2621. // Set language negotiation.
  2622. $language_type = LANGUAGE_TYPE_INTERFACE;
  2623. $edit = array(
  2624. "{$language_type}[enabled][locale-url]" => TRUE,
  2625. );
  2626. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  2627. // Configure date formats.
  2628. $this->drupalGet('admin/config/regional/date-time/locale');
  2629. $this->assertText('Français', 'Configured languages appear.');
  2630. $edit = array(
  2631. 'date_format_long' => 'd.m.Y - H:i',
  2632. 'date_format_medium' => 'd.m.Y - H:i',
  2633. 'date_format_short' => 'd.m.Y - H:i',
  2634. );
  2635. $this->drupalPost('admin/config/regional/date-time/locale/fr/edit', $edit, t('Save configuration'));
  2636. $this->assertText(t('Configuration saved.'), 'French date formats updated.');
  2637. $edit = array(
  2638. 'date_format_long' => 'j M Y - g:ia',
  2639. 'date_format_medium' => 'j M Y - g:ia',
  2640. 'date_format_short' => 'j M Y - g:ia',
  2641. );
  2642. $this->drupalPost('admin/config/regional/date-time/locale/en/edit', $edit, t('Save configuration'));
  2643. $this->assertText(t('Configuration saved.'), 'English date formats updated.');
  2644. // Create node content.
  2645. $node = $this->drupalCreateNode(array('type' => 'article'));
  2646. // Configure format for the node posted date changes with the language.
  2647. $this->drupalGet('node/' . $node->nid);
  2648. $english_date = format_date($node->created, 'custom', 'j M Y');
  2649. $this->assertText($english_date, 'English date format appears');
  2650. $this->drupalGet('fr/node/' . $node->nid);
  2651. $french_date = format_date($node->created, 'custom', 'd.m.Y');
  2652. $this->assertText($french_date, 'French date format appears');
  2653. }
  2654. }
  2655. /**
  2656. * Functional test for language types/negotiation info.
  2657. */
  2658. class LocaleLanguageNegotiationInfoFunctionalTest extends DrupalWebTestCase {
  2659. public static function getInfo() {
  2660. return array(
  2661. 'name' => 'Language negotiation info',
  2662. 'description' => 'Tests alterations to language types/negotiation info.',
  2663. 'group' => 'Locale',
  2664. );
  2665. }
  2666. function setUp() {
  2667. parent::setUp('locale');
  2668. require_once DRUPAL_ROOT .'/includes/language.inc';
  2669. $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'view the administration theme'));
  2670. $this->drupalLogin($admin_user);
  2671. $this->drupalPost('admin/config/regional/language/add', array('langcode' => 'it'), t('Add language'));
  2672. }
  2673. /**
  2674. * Tests alterations to language types/negotiation info.
  2675. */
  2676. function testInfoAlterations() {
  2677. // Enable language type/negotiation info alterations.
  2678. variable_set('locale_test_language_types', TRUE);
  2679. variable_set('locale_test_language_negotiation_info', TRUE);
  2680. $this->languageNegotiationUpdate();
  2681. // Check that fixed language types are properly configured without the need
  2682. // of saving the language negotiation settings.
  2683. $this->checkFixedLanguageTypes();
  2684. // Make the content language type configurable by updating the language
  2685. // negotiation settings with the proper flag enabled.
  2686. variable_set('locale_test_content_language_type', TRUE);
  2687. $this->languageNegotiationUpdate();
  2688. $type = LANGUAGE_TYPE_CONTENT;
  2689. $language_types = variable_get('language_types', drupal_language_types());
  2690. $this->assertTrue($language_types[$type], 'Content language type is configurable.');
  2691. // Enable some core and custom language providers. The test language type is
  2692. // supposed to be configurable.
  2693. $test_type = 'test_language_type';
  2694. $provider = LOCALE_LANGUAGE_NEGOTIATION_INTERFACE;
  2695. $test_provider = 'test_language_provider';
  2696. $form_field = $type . '[enabled]['. $provider .']';
  2697. $edit = array(
  2698. $form_field => TRUE,
  2699. $type . '[enabled][' . $test_provider . ']' => TRUE,
  2700. $test_type . '[enabled][' . $test_provider . ']' => TRUE,
  2701. );
  2702. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  2703. // Remove the interface language provider by updating the language
  2704. // negotiation settings with the proper flag enabled.
  2705. variable_set('locale_test_language_negotiation_info_alter', TRUE);
  2706. $this->languageNegotiationUpdate();
  2707. $negotiation = variable_get("language_negotiation_$type", array());
  2708. $this->assertFalse(isset($negotiation[$provider]), 'Interface language provider removed from the stored settings.');
  2709. $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, 'Interface language provider unavailable.');
  2710. // Check that type-specific language providers can be assigned only to the
  2711. // corresponding language types.
  2712. foreach (language_types_configurable() as $type) {
  2713. $form_field = $type . '[enabled][test_language_provider_ts]';
  2714. if ($type == $test_type) {
  2715. $this->assertFieldByXPath("//input[@name=\"$form_field\"]", NULL, format_string('Type-specific test language provider available for %type.', array('%type' => $type)));
  2716. }
  2717. else {
  2718. $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, format_string('Type-specific test language provider unavailable for %type.', array('%type' => $type)));
  2719. }
  2720. }
  2721. // Check language negotiation results.
  2722. $this->drupalGet('');
  2723. $last = variable_get('locale_test_language_negotiation_last', array());
  2724. foreach (language_types() as $type) {
  2725. $langcode = $last[$type];
  2726. $value = $type == LANGUAGE_TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en';
  2727. $this->assertEqual($langcode, $value, format_string('The negotiated language for %type is %language', array('%type' => $type, '%language' => $langcode)));
  2728. }
  2729. // Disable locale_test and check that everything is set back to the original
  2730. // status.
  2731. $this->languageNegotiationUpdate('disable');
  2732. // Check that only the core language types are available.
  2733. foreach (language_types() as $type) {
  2734. $this->assertTrue(strpos($type, 'test') === FALSE, format_string('The %type language is still available', array('%type' => $type)));
  2735. }
  2736. // Check that fixed language types are properly configured, even those
  2737. // previously set to configurable.
  2738. $this->checkFixedLanguageTypes();
  2739. // Check that unavailable language providers are not present in the
  2740. // negotiation settings.
  2741. $negotiation = variable_get("language_negotiation_$type", array());
  2742. $this->assertFalse(isset($negotiation[$test_provider]), 'The disabled test language provider is not part of the content language negotiation settings.');
  2743. // Check that configuration page presents the correct options and settings.
  2744. $this->assertNoRaw(t('Test language detection'), 'No test language type configuration available.');
  2745. $this->assertNoRaw(t('This is a test language provider'), 'No test language provider available.');
  2746. }
  2747. /**
  2748. * Update language types/negotiation information.
  2749. *
  2750. * Manually invoke locale_modules_enabled()/locale_modules_disabled() since
  2751. * they would not be invoked after enabling/disabling locale_test the first
  2752. * time.
  2753. */
  2754. private function languageNegotiationUpdate($op = 'enable') {
  2755. static $last_op = NULL;
  2756. $modules = array('locale_test');
  2757. // Enable/disable locale_test only if we did not already before.
  2758. if ($last_op != $op) {
  2759. $function = "module_{$op}";
  2760. $function($modules);
  2761. // Reset hook implementation cache.
  2762. module_implements(NULL, FALSE, TRUE);
  2763. }
  2764. drupal_static_reset('language_types_info');
  2765. drupal_static_reset('language_negotiation_info');
  2766. $function = "locale_modules_{$op}d";
  2767. if (function_exists($function)) {
  2768. $function($modules);
  2769. }
  2770. $this->drupalGet('admin/config/regional/language/configure');
  2771. }
  2772. /**
  2773. * Check that language negotiation for fixed types matches the stored one.
  2774. */
  2775. private function checkFixedLanguageTypes() {
  2776. drupal_static_reset('language_types_info');
  2777. foreach (language_types_info() as $type => $info) {
  2778. if (isset($info['fixed'])) {
  2779. $negotiation = variable_get("language_negotiation_$type", array());
  2780. $equal = count($info['fixed']) == count($negotiation);
  2781. while ($equal && list($id) = each($negotiation)) {
  2782. list(, $info_id) = each($info['fixed']);
  2783. $equal = $info_id == $id;
  2784. }
  2785. $this->assertTrue($equal, format_string('language negotiation for %type is properly set up', array('%type' => $type)));
  2786. }
  2787. }
  2788. }
  2789. }
  2790. /**
  2791. * Functional tests for CSS alter functions.
  2792. */
  2793. class LocaleCSSAlterTest extends DrupalWebTestCase {
  2794. public static function getInfo() {
  2795. return array(
  2796. 'name' => 'CSS altering',
  2797. 'description' => 'Test CSS alter functions.',
  2798. 'group' => 'Locale',
  2799. );
  2800. }
  2801. function setUp() {
  2802. parent::setUp('locale');
  2803. }
  2804. /**
  2805. * Verifies that -rtl.css file is added directly after LTR .css file.
  2806. */
  2807. function testCSSFilesOrderInRTLMode() {
  2808. global $base_url;
  2809. // User to add and remove language.
  2810. $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
  2811. // Log in as admin.
  2812. $this->drupalLogin($admin_user);
  2813. // Install the Arabic language (which is RTL) and configure as the default.
  2814. $edit = array();
  2815. $edit['langcode'] = 'ar';
  2816. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  2817. $edit = array();
  2818. $edit['site_default'] = 'ar';
  2819. $this->drupalPost(NULL, $edit, t('Save configuration'));
  2820. // Verify that the -rtl.css file is added directly after LTR file.
  2821. $this->drupalGet('');
  2822. $query_string = '?' . variable_get('css_js_query_string', '0');
  2823. $this->assertRaw('@import url("' . $base_url . '/modules/system/system.base.css' . $query_string . '");' . "\n" . '@import url("' . $base_url . '/modules/system/system.base-rtl.css' . $query_string . '");' . "\n", 'CSS: system.base-rtl.css is added directly after system.base.css.');
  2824. $this->assertRaw('@import url("' . $base_url . '/modules/system/system.menus.css' . $query_string . '");' . "\n" . '@import url("' . $base_url . '/modules/system/system.menus-rtl.css' . $query_string . '");' . "\n", 'CSS: system.menus-rtl.css is added directly after system.menus.css.');
  2825. $this->assertRaw('@import url("' . $base_url . '/modules/system/system.messages.css' . $query_string . '");' . "\n" . '@import url("' . $base_url . '/modules/system/system.messages-rtl.css' . $query_string . '");' . "\n", 'CSS: system.messages-rtl.css is added directly after system.messages.css.');
  2826. }
  2827. }
  2828. /**
  2829. * Tests locale translation safe string handling.
  2830. */
  2831. class LocaleStringIsSafeTest extends DrupalWebTestCase {
  2832. public static function getInfo() {
  2833. return array(
  2834. 'name' => 'Test if a string is safe',
  2835. 'description' => 'Tests locale translation safe string handling.',
  2836. 'group' => 'Locale',
  2837. );
  2838. }
  2839. function setUp() {
  2840. parent::setUp('locale');
  2841. }
  2842. /**
  2843. * Tests for locale_string_is_safe().
  2844. */
  2845. public function testLocaleStringIsSafe() {
  2846. // Check a translatable string without HTML.
  2847. $string = 'Hello world!';
  2848. $result = locale_string_is_safe($string);
  2849. $this->assertTrue($result);
  2850. // Check a translatable string which includes trustable HTML.
  2851. $string = 'Hello <strong>world</strong>!';
  2852. $result = locale_string_is_safe($string);
  2853. $this->assertTrue($result);
  2854. // Check an untranslatable string which includes untrustable HTML (according
  2855. // to the locale_string_is_safe() function definition).
  2856. $string = 'Hello <img src="world.png" alt="world" />!';
  2857. $result = locale_string_is_safe($string);
  2858. $this->assertFalse($result);
  2859. // Check a translatable string which includes a token in an href attribute.
  2860. $string = 'Hi <a href="[current-user:url]">user</a>';
  2861. $result = locale_string_is_safe($string);
  2862. $this->assertTrue($result);
  2863. }
  2864. }