From ca18b3569353f7e0dd50c40d2675836a6448fa72 Mon Sep 17 00:00:00 2001 From: Henri-Damien LAURENT <hdlaurent@afi-sa.fr> Date: Mon, 29 Nov 2021 15:36:22 +0100 Subject: [PATCH] hotline#145083 : MT : fix free widget : now displays articles according to workflow --- VERSIONS_HOTLINE/145083 | 1 + library/Class/Article/Loader.php | 34 +- library/Class/Article/Select.php | 5 +- .../Intonation/Library/Widget/Free/View.php | 7 +- tests/TearDown.php | 1 + tests/library/Class/Article/SelectForTest.php | 35 ++ .../Class/Article/TableSelectForTest.php | 29 ++ tests/library/Class/ArticleLoaderTest.php | 19 +- tests/library/Class/AvisNoticeTest.php | 318 +++--------------- tests/library/Class/CatalogueTest.php | 28 +- tests/library/Class/ModelTestCase.php | 38 --- .../Class/PanierNotice/TableSelectForTest.php | 29 ++ tests/library/Class/PanierNoticeTest.php | 33 +- tests/library/Class/TableSelectForTest.php | 80 +++++ .../Templates/ChiliAccordionTest.php | 5 +- tests/scenarios/Templates/ChiliLoginTest.php | 2 + tests/scenarios/Templates/ChiliTest.php | 6 +- .../Templates/MuscleTemplateTest.php | 2 + .../Templates/TemplatesArticlesTest.php | 67 +++- .../scenarios/Templates/TemplatesFreeTest.php | 111 +++--- .../Templates/TerreDuMilieuTemplateTest.php | 3 +- 21 files changed, 425 insertions(+), 428 deletions(-) create mode 100644 VERSIONS_HOTLINE/145083 create mode 100644 tests/library/Class/Article/SelectForTest.php create mode 100644 tests/library/Class/Article/TableSelectForTest.php create mode 100644 tests/library/Class/PanierNotice/TableSelectForTest.php create mode 100644 tests/library/Class/TableSelectForTest.php diff --git a/VERSIONS_HOTLINE/145083 b/VERSIONS_HOTLINE/145083 new file mode 100644 index 00000000000..b17fdb92384 --- /dev/null +++ b/VERSIONS_HOTLINE/145083 @@ -0,0 +1 @@ + - ticket #145083 : Magasin de Thèmes : Les articles affichés dans une boite libre respectent maintenant le statut de workflow \ No newline at end of file diff --git a/library/Class/Article/Loader.php b/library/Class/Article/Loader.php index 42ab4c257f4..d7e28b34147 100644 --- a/library/Class/Article/Loader.php +++ b/library/Class/Article/Loader.php @@ -27,6 +27,7 @@ class Class_Article_Loader extends Storm_Model_Loader { ORDER_COMMENTS_ASC = 'CommentCountAsc', ORDER_COMMENTS = 'CommentCount'; + protected static $_select_tool; protected $_all_articles, $_filter_by_local_callback; @@ -94,12 +95,15 @@ class Class_Article_Loader extends Storm_Model_Loader { * @return array */ public function getArticlesByPreferences($preferences) { - $preferences = array_merge($this->getArticlesByPreferencesDefaults(), - $preferences); + $preferences = + array_intersect_key($preferences, + $this->getArticlesByPreferencesDefaults()); - $select = Class_AdminVar::isModuleEnabled('ENABLE_ARTICLES_TIMINGS') - ? new Class_Article_SelectWithTimings($this) - : new Class_Article_Select($this); + $preferences = + array_merge($this->getArticlesByPreferencesDefaults(), + $preferences); + + $select = $this->_getSelectTool(); $articles = $select->findAll($preferences); $articles = $this->_filterByCustomFields($articles, @@ -268,4 +272,24 @@ class Class_Article_Loader extends Storm_Model_Loader { public function setFilterByLocalCallback($callback) { $this->_filter_by_local_callback = $callback; } + + + protected function _getSelectTool() { + if ( static::$_select_tool) + return static::$_select_tool; + + return Class_AdminVar::isModuleEnabled('ENABLE_ARTICLES_TIMINGS') + ? new Class_Article_SelectWithTimings($this) + : new Class_Article_Select($this); + } + + + public static function setSelectTool($select_tool) { + static::$_select_tool = $select_tool; + } + + + public static function reset() { + static::$_select_tool = null; + } } diff --git a/library/Class/Article/Select.php b/library/Class/Article/Select.php index 191a7dab364..991bbee957e 100644 --- a/library/Class/Article/Select.php +++ b/library/Class/Article/Select.php @@ -50,8 +50,9 @@ class Class_Article_Select { if ($this->_sort_order == Class_Article_Loader::ORDER_SELECTION && !$this->_has_selection) return []; - $select = $this->_buildSelect(); - return Class_Article::findAll($select); + return ($select = $this->_buildSelect()) + ? Class_Article::findAll($select) + : []; } diff --git a/library/templates/Intonation/Library/Widget/Free/View.php b/library/templates/Intonation/Library/Widget/Free/View.php index 2112f4ebdfb..27af3ee0e3a 100644 --- a/library/templates/Intonation/Library/Widget/Free/View.php +++ b/library/templates/Intonation/Library/Widget/Free/View.php @@ -47,12 +47,13 @@ class Intonation_Library_Widget_Free_View extends Zendafi_View_Helper_Accueil_Ba protected function _fetchArticle() { - if (empty($this->preferences['id_items'])) + if (! (isset($this->preferences['id_items']) && $this->preferences['id_items'])) return null; - if (!$articles = Class_Article::getArticlesByPreferences($this->preferences)) - return null; + $this->preferences['filter_by_workflow'] = true; + $this->preferences['filter_by_local'] = true; + $articles = Class_Article::getArticlesByPreferences($this->preferences); return array_shift($articles); } diff --git a/tests/TearDown.php b/tests/TearDown.php index 19532ce8e10..325b4bb4d58 100644 --- a/tests/TearDown.php +++ b/tests/TearDown.php @@ -44,6 +44,7 @@ class TearDown { Bokeh_Engine::reset(); Class_Album::setFileSystem(null); + Class_Article_Loader::reset(); Class_Codification::reset(); Class_Codification::resetInstance(); diff --git a/tests/library/Class/Article/SelectForTest.php b/tests/library/Class/Article/SelectForTest.php new file mode 100644 index 00000000000..3dc5f2ecd5f --- /dev/null +++ b/tests/library/Class/Article/SelectForTest.php @@ -0,0 +1,35 @@ +<?php +/** + * Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * 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 + */ + + +class Class_Article_SelectForTest { + public function findAll() { + return Class_Article::findAll(); + } + + public function getSortOrder() { + return ''; + } + + public function getNumberOfArticles() { + return Class_Article::count(); + } +} diff --git a/tests/library/Class/Article/TableSelectForTest.php b/tests/library/Class/Article/TableSelectForTest.php new file mode 100644 index 00000000000..ef0c5e63651 --- /dev/null +++ b/tests/library/Class/Article/TableSelectForTest.php @@ -0,0 +1,29 @@ +<?php +/** + * Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * 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 + */ + + +class Class_Article_TableSelectForTest extends Class_TableSelectForTest { + public function __construct() { + $this->_model_class = Class_Article::class; + $this->_model_table = 'cms_article'; + parent::__construct(); + } +} \ No newline at end of file diff --git a/tests/library/Class/ArticleLoaderTest.php b/tests/library/Class/ArticleLoaderTest.php index 4d9486df50a..b70463bfd35 100644 --- a/tests/library/Class/ArticleLoaderTest.php +++ b/tests/library/Class/ArticleLoaderTest.php @@ -24,23 +24,14 @@ abstract class ArticleLoaderGetArticlesByPreferencesTestCase extends ModelTestCa const WHERE_VISIBLE_CLAUSE = '((DEBUT IS NULL) OR (DEBUT <= CURDATE())) AND ((FIN IS NULL) OR (FIN >= CURDATE()))'; - public function setUp() { - Class_AdminVar::set('ENABLE_ARTICLES_TIMINGS', 0); + protected $select; - $this->select = new Zend_Db_Table_Select( - new Storm_Model_Table(['name' => 'cms_article'])); - $this->tbl_articles = $this->_buildTableMock(Class_Article::class, - ['select', 'fetchAll']); - $this->tbl_articles - ->expects($this->any()) - ->method('select') - ->will($this->returnValue($this->select)); + public function setUp() { + Class_AdminVar::set('ENABLE_ARTICLES_TIMINGS', 0); - $this->tbl_articles - ->expects($this->any()) - ->method('fetchAll') - ->will($this->returnValue($this->_buildRowset($this->_articlesFixtures()))); + $this->select = (new Class_Article_TableSelectForTest) + ->setFetchAllResult($this->_articlesFixtures()); Class_ArticleCategorie::getLoader() ->newInstanceWithId(2) diff --git a/tests/library/Class/AvisNoticeTest.php b/tests/library/Class/AvisNoticeTest.php index 38ebf73c13f..643ca34612e 100644 --- a/tests/library/Class/AvisNoticeTest.php +++ b/tests/library/Class/AvisNoticeTest.php @@ -21,43 +21,6 @@ require_once 'Class/AvisNotice.php'; require_once 'ModelTestCase.php'; -class AvisNoticeFixtures extends TestFixtures { - protected $_fixtures = array( - 'marcus_on_millenium' => array('ID' => 23, - 'ID_USER' => 2, - 'ENTETE' => 'excellent', - 'AVIS' => 'on en redemande', - 'CLEF_OEUVRE' => 'MILLENIUM--LARSSON', - 'DATE_AVIS' => '2010-05-21', - 'NOTE' => '4'), - 'marcus_on_potter' => array('ID' => 25, - 'ID_USER' => 2, - 'ENTETE' => 'pour les enfants', - 'AVIS' => 'ils aiment bien', - 'CLEF_OEUVRE' => 'POTTER', - 'DATE_AVIS' => '2010-05-20', - 'NOTE' => 3), - 'steve_on_millenium' => array('ID' => 12, - 'ID_USER' => 5, - 'ENTETE' => 'ça fait peur', - 'AVIS' => 'très glauque', - 'CLEF_OEUVRE' => 'MILLENIUM--LARSSON', - 'DATE_AVIS' => '2009-05-17', - 'NOTE' => null), - 'steve_on_potter' => array('ID' => 48, - 'ID_USER' => 5, - 'ENTETE' => 'Vive potter', - 'AVIS' => "c'est génial", - 'CLEF_OEUVRE' => 'POTTER', - 'DATE_AVIS' => '2011-05-17', - 'NOTE' => 3) - ); - - public static function instance() { - return new self(); - } -} - class AvisTestBibAbonne extends ModelTestCase { public function setUp() { @@ -114,9 +77,55 @@ class AvisTestBibAbonne extends ModelTestCase { -class AvisTestSortByDateAvisDesc extends ModelTestCase { +abstract class AvisNoticeWithFixturesTestCase extends ModelTestCase { + protected $_storm_default_to_volatile = true; + + public function setUp() { - $this->_setFindAllExpectation('Class_AvisNotice', 'AvisNoticeFixtures'); + parent::setUp(); + $this->fixture(Class_AvisNotice::class, + ['id' => 23, + 'id_user' => 2, + 'entete' => 'excellent', + 'avis' => 'on en redemande', + 'clef_oeuvre' => 'millenium--larsson', + 'date_avis' => '2010-05-21', + 'note' => '4']); + + $this->fixture(Class_AvisNotice::class, + ['id' => 25, + 'id_user' => 2, + 'entete' => 'pour les enfants', + 'avis' => 'ils aiment bien', + 'clef_oeuvre' => 'potter', + 'date_avis' => '2010-05-20', + 'note' => 3]); + + $this->fixture(Class_AvisNotice::class, + ['id' => 12, + 'id_user' => 5, + 'entete' => 'ça fait peur', + 'avis' => 'très glauque', + 'clef_oeuvre' => 'millenium--larsson', + 'date_avis' => '2009-05-17', + 'note' => null]); + + $this->fixture(Class_AvisNotice::class, + ['id' => 48, + 'id_user' => 5, + 'entete' => 'Vive potter', + 'avis' => "c'est génial", + 'clef_oeuvre' => 'potter', + 'date_avis' => '2011-05-17', + 'note' => 3]); + } +} + + + +class AvisTestSortByDateAvisDesc extends AvisNoticeWithFixturesTestCase { + public function setUp() { + parent::setUp(); $this->avis = Class_AvisNotice::getLoader()->findAll(); $this->avis_sorted = Class_AvisNotice::sortByDateAvisDesc($this->avis); } @@ -150,9 +159,12 @@ class AvisNoticeTestLoader extends ModelTestCase { } } -class AvisNoticeTestFindAll extends ModelTestCase { + + + +class AvisNoticeTestFindAll extends AvisNoticeWithFixturesTestCase { public function setUp() { - $this->_setFindAllExpectation('Class_AvisNotice', 'AvisNoticeFixtures'); + parent::setUp(); $this->avis = Class_AvisNotice::getLoader()->findAll(); } @@ -248,109 +260,6 @@ class AvisNoticeTestBelongsToUserRelation extends ModelTestCase { -abstract class AvisTestFindAllTestCase extends ModelTestCase { - public function setUp() { - $this->select = new Zend_Db_Table_Select(new Storm_Model_Table(array('name' => 'notices_avis'))); - $rs_avis = $this->_buildRowset(array( - array('ID' => 25, - 'ENTETE' => 'pour les enfants', - 'ID_USER' => 34, - 'CLEF_OEUVRE' => 'POTTER'), - array('ID' => 48, - 'ENTETE' => 'Vive potter', - 'ID_USER' => 34, - 'CLEF_OEUVRE' => 'POTTER'))); - - $tbl_avis = $this->_buildTableMock('Class_AvisNotice', - array('fetchAll', 'select')); - - $tbl_avis - ->expects($this->once()) - ->method('select') - ->will($this->returnValue($this->select)); - - $tbl_avis - ->expects($this->once()) - ->method('fetchAll') - ->will($this->returnValue($rs_avis)); - } -} - - -class AvisTestFindAllByUserTestCase extends AvisTestFindAllTestCase { - public function setUp() { - parent::setUp(); - - $this->steve = new Class_Users(); - $this->steve - ->setPrenom('Steve') - ->setId(34); - - $this->result = Class_AvisNotice::getLoader()->findAllBy(array('role' => 'user', - 'model' => $this->steve)); - } - - public function testExpectedSQLQuery() { - $this->assertEquals("SELECT `notices_avis`.* FROM `notices_avis` WHERE (id_user=34)", - $this->select->assemble()); - } -} - - - -class AvisTestFindAllByDateAvisWithLimitAndOrderTestCase extends AvisTestFindAllTestCase { - public function setUp() { - parent::setUp(); - $this->result = Class_AvisNotice::getLoader()->findAllBy(array('order' => 'date_avis desc', - 'limit' => 5)); - } - - public function testExpectedSQLQuery() { - $this->assertEquals("SELECT `notices_avis`.* FROM `notices_avis` ORDER BY `date_avis` desc LIMIT 5", - $this->select->assemble()); - } -} - - -class AvisTestFindAllByUsersIdTest extends AvisTestFindAllTestCase { - public function testExpectedSQLQuery() { - $this->result = Class_AvisNotice::getLoader()->findAllBy(array('id_user' => array(23, 34, 4))); - $this->assertEquals("SELECT `notices_avis`.* FROM `notices_avis` WHERE (id_user in (23, 34, 4))", - $this->select->assemble()); - } -} - - -class AvisTestFindAllByUserAndClefOeuvreTestCase extends AvisTestFindAllTestCase { - public function setUp() { - parent::setUp(); - $this->result = Class_AvisNotice::getLoader()->findAllBy(array('clef_oeuvre' => 'POTTER', - 'id_user' => 34)); - } - - public function testResultCountIsTwo() { - $this->assertEquals(2, count($this->result)); - } - - public function testExpectedSQLQuery() { - $this->assertEquals("SELECT `notices_avis`.* FROM `notices_avis` WHERE (clef_oeuvre='POTTER') AND (id_user=34)", - $this->select->assemble()); - } - - public function testFirstAvis() { - $first = $this->result[0]; - $this->assertEquals('pour les enfants', $first->getEntete()); - $this->assertEquals(34, $first->getIdUser()); - } - - public function testSecondAvis() { - $second = $this->result[1]; - $this->assertEquals('Vive potter', $second->getEntete()); - } -} - - - class NoticeTestHasManyAvisTest extends ModelTestCase { public function setUp() { @@ -547,127 +456,6 @@ class AvisSetAbonOuBibTest extends ModelTestCase { -class AvisLoaderGetAvisFromPreferencesTest extends AvisTestFindAllTestCase { - public function setUp() { - parent::setUp(); - - $this->modo_avis = new Class_AdminVar(); - $this->modo_avis - ->setId('MODO_AVIS') - ->setValeur(0); - - $this->modo_avis_biblio = new Class_AdminVar(); - $this->modo_avis_biblio - ->setId('MODO_AVIS_BIBLIO') - ->setValeur(0); - - Class_AdminVar::getLoader() - ->cacheInstance($this->modo_avis) - ->cacheInstance($this->modo_avis_biblio); - - $this->preferences = array( 'id_panier' => null, - 'id_catalogue' => null, - 'abon_ou_bib' => '', - 'only_img' => 0); - } - - protected function assertQueryIs($expected) { - Class_AvisNotice::getLoader()->getAvisFromPreferences($this->preferences); - $this->assertEquals(trim($expected), - trim(str_replace("\n", "", $this->select->assemble()))); - } - - - public function testDefaultSQLQuery() { - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } - - public function testWithAllAndModoAPosteriori() { - $this->preferences['abon_ou_bib'] = 'all'; - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } - - - public function testWithAllAndModoAPrioriForReaders() { - $this->modo_avis->setValeur(1); - $this->preferences['abon_ou_bib'] = 'all'; - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) AND ((STATUT=1 OR ABON_OU_BIB=1)) ". - "ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } - - - public function testWithAllAndModoAPrioriForBiblio() { - $this->modo_avis_biblio->setValeur(1); - $this->preferences['abon_ou_bib'] = 'all'; - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) AND ((STATUT=1 OR ABON_OU_BIB=0)) ". - "ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } - - - public function testWithAllAndModoAPrioriForBiblioAndReaders() { - $this->modo_avis->setValeur(1); - $this->modo_avis_biblio->setValeur(1); - - $this->preferences['abon_ou_bib'] = 'all'; - - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) AND ((STATUT=1 OR ABON_OU_BIB=1) ". - "AND (STATUT=1 OR ABON_OU_BIB=0)) ". - "ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } - - - public function testWithReadersAndModoAPosteriori() { - $this->preferences['abon_ou_bib'] = '0'; - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) AND (ABON_OU_BIB='0') ". - "ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } - - - public function testWithReadersAndModoAPrioriForReaders() { - $this->modo_avis->setValeur(1); - - $this->preferences['abon_ou_bib'] = '0'; - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) AND (ABON_OU_BIB='0') ". - "AND ((STATUT=1 OR ABON_OU_BIB=1)) ". - "ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } - - - public function testWithBiblioAndModoAPosteriori() { - $this->preferences['abon_ou_bib'] = '1'; - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) AND (ABON_OU_BIB='1') ". - "ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } - - public function testWithBiblioAndModoAPriori() { - $this->modo_avis_biblio->setValeur(1); - - $this->preferences['abon_ou_bib'] = '1'; - $this->assertQueryIs("SELECT `notices_avis`.* ". - "FROM `notices_avis` ". - "WHERE (flags=0) AND (ABON_OU_BIB='1') ". - "AND ((STATUT=1 OR ABON_OU_BIB=0)) ". - "ORDER BY `DATE_AVIS` DESC LIMIT 30"); - } -} - - class AvisVisibilityTest extends ModelTestCase { public function setUp() { diff --git a/tests/library/Class/CatalogueTest.php b/tests/library/Class/CatalogueTest.php index f633104bbef..496a74be162 100644 --- a/tests/library/Class/CatalogueTest.php +++ b/tests/library/Class/CatalogueTest.php @@ -18,26 +18,26 @@ * along with BOKEH; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -require_once 'ModelTestCase.php'; class CatalogueTestGetRequetesPanierWithIdUserAndIdPanier extends ModelTestCase { - public function setUp() { - $panier_row = $this->_buildRowset([['ID' => 3, 'ID_USER' => 3, - 'ID_PANIER' => 2, 'NOTICES' => ';STARWARS;JAMESBOND;']]); + protected $requetes, + $_requests_with_basket_order; - $this->select_paniers = new Zend_Db_Table_Select(new Storm_Model_Table(['name' => 'notices_paniers'])); - $tbl_paniers = $this->_buildTableMock('Class_PanierNotice', array('fetchAll', 'select')); - $tbl_paniers - ->expects($this->exactly(2)) - ->method('select') - ->will($this->returnValue($this->select_paniers)); + public function setUp() { + parent::setUp(); - $tbl_paniers - ->expects($this->exactly(2)) - ->method('fetchAll') - ->will($this->returnValue($panier_row)); + $this->fixture(Class_PanierNotice::class, + ['id' => 3, + 'id_user' => 3, + 'id_panier' => 2, + 'notices' => ';STARWARS;JAMESBOND;']); + + $this->fixture(Class_PanierNotice::class, + ['id' => 2, + 'id_panier' => 2, + 'notices' => ';STARWARS;JAMESBOND;']); $search_params = ['id_panier' => 2, 'id_user' => 3, diff --git a/tests/library/Class/ModelTestCase.php b/tests/library/Class/ModelTestCase.php index 703002c6ce3..a152924c6c8 100644 --- a/tests/library/Class/ModelTestCase.php +++ b/tests/library/Class/ModelTestCase.php @@ -44,19 +44,6 @@ abstract class ModelTestCase extends Storm_Test_ModelTestCase { $_registry_sql; - protected function _buildTableMock($model, $methods) { - $table = $this->createMock('Storm_Model_Table'.$model,$methods); - $loader = call_user_func([$model, 'getLoader']); - $loader->setTable($table); - return $table; - } - - - protected function _buildRowset($data) { - return new Zend_Db_Table_Rowset(['data' => $data]); - } - - protected function setUp() { Storm_Model_Abstract::unsetLoaders(); if($this->_storm_default_to_volatile) { @@ -103,29 +90,4 @@ abstract class ModelTestCase extends Storm_Test_ModelTestCase { $this->_registry_sql); return parent::tearDown(); } - - - protected function _setFindAllExpectation($model, $fixtures) { - if (!is_array($fixtures)) { - $finst = new $fixtures(); - $fixtures = $finst->all(); - } - - $mock_results = $this->_buildRowset($fixtures); - $tbl_newsletters = $this->_buildTableMock($model, ['fetchAll']); - - $tbl_newsletters - ->expects($this->once()) - ->method('fetchAll') - ->will($this->returnValue($mock_results)); - - return $tbl_newsletters; - } - - - protected function _generateLoaderFor($model, $methods) { - $loader = $this->createMock('Mock'.$model, $methods); - Storm_Model_Abstract::setLoaderFor($model, $loader); - return $loader; - } } \ No newline at end of file diff --git a/tests/library/Class/PanierNotice/TableSelectForTest.php b/tests/library/Class/PanierNotice/TableSelectForTest.php new file mode 100644 index 00000000000..f5a1916e398 --- /dev/null +++ b/tests/library/Class/PanierNotice/TableSelectForTest.php @@ -0,0 +1,29 @@ +<?php +/** + * Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * 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 + */ + + +class Class_PanierNotice_TableSelectForTest extends Class_TableSelectForTest { + public function __construct() { + $this->_model_class = Class_PanierNotice::class; + $this->_model_table = 'notices_paniers'; + parent::__construct(); + } +} diff --git a/tests/library/Class/PanierNoticeTest.php b/tests/library/Class/PanierNoticeTest.php index 9704432ced9..b532bdb89d7 100644 --- a/tests/library/Class/PanierNoticeTest.php +++ b/tests/library/Class/PanierNoticeTest.php @@ -26,26 +26,8 @@ class PanierNoticeLoaderTestFindAllBelongsToAdmin extends ModelTestCase { public function setUp() { parent::setUp(); - $paniers_rowset = $this->_buildRowset(array(array('ID' => 3, - 'LIBELLE' => 'fictions'), - array('ID' => 10, - 'LIBELLE' => 'musique'))); - - $this->select_paniers = new Zend_Db_Table_Select(new Storm_Model_Table(array('name' => 'notices_paniers'))); - - $tbl_paniers = $this->_buildTableMock('Class_PanierNotice', array('fetchAll', 'select')); - $tbl_paniers - ->expects($this->once()) - ->method('select') - ->will($this->returnValue($this->select_paniers)); - - $tbl_paniers - ->expects($this->once()) - ->method('fetchAll') - ->will($this->returnValue($paniers_rowset)); - - - $this->paniers = Class_PanierNotice::getLoader()->findAllBelongsToAdmin(); + $this->select_paniers = new Class_PanierNotice_TableSelectForTest; + Class_PanierNotice::findAllBelongsToAdmin(); } @@ -54,17 +36,6 @@ class PanierNoticeLoaderTestFindAllBelongsToAdmin extends ModelTestCase { "INNER JOIN `bib_admin_users` ON notices_paniers.id_user = bib_admin_users.id_user WHERE (bib_admin_users.ROLE_LEVEL >= 3) ORDER BY `notices_paniers`.`libelle` ASC", $this->select_paniers->assemble()); } - - - public function testFirstPanier() { - $first = $this->paniers[0]; - $this->assertEquals(3, $first->getId()); - } - - public function testSecondPanier() { - $second = $this->paniers[1]; - $this->assertEquals(10, $second->getId()); - } } diff --git a/tests/library/Class/TableSelectForTest.php b/tests/library/Class/TableSelectForTest.php new file mode 100644 index 00000000000..4cac03f5f42 --- /dev/null +++ b/tests/library/Class/TableSelectForTest.php @@ -0,0 +1,80 @@ +<?php +/** + * Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * 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 + */ + + +class Class_TableSelectForTest { + + protected $_select, + $_table, + $_model_table, + $_model_class, + $_mock; + + public function __construct() { + $this->_mock = new Class_TableSelectForTestMock; + $this->_select = + new Zend_Db_Table_Select(new Storm_Model_Table(['name' => $this->_model_table])); + + $this->_table = $this->_buildTableMock($this->_model_class, + ['select', 'fetchAll']); + $this->_table + ->expects($this->_mock->any()) + ->method('select') + ->will($this->_mock->returnValue($this->_select)); + } + + + protected function _buildRowset($data) { + return new Zend_Db_Table_Rowset(['data' => $data]); + } + + + protected function _buildTableMock($model, $methods) { + $table = $this->_mock->_createMock('Storm_Model_Table' . $model, $methods); + $loader = call_user_func([$model, 'getLoader']); + $loader->setTable($table); + return $table; + } + + + public function setFetchAllResult($fixtures_as_array) { + $this->_table + ->expects($this->_mock->any()) + ->method('fetchAll') + ->will($this->_mock->returnValue($this->_buildRowset($fixtures_as_array))); + + return $this; + } + + + public function assemble(){ + return $this->_select->assemble(); + } +} + + + + +class Class_TableSelectForTestMock extends ModelTestCase { + public function _createMock($model, $methods) { + return $this->createMock($model, $methods); + } +} diff --git a/tests/scenarios/Templates/ChiliAccordionTest.php b/tests/scenarios/Templates/ChiliAccordionTest.php index c5fc459463b..7e793c1342e 100644 --- a/tests/scenarios/Templates/ChiliAccordionTest.php +++ b/tests/scenarios/Templates/ChiliAccordionTest.php @@ -43,13 +43,13 @@ class ChiliAccordionWidgetTest extends AbstractControllerTestCase { 'size' => 5, 'description_length' => 4]); - $this->fixture('Class_Article', + $this->fixture(Class_Article::class, ['id' => 56, 'titre' => 'Le systeme Solaire', 'contenu' => '9 planètes' ]); - $this->fixture('Class_Article', + $this->fixture(Class_Article::class, ['id' => 57, 'titre' => 'La galaxie', 'contenu' => 'Milkyway' @@ -60,6 +60,7 @@ class ChiliAccordionWidgetTest extends AbstractControllerTestCase { 'titre' => 'Bibliographies', 'contenu' => '<iframe allowfullscreen="" allowtransparency="" frameborder="0" height="300" scrolling="no" src="//v.calameo.com/?bkcode=00261361896ea4b0d3648&mode=mini&authid=b9EvnNDuyW4J" style="margin:0 auto;" width="500"></iframe>']); + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); $this->dispatch('/opac/index/index'); } diff --git a/tests/scenarios/Templates/ChiliLoginTest.php b/tests/scenarios/Templates/ChiliLoginTest.php index fce308f9703..09543a17ed0 100644 --- a/tests/scenarios/Templates/ChiliLoginTest.php +++ b/tests/scenarios/Templates/ChiliLoginTest.php @@ -263,6 +263,8 @@ class ChiliLoginDispatchMenuArticlesTest extends ChiliLoginWidgetTestCase { 'titre' => 'New article', 'contenu' => '<i>A new article is readeable.</i>']); + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); + $this->dispatch('/widget/render-menu-entry/parent/0/menu_profil/4/menu/6'); } diff --git a/tests/scenarios/Templates/ChiliTest.php b/tests/scenarios/Templates/ChiliTest.php index e3a3223e50e..ef377cc3803 100644 --- a/tests/scenarios/Templates/ChiliTest.php +++ b/tests/scenarios/Templates/ChiliTest.php @@ -45,6 +45,7 @@ abstract class ChiliTemplateTestCase extends Admin_AbstractControllerTestCase { (new Class_Profil_Promoter)->promote(Class_Profil::find($id)); Class_Profil::find(1)->setHeaderCss('live_editor.css')->save(); + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); } @@ -90,6 +91,7 @@ class ChiliTemplateProfilePatcherTest extends ChiliTemplateTestCase { 'id_cat' => 34 ]); + $this->dispatch('/opac/index/index'); } @@ -138,7 +140,8 @@ class ChiliTemplateProfilePatcherTest extends ChiliTemplateTestCase { /** @test */ public function topHighlightCarouselShouldBePresent() { - $this->assertXPath('//div[contains(@class, "top_highlight_carousel")]'); + $this->assertXPath('//div[contains(@class, "top_highlight_carousel")]', + $this->_response->getBody()); } } @@ -1218,7 +1221,6 @@ class ChiliSearchResultPage1Test extends ChiliSearchResultPageTestCase { $this->dispatch('/recherche/pomme/page/1'); } - /** @test */ public function spanResultPagerShouldContainsPage1On3() { $this->assertXPathContentContains('//li[contains(@class, "result_pager")]//span', 'Page 1 / 3'); diff --git a/tests/scenarios/Templates/MuscleTemplateTest.php b/tests/scenarios/Templates/MuscleTemplateTest.php index b3e3dbb8afe..29042a72780 100644 --- a/tests/scenarios/Templates/MuscleTemplateTest.php +++ b/tests/scenarios/Templates/MuscleTemplateTest.php @@ -165,6 +165,8 @@ class MuscleTemplateProfilePatcherTest extends MuscleTemplateTestCase { 'id_cat' => 34 ]); + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); + $this->dispatch('/opac/index/index'); } diff --git a/tests/scenarios/Templates/TemplatesArticlesTest.php b/tests/scenarios/Templates/TemplatesArticlesTest.php index ce4b7a53bfe..43fad3a68bc 100644 --- a/tests/scenarios/Templates/TemplatesArticlesTest.php +++ b/tests/scenarios/Templates/TemplatesArticlesTest.php @@ -81,6 +81,8 @@ abstract class TemplatesArticlesWidgetTestCase extends Admin_AbstractControllerT 'events_debut' => '2020-05-13 00:00:00', 'events_fin' => '2020-07-31 00:00:00', 'all_day' => 1]); + + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); } } @@ -518,6 +520,8 @@ class TemplatesArticlesWithInvalidConfigurationTest extends TemplatesArticlesWid public function setUp() { parent::setup(); + Class_Article_Loader::setSelectTool(null); + Class_Profil::find(1) ->setModuleAccueilPreferences(1, Intonation_Library_Widget_Carousel_Article_Definition::CODE, @@ -573,22 +577,19 @@ class TemplatesArticlesRenderWithDescriptionLengthTest extends AbstractControlle public function setUp() { parent::setup(); + $profile = + $this->_buildTemplateProfil(['id' => 1, + 'template' => 'MUSCLE']); + + $this->_profile_patcher = (new Class_Template_ProfilePatcher(null)) + ->setProfile($profile); + $this->fixture('Class_Article', ['id' => 9, 'titre' => 'Savez-vous compter ?', 'contenu' => implode(' ' , range(1, 100))]); - Class_AdminVar::set('TEMPLATING', 1); - - $profile = $this->fixture('Class_Profil', - ['id' => 1, - 'template' => 'MUSCLE' - ]); - - $profile->beCurrentProfil(); - - $this->_profile_patcher = (new Class_Template_ProfilePatcher(null)) - ->setProfile($profile); + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); } @@ -678,6 +679,7 @@ class TemplatesArticlesWidgetWithDescriptionLengthTest extends AbstractControlle 'titre' => 'Parlez-vous français ?', 'description' => '<canvas></canvas>La description s\'arrête ici et pas plus loin.', 'contenu' => '<p>Une b...</p>'])]); + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); $profile_patcher ->addWidget(Intonation_Library_Widget_Carousel_Article_Definition::CODE, @@ -768,6 +770,8 @@ abstract class TemplatesArticlesWidgetAjaxRenderingTestCase extends AbstractCont 'titre' => 'Encore un ' . $id, 'contenu' => 'Toujours le même … ' . $id, 'id_cat' => 1]); + + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); } @@ -807,6 +811,7 @@ class TemplatesArticlesWidgetAjaxRenderingTest extends TemplatesArticlesWidgetAj public function setUp() { parent::setUp(); + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); $this->dispatch('/widget/render/widget_id/1/profile_id/34'); } @@ -987,6 +992,46 @@ class TemplatesArticlesWithWidgetRenderWallLoggedAsAdminTest + +class TemplatesArticlesSqlRequestTest + extends AbstractControllerTestCase { + + protected + $_storm_default_to_volatile = true, + $_select; + + + public function setUp() { + parent::setUp(); + ZendAfi_Auth::getInstance()->clearIdentity(); + + Class_AdminVar::set("WORKFLOW", 1); + + $this->_select = new Class_Article_TableSelectForTest; + + $profil = $this->_buildTemplateProfil(['id' => 12]); + $profile_patcher = (new Class_Template_ProfilePatcher(null)) + ->setProfile($profil) + ->addWidget(Intonation_Library_Widget_Carousel_Article_Definition::CODE, + Class_Profil::DIV_MAIN, + ['rendering' => 'card-description', + 'layout' => 'list', + 'size' => 10, + 'order' => 'EventDebut', + 'description_html' => 1]); + + $this->dispatch('/opac/index'); + } + + + /** @test */ + public function sqlRequestForArticleShouldContainsFilterStatus() { + $this->assertEquals('SELECT `cms_article`.* FROM `cms_article` WHERE ((DEBUT IS NULL) OR (DEBUT <= CURDATE())) AND ((FIN IS NULL) OR (FIN >= CURDATE())) AND (PARENT_ID=0) AND (STATUS in (3)) ORDER BY `DATE_CREATION` DESC LIMIT 1000', $this->_select->assemble()); + } +} + + + class TemplatesArticlesWithLibraryUrlRewriteTest extends AbstractControllerTestCase { protected $_storm_default_to_volatile = true; diff --git a/tests/scenarios/Templates/TemplatesFreeTest.php b/tests/scenarios/Templates/TemplatesFreeTest.php index 8a5c944ee8e..9b1d607aa57 100644 --- a/tests/scenarios/Templates/TemplatesFreeTest.php +++ b/tests/scenarios/Templates/TemplatesFreeTest.php @@ -20,43 +20,43 @@ */ -require_once('TemplatesTest.php'); - - abstract class TemplatesFreeWidgetTestCase extends Admin_AbstractControllerTestCase { + protected $_storm_default_to_volatile = true; public function setUp() { parent::setUp(); - - - $this->fixture(Class_Article::class, - ['id' => 5, - 'titre' => 'I have a dream', - 'contenu' => 'all Bokeh should be free (of bugs ...)<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/4Op2W-V9BrQ" title="YouTube video player" width="560"></iframe>', - 'categorie' => $this->fixture(Class_ArticleCategorie::class, - ['id' => 3, - 'libelle' => 'Sofware'])]); - - $this->_buildTemplateProfil(['id' => 1, - 'libelle' => 'Free my site']) - ->setBoiteOfTypeInDivision(2, - Intonation_Library_Widget_Free_Definition::CODE, - [ - 'id_items' => '5' - ]) - ->setBoiteOfTypeInDivision(4, - Intonation_Library_Widget_Search_Definition::CODE, - []) - ->setBoiteOfTypeInDivision(3, - Intonation_Library_Widget_Free_Definition::CODE, - [ - 'id_items' => '', - 'titre' => 'Boite seule' - ]) - ->assertSave(); - + $i_have_a_dream = + $this->fixture(Class_Article::class, + ['id' => 5, + 'titre' => 'I have a dream', + 'contenu' => 'all Bokeh should be free (of bugs ...)<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/4Op2W-V9BrQ" title="YouTube video player" width="560"></iframe>', + 'categorie' => $this->fixture(Class_ArticleCategorie::class, + ['id' => 3, + 'libelle' => 'Sofware'])]); + + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); + + $free_my_site = $this + ->_buildTemplateProfil(['id' => 123, + 'libelle' => 'Free my site']); + + $profile_patcher = (new Class_Template_ProfilePatcher(null)) + ->setProfile($free_my_site); + + $profile_patcher + ->addWidget(Intonation_Library_Widget_Free_Definition::CODE, + Class_Profil::DIV_MAIN, + ['id_items' => '5']) + + ->addWidget(Intonation_Library_Widget_Search_Definition::CODE, + Class_Profil::DIV_BANNIERE) + + ->addWidget(Intonation_Library_Widget_Free_Definition::CODE, + Class_Profil::DIV_SECOND_SIDE, + ['id_items' => '', + 'titre' => 'Boite seule']); } } @@ -66,25 +66,26 @@ abstract class TemplatesFreeWidgetTestCase extends Admin_AbstractControllerTestC class TemplatesFreeWidgetIndexWithCookiesTest extends TemplatesFreeWidgetTestCase { public function setUp() { parent::setUp(); + Class_Adminvar_Cookies::setManager(new Class_Cookies_TarteAuCitron()); - $this->dispatch('/opac/index/index/id_profil/1'); + $this->dispatch('/opac/index/index'); } /** @test */ - public function widgetOneTitleShouldBeBoiteLibre() { + public function widgetOneShouldContainsHeaderDefaultTitleBoiteLibre() { $this->assertXPathContentContains('//div[@id="boite_1"]//div[contains(@class, "card-header")]', - 'Boite libre'); + 'Boite libre', + $this->_response->getBody()); } /** @test */ - public function widgetOneShouldContainsIHaveADream() { - $this->assertXPathContentContains('//div[@id="boite_1"]', 'I have a dream'); + public function widgetOneShouldContainsEditLinkIHaveADream() { + $this->assertXPath('//div[@id="boite_1"]//a[@title="Modifier l\'article I have a dream"]'); } - /** @test */ public function widgetOneShouldHaveLinkToAddNewArticle() { $this->assertXPathContentContains('//a[contains(@href, "admin/cms/add")]', @@ -98,7 +99,6 @@ class TemplatesFreeWidgetIndexWithCookiesTest extends TemplatesFreeWidgetTestCas } - /** @test */ public function widgetThreeTitleShouldBeBoiteSeule() { $this->assertXPathContentContains('//div[@id="boite_3"]//div[contains(@class, "card-header")]', @@ -115,11 +115,10 @@ class TemplatesFreeWidgetIndexWithCookiesTest extends TemplatesFreeWidgetTestCas - class TemplatesDispatchEditFreeWidgetTest extends TemplatesFreeWidgetTestCase { public function setUp() { parent::setUp(); - $this->dispatch('/admin/widget/edit-widget/id/1/id_profil/1', true); + $this->dispatch('/admin/widget/edit-widget/id/1/id_profil/123'); } @@ -140,3 +139,35 @@ class TemplatesDispatchEditFreeWidgetTest extends TemplatesFreeWidgetTestCase { $this->assertXpathContentContains('//select[contains(@name, "VisibleWhenHidden")]/optgroup[contains(@label, "Division bann")]/option[@value="2"]', 'Boite recherche'); } } + + + + +class TemplatesFreeWidgetInIndexWithNotLoggedUserTest extends TemplatesFreeWidgetTestCase { + protected $_select; + + + public function setUp() { + parent::setUp(); + Class_Article_Loader::setSelectTool(null); + $this->_select = (new Class_Article_TableSelectForTest); + ZendAfi_Auth::getInstance()->clearIdentity(); + } + + + /** @test */ + public function freeWidgetRequestShouldFilterArticlesByWorkflow() { + Class_AdminVar::set('WORKFLOW', '[{"id":10, "label":"Validé 1"}, {"id":11, "label":"Validé 2u"}, {"id":12, "label":"Pré-publié"}]'); + $this->dispatch('/opac/index/index'); + + $this->assertEquals('SELECT `cms_article`.* FROM `cms_article` WHERE ((DEBUT IS NULL) OR (DEBUT <= CURDATE())) AND ((FIN IS NULL) OR (FIN >= CURDATE())) AND (id_article in (5)) AND (STATUS in (3)) ORDER BY FIELD(ID_ARTICLE, 5) ASC', $this->_select->assemble()); + } + + + /** @test */ + public function freeWidgetRequestShouldNotFilterArticlesByWorkflow() { + $this->dispatch('/opac/index/index'); + + $this->assertEquals('SELECT `cms_article`.* FROM `cms_article` WHERE ((DEBUT IS NULL) OR (DEBUT <= CURDATE())) AND ((FIN IS NULL) OR (FIN >= CURDATE())) AND (id_article in (5)) ORDER BY FIELD(ID_ARTICLE, 5) ASC', $this->_select->assemble()); + } +} \ No newline at end of file diff --git a/tests/scenarios/Templates/TerreDuMilieuTemplateTest.php b/tests/scenarios/Templates/TerreDuMilieuTemplateTest.php index 1b4870ee3e9..743ab29b366 100644 --- a/tests/scenarios/Templates/TerreDuMilieuTemplateTest.php +++ b/tests/scenarios/Templates/TerreDuMilieuTemplateTest.php @@ -33,7 +33,6 @@ abstract class TerreDuMilieuTemplateTestCase extends AbstractControllerTestCase (new TerreDuMilieu_Template)->tryOn($profile); (new Class_Profil_Promoter())->promote($profile); - } @@ -125,6 +124,8 @@ class TerreDuMilieuTemplateOpacIndexWithUserAgentTest extends TerreDuMilieuTempl 'id_cat' => 34 ]); + Class_Article_Loader::setSelectTool(new Class_Article_SelectForTest); + $this->dispatch('/opac/index/index'); } -- GitLab