/ckanext/multilingual/tests/test_multilingual_plugin.py

https://gitlab.com/iislod/ckan · Python · 216 lines · 187 code · 20 blank · 9 comment · 19 complexity · f81000e60a697fb48597bfd7ddcd5121 MD5 · raw file

  1. import ckan.plugins
  2. import ckanext.multilingual.plugin as mulilingual_plugin
  3. import ckan.lib.helpers
  4. import ckan.lib.create_test_data
  5. import ckan.logic.action.update
  6. import ckan.model as model
  7. import ckan.tests.legacy
  8. import ckan.tests.legacy.html_check
  9. import routes
  10. import paste.fixture
  11. import pylons.test
  12. _create_test_data = ckan.lib.create_test_data
  13. class TestDatasetTermTranslation(ckan.tests.legacy.html_check.HtmlCheckMethods):
  14. 'Test the translation of datasets by the multilingual_dataset plugin.'
  15. @classmethod
  16. def setup(cls):
  17. cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
  18. ckan.plugins.load('multilingual_dataset')
  19. ckan.plugins.load('multilingual_group')
  20. ckan.plugins.load('multilingual_tag')
  21. ckan.tests.legacy.setup_test_search_index()
  22. _create_test_data.CreateTestData.create_translations_test_data()
  23. cls.sysadmin_user = model.User.get('testsysadmin')
  24. cls.org = {'name': 'test_org',
  25. 'title': 'russian',
  26. 'description': 'Roger likes these books.'}
  27. ckan.tests.legacy.call_action_api(cls.app, 'organization_create',
  28. apikey=cls.sysadmin_user.apikey,
  29. **cls.org)
  30. dataset = {'name': 'test_org_dataset',
  31. 'title': 'A Novel By Tolstoy',
  32. 'owner_org': cls.org['name']}
  33. ckan.tests.legacy.call_action_api(cls.app, 'package_create',
  34. apikey=cls.sysadmin_user.apikey,
  35. **dataset)
  36. # Add translation terms that match a couple of group names and package
  37. # names. Group names and package names should _not_ get translated even
  38. # if there are terms matching them, because they are used to form URLs.
  39. for term in ('roger', 'david', 'annakarenina', 'warandpeace'):
  40. for lang_code in ('en', 'de', 'fr'):
  41. data_dict = {'term': term,
  42. 'term_translation': 'this should not be rendered',
  43. 'lang_code': lang_code}
  44. context = {'model': ckan.model,
  45. 'session': ckan.model.Session,
  46. 'user': 'testsysadmin'}
  47. ckan.logic.action.update.term_translation_update(
  48. context, data_dict)
  49. @classmethod
  50. def teardown(cls):
  51. ckan.plugins.unload('multilingual_dataset')
  52. ckan.plugins.unload('multilingual_group')
  53. ckan.plugins.unload('multilingual_tag')
  54. ckan.model.repo.rebuild_db()
  55. ckan.lib.search.clear_all()
  56. def test_user_read_translation(self):
  57. '''Test the translation of datasets on user view pages by the
  58. multilingual_dataset plugin.
  59. '''
  60. # It is testsysadmin who created the dataset, so testsysadmin whom
  61. # we'd expect to see the datasets for.
  62. for user_name in ('testsysadmin',):
  63. offset = routes.url_for(
  64. controller='user', action='read', id=user_name)
  65. for (lang_code, translations) in (
  66. ('de', _create_test_data.german_translations),
  67. ('fr', _create_test_data.french_translations),
  68. ('en', _create_test_data.english_translations),
  69. ('pl', {})):
  70. response = self.app.get(
  71. offset,
  72. status=200,
  73. extra_environ={'CKAN_LANG': lang_code,
  74. 'CKAN_CURRENT_URL': offset})
  75. terms = ('A Novel By Tolstoy')
  76. for term in terms:
  77. if term in translations:
  78. assert translations[term] in response, response
  79. elif term in _create_test_data.english_translations:
  80. assert (_create_test_data.english_translations[term]
  81. in response)
  82. else:
  83. assert term in response
  84. assert 'this should not be rendered' not in response
  85. def test_org_read_translation(self):
  86. for (lang_code, translations) in (
  87. ('de', _create_test_data.german_translations),
  88. ('fr', _create_test_data.french_translations),
  89. ('en', _create_test_data.english_translations),
  90. ('pl', {})):
  91. offset = '/{0}/organization/{1}'.format(
  92. lang_code, self.org['name'])
  93. response = self.app.get(offset, status=200)
  94. terms = ('A Novel By Tolstoy',
  95. 'russian',
  96. 'Roger likes these books.')
  97. for term in terms:
  98. if term in translations:
  99. assert translations[term] in response
  100. elif term in _create_test_data.english_translations:
  101. assert (_create_test_data.english_translations[term]
  102. in response)
  103. else:
  104. assert term in response
  105. assert 'this should not be rendered' not in response
  106. def test_org_index_translation(self):
  107. for (lang_code, translations) in (
  108. ('de', _create_test_data.german_translations),
  109. ('fr', _create_test_data.french_translations),
  110. ('en', _create_test_data.english_translations),
  111. ('pl', {})):
  112. offset = '/{0}/organization'.format(lang_code)
  113. response = self.app.get(offset, status=200)
  114. for term in ('russian', 'Roger likes these books.'):
  115. if term in translations:
  116. assert translations[term] in response
  117. elif term in _create_test_data.english_translations:
  118. assert (_create_test_data.english_translations[term]
  119. in response)
  120. else:
  121. assert term in response, response
  122. assert ('/{0}/organization/{1}'.format(lang_code, self.org['name'])
  123. in response)
  124. assert 'this should not be rendered' not in response
  125. class TestDatasetSearchIndex():
  126. @classmethod
  127. def setup_class(cls):
  128. ckan.plugins.load('multilingual_dataset')
  129. ckan.plugins.load('multilingual_group')
  130. data_dicts = [
  131. {'term': 'moo',
  132. 'term_translation': 'french_moo',
  133. 'lang_code': 'fr'},
  134. {'term': 'moo',
  135. 'term_translation': 'this should not be rendered',
  136. 'lang_code': 'fsdas'},
  137. {'term': 'an interesting note',
  138. 'term_translation': 'french note',
  139. 'lang_code': 'fr'},
  140. {'term': 'moon',
  141. 'term_translation': 'french moon',
  142. 'lang_code': 'fr'},
  143. {'term': 'boon',
  144. 'term_translation': 'french boon',
  145. 'lang_code': 'fr'},
  146. {'term': 'boon',
  147. 'term_translation': 'italian boon',
  148. 'lang_code': 'it'},
  149. {'term': 'david',
  150. 'term_translation': 'french david',
  151. 'lang_code': 'fr'},
  152. {'term': 'david',
  153. 'term_translation': 'italian david',
  154. 'lang_code': 'it'}
  155. ]
  156. context = {
  157. 'model': ckan.model,
  158. 'session': ckan.model.Session,
  159. 'user': 'testsysadmin',
  160. 'ignore_auth': True,
  161. }
  162. for data_dict in data_dicts:
  163. ckan.logic.action.update.term_translation_update(
  164. context, data_dict)
  165. @classmethod
  166. def teardown(cls):
  167. ckan.plugins.unload('multilingual_dataset')
  168. ckan.plugins.unload('multilingual_group')
  169. def test_translate_terms(self):
  170. sample_index_data = {
  171. 'download_url': u'moo',
  172. 'notes': u'an interesting note',
  173. 'tags': [u'moon', 'boon'],
  174. 'title': u'david',
  175. }
  176. result = mulilingual_plugin.MultilingualDataset().before_index(
  177. sample_index_data)
  178. assert result == {
  179. 'text_pl': '',
  180. 'text_de': '',
  181. 'text_ro': '',
  182. 'title': u'david',
  183. 'notes': u'an interesting note',
  184. 'tags': [u'moon', 'boon'],
  185. 'title_en': u'david',
  186. 'download_url': u'moo',
  187. 'text_it': u'italian boon',
  188. 'text_es': '',
  189. 'text_en': u'an interesting note moon boon moo',
  190. 'text_nl': '',
  191. 'title_it': u'italian david',
  192. 'text_pt': '',
  193. 'title_fr': u'french david',
  194. 'text_fr': u'french note french boon french_moo french moon'
  195. }, result