From 7ac3468e76ea31154dddc241fe29c916137bbf3c Mon Sep 17 00:00:00 2001
From: lbrun <leo@sandbox.pergame.net>
Date: Thu, 22 Oct 2015 17:08:37 +0200
Subject: [PATCH] dev#25728 : add rss for sitotheques + tests

---
 VERSIONS_WIP/25728                            |  1 +
 .../views/scripts/accueil/sitotheque.phtml    |  9 +++
 .../opac/controllers/SitoController.php       | 61 ++++++++++++++++
 .../Systeme/ModulesAccueil/Sitotheque.php     | 19 ++---
 library/ZendAfi/View/Helper/Accueil/Sito.php  |  3 +
 .../controllers/AccueilControllerTest.php     |  5 ++
 .../ProfilOptionsControllerTest.php           | 16 +++-
 .../opac/controllers/SitoControllerTest.php   | 73 +++++++++++++++++--
 8 files changed, 169 insertions(+), 18 deletions(-)
 create mode 100644 VERSIONS_WIP/25728

diff --git a/VERSIONS_WIP/25728 b/VERSIONS_WIP/25728
new file mode 100644
index 00000000000..e86c6a5eb07
--- /dev/null
+++ b/VERSIONS_WIP/25728
@@ -0,0 +1 @@
+ - ticket #25728 : [Bonus] Ajouter la possibilité de flux rss à la boite sitothèque
\ No newline at end of file
diff --git a/application/modules/admin/views/scripts/accueil/sitotheque.phtml b/application/modules/admin/views/scripts/accueil/sitotheque.phtml
index 3d3d72d1c5c..b88abfa23fd 100644
--- a/application/modules/admin/views/scripts/accueil/sitotheque.phtml
+++ b/application/modules/admin/views/scripts/accueil/sitotheque.phtml
@@ -39,6 +39,15 @@ formSelectToggleVisibilityForElement("input[name=display_order]", "#option_nb_af
             <td class="droite">Titre&nbsp;</td>
             <td class="gauche"><input type="text" name="titre" size="50" value="<?php print($this->preferences['titre']); ?>"></td>
           </tr>
+          <tr>
+            <td class="droite">Proposer un fil Rss&nbsp;</td>
+            <td class="gauche">
+              <?php echo $this->formCheckbox('rss_avis',
+                                             $this->preferences['rss_avis'],
+                                             null,
+                                             array('1', '0')) ?>
+            </td>
+          </tr>
         </table>
       </fieldset>
       <br>
diff --git a/application/modules/opac/controllers/SitoController.php b/application/modules/opac/controllers/SitoController.php
index e10c79e9ddc..136660b75f6 100644
--- a/application/modules/opac/controllers/SitoController.php
+++ b/application/modules/opac/controllers/SitoController.php
@@ -86,4 +86,65 @@ class SitoController extends Zend_Controller_Action {
     $webThumbnail = new Class_WebService_WebSiteThumbnail();
     $this->_redirect($webThumbnail->fetchFullUrl($url));
   }
+
+
+  public function sitoRssAction() {
+    $id_profil = (int)$this->_getParam('id_profil');
+    $id_module = (int)$this->_getParam('id_module');
+
+    $data_rss =
+      [
+       'title'   => 'Flux indisponible',
+       'link'    => $this->_request->getScheme() . '://'
+       . $this->_request->getServer('HTTP_HOST'),
+       'charset'   => 'utf-8',
+       'description' => '',
+       'lastUpdate'  => time()
+    ];
+
+    $sitotheques = [];
+
+    if (null != ($profil = Class_Profil::find($id_profil))) {
+      $preferences  = $profil->getModuleAccueilPreferences($id_module, 'SITO');
+
+      $data_rss = array_merge(
+                              $data_rss,
+                              [
+                               'title'  => trim($preferences['titre'])
+                               ? trim($preferences['titre'])
+                               : $profil->getLibelle(),
+                               'link'    => '',
+                               'description' => $preferences['titre']
+                              ]
+      );
+
+      $sitotheques =
+        Class_Sitotheque::getSitesFromIdsAndCategories(
+                                                       explode('-', $preferences['id_items']),
+                                                       explode('-', $preferences['id_categorie']));
+    }
+    $this->_renderRSS($sitotheques, $data_rss);
+  }
+
+
+  protected function _renderRSS($sitotheques, $rss_array) {
+    $entries = [];
+    foreach ($sitotheques as $sito) {
+      $entries[] =
+        [
+         'title'       => html_entity_decode($sito->getTitre()),
+         'link'        => $sito->getUrl(),
+         'lastUpdate'  => strtotime($sito->getDateMaj()),
+         'description' => html_entity_decode($sito->getDescription())
+        ];
+    }
+
+    $rss_array['entries'] = $entries;
+
+    $feed = Zend_Feed::importArray($rss_array, 'rss');
+
+    $this->getHelper('ViewRenderer')->setNoRender();
+    $this->_response->setHeader('Content-Type', 'application/rss+xml;charset=utf-8') ;
+    $this->_response->setBody($feed->saveXML());
+  }
 }
\ No newline at end of file
diff --git a/library/Class/Systeme/ModulesAccueil/Sitotheque.php b/library/Class/Systeme/ModulesAccueil/Sitotheque.php
index c0592951f2b..2269f87750d 100644
--- a/library/Class/Systeme/ModulesAccueil/Sitotheque.php
+++ b/library/Class/Systeme/ModulesAccueil/Sitotheque.php
@@ -38,14 +38,15 @@ class Class_Systeme_ModulesAccueil_Sitotheque extends Class_Systeme_ModulesAccue
   protected $_isPhone = false;
 
   /** @var array */
-  protected $_defaultValues = array(
-    'titre' => 'Sitothèque', // Titre de la boite
-    'type_aff' => 1, // Type a afficher : 1=sélection libre, 2=les + récents
-    'id_categorie' => '', // Liste d'id_categorie séparés par des tirets
-    'id_items' => '', // Liste d'id_site séparés par des tirets
-    'nb_aff' => '2', // Nombre à afficher
-    'group_by_categorie' => false, //grouper les sites par categorie sous forme de menu,
-    'display_order' => 'Random' //ordre d'affichage: Random ou Selection
-  );
+  protected $_defaultValues = [
+                               'titre' => 'Sitothèque', // Titre de la boite
+                               'type_aff' => 1, // Type a afficher : 1=sélection libre, 2=les + récents
+                               'id_categorie' => '', // Liste d'id_categorie séparés par des tirets
+                               'id_items' => '', // Liste d'id_site séparés par des tirets
+                               'nb_aff' => '2', // Nombre à afficher
+                               'rss_avis' => '0',
+                               'group_by_categorie' => false, //grouper les sites par categorie sous forme de menu,
+                               'display_order' => 'Random' //ordre d'affichage: Random ou Selection
+  ];
 }
 ?>
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/Accueil/Sito.php b/library/ZendAfi/View/Helper/Accueil/Sito.php
index f440aab07c9..bad7ef60ec7 100644
--- a/library/ZendAfi/View/Helper/Accueil/Sito.php
+++ b/library/ZendAfi/View/Helper/Accueil/Sito.php
@@ -47,6 +47,9 @@ class ZendAfi_View_Helper_Accueil_Sito extends ZendAfi_View_Helper_Accueil_Base
     $contenu = '';
     $nb_aff = $this->getPreference('nb_aff');
 
+    if ($this->preferences['rss_avis'])
+      $this->rss_interne = $this->_getRSSurl('sito', 'sito-rss');
+
     $box_title = $this->getPreference('titre') ?
       $this->getPreference('titre') : $this->_('Derniers sites ajoutés');
 
diff --git a/tests/application/modules/admin/controllers/AccueilControllerTest.php b/tests/application/modules/admin/controllers/AccueilControllerTest.php
index 4e8692448cd..ef773048f31 100644
--- a/tests/application/modules/admin/controllers/AccueilControllerTest.php
+++ b/tests/application/modules/admin/controllers/AccueilControllerTest.php
@@ -315,6 +315,11 @@ class AccueilControllerConfigSitothequeDefaultsTest extends Admin_AbstractContro
   public function displayOrderSelectionShouldBePresent() {
     $this->assertXPath('//input[@type="radio"][@name="display_order"][@value="Selection"]');
   }
+
+/** @test */
+  public function rssFlowPreferenceShouldBePresent() {
+    $this->assertXPath('//input[@type="checkbox"][@name="rss_avis"]', $this->_response->getBody());
+  }
 }
 
 
diff --git a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
index b87bdc6d4b4..4007fb9713a 100644
--- a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
+++ b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
@@ -1181,7 +1181,10 @@ class ProfilOptionsControllerProfilBreadcrumbTest extends ProfilOptionsControlle
   }
 
 
-
+  /** @test */
+  public function sitothequesShouldNotContainsLinkToRss() {
+    $this->assertNotXpath('//div[@class="rss"]/a[contains(@href, "/sito/sito-rss")]');
+  }
 }
 
 
@@ -1302,8 +1305,6 @@ class ProfilOptionsControllerProfilJeunesseViewPageJeuxTest extends ProfilOption
 
 
 class ProfilOptionsControllerPagesJeuxWithSitotheque extends ProfilOptionsControllerProfilJeunesseWithPagesJeuxMusiqueTestCase {
-
-
   public function setUp() {
     parent::setUp();
 
@@ -1311,7 +1312,8 @@ class ProfilOptionsControllerPagesJeuxWithSitotheque extends ProfilOptionsContro
 
                                                    '10' => ['division' => '2',
                                                             'type_module' => 'SITO',
-                                                            'preferences' => ['id_categorie' => '2',
+                                                            'preferences' => ['rss_avis' => 1,
+                                                                              'id_categorie' => '2',
                                                                               'group_by_categorie' => true]]],
                                      'options' =>   []]);
 
@@ -1350,6 +1352,12 @@ class ProfilOptionsControllerPagesJeuxWithSitotheque extends ProfilOptionsContro
   }
 
 
+  /** @test */
+  public function sitothequesShouldContainsLinkToRss() {
+    $this->assertXpath('//div[@class="rss"]/a[contains(@href, "/sito/sito-rss")]');
+  }
+
+
   /** @test */
   public function categoryMySitesShouldBeInUlSitotheque() {
     $this->assertXPathContentContains('//ul[@class="sitotheque"]/li/h2/a',
diff --git a/tests/application/modules/opac/controllers/SitoControllerTest.php b/tests/application/modules/opac/controllers/SitoControllerTest.php
index e6be26938dc..f850867a9e7 100644
--- a/tests/application/modules/opac/controllers/SitoControllerTest.php
+++ b/tests/application/modules/opac/controllers/SitoControllerTest.php
@@ -49,7 +49,8 @@ abstract class SitoControllerTestCase extends AbstractControllerTestCase {
                             'division' => '2',
                             'type_module' => 'SITO',
                             'preferences' => ['id_categorie' => '3',
-                                              'id_items' => '25-28']
+                                              'id_items' => '25-28',
+                                              'rss_status' => 1]
                             ]
                           ],
                         'options' =>  []]);
@@ -60,7 +61,7 @@ abstract class SitoControllerTestCase extends AbstractControllerTestCase {
 
 
 
-abstract class SitoControllerViewCategoyTestCase extends SitoControllerTestCase {
+abstract class SitoControllerViewCategoryTestCase extends SitoControllerTestCase {
   public function setUp() {
     parent::setUp();
     $collectif = $this->fixture('Class_SitothequeCategorie',
@@ -104,7 +105,8 @@ abstract class SitoControllerViewCategoyTestCase extends SitoControllerTestCase
                                              'type_module' => 'SITO',
                                              'preferences' => ['id_categorie' => '12',
                                                                'type_aff' => '3',
-                                                               'id_items' => '280-281']]],
+                                                               'id_items' => '280-281',
+                                                               'rss_avis' => 1]]],
                        'options' => []]);
   }
 }
@@ -112,7 +114,7 @@ abstract class SitoControllerViewCategoyTestCase extends SitoControllerTestCase
 
 
 
-class SitoControllerViewCategoyTest extends SitoControllerViewCategoyTestCase {
+class SitoControllerViewCategoryTest extends SitoControllerViewCategoryTestCase {
   public function setUp() {
     parent::setUp();
     $this->dispatch('/sito/viewcategory/id_cat/12/start_cat/12', true);
@@ -168,7 +170,7 @@ class SitoControllerViewRecentTest extends SitoControllerTestCase {
 
 
 
-class SitoControllerViewCategorySearchTest extends SitoControllerViewCategoyTestCase {
+class SitoControllerViewCategorySearchTest extends SitoControllerViewCategoryTestCase {
   public function setUp() {
     parent::setUp();
 
@@ -321,4 +323,65 @@ class SitoControllerSitoWebThumbnailUrl extends SitoControllerTestCase {
                             $this->getResponseLocation());
   }
 }
+
+
+
+class SitoControllerRssWithProfileTest extends SitoControllerViewCategoryTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    Class_Profil::getCurrentProfil()
+      ->setCfgAccueil(['modules' => ['1' => ['division' => '2',
+                                             'type_module' => 'SITO',
+                                             'preferences' => ['id_categorie' => '19',
+                                                               'type_aff' => '3',
+                                                               'id_items' => '281',
+                                                               'rss_avis' => 1]]],
+                       'options' => []]);
+
+    $this->dispatch('sito/sito-rss?id_module=1&id_profil=2');
+  }
+
+/** @test */
+  public function rssShouldOnlyContainsTwoSitos() {
+    $this->assertXPathCount('//item', 2);
+  }
+
+
+  /** @test */
+  public function linuxFrShouldNotBeShown() {
+    $this->assertNotXPathContentContains('link', 'http://april.org');
+  }
+
+
+  /** @test */
+  public function framasoftShouldBeShown() {
+    $this->assertXPathContentContains('//link', 'http://framasoft.org');
+  }
+
+
+  /** @test */
+  public function laquadratureShouldBeShown() {
+    $this->assertXPathContentContains('//link', 'http://laquadrature.net');
+  }
+}
+
+
+
+class SitoControllerRssWithoutProfileTest extends AbstractControllerTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Profil')
+      ->whenCalled('find')
+      ->answers(null);
+
+    $this->dispatch('sito/sito-rss');
+  }
+
+  /** @test */
+  public function titleShouldBeFluxIndisponible() {
+    $this->assertXPathContentContains('//channel/title', 'Flux indisponible', $this->_response->getBody());
+  }
+}
 ?>
-- 
GitLab