From 52cfc75e6f4cb73be84e0b23e5a521c3e291a57c Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@git-test.afi-sa.fr>
Date: Mon, 21 May 2012 16:13:26 +0000
Subject: [PATCH] Passage sous test du helper accueil Tags

---
 .gitattributes                                |   1 +
 library/ZendAfi/View/Helper/Accueil/Tags.php  | 120 ++++++++++--------
 .../ZendAfi/View/Helper/Accueil/TagsTest.php  |  59 +++++++++
 3 files changed, 125 insertions(+), 55 deletions(-)
 create mode 100644 tests/library/ZendAfi/View/Helper/Accueil/TagsTest.php

diff --git a/.gitattributes b/.gitattributes
index cdbf53c707c..bc7a34c9a5f 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -3615,6 +3615,7 @@ tests/library/ZendAfi/View/Helper/Accueil/MenuVerticalTest.php -text
 tests/library/ZendAfi/View/Helper/Accueil/NewsTest.php -text
 tests/library/ZendAfi/View/Helper/Accueil/RssTest.php -text
 tests/library/ZendAfi/View/Helper/Accueil/SitoTest.php -text
+tests/library/ZendAfi/View/Helper/Accueil/TagsTest.php -text
 tests/library/ZendAfi/View/Helper/Admin/AdminHelpLinkTest.php -text
 tests/library/ZendAfi/View/Helper/Admin/ImageViewersOptionsTest.php -text
 tests/library/ZendAfi/View/Helper/AvisTest.php -text
diff --git a/library/ZendAfi/View/Helper/Accueil/Tags.php b/library/ZendAfi/View/Helper/Accueil/Tags.php
index 94c668a392f..547f004f293 100644
--- a/library/ZendAfi/View/Helper/Accueil/Tags.php
+++ b/library/ZendAfi/View/Helper/Accueil/Tags.php
@@ -18,17 +18,8 @@
  * 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 
  */
-/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// OPAC3 - Class_Module_Tags
-/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 class ZendAfi_View_Helper_Accueil_Tags extends ZendAfi_View_Helper_Accueil_Base {
-
-
-//---------------------------------------------------------------------
-// Construction HTML 
-//---------------------------------------------------------------------
-	public function getHtml()
-	{
+	public function getHtml() {
 		/* Paramètres reçus
 		 * limite: nombre de notices à analyser
 		 * nombre: nombre de notices à afficher dans le nuage
@@ -50,67 +41,86 @@ class ZendAfi_View_Helper_Accueil_Tags extends ZendAfi_View_Helper_Accueil_Base
 		$this->preferences["nb_notices"] = $this->preferences["limite"];
 		$this->preferences["aleatoire"] = 0; //pour ne prendre en compte les limites
 
-		if ($this->preferences["notices"] > 0)
-			$this->preferences["tri"] = $this->preferences["notices"];
-		else
+		$this->preferences["tri"] = ($this->preferences["notices"] > 0) ?
+			$this->preferences["notices"] :
 			$this->preferences["tri"] = -1; // Permet de ne pas faire d'order by.
 
 		$notices = $catalogue->getNoticesByPreferences($this->preferences);
+		$facettes = $this->_getFacettesFromNotices($notices);
 
-		foreach ($notices as $notice)	{
-			$type="T".$notice["type_doc"];
-			$facettes["T"][$type]++;
-			$items=explode(" ",trim($notice["facettes"]));
-			foreach($items as $item)
-			{
-				$type=substr($item,0,1);
-				$facettes[$type][$item]++;
-			}
-		}
+		$nb = 0;
+		$sorted = array();
 
-		// Constituer le tableau des tags
-		$nb=0;
-		while(true)
-		{
+		while (true) {
 			// On cherche le plus fort pour chaque type
-			$controle=array("nombre" => 0);
-			$yen_a_plus=true;
-			for($i=0; $i<strlen($this->preferences["type_tags"]); $i++)
-			{
-				$type=$this->preferences["type_tags"][$i];
-				if(!$facettes[$type]) continue;
-				$yen_a_plus=false;
+			$controle = array("nombre" => 0);
+			$yen_a_plus = true;
+
+			for ($i = 0; $i < strlen($this->preferences["type_tags"]); $i++) {
+				$type = $this->preferences["type_tags"][$i];
+				if (!array_key_exists($type, $facettes)) continue;
+				$yen_a_plus = false;
 				
-				if(!$sorted[$type]) {arsort($facettes[$type]); $sorted[$type]=true;}
+				if (!array_key_exists($type, $sorted)) {
+					arsort($facettes[$type]); 
+					$sorted[$type] = true;
+				}
 				
-				$lig=array_slice($facettes[$type],0,1);
-				$compare=array_values($lig);
-				if($compare[0] > $controle["nombre"])
-				{
-					$controle["nombre"]=$compare[0];
-					$controle["type"]=$type;
-					$controle["clef"]=$lig;
+				$lig = array_slice($facettes[$type], 0, 1);
+				if (!$lig)
+					continue;
+				$compare = array_values($lig);
+
+				if ($compare[0] > $controle["nombre"]) {
+					$controle["nombre"] = $compare[0];
+					$controle["type"] = $type;
+					$controle["clef"] = $lig;
 				}
 			}
-			// Si max atteint ou plus de facettes c'est fini
-			if($yen_a_plus == true) break;
+
+			if ($yen_a_plus)
+				break;
+
 			$nb++;
-			if($nb > $this->preferences["nombre"]) break;
-	
-			// On depile l'item des facettes et on empile dans le resultat
-			foreach($controle["clef"] as $clef => $nombre);
+			if ($nb > $this->preferences['nombre'])
+				break;
+
+			$clef = key($controle['clef']);
+			$table[$nb]["id"] = $clef;
+			$table[$nb]["libelle"] = Class_Codification::getLibelleFacette($clef);
+			$table[$nb]["nombre"] = current($controle['clef']);
+
 			array_shift($facettes[$controle["type"]]);
-			$table[$nb]["id"]=$clef;
-			$table[$nb]["libelle"]=Class_Codification::getLibelleFacette($clef);
-			$table[$nb]["nombre"]=$nombre;
 		}
 	
 		// Html avec le view helper
-		$cls=new ZendAfi_View_Helper_NuageTags();
-		$this->contenu.= $cls->nuageTags($table).'<br />';
+		$cls = new ZendAfi_View_Helper_NuageTags();
+		$this->contenu .= $cls->nuageTags($table).'<br />';
 	
-		// Valorisation du html accessible
-
 		return $this->getHtmlArray();
 	}
+
+
+	protected function _getFacettesFromNotices($notices) {
+		$facettes = array('T' => array());
+		foreach ($notices as $notice)	{
+			$type = 'T' . $notice['type_doc'];
+			array_key_exists($type, $facettes['T']) ? 
+				$facettes['T'][$type]++ : 
+				$facettes['T'][$type] = 1;
+
+			$items = explode(" ", trim($notice['facettes']));
+			foreach ($items as $item) {
+				$type = substr($item, 0, 1);
+				if (!array_key_exists($type, $facettes))
+					$facettes[$type] = array();
+
+				array_key_exists($item, $facettes[$type]) ? 
+					$facettes[$type][$item]++ :
+					$facettes[$type][$item] = 1;
+			}
+		}
+
+		return $facettes;
+	}
 }
\ No newline at end of file
diff --git a/tests/library/ZendAfi/View/Helper/Accueil/TagsTest.php b/tests/library/ZendAfi/View/Helper/Accueil/TagsTest.php
new file mode 100644
index 00000000000..859f93ed8e5
--- /dev/null
+++ b/tests/library/ZendAfi/View/Helper/Accueil/TagsTest.php
@@ -0,0 +1,59 @@
+<?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 
+ */
+require_once 'library/ZendAfi/View/Helper/ViewHelperTestCase.php';
+
+class TagsTest extends ViewHelperTestCase {
+	public function setUp() {
+		parent::setUp();
+		Zend_Registry::set('sql', 
+											 Storm_Test_ObjectWrapper::mock()
+											 ->whenCalled('fetchAll')
+											 ->answers(array(array('type_doc' => 1,
+																						 'facettes' => 'A777',
+																						 'id_notice' => 34,
+																						 'editeur' => '',
+																						 'annee' => '',
+																						 'date_creation' => '',
+																						 'clef_oeuvre' => '')))
+											 
+											 ->whenCalled('fetchEnreg')
+											 ->answers(array('unimarc' => '',
+																			 'type_doc' => 1))
+
+											 ->whenCalled('fetchOne')
+											 ->answers('Terry Pratchett'));
+
+		$helper = new ZendAfi_View_Helper_Accueil_Tags(3, array('type_module' => 'TAGS',
+																														'division' => 2,
+																														'preferences' => array('type_tags' => 'AMDPZ',
+																																									 'nombre' => 1)));
+		$this->_html = $helper->getBoite();
+	}
+
+
+	/** @test */
+	public function terryPratchetShouldBePresent() {
+		$this->assertXPathContentContains($this->_html, 
+																			'//a[contains(@href, "recherche/rebond")]', 
+																			'Terry Pratchett');
+	}
+
+}
\ No newline at end of file
-- 
GitLab