From ea953fab2094d17a41ab416aaee768fe11a57459 Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Wed, 28 Nov 2018 11:07:20 +0100
Subject: [PATCH] dev #82369 display cart authors on cart selection popup

---
 .../views/scripts/panier/add-selection.phtml  |  4 +-
 library/Class/Catalogue.php                   |  4 +-
 library/Class/Users.php                       |  4 +-
 .../AbonneControllerPaniersTest.php           | 84 ++++++++++++++-----
 .../SearchSelection/SearchSelectionTest.php   | 43 +++++++++-
 5 files changed, 115 insertions(+), 24 deletions(-)

diff --git a/application/modules/opac/views/scripts/panier/add-selection.phtml b/application/modules/opac/views/scripts/panier/add-selection.phtml
index 16548345ae6..83cd1ab4b51 100644
--- a/application/modules/opac/views/scripts/panier/add-selection.phtml
+++ b/application/modules/opac/views/scripts/panier/add-selection.phtml
@@ -18,7 +18,9 @@ $actions = $this->tag('div', $this->tagAnchor($this->url(['controller' => 'panie
 echo $this->tag('div',
                 $this->tag('p',
                            $this->_('Ajouter ma sélection au panier "%s" ?',
-                                    $this->panier->getLibelle()),
+                                    (Class_Users::isCurrentUserCanAccesBackend()
+                                      ? $this->panier->getLibelleForAdmins()
+                                      : $this->panier->getLibelle())),
                            ['class' => 'center'])
                                      . $actions);
 
diff --git a/library/Class/Catalogue.php b/library/Class/Catalogue.php
index 9c0f019e22a..d6d2b551d0c 100644
--- a/library/Class/Catalogue.php
+++ b/library/Class/Catalogue.php
@@ -1028,7 +1028,9 @@ class Class_Catalogue extends Storm_Model_Abstract {
     }
     $paniers=$this->getPanierNotices();
     foreach ($paniers as $panier) {
-      $data_panier[]=$panier->toDataForJson();
+      $json = $panier->toDataForJson();
+      $json['label'] = htmlspecialchars($panier->getLibelleForAdmins());
+      $data_panier[] = $json;
     }
 
     return $this->toDataForJsonWithCategoriesItems($data_domaines, $data_panier, $options);
diff --git a/library/Class/Users.php b/library/Class/Users.php
index 8ab61505e43..b1a9513cad1 100644
--- a/library/Class/Users.php
+++ b/library/Class/Users.php
@@ -1217,7 +1217,9 @@ class Class_Users extends Storm_Model_Abstract {
 
 
   public function getPanierCourant() {
-    return ($panier_courant = parent::getPanierCourant()) ? $panier_courant : Class_PanierNotice::newForUser($this);
+    return ($panier_courant = parent::getPanierCourant())
+      ? $panier_courant
+      : Class_PanierNotice::newForUser($this);
   }
 
 
diff --git a/tests/application/modules/opac/controllers/AbonneControllerPaniersTest.php b/tests/application/modules/opac/controllers/AbonneControllerPaniersTest.php
index 95d46b10961..007beb22092 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerPaniersTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerPaniersTest.php
@@ -16,41 +16,69 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class AbonneControllerPaniersForMarcusTest extends AbstractControllerTestCase {
-  protected 
+  protected
+    $_storm_default_to_volatile = true,
     $marcus,
-    $_xpath, 
+    $_xpath,
     $_json,
     $panier_bd;
 
   public function setup() {
     parent::setup();
-        
-    $this->marcus = Class_Users::newInstanceWithId(45)
-      ->setIdabon(45)
-      ->setPrenom('James')
-      ->setNom('Bond');
+
+    $this->marcus = $this->fixture('Class_Users',
+                                   ['id' => 45,
+                                    'idabon' => 45,
+                                    'login' => 'marcus',
+                                    'password' => 'secret',
+                                    'prenom' => 'marcus',
+                                    'nom' => 'bond']);
+
+    $marcel = $this->fixture('Class_Users',
+                             ['id' => 47,
+                              'login' => 'marcel',
+                              'password' => 'secret',
+                              'prenom' => 'marcel',
+                              'nom' => 'doob']);
+    $marcel->beAdminPortail()->assertSave();
+
+    $coups_coeur_marcel = $this->fixture('Class_PanierNotice',
+                                         ['id' => 4,
+                                          'id_panier' => 2,
+                                          'libelle' => 'Mes coups de coeur',
+                                          'date_maj' => '10/02/2011',
+                                          'notices' => 'POMME API',
+                                          'user' => $marcel]);
+
+    $domaine_coups_coeur = $this->fixture('Class_Catalogue',
+                                          ['id' => 1,
+                                           'libelle' => 'Coups de coeur',
+                                           'panier_notices' => [$coups_coeur_marcel]]);
+
 
 
     ZendAfi_Auth::getInstance()->logUser($this->marcus);
 
-    $this->panier_bd = Class_PanierNotice::newInstanceWithId(2,
-                                                             ['id_panier' => 1,
-                                                              'libelle' => 'Mes BD',
-                                                              'date_maj' => '10/02/2011',
-                                                              'notices' => 'COMBAT ORDINAIRE;BLACKSAD',
-                                                              'user' => $this->marcus]);
-    
+    $this->panier_bd = $this->fixture('Class_PanierNotice',
+                                      ['id' => 2,
+                                       'id_panier' => 1,
+                                       'libelle' => 'Mes BD',
+                                       'date_maj' => '10/02/2011',
+                                       'notices' => 'COMBAT ORDINAIRE;BLACKSAD',
+                                       'user' => $this->marcus]);
+
+
     Storm_Test_ObjectWrapper::onLoaderOfModel('Class_PanierNotice')
       ->whenCalled('findAllBy')
       ->answers([$this->panier_bd]);
 
     Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Catalogue')
       ->whenCalled('findTopCatalogues')
-      ->answers([]);
+      ->answers([$domaine_coups_coeur]);
   }
 
 
@@ -61,7 +89,7 @@ class AbonneControllerPaniersForMarcusTest extends AbstractControllerTestCase {
     $this->dispatch('abonne/get-paniers.json',true);
 
     $this->assertJsonStringEqualsJsonString(
-      $this->_response->getBody(), 
+      $this->_response->getBody(),
       json_encode([
                     ["id" => "panier_for_user",
                      "label" => "Mes paniers",
@@ -71,9 +99,25 @@ class AbonneControllerPaniersForMarcusTest extends AbstractControllerTestCase {
                         "label" => "Mes BD",
                         "options" => ["ico" => BASE_URL."/public/admin/images/picto/paniers_16.png"]]],
                      "options" => ["multipleSelection" => false]],
-                    ["id" => "domaines_paniers",
+
+                    [
+                     "id" => "domaines_paniers",
                      "label" => "Domaines",
-                     "categories" => [],
+                     "categories" => [
+                                      ['id' => 1,
+                                       'label' => 'Coups de coeur',
+                                       'categories' => [],
+                                       'items' => [
+                                                   ['id' => 4,
+                                                    'label' => 'Mes coups de coeur - marcel doob',
+                                                    'options' => [
+                                                                  'ico' => BASE_URL.'/public/admin/images/picto/paniers_16.png']]
+                                       ],
+
+                                       'options' => ['ico' => BASE_URL.'/public/admin/images/picto/domaines_16.png',
+                                                                 'removeCheckbox' => true]
+                                      ]
+                     ],
                      "items" => [],
                      "options" => ["ico" => BASE_URL."/public/admin/images/picto/domaines_16.png",
                                    "multipleSelection" => false]]]),
@@ -88,7 +132,7 @@ class AbonneControllerPaniersForMarcusTest extends AbstractControllerTestCase {
     $this->dispatch('abonne/get-paniers.json',true);
 
     $this->assertJsonStringEqualsJsonString(
-      $this->_response->getBody(), 
+      $this->_response->getBody(),
       json_encode([
                     ["id" => "panier_for_user",
                      "label" => "Mes paniers",
diff --git a/tests/scenarios/SearchSelection/SearchSelectionTest.php b/tests/scenarios/SearchSelection/SearchSelectionTest.php
index b3e8038cb0c..0e1c935438f 100644
--- a/tests/scenarios/SearchSelection/SearchSelectionTest.php
+++ b/tests/scenarios/SearchSelection/SearchSelectionTest.php
@@ -441,7 +441,6 @@ abstract class SearchSelectionAddToCartWithUserTestCase extends SearchSelectionT
                                   'libelle' => 'my cart',
                                   'date_maj' => '25/05/2010',
                                   'notices' => '',
-                                  'user' => $this->user,
                                   'catalogues' => [$histoire]]);
 
     $this->user = $this->fixture('Class_Users',
@@ -450,6 +449,7 @@ abstract class SearchSelectionAddToCartWithUserTestCase extends SearchSelectionT
                                   'nom' => 'john',
                                   'login' => 'jzorn',
                                   'password' => '123',
+                                  'panier_notices' => [$this->cart],
                                   'panier_courant' => $this->cart]);
 
     ZendAfi_Auth::getInstance()->logUser($this->user);
@@ -501,6 +501,47 @@ class SearchSelectionAddToCartTest extends SearchSelectionAddToCartWithUserTestC
 
 
 
+class SearchSelectionAddToCartWithOtherUserCartAsCurrentCartTest extends SearchSelectionAddToCartWithUserTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $other_user = $this->fixture('Class_Users',
+                                 ['id' => 29,
+                                  'nom' => 'mero',
+                                  'prenom' => 'cali',
+                                  'login' => 'cali',
+                                  'password' => 'jedispas']);
+
+    $other_cart = $this->fixture('Class_PanierNotice',
+                                 ['id' => 4,
+                                  'id_panier' => 4,
+                                  'libelle' => 'favorites',
+                                  'date_maj' => '25/05/2010',
+                                  'notices' => '',
+                                  'user' => $other_user,
+                                  'catalogues' => [Class_Catalogue::find(97)]]);
+
+
+    $this->user
+      ->beAdminPortail()
+      ->setPanierCourant($other_cart)
+      ->assertSave();
+
+
+    $this->dispatch('/panier/add-selection', true);
+  }
+
+
+  /** @test */
+  public function pageShouldContainsLinkAddToCalimeroFavorites() {
+    $this->assertXPathContentContains('//p', 'Ajouter ma sélection au panier "favorites - cali mero"',
+                                      $this->_response->getBody());
+  }
+}
+
+
+
+
 class SearchSelectionPostAddSelectionToCartTest extends SearchSelectionAddToCartWithUserTestCase {
   public function setUp() {
     parent::setUp();
-- 
GitLab