From 5ed77c74520dbb17c79e3fed2f71df7f41088aed Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Fri, 30 Aug 2013 09:33:59 +0000
Subject: [PATCH] =?UTF-8?q?Contr=C3=B4le=20saisie=20t=C3=A9l=C3=A9phone=20?=
 =?UTF-8?q?fiche=20utilisateur=20rel=20#9286?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitattributes                                |  1 +
 .../opac/controllers/AbonneController.php     |  4 ++
 .../controllers/AbonneControllerEditTest.php  | 20 +++++-
 .../ZendAfi/Validate/PhoneNumberTest.php      | 66 +++++++++++++++++++
 4 files changed, 89 insertions(+), 2 deletions(-)
 create mode 100644 tests/library/ZendAfi/Validate/PhoneNumberTest.php

diff --git a/.gitattributes b/.gitattributes
index e5df418c8d6..5f735402346 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -6116,6 +6116,7 @@ tests/library/ZendAfi/Form/Element/ImageDeleteTest.php -text
 tests/library/ZendAfi/Form/Element/ImageTest.php -text
 tests/library/ZendAfi/TranslateTest.php -text
 tests/library/ZendAfi/Validate/IsbnTest.php -text
+tests/library/ZendAfi/Validate/PhoneNumberTest.php -text
 tests/library/ZendAfi/View/Helper/Abonne/AbonnementTest.php -text
 tests/library/ZendAfi/View/Helper/Abonne/ResumeTest.php -text
 tests/library/ZendAfi/View/Helper/Accueil/BibNumeriqueTest.php -text
diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php
index 8442e305525..c18fc1b0cd7 100644
--- a/application/modules/opac/controllers/AbonneController.php
+++ b/application/modules/opac/controllers/AbonneController.php
@@ -393,6 +393,10 @@ class AbonneController extends ZendAfi_Controller_Action {
 		if ($mail_element = $form->getElement('mail'))
 			$mail_element->addValidator(new ZendAfi_Validate_EmailAddress());
 
+		if ($phone_element = $form->getElement('telephone'))
+			$phone_element->addValidator(new ZendAfi_Validate_PhoneNumber());
+
+
 		if (in_array('password', $fields_to_show)) {
 			$fields_to_show []= 'confirm_password';
 			$new_password = new Zend_Form_Element_Password('password');
diff --git a/tests/application/modules/opac/controllers/AbonneControllerEditTest.php b/tests/application/modules/opac/controllers/AbonneControllerEditTest.php
index 231c906d6c2..18587d65831 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerEditTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerEditTest.php
@@ -98,7 +98,23 @@ class AbonneControllerEditWithContactMailSmsPostTest extends AbonneControllerEdi
 class AbonneControllerEditWithContactMailPostErrorsTest extends AbonneControllerEditActionWithAbonneSIGBTestCase {
 	/** @test */
 	public function pageShouldDisplayErrorNoMailProvided() {
+		Class_AdminVar::newInstanceWithId('CHAMPS_FICHE_UTILISATEUR', 
+																			['valeur' => 'prenom;mode_contact;telephone;mail']);
+
+		$this->postDispatch('/abonne/edit',
+												['telephone' => '12 34 56 78 98',
+												 'mail' => '',
+												 'mode_contact' => 'is_contact_mail',
+												 'prenom' => 'Marcel',
+												 'nom' => 'Mazout']);
 
+		$this->assertXPathContentContains('//ul[@class="errors"]//li', 
+																			'Vous devez fournir une adresse mail valide');
+	}
+
+
+	/** @test */
+	public function pageShouldDisplayErrorInvalidPhone() {
 		Class_AdminVar::newInstanceWithId('CHAMPS_FICHE_UTILISATEUR', 
 																			['valeur' => 'prenom;mode_contact;telephone;mail']);
 
@@ -110,7 +126,7 @@ class AbonneControllerEditWithContactMailPostErrorsTest extends AbonneController
 												 'nom' => 'Mazout']);
 
 		$this->assertXPathContentContains('//ul[@class="errors"]//li', 
-																			'Vous devez fournir une adresse mail valide');
+																			"'12 34 56' n'est pas un numéro de téléphone");
 	}
 
 
@@ -143,7 +159,7 @@ class AbonneControllerEditWithContactMailPostErrorsTest extends AbonneController
 												 'nom' => 'Mazout']);
 
 		$this->assertXPathContentContains('//ul[@class="errors"]//li', 
-																			"'glouglou' n'est pas un courriel valide",
+																			"'glouglou' n'est pas un email valide",
 																			$this->_response->getBody());
 	}
 
diff --git a/tests/library/ZendAfi/Validate/PhoneNumberTest.php b/tests/library/ZendAfi/Validate/PhoneNumberTest.php
new file mode 100644
index 00000000000..12ffb769584
--- /dev/null
+++ b/tests/library/ZendAfi/Validate/PhoneNumberTest.php
@@ -0,0 +1,66 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * AFI-OPAC 2.0 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 AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+class ZendAfi_Validate_PhoneNumberTest extends PHPUnit_Framework_TestCase {
+		public function setUp() {
+		$this->_validator = new ZendAfi_Validate_PhoneNumber();
+	}
+
+
+	public function validPhoneNumbers() {
+		return [
+						['06 12 34 87 98'],
+						['0612348798'],
+						['+33-6-12-34-87-98'],
+						['(33) 6 12 348 798'],
+						['06/12/34/87/98']
+						];
+	}
+
+
+	public function invalidPhoneNumbers() {
+		return [ ['234'], 
+						 ['23-98,34'],
+						 ['123456789012345678901'],
+						 ['23 (98) 06 18 34'],
+						 ['xz'] ];
+	}
+
+
+	/**
+	 * @test
+	 * @dataProvider validPhoneNumbers
+	 */
+	public function phoneShouldBeValid($phone)    {
+		$this->assertTrue($this->_validator->isValid($phone));
+	}
+
+
+	/**
+	 * @test
+	 * @dataProvider invalidPhoneNumbers
+	 */
+	public function phonehouldNotBeValid($phone)    {
+		$this->assertFalse($this->_validator->isValid($phone));
+	}
+}
+
+?>
\ No newline at end of file
-- 
GitLab