From 38d38420fd1d897f7805460d0f402f6514b8c633 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Thu, 5 Nov 2015 16:49:39 +0100
Subject: [PATCH] dev #10430 add tests + update agenda table

---
 .../admin/controllers/CmsController.php       |  9 +--
 library/Class/Article.php                     | 57 ++++++++++++-------
 library/ZendAfi/Form/Admin/News.php           | 14 ++---
 .../ZendAfi/View/Helper/Calendar/Table.php    |  6 +-
 .../admin/controllers/CmsControllerTest.php   |  2 +-
 .../ProfilOptionsControllerTest.php           | 43 +++++++++++---
 tests/library/Class/ArticleTest.php           |  6 ++
 7 files changed, 91 insertions(+), 46 deletions(-)

diff --git a/application/modules/admin/controllers/CmsController.php b/application/modules/admin/controllers/CmsController.php
index 5a536570a8b..576bd406d6d 100644
--- a/application/modules/admin/controllers/CmsController.php
+++ b/application/modules/admin/controllers/CmsController.php
@@ -518,18 +518,11 @@ class Admin_CmsController extends ZendAfi_Controller_Action {
     foreach(['description', 'contenu'] as $content_field)
       $attributes[$content_field] = Class_CmsUrlTransformer::forEditing($attributes[$content_field]);
 
-    $attributes['pick_day'] = $this->_getParamsAsArray(',', $attributes['pick_day']);
+    $attributes['pick_day'] = $model->getPickDayAsArray();
     return $attributes;
   }
 
 
-  protected function _getParamsAsArray($sec, $param) {
-    if(empty($result = explode($sec, $param)))
-       return [$param];
-    return $result;
-  }
-
-
   protected function _findModel() {
     if (!$article = Class_Article::find((int)$this->_getParam('id')))
       return null;
diff --git a/library/Class/Article.php b/library/Class/Article.php
index 20722224c31..041e6bd629b 100644
--- a/library/Class/Article.php
+++ b/library/Class/Article.php
@@ -514,28 +514,29 @@ class Class_Article extends Storm_Model_Abstract {
                                       'refus_message'];
 
   protected $_default_attribute_values = [
-    'id_notice' => null,
-    'titre' => '',
-    'description' => '',
-    'contenu' => '',
-    'debut' => null,
-    'fin' => null,
-    'avis' => false,
-    'tags' => '',
-    'events_debut' => null,
-    'events_fin' => null,
-    'indexation' => 1,
-    'cacher_titre' => 0,
-    'date_maj' => '',
-    'date_creation' => '',
-    'status' => self::STATUS_DRAFT,
-    'id_lieu' => 0,
-    'id_cat' => 0,
-    'domaine_ids' => '',
-    'id_origine' => 0,
-    'refus_message' => '',
-    'destination_email' => '',
-    'all_day' => FALSE];
+                                          'id_notice' => null,
+                                          'titre' => '',
+                                          'description' => '',
+                                          'contenu' => '',
+                                          'debut' => null,
+                                          'fin' => null,
+                                          'avis' => false,
+                                          'tags' => '',
+                                          'events_debut' => null,
+                                          'events_fin' => null,
+                                          'indexation' => 1,
+                                          'cacher_titre' => 0,
+                                          'date_maj' => '',
+                                          'date_creation' => '',
+                                          'status' => self::STATUS_DRAFT,
+                                          'id_lieu' => 0,
+                                          'id_cat' => 0,
+                                          'domaine_ids' => '',
+                                          'id_origine' => 0,
+                                          'refus_message' => '',
+                                          'destination_email' => '',
+                                          'all_day' => FALSE,
+                                          'pick_day' => null];
 
 
   public function describeAssociationsOn($associations) {
@@ -1324,5 +1325,17 @@ class Class_Article extends Storm_Model_Abstract {
       return '';
     return $author->getLoginOrFullName();
   }
+
+
+  public function getPickDayAsArray() {
+    $param = parent::_get('pick_day');
+    if($param == '')
+      return [];
+
+    if(empty($result = explode(',', $param)))
+      return [$param];
+
+    return $result;
+  }
 }
 ?>
\ No newline at end of file
diff --git a/library/ZendAfi/Form/Admin/News.php b/library/ZendAfi/Form/Admin/News.php
index 3388b18987b..e1199dcae3c 100644
--- a/library/ZendAfi/Form/Admin/News.php
+++ b/library/ZendAfi/Form/Admin/News.php
@@ -86,13 +86,13 @@ class ZendAfi_Form_Admin_News extends ZendAfi_Form {
       ->addElement('multiCheckbox',
                    'pick_day',
                    ['label' => $this->_('Tous les'),
-                    'multiOptions' => ['monday' => $this->_('lundi'),
-                                       'tuesday' => $this->_('mardi'),
-                                       'wensday' => $this->_('mercredi'),
-                                       'tursday' => $this->_('jeudi'),
-                                       'friday' => $this->_('vendredi'),
-                                       'saturday' => $this->_('samedi'),
-                                       'sunday' => $this->_('dimanche')],
+                    'multiOptions' => ['1' => $this->_('lundi'),
+                                       '2' => $this->_('mardi'),
+                                       '3' => $this->_('mercredi'),
+                                       '4' => $this->_('jeudi'),
+                                       '5' => $this->_('vendredi'),
+                                       '6' => $this->_('samedi'),
+                                       '0' => $this->_('dimanche')],
                     'order' => 12 ])
 
       ->addElement('select', 'id_lieu', ['label' => $this->_('Lieu'),
diff --git a/library/ZendAfi/View/Helper/Calendar/Table.php b/library/ZendAfi/View/Helper/Calendar/Table.php
index f4dc2d86a40..2e6e0fa6f6e 100644
--- a/library/ZendAfi/View/Helper/Calendar/Table.php
+++ b/library/ZendAfi/View/Helper/Calendar/Table.php
@@ -129,6 +129,7 @@ class ZendAfi_View_Helper_Calendar_Table extends Zend_View_Helper_HtmlElement {
     // le dictionnaire et virer tout ce code bizarre.
     $day = (int)gmdate("j", $date);
     $month = (int)gmdate("m", $date);
+    $day_num = gmdate("w", $date);
 
     foreach($events as $event) {
       $event_debut = strtotime($event->getEventsDebut());
@@ -139,7 +140,10 @@ class ZendAfi_View_Helper_Calendar_Table extends Zend_View_Helper_HtmlElement {
       $jour_fin = (int)date('j', $event_fin);
       $mois_fin = (int)date('m', $event_fin);
 
-      // Jour clickable
+      if(!empty($event->getPickDayAsArray()) && !in_array($day_num, $event->getPickDayAsArray()))
+        return false;
+
+      // Jour clickable5#
       if($mois_debut == $month && $mois_fin == $month) {
         if($day >= $jour_debut && $day <= $jour_fin)
           return true;
diff --git a/tests/application/modules/admin/controllers/CmsControllerTest.php b/tests/application/modules/admin/controllers/CmsControllerTest.php
index e3545003140..8c1edd3434f 100644
--- a/tests/application/modules/admin/controllers/CmsControllerTest.php
+++ b/tests/application/modules/admin/controllers/CmsControllerTest.php
@@ -2785,6 +2785,6 @@ class CmsControllerEditArticleWithDate30December2014Test extends CmsControllerte
 
   /** @test */
   public function mondayShouldBePicked() {
-    $this->assertXPath('//input[@checked="checked"][@value="monday"]');
+    $this->assertXPath('//input[@checked="checked"][@value="1"]');
   }
 }
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
index 1663c1f936e..f221c228261 100644
--- a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
+++ b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
@@ -1628,16 +1628,34 @@ abstract class ProfilOptionsControllerProfilBoiteCalendarTestCase extends Abstra
   public function setup() {
     parent::setup();
 
+    $fiesta_annecy = $this->fixture('Class_ArticleCategorie',
+                                    ['id' => 1,
+                                     'libelle' => 'Fiesta à Annecy']);
+
+    $fete_caion = $this->fixture('Class_Article',
+                                    ['id' => 34,
+                                     'titre' => 'Fête du caion',
+                                     'contenu' => 'Fiesta fiesta',
+                                     'description' => 'à Annecy',
+                                     'events_debut' => '2011-09-01',
+                                     'events_fin' => '2011-09-01',
+                                     'categorie' => $fiesta_annecy]);
+
+    $fete_moulins = $this->fixture('Class_Article',
+                                   ['id' => 35,
+                                    'titre' => 'Fête du livre',
+                                    'contenu' => 'Fiesta fiesta',
+                                    'description' => 'à Moulins',
+                                    'events_debut' => '2011-09-05',
+                                    'events_fin' => '2011-10-05',
+                                    'pick_day' => '12345',
+                                    'categorie' => $fiesta_annecy]);
+
     Class_AdminVar::beVolatile();
     Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Article')
       ->whenCalled('getArticlesByPreferences')
-      ->answers([Class_Article::newInstanceWithId(34, [
-                                                       'titre' => 'Fête du caion',
-                                                       'description' => 'à Annecy',
-                                                       'events_debut' => '2011-09-03',
-                                                       'events_fin' => '2011-09-03',
-                                                       'categorie' => Class_ArticleCategorie::newInstanceWithId(1,
-                                                                                                                ['libelle' => 'Fiesta à Annecy'])])]);
+      ->answers([$fete_caion,
+                 $fete_moulins]);
   }
 }
 
@@ -1659,6 +1677,11 @@ class ProfilOptionsControllerProfilBoiteCalendarWithCalendarDisplayedAndModeSimp
       ->setLibelle('Profil Portail')
       ->setCfgAccueil($cfg_accueil);
 
+    $time_source = new TimeSourceForTest('2011-09-03 12:30:00');
+
+    Class_Calendar::setTimeSource($time_source);
+    ZendAfi_View_Helper_CalendarContent::setTimeSource($time_source);
+
     $this->dispatch('/opac');
   }
 
@@ -1728,6 +1751,12 @@ class ProfilOptionsControllerProfilBoiteCalendarWithCalendarDisplayedAndModeSimp
   public function boiteCalendarLiShouldContainsCalendarEventTitle() {
     $this->assertXPathContentContains('//li/a[@class="calendar_event_title"]', 'Fête du caion');
   }
+
+
+  /** @test */
+  public function sundayShouldNotContainsLink() {
+    $this->assertNotXPath('//td[@class="calendar_weekend"]//a[contains(@class, "day_clickable")]',  $this->_response->getBody());
+  }
 }
 
 
diff --git a/tests/library/Class/ArticleTest.php b/tests/library/Class/ArticleTest.php
index dfa55991f66..dabfc3b3daa 100644
--- a/tests/library/Class/ArticleTest.php
+++ b/tests/library/Class/ArticleTest.php
@@ -1321,4 +1321,10 @@ class ArticleFirstImagePathTest extends Storm_Test_ModelTestCase {
   public function firstImagePathShoulReturnUserfileImagesCatMyImage() {
     $this->assertEquals('./userfiles/images/cat/my_image.jpg', Class_Article::find(1)->getFirstImagePath());
   }
+
+
+  /** @test */
+  public function pickDayShouldBeAnEmptyArray() {
+    $this->assertEquals([], $this->_article->getPickDayAsArray());
+  }
 }
-- 
GitLab