From ae7bb8e35f09614886e7f76967615b7d891350dd Mon Sep 17 00:00:00 2001
From: efalcy <stl@gresille.org>
Date: Thu, 6 Nov 2014 18:37:49 +0100
Subject: [PATCH] dev #17360 : by default display training for current year and
  fix some bugs :   - display sessions if training contains sessions in 2
 differents years   - display training in box event if selected training in
 preferences contains deleted training

---
 .../admin/controllers/FormationController.php | 26 ++++++-----
 library/Class/Date/Holiday.php                |  6 +--
 library/Class/SessionFormation.php            | 25 ++++++++++-
 library/Trait/TimeSource.php                  |  9 +++-
 .../View/Helper/Accueil/FormationsWidget.php  |  7 ++-
 .../controllers/FormationControllerTest.php   | 44 +++++++++++++++++++
 .../AbonneControllerFormationsTest.php        |  5 +--
 .../Helper/Accueil/FormationsWidgetTest.php   | 29 +++++++++++-
 8 files changed, 125 insertions(+), 26 deletions(-)

diff --git a/application/modules/admin/controllers/FormationController.php b/application/modules/admin/controllers/FormationController.php
index 79786dd3435..fd9ced7aaf9 100644
--- a/application/modules/admin/controllers/FormationController.php
+++ b/application/modules/admin/controllers/FormationController.php
@@ -20,7 +20,7 @@
  */
 class Admin_FormationController extends ZendAfi_Controller_Action {
 	use Trait_Translator;
-
+	use Trait_TimeSource;
 	public function getRessourceDefinitions() {
 		return [
 			'model' => [
@@ -41,6 +41,18 @@ class Admin_FormationController extends ZendAfi_Controller_Action {
 			'after_add' => function($formation) { $this->_redirect('/admin/session-formation/add/formation_id/' . $formation->getId());} ];
 	}
 
+	protected function setYearsForCombo() {
+		$years = Class_SessionFormation::getYears();
+
+		$current_year= self::getCurrentYear();
+		if (empty($years))
+			$years[$current_year]=$current_year;
+		$this->view->years = $years;
+		return $this->view->selected_year = $this->_getParam('year',
+																												 in_array($current_year, $this->view->years) ?
+																												 $current_year:reset($years));
+	}
+
 
 	public function indexAction() {
 		if ($this->_request->isPost()) {
@@ -50,17 +62,12 @@ class Admin_FormationController extends ZendAfi_Controller_Action {
 
 		$this->view->titre = 'Mise à jour des formations';
 
-		$formations_by_year = Class_Formation::indexByYear(Class_Formation::findAllBy(['order' => 'id desc']), true);
-		$years = array_keys($formations_by_year);
-		$this->view->years = array_combine($years, $years);
-		$this->view->selected_year = $this->_getParam('year', end($years));
-		$this->view->formations = array_filter($formations_by_year[$this->view->selected_year],
+		$this->view->formations = array_filter(Class_Formation::findAllBy(['order' => 'id desc']),
 																					 function($f) {return !$f->hasSessions();});
 
 
-		$sessions = Class_SessionFormation::findAllBy(['where' => 'left(date_debut, 4)="'.$this->view->selected_year.'"',
-																									 'order' => 'date_debut']);
-		$this->view->sessions = $sessions;
+		$this->setYearsForCombo();
+		$this->view->sessions = Class_SessionFormation::getAllSessionsForYear($this->view->selected_year);
 		$this->_setParam('year', $this->view->selected_year);
 	}
 
@@ -68,7 +75,6 @@ class Admin_FormationController extends ZendAfi_Controller_Action {
 	public function getAction() {
 		$this->_helper->viewRenderer->setNoRender();
 		$this->getResponse()->setHeader('Content-Type', 'application/json; charset=utf-8');
-
 		$formations_years = Class_Formation::indexByYear(
 			Class_Formation::findAllBy(['order' => 'libelle']),
 			true);
diff --git a/library/Class/Date/Holiday.php b/library/Class/Date/Holiday.php
index c907d07f8a7..aef97af6b81 100644
--- a/library/Class/Date/Holiday.php
+++ b/library/Class/Date/Holiday.php
@@ -16,13 +16,13 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with AFI-OPAC 2.0; 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 Class_Date_Holiday {
 	public static function getTimestampsForYear($year = null) {
 		if (null == $year)
-			$year = (int) date('Y');
+ 			$year = (int) date('Y');
 
 		$stamps = array(
 			mktime(0, 0, 0, 1,  1,  $year),  // 1er janvier
@@ -39,7 +39,7 @@ class Class_Date_Holiday {
 		// php ne les calcul que pour des timestamp valides
 		if (1970 > $year or 2037 < $year)
 			return $stamps;
-		
+
 		$easterDate = easter_date($year);
 		$easterDay = date('j', $easterDate);
 		$easterMonth = date('n', $easterDate);
diff --git a/library/Class/SessionFormation.php b/library/Class/SessionFormation.php
index 33424eee176..2237c4c296a 100644
--- a/library/Class/SessionFormation.php
+++ b/library/Class/SessionFormation.php
@@ -19,11 +19,33 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
+class SessionFormationLoader extends Storm_Model_Loader {
+
+	public function getAllSessionsForYear($year) {
+		return Class_SessionFormation::findAllBy(['where' => 'left(date_debut, 4)="'.$year.'"',
+																							'order' => 'date_debut']);
+	}
+	public function getYears() {
+		$sessions = Class_SessionFormation::findAllBy(['order' => 'id desc']);
+		$years =  [];
+		foreach ($sessions as $session) {
+			$years[$session->getAnnee()]=$session->getAnnee();
+		}
+		arsort($years);
+		return $years;
+	}
+
+	public function getCurrentYear() {
+		return self::getCurrentYear();
+	}
+
+}
+
 
 class Class_SessionFormation extends Storm_Model_Abstract {
 	use Trait_TimeSource, Trait_Translator;
 	protected $_table_name = 'sessions_formation';
-
+	protected $_loader_class = 'SessionFormationLoader';
 	protected $_belongs_to = ['formation' => ['model' => 'Class_Formation'],
 														'lieu' => ['model' => 'Class_Lieu']];
 
@@ -264,6 +286,7 @@ class Class_SessionFormation extends Storm_Model_Abstract {
 		return $this->setVisible(true);
 	}
 
+
 }
 
 ?>
\ No newline at end of file
diff --git a/library/Trait/TimeSource.php b/library/Trait/TimeSource.php
index 9faad329722..31aa0ef58a1 100644
--- a/library/Trait/TimeSource.php
+++ b/library/Trait/TimeSource.php
@@ -16,13 +16,13 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with AFI-OPAC 2.0; 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
  */
 
 trait Trait_TimeSource {
 	/** @var Class_TimeSource */
 	protected static $_time_source;
-		
+
 
 	/**
 	 * @category testing
@@ -33,6 +33,11 @@ trait Trait_TimeSource {
 	}
 
 
+	public static function getCurrentYear() {
+		return date('Y',self::getTimeSource()->time());
+	}
+
+
 	/** @return Class_TimeSource */
 	public static function getTimeSource() {
 		if (null == self::$_time_source)
diff --git a/library/ZendAfi/View/Helper/Accueil/FormationsWidget.php b/library/ZendAfi/View/Helper/Accueil/FormationsWidget.php
index 140ae39c4dd..85a6961ec0a 100644
--- a/library/ZendAfi/View/Helper/Accueil/FormationsWidget.php
+++ b/library/ZendAfi/View/Helper/Accueil/FormationsWidget.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with AFI-OPAC 2.0; 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
  */
 
 
@@ -26,11 +26,10 @@ class ZendAfi_View_Helper_Accueil_FormationsWidget extends ZendAfi_View_Helper_A
 		$selected_formations = array_filter(explode('-', $this->preferences['selected_formations']));
 		$formations = [];
 		foreach($selected_formations as $selected_formation) {
-			$formations[] = Class_Formation::find($selected_formation);
+			if ($formation=Class_Formation::find($selected_formation))
+				$formations[] = $formation;
 		}
-		
 		$formations_by_year = Class_Formation::indexByYear($formations);
-		
 		$this->contenu = $this->view->renderFormationsByYear($formations_by_year, 'Widget');
 		return $this->getHtmlArray();
 	}
diff --git a/tests/application/modules/admin/controllers/FormationControllerTest.php b/tests/application/modules/admin/controllers/FormationControllerTest.php
index 75e0f858855..f56ab069300 100644
--- a/tests/application/modules/admin/controllers/FormationControllerTest.php
+++ b/tests/application/modules/admin/controllers/FormationControllerTest.php
@@ -861,6 +861,50 @@ class Admin_FormationControllerShowLearnPythonTest extends  Admin_FormationContr
 }
 
 
+class Admin_FormationControllerSessionDefaultYearSelectionTest extends  Admin_FormationControllerTestCase  {
+	public function setUp() {
+		parent::setUp();
+		$_session_python_mars = $this->fixture('Class_SessionFormation',
+																										['id'=>144,
+																										 'formation_id' => 12,
+																										 'date_debut' => '2010-03-21',
+																										 'stagiaires' => []
+																										 ]);
+		$this->fixture('Class_SessionFormation',
+									 ['id'=>148,
+										'formation_id' => 12,
+										'date_debut' => '2012-06-21',
+										'stagiaires' => []
+									 ]);
+
+		$time_source = new TimeSourceForTest('2010-12-27 09:00:00');
+		Trait_TimeSource::setTimeSource($time_source);
+
+	}
+
+	/** @test */
+	public function defaultSelectedYearShouldBe2010() {
+		$this->dispatch('/admin/formation/index');
+		$this->assertXPathContentContains('//select[@name="year"]//option[@selected="selected"][@value="2010"]', '2010');
+
+	}
+
+	/** @test */
+	public function selectedYear2012ShouldDisplayTrainingWithoutSession() {
+		$this->dispatch('/admin/formation/index/year/2012');
+		$this->assertXPathContentContains('//tr//td', 'Learn emptyness');
+
+	}
+
+	/** @test */
+	public function selectedYear2009ShouldDisplayTrainingWithoutSession() {
+		$this->dispatch('/admin/formation/index/year/2012');
+		$this->assertXPathContentContains('//tr//td', 'Learn emptyness');
+	}
+
+}
+
+
 
 class Admin_FormationControllerSessionLearnPythonTest extends  Admin_FormationControllerTestCase  {
 	public function setUp() {
diff --git a/tests/application/modules/opac/controllers/AbonneControllerFormationsTest.php b/tests/application/modules/opac/controllers/AbonneControllerFormationsTest.php
index 0085310b4bf..a403d10a72a 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerFormationsTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerFormationsTest.php
@@ -216,16 +216,13 @@ class AbonneControllerFormationsListWithLearnJavaNotVisibleTest extends Abstract
 
 
 
-
-
-
 class AbonneControllerFormationsSessionListWithLearnSmalltalkNotVisibleTest extends AbstractAbonneControllerFormationsTestCase {
 	public function setUp() {
 		parent::setUp();
 
 	}
 
-/** @test */
+  /** @test */
 	public function trainingJavaShouldBeDisplayed() {
 		$this->dispatch('/opac/formations', true);
 		$this->assertXPathContentContains('//h2', 'Learn Java',$this->response->getBody());
diff --git a/tests/library/ZendAfi/View/Helper/Accueil/FormationsWidgetTest.php b/tests/library/ZendAfi/View/Helper/Accueil/FormationsWidgetTest.php
index a1377f4a48d..61983be6a01 100644
--- a/tests/library/ZendAfi/View/Helper/Accueil/FormationsWidgetTest.php
+++ b/tests/library/ZendAfi/View/Helper/Accueil/FormationsWidgetTest.php
@@ -60,7 +60,7 @@ class ZendAfi_View_Helper_Accueil_FormationsWidgetWithNoSessionTest extends Zend
 																											 'date_debut' => '2014-05-05'])]]),
 												['division' => 1,
 												 'type_module' => 'FORMATIONS_WIDGET',
-												 'preferences' => ['selected_formations' => '1']]);
+												 'preferences' => ['titre' => 'Formations','selected_formations' => '1']]);
 	}
 
 
@@ -72,7 +72,7 @@ class ZendAfi_View_Helper_Accueil_FormationsWidgetWithNoSessionTest extends Zend
 
 	/** @test */
 	public function titleShouldBeBrowseJazzDomain() {
-		$this->assertXPathContentContains($this->_html, '//div[@class="titre"]', 'Formations');
+		$this->assertXPathContentContains($this->_html, '//div[@class="titre"]', 'Formations',$this->_html);
 	}
 
 
@@ -225,4 +225,29 @@ class ZendAfi_View_Helper_Accueil_FormationsWidgetWithInvisibleFormationTest ext
 		$this->assertNotXPathContentContains($this->_html, '//ul/li', 'Air Dogfight chapter 1');
 	}
 }
+
+class ZendAfi_View_Helper_Accueil_FormationsWidgetWithDeletedFormationsInPreferencesTest extends ZendAfi_View_Helper_Accueil_FormationsWidgetTestCase {
+	public function setup() {
+		parent::setup();
+
+		$this->renderWidget(
+			$this->fixture('Class_Formation',
+										 ['id' => 1,
+											'libelle' => 'Air Dogfight chapter 1',
+											'visible' => true,
+											'sessions' => [$this->fixture('Class_SessionFormation',
+																										['id' => 1,
+																										 'date_debut' => '2014-06-01',
+																										 'date_fin' => '2014-06-10'])]]),
+			['division' => 1,
+			 'type_module' => 'FORMATIONS_WIDGET',
+			 'preferences' => ['selected_formations' => '3-1-2']]);
+	}
+
+		/** @test */
+	public function airDogFightShouldBeDisplayed() {
+		$this->assertXPathContentContains($this->_html, '//ul/li', 'Air Dogfight chapter 1',$this->_html);
+	}
+
+}
 ?>
\ No newline at end of file
-- 
GitLab