From 9918a5285192d31fcad1451a356ad8bd492a8f51 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Tue, 29 Jan 2019 15:10:58 +0100
Subject: [PATCH] dev #64573 reviews on record page

---
 .../opac/controllers/AbonneController.php     | 38 +++++++++++++
 library/Class/AvisNotice.php                  |  9 +++-
 library/ZendAfi/Auth/Others.php               |  8 +--
 .../Controller/Plugin/Manager/Manager.php     |  3 --
 library/ZendAfi/Form/Review.php               |  2 -
 .../Intonation/View}/Notice/Reviews.php       | 17 +++---
 tests/scenarios/Templates/TemplatesTest.php   | 54 +++++++++++++++++++
 7 files changed, 110 insertions(+), 21 deletions(-)
 rename library/{ZendAfi/View/Helper => templates/Intonation/View}/Notice/Reviews.php (85%)

diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php
index cfb930f0186..51bc9ae6a73 100644
--- a/application/modules/opac/controllers/AbonneController.php
+++ b/application/modules/opac/controllers/AbonneController.php
@@ -1317,4 +1317,42 @@ class AbonneController extends ZendAfi_Controller_Action {
     $this->renderPopupResult($this->_('Compléter votre adresse  email'),
                              $this->view->render('abonne/ajax-email.phtml'));
   }
+
+
+  public function reviewAction() {
+    if(!$this->_user)
+      return $this->_forward('popup-login', 'auth', 'opac');
+
+    if(!$this->_request->isPost())
+      return $this->_redirectToReferer();
+
+    $form = new ZendAfi_Form_Review;
+
+    $post = $this->_request->getPost();
+    $form->populate($post);
+
+    $rating = ($mine = Class_AvisNotice::find($this->_getParam('id')))
+      ? $mine
+      : new Class_AvisNotice;
+
+    $rating
+      ->beImportMode()
+      ->setUser($this->_user)
+      ->setIdNotice($this->_getParam('record_id'))
+      ->setClefOeuvre($this->_getParam('record_key'))
+      ->setUserRole($this->_user)
+      ->updateAttributes($post);
+
+    if (!$this->_getPost('entete') && !$this->_getPost('avis'))
+      $rating->setModerationOK();
+
+    if (!$form->isValidModelAndArray($rating, $post))
+      return $this->_redirectToReferer();
+
+    if (!$rating->save())
+      return false;
+
+    $this->_helper->notify($this->_('Votre avis a été pris en compte.'));
+    return $this->_redirectToReferer();
+  }
 }
\ No newline at end of file
diff --git a/library/Class/AvisNotice.php b/library/Class/AvisNotice.php
index 23bee9c3d7e..d23e5bb0fc9 100644
--- a/library/Class/AvisNotice.php
+++ b/library/Class/AvisNotice.php
@@ -235,7 +235,8 @@ class Class_AvisNotice  extends Storm_Model_Abstract {
                                           'type_doc' => null,
                                           'avis' => '',
                                           'entete' => '',
-                                          'note' => 0];
+                                          'note' => 0,
+                                          'id_user' => 0];
 
   protected $_notices;
   protected $_import_mode = false;
@@ -435,7 +436,13 @@ class Class_AvisNotice  extends Storm_Model_Abstract {
     if (null !== $user)
       $this->setUserKey($this->getLoader()->keyForUser($user));
 
+    $this->setUserRole($user);
+  }
+
+
+  public function setUserRole($user) {
     $this->setAbonOuBib((null !== $user) && $user->isBibliothecaire() ? 1 : 0);
+    return $this;
   }
 
 
diff --git a/library/ZendAfi/Auth/Others.php b/library/ZendAfi/Auth/Others.php
index d7b5add8929..19a0f9fdcdf 100644
--- a/library/ZendAfi/Auth/Others.php
+++ b/library/ZendAfi/Auth/Others.php
@@ -159,13 +159,7 @@ class ZendAfi_Auth_Others {
 
 
   protected function _tryLogOther($other) {
-    $auth = ZendAfi_Auth::getInstance();
-
-    if ($other->isAbonne())
-      return $auth
-        ->authenticateLoginPassword($other->getLogin(), $other->getPassword());
-
-    $auth->logUser($other);
+    $auth = ZendAfi_Auth::getInstance()->logUser($other);
     return true;
   }
 
diff --git a/library/ZendAfi/Controller/Plugin/Manager/Manager.php b/library/ZendAfi/Controller/Plugin/Manager/Manager.php
index 3312ad59107..27045cde4e5 100644
--- a/library/ZendAfi/Controller/Plugin/Manager/Manager.php
+++ b/library/ZendAfi/Controller/Plugin/Manager/Manager.php
@@ -268,9 +268,6 @@ class ZendAfi_Controller_Plugin_Manager_Manager extends ZendAfi_Controller_Plugi
       $form->populateFormFromGroupsDefinitions($this->_getDisplayGroups());
     }
 
-    $form = $form->populate($this->_request->getParams());
-    $form = $form->populate($this->_getFormValues($model));
-
     return $form
       ->populate($this->_request->getParams())
       ->populate($this->_getFormValues($model));
diff --git a/library/ZendAfi/Form/Review.php b/library/ZendAfi/Form/Review.php
index bdd1de09ae0..40901a8259c 100644
--- a/library/ZendAfi/Form/Review.php
+++ b/library/ZendAfi/Form/Review.php
@@ -37,8 +37,6 @@ class ZendAfi_Form_Review extends ZendAfi_Form {
       ->addElement('textarea',
                    'avis',
                    ['label' => $this->_('Texte'),
-                    'required' => true,
-                    'allowEmpty' => false,
                     'rows' => 3,
                     'cols' => 25])
 
diff --git a/library/ZendAfi/View/Helper/Notice/Reviews.php b/library/templates/Intonation/View/Notice/Reviews.php
similarity index 85%
rename from library/ZendAfi/View/Helper/Notice/Reviews.php
rename to library/templates/Intonation/View/Notice/Reviews.php
index 842bc827647..5eacc258258 100644
--- a/library/ZendAfi/View/Helper/Notice/Reviews.php
+++ b/library/templates/Intonation/View/Notice/Reviews.php
@@ -20,7 +20,7 @@
  */
 
 
-class ZendAfi_View_Helper_Notice_Reviews extends ZendAfi_View_Helper_BaseHelper {
+class Intonation_View_Notice_Reviews extends ZendAfi_View_Helper_BaseHelper {
 
   protected $_record;
 
@@ -32,7 +32,7 @@ class ZendAfi_View_Helper_Notice_Reviews extends ZendAfi_View_Helper_BaseHelper
              $this->_renderUsers(),
              $this->_mine()];
 
-    return $this->view->grid(implode($html), [], ['class' => 'justify-content-center']);
+    return $this->view->grid(implode($html), [], ['class' => '']);
   }
 
 
@@ -76,15 +76,16 @@ class ZendAfi_View_Helper_Notice_Reviews extends ZendAfi_View_Helper_BaseHelper
 
     $mine = ($mine = $me->getFirstAvisByIdNotice($this->_record->getId()))
       ? $mine
-      : new Class_Avis;
+      : new Class_AvisNotice;
 
-    $params = $mine->isNew()
-      ? ['action' => 'add-review']
-      : ['action' => 'edit-review',
-         'id' => $mine->getId()];
+    $params =  ['controller' => 'abonne',
+                'action' => 'review',
+                'record_id' => $this->_record->getId(),
+                'record_key' => $this->_record->getClefOeuvre(),
+                'id' => $mine->getId()];
 
     $form = ZendAfi_Form_Review::newWith($mine->toArray())
-      ->setAction($this->view->url($params));
+      ->setAction($this->view->url($params,null,true));
 
     return $this->_div(['class' => 'col-12 col-md-6 col-xl-4'],
                        $this->_tag('h3', $this->_('Votre avis'), ['class' => 'card-title'])
diff --git a/tests/scenarios/Templates/TemplatesTest.php b/tests/scenarios/Templates/TemplatesTest.php
index 7c7848fd460..f0ecab1f7d4 100644
--- a/tests/scenarios/Templates/TemplatesTest.php
+++ b/tests/scenarios/Templates/TemplatesTest.php
@@ -1759,6 +1759,12 @@ class TemplatesViewRecordTest extends TemplatesIntonationTestCase {
   }
 
 
+  /** @test */
+  public function ratingShouldBeDisplayed() {
+    $this->assertXPathContentContains('//');
+  }
+
+
   /** @test */
   public function bagesShouldContainsTopicRoman() {
     $this->assertXPathContentContains('//a[contains(@class, "badge")][contains(@href, "/recherche/simple/code_rebond/G13")]', 'Roman');
@@ -1788,4 +1794,52 @@ class TemplatesDispatchNoticeAjaxResourcesTest extends TemplatesIntonationTestCa
     $this->dispatch('/noticeajax/resources/id/34/id_profil/72');
     $this->assertXPath('//div', $this->_response->getBody());
   }
+}
+
+
+
+
+class TemplatesAddReviewTest extends TemplatesIntonationTestCase {
+
+  protected $_review;
+
+
+  public function setUp() {
+    parent::setUp();
+    $this->postDispatch('/opac/abonne/review/record_id/456/record_key/mykey' ,
+                        ['note' => 5,
+                         'entete' => '',
+                         'avis' => '']);
+
+    $this->_review = Class_AvisNotice::find(1);
+  }
+
+
+  /** @test */
+  public function noEnteteRatingShouldHaveBeenCreated() {
+    $this->assertNotNull($this->_review);
+  }
+
+
+  /** @test */
+  public function ratingShouldBeSetToUser() {
+    $this->assertEquals(666, $this->_review->getIdUser());
+  }
+
+
+  /** @test */
+  public function ratingShouldBeSetToNotice() {
+    $this->assertEquals(456,$this->_review->getIdNotice());
+  }
+
+  /** @test */
+  public function ratingShouldBeAutoModerated() {
+    $this->assertEquals(Trait_Avis::$STATUT_OK, $this->_review->getStatut());
+  }
+
+  /** @test */
+  public function ratingRecordKeyShouldBeSaved() {
+    $this->assertEquals('mykey', $this->_review->getClefOeuvre());
+  }
+
 }
\ No newline at end of file
-- 
GitLab