From 02b4e0b703553aa0ce2f204be552ebb21eb1807a Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 12 Feb 2016 20:49:03 +0100 Subject: [PATCH] - [x] [phpunit/tests/Database_delNumeric] Tests unitaire de delNumeric() - [x] [Database] Mise a jour des methodes de Database - [x] [Database::check] Suite de l'implementation (couverture des types de la BDD actuelle: 100%) - [x] [Database::delNumeric] Prevention si oubli @oneDimension + ne supprime plus les indices numeriques associees a aucun indice textuel - [x] [phpunit/tests/Database_check] Tests unitaire du checker --- automate.php | 3 +- manager/Database.php | 69 ++++++++++------- phpunit/phpunit.xml | 3 +- phpunit/tests/Database_check.php | 66 ++++++++++++++-- phpunit/tests/Database_delNumeric.php | 106 ++++++++++++++++++++++++++ todo.md | 6 +- 6 files changed, 215 insertions(+), 38 deletions(-) create mode 100644 phpunit/tests/Database_delNumeric.php diff --git a/automate.php b/automate.php index 21cf93d..ed33567 100755 --- a/automate.php +++ b/automate.php @@ -280,7 +280,6 @@ return true; - }testDatabaseChecker(); - + }//testDatabaseChecker(); ?> \ No newline at end of file diff --git a/manager/Database.php b/manager/Database.php index 3585d86..6f51cee 100755 --- a/manager/Database.php +++ b/manager/Database.php @@ -73,21 +73,29 @@ * */ public static function delNumeric($fetchData, $oneDimension=false){ + $nextEquivalent = false; // Vaut VRAI si le prochain est peut-etre un equivalent numerique /* [1] 2 dimensions ===============================================*/ - if( !$oneDimension ){ + if( !$oneDimension && is_array($fetchData[0]) ){ - // on supprime les doublons des entrées (indice numérique) - for( $i = 0 ; $i < count($fetchData) ; $i++ ) // pour tout les utilisateurs - foreach($fetchData[$i] as $col => $val){ // pour toutes les entrées - - if( !mb_detect_encoding($val, 'UTF-8') ) - $fetchData[$i][$col] = utf8_encode($val); - - if( is_int($col) ) // si l'indice est un entier - unset( $fetchData[$i][$col] ); // on le supprime - } + // on supprime les doublons des entrées (indice numérique) + for( $i = 0 ; $i < count($fetchData) ; $i++ ) // pour tout les utilisateurs + foreach($fetchData[$i] as $col => $val){ // pour toutes les entrées + + if( !mb_detect_encoding($val, 'UTF-8') ) + $fetchData[$i][$col] = utf8_encode($val); + + if( is_int($col) ){ // Si indice numerique + if( $nextEquivalent ) // Si suit un indice textuel + unset( $fetchData[$i][$col] ); // on supprime l'indice + + $nextEquivalent = false; // Dans tous les cas, on dit que le prochain ne pourra pas etre supprime si numerique + + }else // Si l'indice n'est pas un entier + $nextEquivalent = true; // On signale qu'il y aura peut etre un indice numerique suivant + + } /* [2] 1 dimensions ===============================================*/ @@ -99,8 +107,15 @@ if( !mb_detect_encoding($val, 'UTF-8') ) $fetchData[$i] = utf8_encode($val); - if( is_int($i) ) // si l'indice est un entier - unset( $fetchData[$i] ); // on le supprime + if( is_int($i) ){ // Si indice numerique + if( $nextEquivalent ) // Si suit un indice textuel + unset( $fetchData[$i] ); // on supprime l'indice + + $nextEquivalent = false; // Dans tous les cas, on dit que le prochain ne pourra pas etre supprime si numerique + + }else // Si l'indice n'est pas un entier + $nextEquivalent = true; // On signale qu'il y aura peut etre un indice numerique suivant + } } @@ -139,33 +154,29 @@ /* (2) Utilisateur */ case 'user.code': + case 'machine.code': return $checker && is_string($value) && preg_match('/^[\dA-F]{2}(\-[\dA-F]{2}){3,5}$/i', $value); break; - case 'user.username': case 'user.lastname': - return $checker && is_string($value) && preg_match('/^[\w-]{3,30}$/i', $value); + + case 'user.username': + case 'machine.name': + case 'group.name': + return $checker && is_string($value) && preg_match('/^[\w-]{1,30}$/i', $value); break; - case 'user.firstname': case 'user.lastname': + + case 'user.firstname': + case 'user.lastname': return $checker && is_string($value) && preg_match('/^[a-z -]{3,30}$/i', $value); break; + case 'user.mail': return $checker && is_string($value) && strlen($value) <= 50 && preg_match('/^[\w\.-]+@[\w\.-]+\.[a-z]{2,4}$/i', $value); break; + case 'user.password': - return $checker && is_numeric($value) && preg_match('/^[\da-f]{40}$/i', $value); + return $checker && is_string($value) && preg_match('/^[\da-f]{40}$/i', $value); break; - /* (3) Machine */ - case 'groupe.nom': - return $checker && is_string($value) && preg_match('/^[a-z0-9 -]{1,10}$/i', $value); - break; - - /* (4) Groupe */ - case 'formation.code': - return $checker && is_string($value) && preg_match('/[\w]{0,10}/i', $value); - break; - case 'formation.nom': - return $checker && is_string($value) && preg_match('/[\w ]{0,100}/i', $value); - break; } return $checker; diff --git a/phpunit/phpunit.xml b/phpunit/phpunit.xml index d77ed02..2470e97 100644 --- a/phpunit/phpunit.xml +++ b/phpunit/phpunit.xml @@ -1,8 +1,9 @@ - + tests/Database_check.php + tests/Database_delNumeric.php diff --git a/phpunit/tests/Database_check.php b/phpunit/tests/Database_check.php index 8d9eb1b..ea3322d 100644 --- a/phpunit/tests/Database_check.php +++ b/phpunit/tests/Database_check.php @@ -63,7 +63,7 @@ - /* [2] username + /* [2] user.username ; machine.name ; group.name =========================================================*/ /* (1) Type */ public function testUsernameTypeStringCorrect(){ @@ -90,18 +90,22 @@ /* (3) Size */ public function testUsernameSize3Correct(){ + $this->assertEquals( 3, strlen('012') ); $this->assertTrue( \manager\Database::check('user.username', '012') ); } public function testUsernameSize30Correct(){ + $this->assertEquals( 30, strlen('0123456789abcdefghijklmno_-sda') ); $this->assertTrue( \manager\Database::check('user.username', '0123456789abcdefghijklmno_-sda') ); } public function testUsernameSizeLt3Incorrect(){ - $this->assertFalse( \manager\Database::check('user.username', '01') ); + $this->assertLessThan( 3, strlen('') ); + $this->assertFalse( \manager\Database::check('user.username', '') ); } public function testUsernameSizeGt30Incorrect(){ + $this->assertGreaterThan( 30, strlen('0123456789abcdefghijklmno_-sdaa') ); $this->assertFalse( \manager\Database::check('user.username', '0123456789abcdefghijklmno_-sdaa') ); } @@ -131,37 +135,89 @@ /* (3) Size */ public function testFirstnameSize3Correct(){ + $this->assertEquals( 3, strlen('abc') ); $this->assertTrue( \manager\Database::check('user.firstname', 'abc') ); } public function testFirstnameSize30Correct(){ + $this->assertEquals( 30, strlen('abcdefghijklmnopqrstuvwxyz-k s') ); $this->assertTrue( \manager\Database::check('user.firstname', 'abcdefghijklmnopqrstuvwxyz-k s') ); } public function testFirstnameSizeLt3Incorrect(){ + $this->assertLessThan( 3, strlen('ab') ); $this->assertFalse( \manager\Database::check('user.firstname', 'ab') ); } public function testFirstnameSizeGt30Incorrect(){ + $this->assertGreaterThan( 30, strlen('abcdefghijklmnopqrstuvwxyz-k ss') ); $this->assertFalse( \manager\Database::check('user.firstname', 'abcdefghijklmnopqrstuvwxyz-k ss') ); } /* [4] Adresse mail =========================================================*/ - public function testMailContentCorrect(){ + /* (1) Size */ + public function testMailSizeEqCorrect(){ + $this->assertLessThanOrEqual( 50, 'nom-prenom.mot@domaine-d.gouv' ); $this->assertTrue( \manager\Database::check('user.mail', 'nom-prenom.mot@domaine-d.gouv') ); } + public function testMailSizeSupCorrect(){ + $this->assertGreaterThan( 50, strlen('ab12345678901234567890nom-prenom.mot@domaine-d.gouv') ); + $this->assertFalse( \manager\Database::check('user.mail', 'ab12345678901234567890nom-prenom.mot@domaine-d.gouv') ); + } + + /* (2) Content */ + public function testMailContentCorrect(){ + $this->assertTrue( \manager\Database::check('user.mail', '0nom-prenom.mot@domaine-d.gouv') ); + } + public function testMailContentIncorrect1(){ - $this->assertFalse( \manager\Database::check('user.mail', 'nom-prenom.mot@domaine-d.gouve') ); + $this->assertFalse( \manager\Database::check('user.mail', '0nom-prenom.mot@domaine-d.gouve') ); } public function testMailContentIncorrect2(){ - $this->assertFalse( \manager\Database::check('user.mail', 'nom-prenom.mot@domaine-d.g') ); + $this->assertFalse( \manager\Database::check('user.mail', '0nom-prenom.mot@domaine-d.g') ); } + /* [5] Mot de passe + =========================================================*/ + public function testPasswordSizeEqCorrect(){ + $password_hash = sha1('monmotdepasse'); + + $this->assertEquals( 40, strlen($password_hash) ); + $this->assertTrue( \manager\Database::check('user.password', $password_hash) ); + } + + public function testPasswordSizeInfIncorrect(){ + $password_hash = 'a'; + + $this->assertLessThan( 40, strlen($password_hash) ); + $this->assertFalse( \manager\Database::check('user.password', $password_hash) ); + } + + public function testPasswordSizeSupIncorrect(){ + $password_hash = sha1('monmotdepasse').'a'; + + $this->assertGreaterThan( 40, strlen($password_hash) ); + $this->assertFalse( \manager\Database::check('user.password', $password_hash) ); + } + + + public function testPasswordContentCorrect(){ + $this->assertTrue( \manager\Database::check('user.password', 'dd629d39c4576731a2bef003c72ff89d6fc2a99a') ); + } + + public function testPasswordContentIncorrect(){ + $this->assertContains( 'g', 'dd629d39c4576731a2bef003c72ff89d6fc2a9g' ); + $this->assertFalse( \manager\Database::check('user.password', 'dd629d39c4576731a2bef003c72ff89d6fc2a9g') ); + } + + + + } ?> \ No newline at end of file diff --git a/phpunit/tests/Database_delNumeric.php b/phpunit/tests/Database_delNumeric.php new file mode 100644 index 0000000..c81bb01 --- /dev/null +++ b/phpunit/tests/Database_delNumeric.php @@ -0,0 +1,106 @@ + '000001', + 0 => '000001', + 'nom' => 'Jean Dupont', + 1 => 'Jean Dupont', + )); + + $computed_array = \manager\Database::delNumeric( $fetchData ); + + $this->assertArrayHasKey( 'id', $computed_array[0] ); + $this->assertArrayHasKey( 'nom', $computed_array[0] ); + $this->assertArrayNotHasKey( 0, $computed_array[0] ); + $this->assertArrayNotHasKey( 1, $computed_array[0] ); + } + + /* (2) Verification d'indices numeriques dans les donnees */ + public function testGlobal2DimWithNumericIndexes(){ + $fetchData = array(array( + 'id' => '000001', + 0 => '000001', + 'nom' => 'Jean Dupont', + 1 => 'Jean Dupont', + 2 => 'Bla', + 3 => 'Bla', + 4 => 'Bla', + 5 => 'Bla', + 6 => 'Bla', + 7 => 'Bla' + )); + + $computed_array = \manager\Database::delNumeric( $fetchData ); + + $this->assertArrayHasKey( 'id', $computed_array[0] ); + $this->assertArrayHasKey( 'nom', $computed_array[0] ); + $this->assertArrayNotHasKey( 0, $computed_array[0] ); + $this->assertArrayNotHasKey( 1, $computed_array[0] ); + + $this->assertArrayHasKey( 2, $computed_array[0] ); + $this->assertArrayHasKey( 3, $computed_array[0] ); + $this->assertArrayHasKey( 4, $computed_array[0] ); + $this->assertArrayHasKey( 5, $computed_array[0] ); + $this->assertArrayHasKey( 6, $computed_array[0] ); + $this->assertArrayHasKey( 7, $computed_array[0] ); + } + + + /* [2] Verification pour 1 dimensions + =========================================================*/ + /* (1) Global */ + public function testGlobal1Dim(){ + $fetchData = array( + 'id' => '000001', + 0 => '000001', + 'nom' => 'Jean Dupont', + 1 => 'Jean Dupont' + ); + + $computed_array = \manager\Database::delNumeric( $fetchData ); + + $this->assertArrayHasKey( 'id', $computed_array ); + $this->assertArrayHasKey( 'nom', $computed_array ); + $this->assertArrayNotHasKey( 0, $computed_array ); + $this->assertArrayNotHasKey( 1, $computed_array ); + } + + /* (2) Verification d'indices numeriques dans les donnees */ + public function testGlobal1DimWithNumericIndexes(){ + $fetchData = array( + 'id' => '000001', + 0 => '000001', + 'nom' => 'Jean Dupont', + 1 => 'Jean Dupont', + 2 => 'Bla', + 3 => 'Bla', + 4 => 'Bla', + 5 => 'Bla', + 6 => 'Bla', + 7 => 'Bla' + ); + + $computed_array = \manager\Database::delNumeric( $fetchData ); + + $this->assertArrayHasKey( 'id', $computed_array ); + $this->assertArrayHasKey( 'nom', $computed_array ); + $this->assertArrayNotHasKey( 0, $computed_array ); + $this->assertArrayNotHasKey( 1, $computed_array ); + + $this->assertArrayHasKey( 2, $computed_array ); + $this->assertArrayHasKey( 3, $computed_array ); + $this->assertArrayHasKey( 4, $computed_array ); + $this->assertArrayHasKey( 5, $computed_array ); + $this->assertArrayHasKey( 6, $computed_array ); + $this->assertArrayHasKey( 7, $computed_array ); + } + + } + +?> \ No newline at end of file diff --git a/todo.md b/todo.md index 8d3adcc..cf7ff99 100755 --- a/todo.md +++ b/todo.md @@ -13,7 +13,6 @@ ############ # EN COURS # ############ -- [.] [phpunit/tests/Database_check] Tests unitaire du checker - [ ] [Database] Checker de type - [ ] Gestion des groupes (utilisateurs/machines) - [x] bdd @@ -41,6 +40,11 @@ ######## # FAIT # ######## +- [x] [phpunit/tests/Database_delNumeric] Tests unitaire de delNumeric() + - [x] [Database] Mise a jour des methodes de Database + - [x] [Database::check] Suite de l'implementation (couverture des types de la BDD actuelle: 100%) + - [x] [Database::delNumeric] Prevention si oubli @oneDimension + ne supprime plus les indices numeriques associees a aucun indice textuel +- [x] [phpunit/tests/Database_check] Tests unitaire du checker - [x] [phpunit/] Install+Config phpunit - [x] [manager/Repo] Gestion des Repo - [x] [ManagerError] Correction/ajout des codes erreurs