From 0fe50c80700cc568efbd1dd5786a81c4b8daeb57 Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Mon, 2 Jun 2014 11:08:56 +0200
Subject: [PATCH] dev #13060 add validators for same mail and password.

---
 .../opac/controllers/AuthController.php       |  6 ++-
 library/Class/Users.php                       |  7 ---
 library/ZendAfi/Validate/Equals.php           | 47 +++++++++++++++++
 library/ZendAfi/Validate/MailEquals.php       | 28 +++++++++++
 library/ZendAfi/Validate/PasswordEquals.php   | 28 +++++++++++
 .../opac/controllers/AuthControllerTest.php   | 50 +++++++++++++++----
 tests/library/Class/UsersTest.php             |  5 --
 7 files changed, 148 insertions(+), 23 deletions(-)
 create mode 100644 library/ZendAfi/Validate/Equals.php
 create mode 100644 library/ZendAfi/Validate/MailEquals.php
 create mode 100644 library/ZendAfi/Validate/PasswordEquals.php

diff --git a/application/modules/opac/controllers/AuthController.php b/application/modules/opac/controllers/AuthController.php
index 37c56063cae..3e459e7f183 100644
--- a/application/modules/opac/controllers/AuthController.php
+++ b/application/modules/opac/controllers/AuthController.php
@@ -183,7 +183,8 @@ class AuthController extends ZendAfi_Controller_Action {
 
 			->addElement('password', 'mdp2', ['label' => $this->view->_('Confirmez votre mot de passe'),
 																				'maxlength' => 15,
-																				'required' => true])
+																				'required' => true,
+																				'validators' => [new ZendAfi_Validate_PasswordEquals('mdp')]])
 
 			->addElement('email', 'mail', ['label' => $this->view->_('E-mail'),
 																		 'maxlength' => 50,
@@ -191,7 +192,8 @@ class AuthController extends ZendAfi_Controller_Action {
 
 			->addElement('email', 'mail2', ['label' => $this->view->_('Confirmez votre e-mail'),
 																			'maxlength' => 50,
-																			'required' => true])
+																			'required' => true,
+																			'validators' => [new ZendAfi_Validate_MailEquals('mail')]])
 
 			->addDisplayGroup(['login', 'mdp', 'mdp2', 'mail', 'mail2'], 
 												'fields',
diff --git a/library/Class/Users.php b/library/Class/Users.php
index dd2200a2c02..334636cbaf8 100644
--- a/library/Class/Users.php
+++ b/library/Class/Users.php
@@ -841,13 +841,6 @@ class Class_Users extends Storm_Model_Abstract {
 		$errors = array();
 		if($this->ifLoginExist($login))
 			$errors []= $this->_("Cet identifiant existe déjà.");
-		elseif(trim($login) =="")
-			$errors []= $this->_("Vous n'avez pas saisi de login.");
-
-		if($mdp != $mdp2)
-			$errors []= $this->_("Vous n'avez pas saisi les mêmes mots de passe.");
-		elseif(trim($mdp) =="")
-			$errors []= $this->_("Le mot de passe est obligatoire.");
 
 		if($test_mail == 0)
 			$errors []= $this->_("L'adresse e-mail est invalide ou est déjà utilisée.");
diff --git a/library/ZendAfi/Validate/Equals.php b/library/ZendAfi/Validate/Equals.php
new file mode 100644
index 00000000000..9966798cedb
--- /dev/null
+++ b/library/ZendAfi/Validate/Equals.php
@@ -0,0 +1,47 @@
+<?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 
+ */
+
+abstract class ZendAfi_Validate_Equals extends Zend_Validate_Abstract {
+	const DIFFERENT_VALUES_ERROR = 'differentValuesError';
+	
+	/** @var string */
+	protected $_field_to_compare;
+
+	/**
+	 * @param $field_to_compare string
+	 */
+	public function __construct($field_to_compare) {
+		$this->_field_to_compare = $field_to_compare;
+	}
+
+	
+	/**
+	 * @param $value mixed
+	 * @param $fields array
+	 * @return boolean
+	 */
+	public function isValid($value, array $fields_values = array()) {
+		if (!$valid = ($value == $fields_values[$this->_field_to_compare]))
+			$this->_error(self::DIFFERENT_VALUES_ERROR);
+		return $valid;
+	}
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/Validate/MailEquals.php b/library/ZendAfi/Validate/MailEquals.php
new file mode 100644
index 00000000000..4681e2ce0d7
--- /dev/null
+++ b/library/ZendAfi/Validate/MailEquals.php
@@ -0,0 +1,28 @@
+<?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_MailEquals extends ZendAfi_Validate_Equals {
+	protected $_messageTemplates = [
+		self::DIFFERENT_VALUES_ERROR   => "Les champs 'E-mail' sont différents"
+		];
+
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/Validate/PasswordEquals.php b/library/ZendAfi/Validate/PasswordEquals.php
new file mode 100644
index 00000000000..19d55fb0a04
--- /dev/null
+++ b/library/ZendAfi/Validate/PasswordEquals.php
@@ -0,0 +1,28 @@
+<?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_PasswordEquals extends ZendAfi_Validate_Equals {
+	protected $_messageTemplates = [
+		self::DIFFERENT_VALUES_ERROR   => "Les champs 'Mot de passe' sont différents"
+		];
+
+}
+?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/AuthControllerTest.php b/tests/application/modules/opac/controllers/AuthControllerTest.php
index 298f27a3783..2933e05847e 100644
--- a/tests/application/modules/opac/controllers/AuthControllerTest.php
+++ b/tests/application/modules/opac/controllers/AuthControllerTest.php
@@ -1055,7 +1055,7 @@ class AuthControllerNobodyLoggedAndRegistrationAllowedRegisterTest extends AuthC
 
 
 
-class AuthControllerNobodyLoggedRegisterPostEmptyDataTest extends AuthControllerNobodyLoggedTestCase {
+class AuthControllerNobodyLoggedRegisterPostWrongDataTest extends AuthControllerNobodyLoggedTestCase {
 	public function setUp() {
 		parent::setUp();
 		$this->fixture('Class_Users',
@@ -1064,14 +1064,6 @@ class AuthControllerNobodyLoggedRegisterPostEmptyDataTest extends AuthController
 										'password' => 'secret']);
 
 		Class_AdminVar::newInstanceWithId('INTERDIRE_ENREG_UTIL', ['valeur' => 0]);
-		$this->postDispatch('auth/register', 
-												['login' => '',
-												 'mail' => '',
-												 'mail2' => '',
-												 'mdp' => '',
-												 'mdp2' => '',
-												 'captcha[id]' => '93248',
-												 'captcha[input]' => '']);
 	}
 
 	public function fields() {
@@ -1090,10 +1082,50 @@ class AuthControllerNobodyLoggedRegisterPostEmptyDataTest extends AuthController
 	 * @test 
 	 */
 	public function errorForEmptyFieldShouldBeVisible($field) {
+		$this->postDispatch('auth/register', 
+												['login' => '',
+												 'mail' => '',
+												 'mail2' => '',
+												 'mdp' => '',
+												 'mdp2' => '',
+												 'captcha[id]' => '93248',
+												 'captcha[input]' => '']);
+
 		$this->assertXPathContentContains('//ul[@class="errors"][preceding-sibling::input[@name="'.$field.'"]]//li', 
 																			'Une valeur est requise',
 																			$this->_response->getBody());
 	}
+
+
+	/** @test */
+	public function withMailsNotWellFormedShouldDisplayErrorInvalidMail() {
+		$this->postDispatch('auth/register', 
+												['mail' => 'blib',
+												 'mail2' => 'blib']);
+		$this->assertXPathContentContains('//ul[@class="errors"][preceding-sibling::input[@name="mail"]]//li', 
+																			"'blib' n'est pas un email valide de la forme 'compte@hote.ext'");
+												 
+	}
+
+
+	/** @test */
+	public function withDifferentMailsShouldDisplayErrorMailAreNotTheSame() {
+		$this->postDispatch('auth/register', 
+												['mail' => 'grand-schtroumpf@champi.gnon',
+												 'mail2' => 'schtroumpfette@champi.gnon']);
+		$this->assertXPathContentContains('//ul[@class="errors"][preceding-sibling::input[@name="mail2"]]//li', 
+																			"Les champs 'E-mail' sont différents");
+	}
+
+
+	/** @test */
+	public function withDifferentPasswordsShouldDisplayErrorPasswordsAreNotTheSame() {
+		$this->postDispatch('auth/register', 
+												['mdp' => 'secret',
+												 'mdp2' => 'sicrette']);
+		$this->assertXPathContentContains('//ul[@class="errors"][preceding-sibling::input[@name="mdp2"]]//li', 
+																			"Les champs 'Mot de passe' sont différents");
+	}
 }
 
 
diff --git a/tests/library/Class/UsersTest.php b/tests/library/Class/UsersTest.php
index 67083e5c857..7d863d19e8c 100644
--- a/tests/library/Class/UsersTest.php
+++ b/tests/library/Class/UsersTest.php
@@ -633,11 +633,6 @@ class UsersRegistrationLoginLaurentTest extends UsersRegistrationTestCase {
 		$this->assertErrorContains('Cet identifiant existe déjà');
 	}
 
-	/** @test */
-	public function shouldHavePasswordError() {
-		$this->assertErrorContains('Vous n\'avez pas saisi les mêmes mots de passe');
-	}
-
 	/** @test */
 	public function shouldHaveMailError() {
 		$this->assertErrorContains('L\'adresse e-mail est invalide ou est déjà utilisée.');
-- 
GitLab