From 7c36268531dfd24d5830e22c7e6522335a38425d Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Wed, 9 Dec 2020 17:49:35 +0100
Subject: [PATCH] hotline #121676 Nanook / Koha pre-registration : check birth
 date input field format

---
 VERSIONS_HOTLINE/121676                       |  1 +
 library/ZendAfi/Form/PreRegistration/Koha.php |  5 +-
 .../ZendAfi/Form/PreRegistration/Nanook.php   |  3 +-
 library/ZendAfi/Validate/DateFormat.php       | 14 +++-
 .../AuthControllerPreRegistrationTest.php     | 78 +++++++++++++++++--
 .../scenarios/Templates/TemplatesFormTest.php |  2 +-
 6 files changed, 88 insertions(+), 15 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/121676

diff --git a/VERSIONS_HOTLINE/121676 b/VERSIONS_HOTLINE/121676
new file mode 100644
index 00000000000..8fad1d03115
--- /dev/null
+++ b/VERSIONS_HOTLINE/121676
@@ -0,0 +1 @@
+ - ticket #121676 : Nanook / Koha : formulaire de pré-inscriptions : vérification du format de la date de naissance
\ No newline at end of file
diff --git a/library/ZendAfi/Form/PreRegistration/Koha.php b/library/ZendAfi/Form/PreRegistration/Koha.php
index db9b44ba1b8..40672625453 100644
--- a/library/ZendAfi/Form/PreRegistration/Koha.php
+++ b/library/ZendAfi/Form/PreRegistration/Koha.php
@@ -41,10 +41,9 @@ class ZendAfi_Form_PreRegistration_Koha extends ZendAfi_Form {
                    ['label' => $this->_('Prénom'),
                     'required' => true])
 
-      ->addElement('datePicker',
+      ->addElement('date',
                    'dateofbirth',
-                   ['label' => $this->_('Date de naissance'),
-                    'DateFormat' => 'dd-MM-YYYY'])
+                   ['label' => $this->_('Date de naissance')])
 
       ->addElement('text',
                    'streetnumber',
diff --git a/library/ZendAfi/Form/PreRegistration/Nanook.php b/library/ZendAfi/Form/PreRegistration/Nanook.php
index edeb9bc2133..d8b3f1045d5 100644
--- a/library/ZendAfi/Form/PreRegistration/Nanook.php
+++ b/library/ZendAfi/Form/PreRegistration/Nanook.php
@@ -72,11 +72,10 @@ class ZendAfi_Form_PreRegistration_Nanook extends ZendAfi_Form {
                     'autocomplete' => 'mew-password',
                     'validators' => [new ZendAfi_Validate_PasswordEquals('password')]])
 
-      ->addElement('datePicker',
+      ->addElement('date',
                    'birthDate',
                    ['label' => $this->_('Date de naissance'),
                     'required' => true,
-                    'DateFormat' => 'YYYY-MM-dd',
                     'allowEmpty' => false])
 
       ->addElement('text',
diff --git a/library/ZendAfi/Validate/DateFormat.php b/library/ZendAfi/Validate/DateFormat.php
index 7f97a0efd98..b681e24f1d9 100644
--- a/library/ZendAfi/Validate/DateFormat.php
+++ b/library/ZendAfi/Validate/DateFormat.php
@@ -21,7 +21,11 @@
 
 
 class ZendAfi_Validate_DateFormat extends Zend_Validate_Abstract {
-  protected $_format = 'Y-m-d';
+  const INVALID_DATE = 'invalidDate';
+
+  protected
+    $_messageTemplates = [self::INVALID_DATE   => "'%value%' n'est pas une date au format attendu"],
+    $_format = 'Y-m-d';
 
   public function setFormat($format) {
     $this->_format = $format;
@@ -33,7 +37,13 @@ class ZendAfi_Validate_DateFormat extends Zend_Validate_Abstract {
     if (!$string)
       return false;
 
+    $this->_setValue($string);
+
     $date = DateTime::createFromFormat($this->_format, $string);
-    return $date && ($date->format($this->_format) === $string);
+    if ($date && ($date->format($this->_format) === $string))
+      return true;
+
+    $this->_error(self::INVALID_DATE);
+    return false;
   }
 }
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/AuthControllerPreRegistrationTest.php b/tests/application/modules/opac/controllers/AuthControllerPreRegistrationTest.php
index f5709df3af6..5d102d33d9c 100644
--- a/tests/application/modules/opac/controllers/AuthControllerPreRegistrationTest.php
+++ b/tests/application/modules/opac/controllers/AuthControllerPreRegistrationTest.php
@@ -125,7 +125,7 @@ abstract class AuthControllerPreRegistrationKohaPostDispatchTestCase extends Aut
                         array_merge ($values, ['branchcode' => '41|GRA',
                                                'surname' => 'Boulard',
                                                'firstname' => 'Thom',
-                                               'dateofbirth' => '15-09-1940',
+                                               'dateofbirth' => '1940-09-15',
                                                'streetnumber' => '10',
                                                'address' => 'rue secret',
                                                'zipcode' => '01630',
@@ -166,8 +166,9 @@ class AuthControllerPreRegistrationKohaPostDispatchTest extends AuthControllerPr
 
 
 class AuthControllerPreRegistrationKohaWithRequiredCondtionChecked extends AuthControllerPreRegistrationKohaPostDispatchTestCase {
-  protected $_required_checkbox = true;
-  protected $_required_value = 1;
+  protected
+    $_required_checkbox = true,
+    $_required_value = 1;
 
   /** @test */
   public function responseShouldRedirectToPreRegistrationSuccess() {
@@ -179,8 +180,9 @@ class AuthControllerPreRegistrationKohaWithRequiredCondtionChecked extends AuthC
 
 
 class AuthControllerPreRegistrationKohaWithRequiredCondtionNotChecked extends AuthControllerPreRegistrationKohaPostDispatchTestCase {
-  protected $_required_checkbox = true;
-  protected $_required_value = 0;
+  protected
+    $_required_checkbox = true,
+    $_required_value = 0;
 
 
   /** @test */
@@ -218,6 +220,32 @@ class AuthControllerKohaPreRegistrationSpammedPostDispatchTest extends AuthContr
 
 
 
+class AuthControllerKohaPreRegistrationInvalidBirthDatePostDispatchTest extends AuthControllerPreRegistrationKohaTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $this->postDispatch('auth/pre-registration',
+                        ['branchcode' => '41|GRA',
+                         'surname' => 'Boulard',
+                         'firstname' => 'Thom',
+                         'dateofbirth' => 'tamalou bobola',
+                         'address' => 'rue secret',
+                         'zipcode' => '01630',
+                         'email' => 'thom@mail.com',
+                         'website' => ''],
+                        true);
+  }
+
+
+  /** @test */
+  public function pageShouldDisplayErrorBirthDateInvalid() {
+    $this->assertXPathContentContains('//ul[@class="errors"]//li', '\'tamalou bobola\' n\'est pas une date au format attendu');
+  }
+}
+
+
+
+
 class AuthControllerPreRegistrationKohaPostDispatchWithErrorReturnedTest extends AuthControllerPreRegistrationKohaTestCase {
   public function setUp() {
     parent::setUp();
@@ -239,7 +267,7 @@ class AuthControllerPreRegistrationKohaPostDispatchWithErrorReturnedTest extends
                         ['branchcode' => '41|GRA',
                          'surname' => 'Boulard',
                          'firstname' => 'Thom',
-                         'dateofbirth' => '15/09/1940',
+                         'dateofbirth' => '1940-09-15',
                          'streetnumber' => '10',
                          'address' => 'rue secret',
                          'zipcode' => '01630',
@@ -365,7 +393,13 @@ class AuthControllerPreRegistrationNanookDispatchTest
 
   /** @test */
   public function formShouldContainsEmail() {
-    $this->assertXpath('//form//input[@name="mail"]');
+    $this->assertXpath('//form//input[@name="mail"][@required="required"]');
+  }
+
+
+  /** @test */
+  public function inputForBirthDateShouldBeRequired() {
+    $this->assertXPath('//form//input[@name="birthDate"][@required="required"][@type="date"]');
   }
 
 
@@ -577,6 +611,36 @@ class AuthControllerPreRegistrationNanookPostDispatchSpamTest
 
 
 
+class AuthControllerPreRegistrationNanookInvalidBirthDateTest
+  extends AuthControllerPreRegistrationNanookTestCase {
+
+  public function setUp() {
+    parent::setUp();
+
+    $this->postDispatch('/opac/auth/pre-registration', ['site' => '1',
+                                                        'lastName' => 'Jiro',
+                                                        'firstName' => 'Tom',
+                                                        'mail' => 'test@test.fr',
+                                                        'mail2' => 'test@test.fr',
+                                                        'password' => 'pwd123456',
+                                                        'password2' => 'pwd123456',
+                                                        'birthDate' => 'pouet',
+                                                        'town' => 'titi',
+                                                        'zipcode' => '123456',
+                                                        'address' => '123',
+                                                        'website' => '']);
+  }
+
+
+  /** @test */
+  public function pageShouldDisplayErrorBirthDateInvalid() {
+    $this->assertXPathContentContains('//ul[@class="errors"]//li', '\'pouet\' n\'est pas une date au format attendu');
+  }
+}
+
+
+
+
 class AuthControllerPreRegistrationNanookSuccessDispatchTest extends AuthControllerPreRegistrationNanookTestCase {
   /** @test */
   public function withSiteGivenShouldDisplayLibraryName() {
diff --git a/tests/scenarios/Templates/TemplatesFormTest.php b/tests/scenarios/Templates/TemplatesFormTest.php
index 84ff8bfd71b..6e4f5835b0f 100644
--- a/tests/scenarios/Templates/TemplatesFormTest.php
+++ b/tests/scenarios/Templates/TemplatesFormTest.php
@@ -77,7 +77,7 @@ class TemplatesFormPreRegistrationWithConditionsPostTest extends TemplatesFormPr
                          'branchcode' => '41|GRA',
                          'surname' => 'Boulard',
                          'firstname' => 'Thom',
-                         'dateofbirth' => '15-09-1940',
+                         'dateofbirth' => '1940-09-15',
                          'streetnumber' => '10',
                          'address' => 'rue secret',
                          'zipcode' => '01630',
-- 
GitLab