test.erp5.testContentTranslation.py 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2009 Nexedi KK, Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
import unittest
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase


class TestContentTranslation(ERP5TypeTestCase):
  """
  Test Content Translation
  """

  def getTitle(self):
    return 'Test Content Translation'

  def getBusinessTemplateList(self):

42 43
    return ('erp5_full_text_mroonga_catalog',
            'erp5_base',
44 45 46
            'erp5_content_translation',
            )

47
  def setUpOnce(self):
48 49
    # set up translation domain on Person type information.
    from Products.ERP5Type.Accessor.Translation import TRANSLATION_DOMAIN_CONTENT_TRANSLATION
50 51 52
    setTranslationDomain = self.portal.portal_types.Person.setTranslationDomain
    setTranslationDomain('first_name', TRANSLATION_DOMAIN_CONTENT_TRANSLATION)
    setTranslationDomain('last_name', TRANSLATION_DOMAIN_CONTENT_TRANSLATION)
53 54
    setTranslationDomain('title', TRANSLATION_DOMAIN_CONTENT_TRANSLATION)
    self.portal.portal_caches.manage_clearAllCache()
55

Yusuke Muraoka's avatar
Yusuke Muraoka committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  def testCatalogSearch(self):
    """
    Search a person's properties and translated properties with catalog.
    """
    portal = self.portal

    portal.Localizer._add_user_defined_language('Nobody Readable', 'nob-read')
    portal.Localizer.add_language('nob-read')
    self.tic()

    person1 = portal.person_module.newContent(portal_type='Person',
                                              first_name='first_name_of_p1',
                                              last_name='last_name_of_p1')
    person2 = portal.person_module.newContent(portal_type='Person',
                                              first_name='first_name_of_p2',
                                              last_name='last_name_of_p2')
    person3 = portal.person_module.newContent(portal_type='Person',
                                              first_name='Yusuke',
                                              last_name='Muraoka')
    person1.setNobReadTranslatedFirstName('XXX')
    person1.setNobReadTranslatedLastName('YYY')
    person2.setNobReadTranslatedFirstName('---')
    person2.setNobReadTranslatedLastName('   ')
    person3.setNobReadTranslatedFirstName('友介')
    person3.setNobReadTranslatedLastName('村岡')
    self.assertEqual(person1.getNobReadTranslatedFirstName('XXX'), 'XXX')
    self.assertEqual(person1.getNobReadTranslatedLastName('YYY'), 'YYY')
    self.assertEqual(person2.getNobReadTranslatedFirstName('---'), '---')
    self.assertEqual(person2.getNobReadTranslatedLastName('   '), '   ')
    self.assertEqual(person3.getNobReadTranslatedFirstName('友介'), '友介')
    self.assertEqual(person3.getNobReadTranslatedLastName('村岡'), '村岡')
    self.tic()

    result1 = portal.portal_catalog(content_translation_title='Yusuke')
90
    self.assertEqual(len(result1), 1)
Yusuke Muraoka's avatar
Yusuke Muraoka committed
91 92 93
    result_obj1 = result1[0].getObject()

    result2 = portal.portal_catalog(content_translation_title='友介')
94
    self.assertEqual(len(result2), 1)
Yusuke Muraoka's avatar
Yusuke Muraoka committed
95 96
    result_obj2 = result2[0].getObject()

97
    self.assertEqual(result_obj1, result_obj2)
Yusuke Muraoka's avatar
Yusuke Muraoka committed
98 99 100 101 102 103

    # re-catalog
    person3.setNobReadTranslatedFirstName('ゆうすけ')
    self.tic()

    result3 = portal.portal_catalog(content_translation_title='友介')
104
    self.assertEqual(len(result3), 0)
Yusuke Muraoka's avatar
Yusuke Muraoka committed
105 106 107 108 109 110

    # un-catalog
    portal.person_module.manage_delObjects(person3.getId())
    self.tic()

    result4 = portal.portal_catalog(content_translation_title='村岡')
111
    self.assertEqual(len(result4), 0)
112

113 114
    # Low level columns test. This behaviour is not guaranteed. I'm not sure
    # content_translation must be a search table - jerome
115
    result5 = portal.portal_catalog(property_name='title')
116
    self.assertGreaterEqual(len(result5), 2)
117
    result6 = portal.portal_catalog(content_language='nob-read')
118
    self.assertEqual(len(result6), 2)
119
    result7 = portal.portal_catalog(translated_text='XXX YYY')
120
    self.assertEqual(len(result7), 1)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

  def testCatalogSearchTranslatedTitleScriptableKey(self):
    # Test 'translated_title' scriptable key.
    portal = self.portal

    portal.Localizer._add_user_defined_language('Nobody Readable', 'nob-read')
    portal.Localizer.add_language('nob-read')
    self.tic()

    person = portal.person_module.newContent(portal_type='Person',
                                             first_name='Yusuke',
                                             last_name='Muraoka')
    person.setNobReadTranslatedFirstName('友介')
    person.setNobReadTranslatedLastName('村岡')
    self.tic()

    # We can search either by the translated title
138
    self.assertEqual(person,
139 140
      portal.portal_catalog.getResultValue(translated_title='友介'))
    # Or the original title
141
    self.assertEqual(person,
142 143
      portal.portal_catalog.getResultValue(translated_title='Yusuke'))

144 145 146 147 148 149 150 151 152
    # Deleting translation should update content_translation table.
    person.setNobReadTranslatedFirstName('')
    person.setNobReadTranslatedLastName('')
    self.tic()
    self.assertEqual(None,
      portal.portal_catalog.getResultValue(translated_title='友介'))
    self.assertEqual(person,
      portal.portal_catalog.getResultValue(translated_title='Yusuke'))

153 154 155 156 157 158
    # documents for which translation is not set can also be found with
    # translated_title key
    not_translated_person = portal.person_module.newContent(portal_type='Person',
                                             first_name='Jérome',
                                             last_name='Perrin')
    self.tic()
159
    self.assertEqual(not_translated_person,
160 161 162
      portal.portal_catalog.getResultValue(translated_title='Jérome'))


163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  def testContentTranslation(self):
    """
    Make sure that translatable properties can have content translation into
    the document and read/write translation text by special accessors.
    """
    portal = self.portal
    person = portal.person_module.newContent(id='yusei',
                                             portal_type='Person',
                                             first_name='Yusei',
                                             last_name='Tahara')
    portal.person_module.newContent(id='tarou',
                                    portal_type='Person',
                                    first_name='Tarou',
                                    last_name='Suzuki')
    portal.person_module.newContent(id='john',
                                    portal_type='Person',
                                    first_name='John',
                                    last_name='Smith')
    self.tic()

    self.assertEqual(getattr(person, 'setJaKanaTranslatedFirstName', False),
                     False)
    self.assertEqual(getattr(person, 'getJaKanaTranslatedFirstName', False),
                     False)
    self.assert_(getattr(person, 'getEnTranslatedFirstName', False))
    self.assert_(getattr(person, 'getEnTranslatedFirstName', False))

190 191 192
    ##
    # Add custom local.language
    ##
193 194 195 196 197 198
    portal.Localizer._add_user_defined_language('Japanese Kana', 'ja-kana')
    portal.Localizer.add_language('ja-kana')
    self.tic()

    self.assert_(getattr(person, 'setJaKanaTranslatedFirstName', False))
    self.assert_(getattr(person, 'getJaKanaTranslatedFirstName', False))
199 200 201
    self.assert_(getattr(person, 'hasJaKanaTranslatedFirstName', False))

    self.assert_(not person.hasJaKanaTranslatedFirstName())
202

203
    # if there is no translation, original value is returned.
204
    self.assertEqual(None, getattr(person, '__translation_dict', None))
205 206 207 208 209 210 211
    self.assertEqual('Yusei', person.getTranslatedFirstName())
    self.assertEqual('Yusei Tahara', person.getTranslatedTitle())
    self.assertEqual('Yusei', person.getJaKanaTranslatedFirstName())
    # if no_original_value parameter is true, an empty string is returned.
    self.assertEqual('', person.getTranslatedFirstName(no_original_value=True))
    self.assertEqual('', person.getTranslatedTitle(no_original_value=True))
    self.assertEqual('', person.getJaKanaTranslatedFirstName(no_original_value=True))
212 213
    # Make sure that until any value is set, we do not create useless __translation_dict
    self.assertEqual(None, getattr(person, '__translation_dict', None))
214

215 216
    person.setJaKanaTranslatedFirstName('タハラ')
    person.setJaKanaTranslatedLastName('ユウセイ')
217 218 219 220 221
    # Since we add some translated value, we now must have some data in __translation_dict
    translation_dict = getattr(person, '__translation_dict', None)
    self.assertNotEqual(None, translation_dict)
    self.assertEqual(set([('last_name', 'ja-kana'), ('first_name', 'ja-kana')]),
                     set(translation_dict.keys()))
222

223 224
    self.assert_(person.hasJaKanaTranslatedFirstName())

225 226
    self.tic()

227 228 229
    x = person.Base_viewContentTranslation().encode('utf-8')
    self.assertTrue('タハラ' in x)
    self.assertTrue('ユウセイ' in x)
230 231 232 233 234 235 236

    self.assertEqual(person.getJaKanaTranslatedFirstName(), 'タハラ')
    self.assertEqual(person.getJaKanaTranslatedLastName(), 'ユウセイ')
    self.assertEqual(person.getTranslatedTitle(language='ja-kana'),
                     'タハラ ユウセイ')

    # check with acquisition
237
    self.assertEqual(person.getAddress(), None)
238

239
    person.setDefaultAddressStreetAddress('Taito-ku Tokyo')
240
    self.assertEqual(person.getDefaultAddressStreetAddress(), 'Taito-ku Tokyo')
241 242

    person.setDefaultAddressJaKanaTranslatedStreetAddress('東京都 台東区')
243 244
    self.assertEqual(person.getDefaultAddressJaKanaTranslatedStreetAddress(), '東京都 台東区')
    self.assertEqual(person.getDefaultAddressTranslatedStreetAddress(language='ja-kana'), '東京都 台東区')
245 246 247

    # check acquired target
    address = person.getDefaultAddress()
248 249 250
    self.assertEqual(address.getStreetAddress(), 'Taito-ku Tokyo')
    self.assertEqual(address.getJaKanaTranslatedStreetAddress(), '東京都 台東区')
    self.assertEqual(address.getTranslatedStreetAddress(language='ja-kana'), '東京都 台東区')
251

252

253 254 255 256 257 258
  def test_getInstancePropertySet(self):
    """Translatable properies are returned by getInstancePropertySet
    """
    self.assertTrue('en_translated_first_name' in
      self.portal.portal_types.Person.getInstancePropertySet())

259 260 261 262
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestContentTranslation))
  return suite