From 4703a4919b3e95420d38efd2b8cc0066b4f546a8 Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Mon, 27 Jul 2015 14:48:35 +0200
Subject: [PATCH] dev #22209 search result feed

more data in atom feed
---
 .../opac/controllers/RechercheController.php  | 72 ++++++++-----------
 library/Class/MoteurRecherche/Result.php      | 15 ++++
 library/Class/Notice.php                      |  1 +
 library/ZendAfi/Feed/SearchResult.php         | 45 +++++++++---
 library/storm                                 |  2 +-
 .../RechercheControllerAtomTest.php           | 43 ++++++++++-
 6 files changed, 124 insertions(+), 54 deletions(-)

diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php
index 8623affc2e0..b6c962e4782 100644
--- a/application/modules/opac/controllers/RechercheController.php
+++ b/application/modules/opac/controllers/RechercheController.php
@@ -42,16 +42,6 @@ class RechercheController extends ZendAfi_Controller_Action {
   }
 
 
-  public function _getListNotices($criteres_recherche, $req) {
-    if (!$req)
-      return [];
-
-    return Class_Notice::findAllByRequeteRecherche($req,
-                                                   $criteres_recherche->getPageSize(),
-                                                   $criteres_recherche->getPage());
-  }
-
-
   public function guideeAction() {
     $this->view->statut = 'guidee';
     $this->simpleAction();
@@ -82,17 +72,28 @@ class RechercheController extends ZendAfi_Controller_Action {
     }
 
     $criteres_recherche = $this->newCriteresRecherches($params);
+    if ($this->view->statut == 'guidee')
+      $criteres_recherche->updateRubrique('guidee');
+
+    $search_result = $this->moteur->lancerRecherche($criteres_recherche);
 
     if ('json' == $this->_getParam('format', '')) {
-      $this->_renderJsonResult($criteres_recherche);
+      $this->_renderJsonResult($search_result);
       return;
     }
 
     if ('atom' == $this->_getParam('format', '')) {
-      $this->_renderAtomResult($criteres_recherche);
+      $this->_renderAtomResult($search_result);
       return;
     }
 
+
+    $this->_renderHtmlResult($search_result);
+  }
+
+
+  protected function _renderHtmlResult($search_result) {
+    $criteres_recherche = $search_result->getCriteresRecherche();
     $this->getFrontController()->getRouter()->getCurrentRoute()
          ->match(str_replace(BASE_URL,
                              '',
@@ -100,22 +101,19 @@ class RechercheController extends ZendAfi_Controller_Action {
 
     $this->view->titre = $this->getTitreRechercheSimple($criteres_recherche);
 
-    if ($this->view->statut == 'guidee')
-      $criteres_recherche->updateRubrique('guidee');
 
     $this->view->current_domain = $criteres_recherche->getCatalogue();
 
     $this->showDomainBreadcrumbOnDomainBrowsing();
     $this->showCSVSearchResults($criteres_recherche);
-    $this->renderResultatRecherche($criteres_recherche);
+    $this->renderResultatRecherche($search_result);
   }
 
 
-  protected function _renderAtomResult($criteres_recherche) {
+  protected function _renderAtomResult($search_result) {
     $this->getHelper('ViewRenderer')->setNoRender();
 
-    $result = $this->moteur->lancerRecherche($criteres_recherche);
-    $builder = new ZendAfi_Feed_SearchResult($result);
+    $builder = new ZendAfi_Feed_SearchResult($search_result, $this->view);
     $feed = Zend_Feed::importBuilder($builder, 'atom');
 
     $this->_response->setHeader('Content-Type', 'application/atom+xml;charset=utf-8');
@@ -123,30 +121,20 @@ class RechercheController extends ZendAfi_Controller_Action {
   }
 
 
-  protected function _renderJsonResult($criteres_recherche) {
-    $datas = ['url' => Class_Url::absolute(),
-              'total' => 0,
-              'page_size' => $criteres_recherche->getPageSize(),
-              'page' => $this->_getParam('page', 1),
-              'records' => []];
-
-    $result = $this->moteur->lancerRecherche($criteres_recherche);
-    if ($result->isError()) {
-      $this->_helper->json($datas);
-      return;
-    }
-
-    $datas['total'] = $result->getRecordsCount();
-    $records = [];
-    $result->recordsDo(
-                       function($record) use (&$records) {
-                         $json = new Class_Notice_JsonVisitor();
-                         $record->acceptVisitor($json);
-                         $records[] = $json->data();
+  protected function _renderJsonResult($search_result) {
+    $records = $search_result
+      ->recordsCollect(
+                       function($record) {
+                         return $record
+                           ->acceptVisitor(new Class_Notice_JsonVisitor())
+                           ->data();
                        });
 
-    $datas['records'] = $records;
-    $this->_helper->json($datas);
+    $this->_helper->json(['url' => Class_Url::absolute(),
+                          'total' => $search_result->getRecordsCount(),
+                          'page_size' => $search_result->getPageSize(),
+                          'page' => $this->_getParam('page', 1),
+                          'records' => $records]);
   }
 
 
@@ -225,8 +213,8 @@ class RechercheController extends ZendAfi_Controller_Action {
   }
 
 
-  public function renderResultatRecherche($criteres_recherche) {
-    $search_result = $this->moteur->lancerRecherche($criteres_recherche);
+  public function renderResultatRecherche($search_result) {
+    $criteres_recherche = $search_result->getCriteresRecherche();
 
     $this->view->criteres_recherche = $criteres_recherche;
     $this->view->url_facette=$this->view->url($criteres_recherche->getUrlCriteresWithFacettes(),
diff --git a/library/Class/MoteurRecherche/Result.php b/library/Class/MoteurRecherche/Result.php
index 18b284fc6b6..c1360055e5c 100644
--- a/library/Class/MoteurRecherche/Result.php
+++ b/library/Class/MoteurRecherche/Result.php
@@ -46,11 +46,21 @@ class Class_MoteurRecherche_Result {
   }
 
 
+  public function getCriteresRecherche() {
+    return $this->_criteres_recherche;
+  }
+
+
   public function getUrl() {
     return $this->_criteres_recherche->getUrlCriteresWithFacettes();
   }
 
 
+  public function getPageSize() {
+    return $this->_criteres_recherche->getPageSize();
+  }
+
+
   public function fetchFacetsAndTags($preferences) {
     return $this->_search_engine->getFacettes($this->_facets_query,
                                               $preferences);
@@ -80,6 +90,11 @@ class Class_MoteurRecherche_Result {
   }
 
 
+  public function recordsCollect($closure) {
+    return array_map($closure, $this->fetchRecords());
+  }
+
+
   public function beError($message = '') {
     $this->_status = static::ERROR;
     $this->_error_message = $message;
diff --git a/library/Class/Notice.php b/library/Class/Notice.php
index 6911d951a3e..b9a3c0ef89e 100644
--- a/library/Class/Notice.php
+++ b/library/Class/Notice.php
@@ -1517,6 +1517,7 @@ class Class_Notice extends Storm_Model_Abstract {
     $visitor->visitVolume($this->getVolume());
     $visitor->visitCollections($this->getCollections());
     $visitor->visitGenres($this->getGenres());
+    return $visitor;
   }
 
 
diff --git a/library/ZendAfi/Feed/SearchResult.php b/library/ZendAfi/Feed/SearchResult.php
index 9624c5674bc..09ace546318 100644
--- a/library/ZendAfi/Feed/SearchResult.php
+++ b/library/ZendAfi/Feed/SearchResult.php
@@ -19,9 +19,15 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 class ZendAfi_Feed_SearchResult implements Zend_Feed_Builder_Interface {
-  use Trait_Translator;
+  use Trait_Translator, Trait_NoticeVisitor;
 
-  public function __construct($search_result) {
+  protected
+    $_entry,
+    $_entry_content,
+    $_view;
+
+  public function __construct($search_result, $view) {
+    $this->_view = $view;
     $this->_search_result = $search_result;
   }
 
@@ -35,19 +41,38 @@ class ZendAfi_Feed_SearchResult implements Zend_Feed_Builder_Interface {
 
 
   public function getEntries() {
-    $entries = [];
-    $this
+    return
+      $this
       ->_search_result
-      ->recordsDo(
-                  function($record) use (&$entries){
-                    $entries []= $this->newEntry($record);
-                  });
-    return $entries;
+      ->recordsCollect([$this, 'newEntry']);
   }
 
 
   public function newEntry($record) {
-    return new Zend_Feed_Builder_Entry($record->getTitrePrincipal(' - '), '', '');
+    $this->_entry = new Zend_Feed_Builder_Entry($record->getTitrePrincipal(' - '),
+                                                Class_Url::absolute($this->_view->urlNotice($record)),
+                                                null);
+    $record->acceptVisitor($this);
+    return $this->_entry;
+  }
+
+
+  public function visitVignette($url_vignette) {
+   $this->_entry->content =
+     $this->_view->tag('img', '', ['src' => $url_vignette,
+                                   'style' => 'float: left; margin-left: 10px'])
+     . $this->_entry->content;
+  }
+
+
+  public function visitResume($resume) {
+    $this->_entry->content = $resume;
+  }
+
+
+  public function visitAuteurs($auteurs) {
+    if ($auteurs)
+      $this->_entry->author = ['name' => $auteurs[0]];
   }
 }
 
diff --git a/library/storm b/library/storm
index 8f8662dfdb8..59632fdd95a 160000
--- a/library/storm
+++ b/library/storm
@@ -1 +1 @@
-Subproject commit 8f8662dfdb89bfbd8c247791e2d844db7dfb7880
+Subproject commit 59632fdd95aedf4eb58e9c0b571ef33c59c9f2aa
diff --git a/tests/application/modules/opac/controllers/RechercheControllerAtomTest.php b/tests/application/modules/opac/controllers/RechercheControllerAtomTest.php
index 1116f9b0c5e..d24bc3e940d 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerAtomTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerAtomTest.php
@@ -65,6 +65,7 @@ class RechercheControllerAtomWithTwoNoticesTestCase extends RechercheControllerA
                               'type_doc' => 1,
                               'unimarc' => $this->_unimarcFrom('harry_potter_ecole_sorciers.txt'),
                               'annee' => '1998',
+                              'resume' => 'Harry Potter il va tout dégommer',
                               'url_vignette' => 'http://ecx.images-amazon.com/images/I/51KVZWGHASL._SL160_.jpg',
                               'url_image' => 'http://ecx.images-amazon.com/images/I/51KVZWGHASL.jpg']);
 
@@ -118,8 +119,9 @@ class RechercheControllerAtomWithTwoNoticesTestCase extends RechercheControllerA
 
   /** @test */
   public function feedSelfLinkShouldBeAbsoluteSearchUrl() {
+    $url = Class_Url::absolute('/recherche/simple/expressionRecherche/Harry+Potter/tri/%2A/format/atom/page_size/5');
     $this->_xpath->assertXPath($this->_response->getBody(),
-                               '//atom:feed/atom:link[@rel="self"][@href="http://localhost/afi-opac3/recherche/simple/expressionRecherche/Harry+Potter/tri/%2A/format/atom/page_size/5"]');
+                               '//atom:feed/atom:link[@rel="self"][@href="' . $url . '"]');
   }
 
 
@@ -131,11 +133,50 @@ class RechercheControllerAtomWithTwoNoticesTestCase extends RechercheControllerA
   }
 
 
+
+  /** @test */
+  public function firstEntryContentShouldContainsThumbnail() {
+    $this->_xpath->assertXPathContentContains($this->_response->getBody(),
+                                              '//atom:entry/atom:content',
+                                              '<img src="http://ecx.images-amazon.com/images/I/51KVZWGHASL._SL160_.jpg"');
+  }
+
+
+  /** @test */
+  public function firstEntryContentShouldContainsSummary() {
+    $this->_xpath->assertXPathContentContains($this->_response->getBody(),
+                                              '//atom:entry/atom:content',
+                                              'Harry Potter il va tout dégommer');
+  }
+
+
+  /** @test */
+  public function firstEntryLinkShouldBePermalink() {
+    $this->_xpath->assertXPath($this->_response->getBody(),
+                               '//atom:entry/atom:link[contains(@href, "clef/HARRYPOTTERALECOLEDESSORCIERS--ROWLINGJ-1-GALLIMARDJEUNESSE-2011-1/id/234")]');
+  }
+
+
+  /** @test */
+  public function firstEntryAuthorShouldBeRowling() {
+    $this->_xpath->assertXPathContentContains($this->_response->getBody(),
+                                              '//atom:entry/atom:author/atom:name',
+                                              'Rowling');
+  }
+
+
   /** @test */
   public function feedSecondEntryShouldBeRendezVous() {
     $this->_xpath->assertXPathContentContains($this->_response->getBody(),
                                               '//atom:entry/atom:title',
                                               'Rendez-vous à Kiruna');
   }
+
+
+  /** @test */
+  public function entrySummaryShouldNotBePresent() {
+    $this->_xpath->assertNotXPath($this->_response->getBody(),
+                                  '//atom:entry/atom:summary');
+  }
 }
 ?>
\ No newline at end of file
-- 
GitLab