Skip to content

Commit

Permalink
restore translation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lenadax committed Nov 8, 2024
1 parent 045cc8c commit 631ae06
Showing 1 changed file with 349 additions and 0 deletions.
349 changes: 349 additions & 0 deletions src/cone/app/tests/test_browser_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,349 @@
from cone.app import testing
from cone.app.model import BaseNode
from cone.app.model import Translation
from node.utils import UNSET
from plumber import plumbing
from yafowil.base import factory
from yafowil.tests import fxml
from yafowil.tests import NodeTestCase


@plumbing(Translation)
class TranslationNode(BaseNode):
child_constraints = None


class TestBrowserTranslation(NodeTestCase):
layer = testing.security

def test_translation_extractor(self):
widget = factory(
'translation:text',
name='field',
props={
'label': 'Field'
})

request = self.layer.new_request()
data = widget.extract(request)
self.assertEqual(data.extracted, UNSET)

request.params['field.en'] = u'Value EN'
request.params['field.de'] = u'Value DE'
data = widget.extract(request)
self.assertIsInstance(data.extracted, dict)
self.assertEqual(data.extracted, {
'en': u'Value EN',
'de': u'Value DE'
})

def test_translation_extractor_factory(self):
widget = factory(
'translation:text',
name='field',
props={
'label': 'Field',
'factory': TranslationNode
})

request = self.layer.new_request()
data = widget.extract(request)
self.assertEqual(data.extracted, UNSET)

request.params['field.en'] = u'Value EN'
request.params['field.de'] = u'Value DE'
data = widget.extract(request)
self.assertIsInstance(data.extracted, TranslationNode)
self.assertEqual(data.extracted.items(), [
('en', u'Value EN'),
('de', u'Value DE')
])

def test_extraction_preset_value(self):
widget = factory(
'translation:text',
name='field',
value={
'en': u'Value EN',
'de': u'Value DE'
},
props={
'label': 'Field'
})

request = self.layer.new_request()
data = widget.extract(request)
self.assertEqual(data.extracted, UNSET)

request.params['field.en'] = u'Value EN new'
request.params['field.de'] = u'Value DE new'
data = widget.extract(request)
self.assertEqual(data.extracted, {
'en': u'Value EN new',
'de': u'Value DE new'
})

def test_extraction_preset_value_factory(self):
value = TranslationNode()
value['en'] = u'Value EN'
value['de'] = u'Value DE'
widget = factory(
'translation:text',
name='field',
value=value,
props={
'label': 'Field',
'factory': TranslationNode
})

request = self.layer.new_request()
data = widget.extract(request)
self.assertEqual(data.extracted, UNSET)

request.params['field.en'] = u'Value EN new'
request.params['field.de'] = u'Value DE new'
data = widget.extract(request)
self.assertIsInstance(data.extracted, TranslationNode)
self.assertEqual(data.extracted.items(), [
('en', u'Value EN new'),
('de', u'Value DE new')
])

def test_translation_edit_renderer_no_value(self):
widget = factory(
'field:translation:text',
'field',
props={
'label': 'Field'
})

self.checkOutput("""
<div class="field mb-3" id="field-field">
<ul class="nav nav-pills mb-2 translation-nav">
<li class="nav-item">
<a class="nav-link py-0 active" href="#translation-field-en">EN</a>
</li>
<li class="nav-item">
<a class="nav-link py-0" href="#translation-field-de">DE</a>
</li>
</ul>
<div class="translation-fields">
<div id="translation-field-en">
<input class="form-control" id="input-field-en" name="field.en"
type="text" value=""/>
</div>
<div id="translation-field-de">
<input class="form-control" id="input-field-de" name="field.de"
type="text" value=""/>
</div>
</div>
</div>
""", fxml(widget()))

def test_translation_edit_renderer_preset_value(self):
widget = factory(
'field:translation:text',
'field',
value={
'en': u'Value EN',
'de': u'Value DE'
},
props={
'label': 'Field'
})

self.checkOutput("""
...
<div id="translation-field-en">
<input class="form-control" id="input-field-en" name="field.en"
type="text" value="Value EN"/>
</div>
<div id="translation-field-de">
<input class="form-control" id="input-field-de" name="field.de"
type="text" value="Value DE"/>
</div>
...
""", fxml(widget()))

def test_translation_edit_renderer_preset_translation_value(self):
value = TranslationNode()
value['en'] = u'Value EN'
value['de'] = u'Value DE'
widget = factory(
'field:translation:text',
'field',
value=value,
props={
'label': 'Field',
'factory': TranslationNode
})

self.checkOutput("""
...
<div id="translation-field-en">
<input class="form-control" id="input-field-en" name="field.en"
type="text" value="Value EN"/>
</div>
<div id="translation-field-de">
<input class="form-control" id="input-field-de" name="field.de"
type="text" value="Value DE"/>
</div>
...
""", fxml(widget()))

def test_render_after_extraction_no_preset_value(self):
widget = factory(
'field:translation:text',
'field',
props={
'label': 'Field'
})

request = self.layer.new_request()
request.params['field.en'] = u'Value EN'
request.params['field.de'] = u'Value DE'
data = widget.extract(request)
self.checkOutput("""
...
<div id="translation-field-en">
<input class="form-control is-valid" id="input-field-en" name="field.en"
type="text" value="Value EN"/>
</div>
<div id="translation-field-de">
<input class="form-control is-valid" id="input-field-de" name="field.de"
type="text" value="Value DE"/>
</div>
...
""", fxml(widget(data=data)))

def test_render_after_extraction_preset_value(self):
widget = factory(
'field:translation:text',
'field',
value={
'en': u'Value EN',
'de': u'Value DE'
},
props={
'label': 'Field'
})

request = self.layer.new_request()
request.params['field.en'] = u'Value EN new'
request.params['field.de'] = u'Value DE new'
data = widget.extract(request)
self.checkOutput("""
...
<div id="translation-field-en">
<input class="form-control is-valid" id="input-field-en" name="field.en"
type="text" value="Value EN new"/>
</div>
<div id="translation-field-de">
<input class="form-control is-valid" id="input-field-de" name="field.de"
type="text" value="Value DE new"/>
</div>
...
""", fxml(widget(data=data)))

def test_render_after_extraction_preset_translation_value(self):
value = TranslationNode()
value['en'] = u'Value EN'
value['de'] = u'Value DE'
widget = factory(
'field:translation:text',
'field',
value=value,
props={
'label': 'Field',
'factory': TranslationNode
})

request = self.layer.new_request()
request.params['field.en'] = u'Value EN new'
request.params['field.de'] = u'Value DE new'
data = widget.extract(request)
self.checkOutput("""
...
<div id="translation-field-en">
<input class="form-control is-valid" id="input-field-en" name="field.en"
type="text" value="Value EN new"/>
</div>
<div id="translation-field-de">
<input class="form-control is-valid" id="input-field-de" name="field.de"
type="text" value="Value DE new"/>
</div>
...
""", fxml(widget(data=data)))

def test_required(self):
widget = factory(
'field:error:translation:text',
'field',
value={
'en': u'Value EN',
'de': u'Value DE'
},
props={
'label': 'Field',
'required': 'Field is mandatory'
})

request = self.layer.new_request()
request.params['field.en'] = u''
request.params['field.de'] = u'Value DE'
data = widget.extract(request)
self.checkOutput("""
<div class="field mb-3" id="field-field">
<ul class="nav nav-pills mb-2 translation-nav">
<li class="nav-item error">
<a class="nav-link py-0 active" href="#translation-field-en">* EN</a>
</li>
<li class="nav-item">
<a class="nav-link py-0" href="#translation-field-de">DE</a>
</li>
</ul>
<div class="translation-fields">
<div id="translation-field-en">
<input class="form-control is-invalid required" id="input-field-en" name="field.en" required="required" type="text" value=""/>
</div>
<div id="translation-field-de">
<input class="form-control required" id="input-field-de" name="field.de" required="required" type="text" value="Value DE"/>
</div>
</div>
<div class="invalid-feedback">Field is mandatory</div>
</div>
""", fxml(widget(data=data)))

def test_translation_display_renderer(self):
widget = factory(
'field:translation:text',
'field',
value={
'en': u'Value EN',
'de': u'Value DE'
},
props={
'label': 'Field'
},
mode='display')

self.checkOutput("""
<div class="field mb-3" id="field-field">
<ul class="nav nav-pills mb-2 translation-nav">
<li class="nav-item">
<a class="nav-link py-0 active" href="#translation-field-en">EN</a>
</li>
<li class="nav-item">
<a class="nav-link py-0" href="#translation-field-de">DE</a>
</li>
</ul>
<div class="translation-fields">
<div id="translation-field-en">
<div class="display-form-control" id="display-field-en">Value EN</div>
</div>
<div id="translation-field-de">
<div class="display-form-control" id="display-field-de">Value DE</div>
</div>
</div>
</div>
""", fxml(widget()))

0 comments on commit 631ae06

Please sign in to comment.