From 5bc84184759a2c112df208d682b80eb533c24658 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@git-test.afi-sa.fr>
Date: Tue, 22 Jan 2013 10:07:34 +0000
Subject: [PATCH] Import Profiles : creation de dossiers et sous dossiers

---
 library/Class/ImportTxtFile.php               | 52 +++++++++++++------
 .../controllers/ImportArboFromTxtFileTest.php | 29 ++++++++---
 2 files changed, 57 insertions(+), 24 deletions(-)

diff --git a/library/Class/ImportTxtFile.php b/library/Class/ImportTxtFile.php
index 5e090ada312..282046d6d75 100644
--- a/library/Class/ImportTxtFile.php
+++ b/library/Class/ImportTxtFile.php
@@ -30,31 +30,53 @@ class Class_ImportTxtFile  {
 	public function readAllProfils() {
 		$profiles=[];
 
-		$firstBlocks = preg_split('/\n\*(?!\*)/', $this->_file_contents,0,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
+		$firstBlocks = $this->getFirstBlocks();
 
 		foreach($firstBlocks as $block) {
-			//			foreach (split_block($block) $
 			$profil = new Class_Profil();
+			$lines = $this->splitLines($block);
+			if (empty($lines)) 
+				continue;
+			$profil_name = $this->getFirstLineName($lines[0]);
+			$profil->setLibelle($profil_name);
 
-			$block=preg_split('/\n/',$block,0,PREG_SPLIT_NO_EMPTY);
-
-			//			$line = preg_grep('/\n\*\s(.*)$/',$block);
-			$line = preg_replace('/^\*(?!\*)/','$1',$block[0]);
-			//	echo 'line:'.$line."--\n";
-			$profil->setLibelle(trim($line));
-			$pages=preg_grep('/\*\*(?!\*)/',$block);
-			$this->createPagesForProfil($pages,$profil);
+			$this->createPagesForProfil($this->getPages($lines),$profil);
 			$profiles[]=$profil;
 		}
 		return $profiles;
 	}
 
 
+	public function getPages($lines) {
+		return preg_grep('/\*\*(?!\*)/',$lines);
+	}
+	
+
+	public function splitLines($block) {
+		return preg_split('/\n/',$block,0,PREG_SPLIT_NO_EMPTY);
+	}
+
+
+	public function getFirstLineName($line) {
+		return trim(preg_replace('/^\*(?!\*)/','$1',$line));
+	}
+
+
+	public function getSubLineName($pageline) {
+		return trim(preg_replace('/^\*\*([\s|A-Z])/','$1',$pageline));
+	}
+
+
+	public function getFirstBlocks() {
+		return preg_split('/\n\*(?!\*)/', $this->_file_contents,0,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
+	}
+
+
 	public function createPagesForProfil($pages,$profil) {
 		foreach ($pages as $pageline) {
 			
 			$page = new Class_Profil();
-			$subline = trim(preg_replace('/^\*\*([\s|A-Z])/','$1',$pageline));
+			$subline = $this->getSubLineName($pageline);
 			
 			if (empty($subline))
 				continue;
@@ -65,11 +87,10 @@ class Class_ImportTxtFile  {
 	}
 
 
-	public function recursiveDirectoryForProfil($profil) {
+	public function createDirectoriesForProfil($profil) {
 		foreach ($profil->getSubProfils() as $key => $subprofil) {
-			$this->createDirectory($profil->getLibelle().'/'.$this->recursiveDirectoryForProfil($subprofil));
+			$this->createDirectory($profil->getLibelle().'/'.$this->createDirectoriesForProfil($subprofil));
 		}
-		//			return $profil->getParentProfil().'/'.$this->recursiveDirectoryNameForProfil($profil);
 		return $profil->getLibelle();
 	}
 
@@ -84,11 +105,10 @@ class Class_ImportTxtFile  {
 
 
 	public function createDirectory($directory_name) {
-
 		$file_writer = self::getFileWriter();
 		$cleaned_name=iconv('UTF-8','ASCII//TRANSLIT',$directory_name);
 		$cleaned_name=str_replace(' ','_',$cleaned_name);
-		echo "$directory_name\n";
+		echo $this->getDefaultPath().'/'.$cleaned_name."\n";
 		$file_writer->mkdir($this->getDefaultPath().'/'.$cleaned_name); 
 		return $cleaned_name;
 	}
diff --git a/tests/application/modules/admin/controllers/ImportArboFromTxtFileTest.php b/tests/application/modules/admin/controllers/ImportArboFromTxtFileTest.php
index 58253c961b9..ef5c23a43ec 100644
--- a/tests/application/modules/admin/controllers/ImportArboFromTxtFileTest.php
+++ b/tests/application/modules/admin/controllers/ImportArboFromTxtFileTest.php
@@ -45,6 +45,15 @@ class ImportArboFormTxtFileReadProfilTest extends PHPUnit_Framework_TestCase {
 		$this->_importTxt->setFileContents($_file_contents);
 	}
 
+
+
+	/** @test */ 
+	public function getSubLineNameShouldReturnNameWithoutEtoiles() {
+		$this->assertEquals('Le nom d\'une page',$this->_importTxt->getSubLineName('**Le nom d\'une page'));
+		
+	}
+
+
 	/** @test */
 	public function importFileShouldCreateProfilVieProfessionnellleEtFormation() {
 		$profils = $this->_importTxt->readAllProfils();
@@ -166,8 +175,11 @@ class ImportArboFormTxtFileDirectoryCreationReadProfilTest extends PHPUnit_Frame
 											->answers(true)
 											->whenCalled('mkdir')
 											->with('/Nature_et_loisirs/Jeux')
-											->answers(true);
-		//											->beStrict();
+											->answers(true)
+											->whenCalled('mkdir')
+											->with('/Cinema/Series_tv')
+											->answers(true)
+											->beStrict();
 		/*		$this->file_writer->whenCalled('mkdir')
 											->do(function (){ echo $directory_name;});
 		*/
@@ -177,19 +189,20 @@ class ImportArboFormTxtFileDirectoryCreationReadProfilTest extends PHPUnit_Frame
 
 
 	/** @test */
-	public function importArboFromTxtFileRecursiveDirectoryCreationForProfilShouldReturnPath() {
+	public function importFileShouldCreateDirectoriesJeuxAndSportForProfilNatureEtLoisirs() {
 		$profils = $this->_importTxt->readAllProfils();
-		$this->_importTxt->recursiveDirectoryForProfil($profils[0]);
-		//		$this->assertTrue($this->file_writer->methodHasBeenCalledWithParams('mkdir','/Nature_et_loisirs/Jeux'));
+		$this->_importTxt->createDirectoriesForProfil($profils[0]);
+		$this->assertTrue($this->file_writer->methodHasBeenCalledWithParams('mkdir',['/Nature_et_loisirs/Jeux']));
+		$this->assertTrue($this->file_writer->methodHasBeenCalledWithParams('mkdir',['/Nature_et_loisirs/Sport']));
 		
 	}
 
 
 	/** @test */
-	public function importFileShouldCreateDirectoriesSportAndJeuxForProfilNatureEtLoisirs() {
+	public function importFileShouldCreateDirectorySeriesTvForProfilCinema() {
 		$profils = $this->_importTxt->readAllProfils();
-		//$this->_importTxt->createDirectoriesForProfil($profils[0]);
-		
+		$this->_importTxt->createDirectoriesForProfil($profils[1]);
+		$this->assertTrue($this->file_writer->methodHasBeenCalledWithParams('mkdir',['/Cinema/Series_tv']));
 	}
 }
 
-- 
GitLab