From 28afedf85651cfe10f0a8e610728aeb1979cc829 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Fri, 25 Sep 2015 14:27:15 +0200
Subject: [PATCH] hotline #23808 : casqy avis : add flags to display avis

---
 .../opac/controllers/BlogController.php       |   1 +
 .../opac/controllers/RssController.php        |   5 +-
 library/Class/AvisNotice.php                  |  16 +-
 library/Class/Users.php                       |   1 +
 library/ZendAfi/View/Helper/Avis.php          |  17 ++-
 .../controllers/AbonneControllerAvisTest.php  | 141 +++++++++++-------
 6 files changed, 108 insertions(+), 73 deletions(-)

diff --git a/application/modules/opac/controllers/BlogController.php b/application/modules/opac/controllers/BlogController.php
index 27a13c20daf..7337599ce47 100644
--- a/application/modules/opac/controllers/BlogController.php
+++ b/application/modules/opac/controllers/BlogController.php
@@ -80,6 +80,7 @@ class BlogController extends ZendAfi_Controller_Action {
   public function lastcritiqueAction()  {
     $nb_avis = (int)$this->_getParam('nb', 20);
     $liste_avis = Class_AvisNotice::findAllBy(['order' => 'date_avis desc',
+                                               'flags' => Class_AvisNotice::NO_FLAG,
                                                'limit' => $nb_avis]);
 
     $this->view->nb_aff = $nb_avis;
diff --git a/application/modules/opac/controllers/RssController.php b/application/modules/opac/controllers/RssController.php
index 8efa311b863..3ddbb1cd58b 100644
--- a/application/modules/opac/controllers/RssController.php
+++ b/application/modules/opac/controllers/RssController.php
@@ -301,10 +301,11 @@ class RssController extends Zend_Controller_Action
 
     $avis_helper = $this->view->getHelper('Avis');
 
-    $items = array();
+    $items = [];
     foreach($user_avis as $avis)  {
       $desc = $avis_helper->avis($avis, 0, $vignette_link_to_avis = true);
-
+      if ($desc=='')
+        continue;
       $items [] = ['titre' => $avis->getEntete(),
                   'lien' => $this->view->absoluteUrl([
                     'controller' => 'blog',
diff --git a/library/Class/AvisNotice.php b/library/Class/AvisNotice.php
index bd265286f71..6de4e750d5a 100644
--- a/library/Class/AvisNotice.php
+++ b/library/Class/AvisNotice.php
@@ -73,12 +73,15 @@ class AvisNoticeLoader extends Storm_Model_Loader {
     - id_panier: le panier de notices dont on veut récupérer les avis
     On prends les avis soit du catalogue, soit du panier, soit les dernier avis
    */
-  public function getAvisFromPreferences($preferences, $limit_page=null) {
+  public function getAvisFromPreferences($preferences, $limit_page=null, $include_orphan = false) {
     $params = ['order' => 'DATE_AVIS DESC'];
-
+    xdebug_break();
+    if (!$include_orphan)
+      $params['flags'] = Class_AvisNotice::NO_FLAG;
     $preferences = array_merge(['id_panier' => 0,
                                 'id_catalogue' => 0,
-                                'abon_ou_bib' => 'all'],
+                                'abon_ou_bib' => 'all',
+                                ],
                                $preferences);
 
     $id_panier = $preferences['id_panier'];
@@ -94,7 +97,7 @@ class AvisNoticeLoader extends Storm_Model_Loader {
     if ($limit_page)
       $params['limitPage'] = $limit_page;
 
-    return Class_AvisNotice::getLoader()->findAllBy($params);
+    return Class_AvisNotice::findAllBy($params);
   }
 
 
@@ -109,6 +112,8 @@ class AvisNoticeLoader extends Storm_Model_Loader {
 
 class Class_AvisNotice  extends Storm_Model_Abstract {
   use Trait_Avis, Trait_Translator;
+  const NO_FLAG=0;
+  const ORPHAN_FLAG=1;
 
   protected $_loader_class = 'AvisNoticeLoader';
   protected $_table_name = 'notices_avis';
@@ -118,6 +123,7 @@ class Class_AvisNotice  extends Storm_Model_Abstract {
                                        'referenced_in' => 'id_user']];
 
   protected $_default_attribute_values = ['statut' => 0,
+                                          'flags' => self::NO_FLAG,
                                           'clef_oeuvre' => '',
                                           'date_avis' => null];
 
@@ -175,8 +181,6 @@ class Class_AvisNotice  extends Storm_Model_Abstract {
 
 
   public function getNotices() {
-    if (!isset($this->_notices))
-      $this->_notices = $this->_findNoticesByClefOeuvre();
     return $this->_notices;
   }
 
diff --git a/library/Class/Users.php b/library/Class/Users.php
index f5dca6e7d9f..78458c16c8c 100644
--- a/library/Class/Users.php
+++ b/library/Class/Users.php
@@ -627,6 +627,7 @@ class Class_Users extends Storm_Model_Abstract {
   public function getLastAvis() {
     return Class_AvisNotice::findAllBy(['id_user' => $this->getId(),
                                         'order' => 'date_avis desc',
+                                        'flags' => Class_AvisNotice::NO_FLAG,
                                         'limit' =>  10]);
   }
 
diff --git a/library/ZendAfi/View/Helper/Avis.php b/library/ZendAfi/View/Helper/Avis.php
index 654ad437334..7ce3e492a7e 100644
--- a/library/ZendAfi/View/Helper/Avis.php
+++ b/library/ZendAfi/View/Helper/Avis.php
@@ -63,7 +63,7 @@ class ZendAfi_View_Helper_Avis extends ZendAfi_View_Helper_BaseHelper {
       $this->setLimitNbWord($limit_nb_word);
 
     return ($avis->isAvisNotice()) ?
-      $this->avisNotice($avis, $limit_nb_word, $vignette_link_to_avis) :
+      $this->avisNotice($avis) :
       $this->avisCms($avis);
   }
 
@@ -86,17 +86,18 @@ class ZendAfi_View_Helper_Avis extends ZendAfi_View_Helper_BaseHelper {
   }
 
 
-  protected function avisNotice($avis, $vignette_link_to_avis) {
+  protected function avisNotice($avis) {
     $url_vignette = URL_ADMIN_IMG . 'supports/vignette_vide.gif';
     $title = $this->_('Oeuvre non trouvée');
-    if (null !== $notice = $avis->getFirstNotice()) {
-      $title = $notice->getTitrePrincipal();
+    $notice = $avis->getFirstNotice();
+    if ((null==$notice))
+      return '';
+    $title = $notice->getTitrePrincipal();
 
-      if (strlen($auteur_principal = $notice->getAuteurPrincipal()) > 0)
-        $title .= ' (' . $auteur_principal . ')';
+    if (strlen($auteur_principal = $notice->getAuteurPrincipal()) > 0)
+      $title .= ' (' . $auteur_principal . ')';
 
-      $url_vignette = $notice->fetchUrlVignette();
-    }
+    $url_vignette = $notice->fetchUrlVignette();
 
     $content = $this->contenu_avis($avis);
 
diff --git a/tests/application/modules/opac/controllers/AbonneControllerAvisTest.php b/tests/application/modules/opac/controllers/AbonneControllerAvisTest.php
index 0fcbf3f2420..a17bc3dc6cc 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerAvisTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerAvisTest.php
@@ -21,6 +21,8 @@
 require_once 'AbstractControllerTestCase.php';
 
 abstract class AbonneFlorenceIsLoggedControllerTestCase extends AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
   protected function _initProfilHook($profil) {
     $profil
       ->setBrowser('opac')
@@ -30,19 +32,23 @@ abstract class AbonneFlorenceIsLoggedControllerTestCase extends AbstractControll
 
   public function setUp() {
     parent::setUp();
-    Class_Users::beVolatile();
-    Class_AvisNotice::beVolatile();
-    Class_Notice::beVolatile();
-    $this->florence = Class_Users::newInstanceWithId(123456)
-      ->setPseudo('FloFlo')
-      ->setRoleLevel(2)
-      ->setRole('abonne_sigb')
-      ->setLogin('florence')
-      ->setPassword('caramel')
-      ->setIdSite(1)
-      ->setIdabon('00123')
-      ->setFicheSIGB(['type_com' => 0]);
-    $this->florence->save();
+    $this->fixture('Class_AdminVar',
+                   ['id' => 'AVIS_MIN_SAISIE',
+                    'valeur' => 0]);
+    $this->fixture('Class_AdminVar',
+                   ['id' => 'AVIS_MAX_SAISIE',
+                    'valeur' => 1000]);
+
+    $this->florence = $this->fixture('Class_Users',
+                                     ['id' => 123456,
+                                      'pseudo' => 'FloFlo',
+                                      'role_level' => 2,
+                                      'role' => 'abonne_sigb',
+                                      'login' => 'florence',
+                                      'password' => 'caramel',
+                                      'id_site' => 1,
+                                      'idabon' => '00123',
+                                      'fiche_sigb' => ['type_com' => 0]]);
 
     ZendAfi_Auth::getInstance()->logUser($this->florence);
 
@@ -207,12 +213,12 @@ class AbonneControllerAvisNoticeWithAvisTest extends AbonneControllerAvisTestCas
   public function setUp() {
     parent::setUp();
 
-    $avis = Class_AvisNotice::newInstanceWithId(12, ['Avis' => 'Excellent livre',
-                                                     'Entete' => 'Le sorcier super mimi',
-                                                     'note' => 4,
-                                                     'clef_oeuvre' => 'POTTER',
-                                                     'user' => $this->florence]);
-    $avis->save();
+    $avis = $this->fixture('Class_AvisNotice', ['id' => 12,
+                                                'Avis' => 'Excellent livre',
+                                                'Entete' => 'Le sorcier super mimi',
+                                                'note' => 4,
+                                                'clef_oeuvre' => 'POTTER',
+                                                'user' => $this->florence]);
     $this->dispatch('/opac/abonne/avis/id_notice/53');
     $this->_xpath = new Storm_Test_XPath();
     $this->_json = json_decode($this->_response->getBody());
@@ -250,12 +256,6 @@ abstract class AvisControllersFixturesTestCase extends AbonneFlorenceIsLoggedCon
   public function setUp() {
     parent::setUp();
 
-    $this->fixture('Class_AdminVar',
-                   ['id' => 'AVIS_MIN_SAISIE',
-                    'valeur' => 0]);
-    $this->fixture('Class_AdminVar',
-                   ['id' => 'AVIS_MAX_SAISIE',
-                    'valeur' => 1000]);
 
     $this->fixture('Class_AdminVar',
                    ['id' => 'MODO_AVIS',
@@ -270,6 +270,18 @@ abstract class AvisControllersFixturesTestCase extends AbonneFlorenceIsLoggedCon
                     'valeur' => '54QCJRHZ31IPBV7GW3DKBPUYYP579A14']);
 
 
+    $lost=$this->fixture('Class_AvisNotice', ['id' => 178,
+                                              'entete' => "Lost highway",
+                                              'clef_oeuvre' => 'LOST-LOST',
+                                              'avis' => 'Oh po po !',
+                                              'note' => 5,
+                                              'date_avis' => '2015-03-18 13:00:00',
+                                              'user' => $this->florence,
+                                              'statut' => 1,
+                                              'flags' =>  Class_AvisNotice::ORPHAN_FLAG,
+                                              'abon_ou_bib'=>1,
+                                              'id_notice' => 30]);
+
     $this->millenium = $this->fixture('Class_Notice', ['id' => 816,
                                                        'titre_principal' => 'Millenium',
                                                        'clef_alpha' => 'MILLENIUM',
@@ -295,14 +307,14 @@ abstract class AvisControllersFixturesTestCase extends AbonneFlorenceIsLoggedCon
 
     $this->avis_millenium =  $this->fixture('Class_AvisNotice', ['id' => 13,
                                                                  'entete' => "J'adore",
-                                                                      'avis' => '<div><ul><li>Suspense Intense !</li><li>Suspense Intense !</li></ul></div>',
-                                                                      'note' => 5,
-                                                                      'date_avis' => '2011-03-18 13:00:00',
-                                                                      'user' => $this->florence,
-                                                                      'statut' => 0,
-                                                                      'abon_ou_bib'=>1 ,
-                                                                      'notices' => [$this->millenium,
-                                                                                    $this->millenium_with_vignette] ]);
+                                                                 'avis' => '<div><ul><li>Suspense Intense !</li><li>Suspense Intense !</li></ul></div>',
+                                                                 'note' => 5,
+                                                                 'date_avis' => '2011-03-18 13:00:00',
+                                                                 'user' => $this->florence,
+                                                                 'statut' => 0,
+                                                                 'abon_ou_bib'=>1 ,
+                                                                 'notices' => [$this->millenium,
+                                                                               $this->millenium_with_vignette] ]);
 
     $this->potter = $this->fixture('Class_Notice',
                                    ['id'=> 22,
@@ -331,6 +343,7 @@ abstract class AvisControllersFixturesTestCase extends AbonneFlorenceIsLoggedCon
                                          'user'=>null,
                                          'statut' => 1,
                                          'abon_out_bib' => 1,
+                                         'flags' => 1,
                                          'notices' => []]);
 
 
@@ -354,6 +367,15 @@ abstract class AvisControllersFixturesTestCase extends AbonneFlorenceIsLoggedCon
                                              'abon_out_bib' => 1,
                                              'notices' =>[$this->millenium]
                                             ]);
+    Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
+      ->whenCalled('findAllBy')
+      ->with(['where' => 'clef_oeuvre like "LOST-%"'])
+      ->answers(null)
+      ->whenCalled('findAllBy')
+      ->with(['where' => 'clef_oeuvre like "MILLENIUM-%"'])
+      ->answers([$this->millenium,$this->millenium_with_vignette])
+      ;
+
 
   }
 }
@@ -387,9 +409,9 @@ class AbonneControllerAvisViewFicheWithAvisTest extends AvisControllersFixturesT
   }
 
   /** @test */
-  public function pageShouldDisplayVousAvezRedigeDeuxAvis() {
+  public function pageShouldDisplayVousAvezRedigeTroiAvis() {
     $this->assertXPathContentContains('//a',
-                                      'Vous avez rédigé 2 avis',
+                                      'Vous avez rédigé 3 avis',
                                       $this->_response->getBody());
   }
 
@@ -398,6 +420,8 @@ class AbonneControllerAvisViewFicheWithAvisTest extends AvisControllersFixturesT
   public function linkAvisShouldGoToAbonneViewAvis() {
     $this->assertXPath('//a[contains(@href, "/abonne/viewavis/id/123456")]');
   }
+
+
 }
 
 
@@ -406,16 +430,25 @@ class AbonneControllerAvisViewFicheWithAvisTest extends AvisControllersFixturesT
 class AbonneControllerAvisBlogControllerViewAuteurActionTest extends AvisControllersFixturesTestCase {
   public function setUp() {
     parent::setUp();
+
+
     $this->dispatch('/opac/blog/viewauteur/id/123456');
   }
 
+
+  /** @test */
+  public function AvisWithoutNoticeShouldBeDisplayedForAuthor() {
+    $this->assertXPathContentContains("//div[@class='critique'][1]//a", 'Lost highway',$this->_response->getBody());
+  }
+
+
   public function testPageIsRendered() {
     $this->assertController('blog');
     $this->assertAction('viewauteur');
   }
 
   public function testMilleniumIsHere() {
-    $this->assertXPathContentContains("//div[@class='critique'][1]//h2", 'Millenium (Stieg Larsson)');
+    $this->assertXPathContentContains("//div[@class='critique'][2]//h2", 'Millenium (Stieg Larsson)',$this->_response->getBody());
   }
 
 
@@ -426,7 +459,7 @@ class AbonneControllerAvisBlogControllerViewAuteurActionTest extends AvisControl
   }
 
   public function testPotterIsHere() {
-    $this->assertXPathContentContains("//div[@class='critique'][2]//h2", 'Potter et la chambre des secrets');
+    $this->assertXPathContentContains("//div[@class='critique'][3]//h2", 'Potter et la chambre des secrets',$this->_response->getBody());
   }
 
   public function testDeleteMilleniumButtonPresent() {
@@ -511,7 +544,7 @@ class AbonneControllerAvisRssControllerViewAvisUserTest extends AvisControllersF
 
   /** @test */
   function firstItemTitleShouldBeJAdore() {
-    $this->assertXPathContentContains('//channel/item/title', "J'adore");
+    $this->assertXPathContentContains('//channel/item/title', "J'adore",$this->_response->getBody());
   }
 
 
@@ -608,8 +641,8 @@ class AbonneControllerAvisBlogControllerLastCritiquesTest extends AvisController
 
 
   /** @test */
-  public function lostRratingShouldBeAtThirdPlace() {
-    $this->assertXPathContentContains("//div[@class='critique'][3]//h2", 'Oeuvre non trouvée');
+  public function lostRratingShouldNotBeDIsplayed() {
+    $this->assertNotXPathContentContains("//div[@class='critique'][3]//h2", 'Oeuvre non trouvée');
   }
 }
 
@@ -619,21 +652,16 @@ abstract class ModuleSelectionCritiquesTestCase extends AvisControllersFixturesT
   public function setUp() {
     parent::setUp();
 
-    $preferences = array('modules' => array(3 => array('division' => 2,
-                                                       'type_module' => 'CRITIQUES',
-                                                       'preferences' => array('titre' => 'Coups de coeur'
-                                                       ))));
-    $profil = Class_Profil::getLoader()
-      ->find(2)
-      ->setCfgModules( ['blog' =>  ['viewcritiques' => ['nb_display' => 2]]])
-      ->setCfgAccueil($preferences);
-
-    $this->_generateLoaderFor('Class_AvisNotice', ['getAvisFromPreferences'])
-//         ->expects($this->once())
-         ->method('getAvisFromPreferences')
-         ->will($this->returnValue([$this->avis_millenium,
-                                    $this->avis_potter,
-                                    $this->avis_lost]));
+    $preferences = ['modules' => [3 => ['division' => 2,
+                                        'type_module' => 'CRITIQUES',
+                                        'preferences' => ['titre' => 'Coups de coeur'
+                                        ]
+        ]]];
+    $profil = $this->fixture('Class_Profil',
+                             ['id'=>2,
+                              'cfg_modules' => ['blog' =>  ['viewcritiques' => ['nb_display' => 2]]],
+                              'cfg_accueil' => $preferences]);
+
   }
 }
 
@@ -647,7 +675,7 @@ class AbonneControllerAvisBlogControllerViewCritiquesTest extends ModuleSelectio
   }
 
   public function testMilleniumIsHere() {
-    $this->assertQueryContentContains('h2', 'Millenium');
+    $this->assertQueryContentContains('h2', 'Millenium',$this->_response->getBody());
   }
 
   public function testPotterIsHere() {
@@ -765,7 +793,6 @@ class AbonneControllerAvisBlogControllerViewReadAvisTest extends  AbonneFlorence
 class AbonneControllerEditAvisNoticeNotFoundActionTest extends AbstractControllerTestCase {
   public function setUp() {
     parent::setUp();
-    Class_AvisNotice::beVolatile();
     $this->dispatch('/opac/abonne/editavisnotice/id/54', true);
   }
 
-- 
GitLab