From 834977cd86dab3d34b2cc88dcced3bbdb4430664 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Thu, 1 Dec 2016 18:00:06 +0100
Subject: [PATCH] hotline #50888 fix statController stat action

---
 VERSIONS_HOTLINE/50888                        |  1 +
 .../admin/controllers/StatController.php      |  6 +-
 .../scripts/stat/rechercheinfructueuse.phtml  | 66 ++++++++-----------
 library/Class/Codification.php                |  3 +
 library/Class/MoteurRecherche.php             | 14 +++-
 library/Class/StatsNotices.php                |  5 +-
 .../admin/controllers/StatControllerTest.php  | 33 ++++++++++
 .../controllers/RechercheControllerTest.php   | 44 ++++++++++++-
 8 files changed, 122 insertions(+), 50 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/50888

diff --git a/VERSIONS_HOTLINE/50888 b/VERSIONS_HOTLINE/50888
new file mode 100644
index 00000000000..aa96fd21ce1
--- /dev/null
+++ b/VERSIONS_HOTLINE/50888
@@ -0,0 +1 @@
+ - ticket #50888 : Administration : correction des statistiques de recherches infructueuses
\ No newline at end of file
diff --git a/application/modules/admin/controllers/StatController.php b/application/modules/admin/controllers/StatController.php
index 60ac69d2364..41f49ba29c1 100644
--- a/application/modules/admin/controllers/StatController.php
+++ b/application/modules/admin/controllers/StatController.php
@@ -41,12 +41,10 @@ class Admin_StatController extends ZendAfi_Controller_Action {
 
   public function rechercheinfructueuseAction() {
     $this->view->titre = $this->_('Recherches infructueuses');
-    $page = $this->_getParam('page', 1);
-    $this->view->nb_par_page = 20;
-    $start = ($page-1) * $this->view->nb_par_page;
+    $this->view->page = $page = $this->_getParam('page', 1);
+    $this->view->nb_par_page = 100;
     $this->view->liste = $this->cls_stat->getNotFoundByPage($page, $this->view->nb_par_page);
     $this->view->nombre_total = $this->cls_stat->getTotalNotFound();
-    $this->view->page = $page;
   }
 
 
diff --git a/application/modules/admin/views/scripts/stat/rechercheinfructueuse.phtml b/application/modules/admin/views/scripts/stat/rechercheinfructueuse.phtml
index e63e99bb282..8bf7b101150 100644
--- a/application/modules/admin/views/scripts/stat/rechercheinfructueuse.phtml
+++ b/application/modules/admin/views/scripts/stat/rechercheinfructueuse.phtml
@@ -1,41 +1,29 @@
-<?php 
-print('<link rel="stylesheet" type="text/css" media="screen" href="'.URL_ADMIN_CSS.'statistique.css" />');
-print('<h4>Nombre de recherches infructueuses : '.$this->nombre_total.'</h4>');
+<?php
+$paginator = $this->paginationControl((new Zend_Paginator(new Zend_Paginator_Adapter_Null($this->nombre_total)))
+                                      ->setItemCountPerPage($this->nb_par_page)
+                                      ->setCurrentPageNumber($this->page));
 
-// Entete
-print('<table class="stat" style="width:740px" border="0">');
-print('<tr>');
-print('<th class="stat">Date</th>');
-print('<th class="stat">Recherche</th>');
-print('</tr>');
+echo $this->tag('h4', $this->_('Nombre de recherches infructueuse : %s', $this->nombre_total))
+  . $paginator
+  . $this->tagModelTable($this->liste,
+                         [$this->_('Date'),
+                          $this->_('Critères')],
+                         ['date_recherche',
+                          'criteres'],
+                         [],
+                         'empty_results',
+                         null,
+                         ['date_recherche' => function($model)
+                          {
+                            return Class_Date::getHumanDate($model->getDateRecherche(), 'd MMMM yyyy');
+                          },
+                          'criteres' => function($model)
+                          {
+                            if(!$crit = unserialize(stripslashes($model->getCriteres())))
+                              return $model->getCriteres();
 
-$codification = new Class_Codification();
-
-// Lignes
-foreach($this->liste as $ligne)
-{
-  $expression="";
-  $criteres="";
-  $crit="";
-  $date=substr($ligne["date_recherche"],8,2).substr($ligne["date_recherche"],4,4).substr($ligne["date_recherche"],0,4);
-  try {
-    if (!$crit = ZendAfi_Filters_Serialize::unserialize($ligne["criteres"]))
-      $crit = [];
-  } catch (Exception $e) {
-    $crit = [];
-  }
-
-  $criteres = $this->tagHistoriqueRecherche((new Class_CriteresRecherche())->setParams($crit));
-  
-  print('<tr>');
-  print('<td valign="top" style="text-align:center">'.$date.'</td>');
-  print('<td valign="top">'.$criteres.'</td>');
-  print('</tr>');
-}
-
-// Fini
-print('</table>');
-$pager=$this->Pager($this->nombre_total,$this->nb_par_page,$this->page,BASE_URL."/admin/stat/rechercheinfructueuse");
-print('<div style="width:740px"><center>'.$pager.'</div>');
-
-?>
+                            return $this->tagHistoriqueRecherche((new Class_CriteresRecherche())->setParams($crit));
+                          }
+                         ])
+  . $paginator;
+?>
\ No newline at end of file
diff --git a/library/Class/Codification.php b/library/Class/Codification.php
index 56abd432250..8f79ac1a8b2 100644
--- a/library/Class/Codification.php
+++ b/library/Class/Codification.php
@@ -281,6 +281,9 @@ class Class_Codification {
 
 
   public static function getLibelleForSerie($notice) {
+    if(!$notice)
+      return '';
+
     $message = 'Voir tous les tomes';
 
     switch ($notice->getTypeDoc()) {
diff --git a/library/Class/MoteurRecherche.php b/library/Class/MoteurRecherche.php
index 1a8f480a1fe..c93b9f0a0a5 100644
--- a/library/Class/MoteurRecherche.php
+++ b/library/Class/MoteurRecherche.php
@@ -463,7 +463,7 @@ class Class_MoteurRecherche {
     $nb = $search_result->getRecordsCount();
 
     if (!$nb && !$this->_shouldExtend()) {
-      $this->addStatEchec(2, $criteres_recherche->getCriteres());
+      $this->addStatEchec($criteres_recherche->getCriteres());
 
       return $search_result->beError($this->_('Aucun résultat trouvé'));
     }
@@ -615,9 +615,17 @@ class Class_MoteurRecherche {
   }
 
 
-  private function addStatEchec($type_recherche,$criteres) {
+  protected function addStatEchec($criteres) {
+    $excluded_keys = ['retour_panier',
+                      'retour_abonne',
+                      'retour_avis'];
+
+    if(array_intersect($excluded_keys,
+                       array_keys($criteres)))
+      return;
+
     (new Class_StatRechercheEchec())
-      ->setTypeRecherche($type_recherche)
+      ->setTypeRecherche(2) // ???
       ->setCriteres(addslashes(serialize($criteres)))
       ->save();
   }
diff --git a/library/Class/StatsNotices.php b/library/Class/StatsNotices.php
index 68e8cfc646e..fd3da490894 100644
--- a/library/Class/StatsNotices.php
+++ b/library/Class/StatsNotices.php
@@ -164,11 +164,14 @@ class Class_StatsNotices {
 
 
   public function getTotalNotFound() {
-    return fetchOne('select count(*) from stats_recherche_echec');
+    return Class_StatRechercheEchec::count();
   }
 
 
   public function getNotFoundByPage($start, $count) {
+    return Class_StatRechercheEchec::findAllBy(['order' => 'id desc' ,
+                                                'limitPage' => [$start, $count]]);
+
     return fetchAll(sprintf('select * from stats_recherche_echec order by id desc limit %s, %s',
                             $start, $count));
   }
diff --git a/tests/application/modules/admin/controllers/StatControllerTest.php b/tests/application/modules/admin/controllers/StatControllerTest.php
index f680b9f0550..bfa2d5ef913 100644
--- a/tests/application/modules/admin/controllers/StatControllerTest.php
+++ b/tests/application/modules/admin/controllers/StatControllerTest.php
@@ -21,14 +21,47 @@
 require_once 'AdminAbstractControllerTestCase.php';
 
 class Admin_StatControllerRecherchesInfructueusesTest extends Admin_AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
   public function setUp() {
     parent::setUp();
+
+    $this->fixture('Class_StatRechercheEchec',
+                   ['id' => 1,
+                    'date_recherche' => '',
+                    'criteres' => serialize(['expressionRecherche' => 'not found',
+                                             'tri' => '*'])]);
+
+    $this->fixture('Class_StatRechercheEchec',
+                   ['id' => 2,
+                    'date_recherche' => '',
+                    'criteres' => 'a:6:{s:11:\"rech_titres\";s:44:\"l\'idée ridicule de ne plus jamais te revoir\";s:12:\"rech_auteurs\";s:12:\"rosa montero\";s:3:\"tri\";s:1:\"*\";s:16:\"operateur_titres\";s:3:\"and\";s:17:\"operateur_auteurs\";s:3:\"and\";s:14:\"type_recherche\";s:8:\"fulltext\";}']);
+
     $this->dispatch('/admin/stat/rechercheinfructueuse', true);
   }
 
+
   /** @test */
   public function titreShouldBeRecherchesInfructueuses() {
     $this->assertXPathContentContains('//h1', 'Recherches infructueuses');
   }
+
+
+  /** @test */
+  public function tdShouldContainsNotFound() {
+    $this->assertXpathContentContains('//td', 'not found');
+  }
+
+
+  /** @test */
+  public function tdShouldContainsLIdeeRidicule() {
+    $this->assertXpathContentContains('//td', 'l\'idée ridicule de ne plus jamais te revoir');
+  }
+
+
+  /** @test */
+  public function totalCountShouldBe2() {
+    $this->assertXPathContentContains('//h4', 'Nombre de recherches infructueuse : 2');
+  }
 }
 ?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/RechercheControllerTest.php b/tests/application/modules/opac/controllers/RechercheControllerTest.php
index e816dded951..49ee1b67924 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerTest.php
@@ -3029,7 +3029,9 @@ class RechercheControllerViewnoticeWithBreadcrumbAndIdPanierParamTest extends Re
 
 
 
-class RechercheControlleSimpleActionWithEmptyDomainSettingsTest extends AbstractControllerTestCase {
+class RechercheControllerSimpleActionWithEmptyDomainSettingsTest extends AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
   public function setUp() {
     parent::setUp();
 
@@ -3037,19 +3039,55 @@ class RechercheControlleSimpleActionWithEmptyDomainSettingsTest extends Abstract
                    ['id'=>3,
                     'libelle' => 'Nouveautés',
                     'auteur' => 'Paul']);
+
     $this->dispatch('/recherche/simple/id_catalogue/3/id_module/9/aleatoire/1', true);
   }
 
 
   /** @test */
   public function onRechercheSimpleCatalogue3IdModule9ResultsShouldBeEmpty() {
-    $this->assertXPathContentContains('//div[@class="info-recherche"]', 'Aucun résultat trouvé',                       $this->_response->getBody());
+    $this->assertXPathContentContains('//div[@class="info-recherche"]',
+                                      'Aucun résultat trouvé',
+                                      $this->_response->getBody());
+  }
+
+
+  /** @test */
+  public function statRechercheEchecShouldHaveBeenCreated() {
+    $this->assertNotEmpty(Class_StatRechercheEchec::findAll());
+  }
+}
+
+
+
+
+class RechercheControllerNoResultOnSubscriberLinkTest extends AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
+  public function setUp() {
+    parent::setUp();
+    $this->dispatch('/recherche/simple/retour_abonne/viewavis/retour_avis/8/page/2/id_module/8/code_rebond/G1', true);
+  }
+
+
+  /** @test */
+  public function noResultMesssageShouldBeDisplay() {
+    $this->assertXPathContentContains('//div[@class="info-recherche"]',
+                                      'Aucun résultat trouvé',
+                                      $this->_response->getBody());
+  }
+
+
+  /** @test */
+  public function statRechercheEchecShouldNotHaveBeenCreated() {
+    $this->assertEmpty(Class_StatRechercheEchec::findAll());
   }
 }
 
 
 
-class RechercheControlleSimpleActionWithEmptyDomainSettingsAndSiteLinkedTest extends RechercheControllerNoticeTestCase {
+
+class RechercheControllerSimpleActionWithEmptyDomainSettingsAndSiteLinkedTest extends RechercheControllerNoticeTestCase {
   public function setUp() {
     parent::setUp();
     $this->fixture('Class_Catalogue',
-- 
GitLab