From 0174cfe5321086b9b66693dc01d13fdf9152b36c Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Tue, 25 Oct 2022 08:54:23 +0200
Subject: [PATCH 11/13] fields: prefill phone value (#69838)

---
 tests/form_pages/test_all.py | 31 +++++++++++++++++++++++++++++++
 tests/test_prefill.py        | 16 +++++++++++++++-
 wcs/fields.py                |  7 +++++++
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/tests/form_pages/test_all.py b/tests/form_pages/test_all.py
index e4d071793..db9da84cf 100644
--- a/tests/form_pages/test_all.py
+++ b/tests/form_pages/test_all.py
@@ -107,6 +107,7 @@ def create_user(pub):
     user = pub.user_class()
     user.name = 'User Name'
     user.email = 'foo@localhost'
+    user.phone = '+33123456789'
     user.store()
     account = PasswordAccount(id='foo')
     account.set_password('foo')
@@ -2064,6 +2065,36 @@ def test_form_tracking_code_as_user(pub, nocache):
     resp = app.get('/code/%s/load' % tracking_code, headers={'User-agent': 'Googlebot'}, status=403)
 
 
+def test_form_phone_prefill(pub, nocache):
+    create_user(pub)
+    formdef = create_formdef()
+    formdef.data_class().wipe()
+    formdef.fields = [fields.StringField(id='0', label='string', prefill={'type': 'user', 'value': 'phone'})]
+    formdef.store()
+
+    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
+    assert resp.pyquery('input#form_f0').val() == '+33123456789'
+    resp.forms[0]['f0'] = '+33987654321'
+    resp = resp.forms[0].submit('submit')
+    assert resp.pyquery('button.form-submit').val() == 'Submit'
+
+
+def test_form_phone_prefill_phone_fr_validation(pub, nocache):
+    create_user(pub)
+    formdef = create_formdef()
+    formdef.data_class().wipe()
+    field = fields.StringField(id='0', label='string', prefill={'type': 'user', 'value': 'phone'})
+    field.validation = {'type': 'phone-fr'}
+    formdef.fields = [field]
+    formdef.store()
+
+    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
+    assert resp.pyquery('input#form_f0').val() == '01 23 45 67 89'
+    resp.forms[0]['f0'] = '0987654321'
+    resp = resp.forms[0].submit('submit')
+    assert resp.pyquery('button.form-submit').val() == 'Submit'
+
+
 def test_form_tracking_code_prefill(pub, nocache):
     create_user(pub)
     formdef = create_formdef()
diff --git a/tests/test_prefill.py b/tests/test_prefill.py
index d7cbc3322..b895dd7ee 100644
--- a/tests/test_prefill.py
+++ b/tests/test_prefill.py
@@ -27,6 +27,7 @@ def user(request):
     pub.user_class.wipe()
     user = pub.user_class(name='user')
     user.email = 'test@example.net'
+    user.phone = '+33123456789'
     user.store()
     get_request()._user = user
     return user
@@ -66,12 +67,25 @@ def test_prefill_string_carddef():
     assert field.get_prefill_value() == ('{{cards|objects:"unknown"|first|get:"foo"}}', False)
 
 
-def test_prefill_user(user):
+def test_prefill_user_email(user):
     field = fields.Field()
     field.prefill = {'type': 'user', 'value': 'email'}
     assert field.get_prefill_value(user=get_request().user) == ('test@example.net', False)
 
 
+def test_prefill_user_phone(user):
+    field = fields.Field()
+    field.prefill = {'type': 'user', 'value': 'phone'}
+    assert field.get_prefill_value(user=get_request().user) == ('+33123456789', False)
+
+
+def test_prefill_user_phone_fr(user):
+    field = fields.Field()
+    field.validation = {'type': 'phone-fr'}
+    field.prefill = {'type': 'user', 'value': 'phone'}
+    assert field.get_prefill_value(user=get_request().user) == ('01 23 45 67 89', False)
+
+
 def test_prefill_user_attribute(user):
     from wcs.admin.settings import UserFieldsFormDef
 
diff --git a/wcs/fields.py b/wcs/fields.py
index bc9d636a8..0fc43d60e 100644
--- a/wcs/fields.py
+++ b/wcs/fields.py
@@ -85,6 +85,7 @@ from .qommon.misc import (
     get_document_types,
     strftime,
     strip_some_tags,
+    try_local_number,
     xml_node_text,
 )
 from .qommon.ods import NS as OD_NS
@@ -539,6 +540,12 @@ class Field:
             x = self.prefill.get('value')
             if x == 'email':
                 return (user.email, explicit_lock or 'email' in (user.verified_fields or []))
+            elif x == 'phone':
+                if hasattr(self, 'validation') and self.validation.get('type', None) == 'phone-fr':
+                    phone = try_local_number(user.phone)
+                else:
+                    phone = user.phone
+                return (phone, explicit_lock or 'phone' in (user.verified_fields or []))
             elif user.form_data:
                 userform = user.get_formdef()
                 for userfield in userform.fields:
-- 
2.37.2

