diff --git a/VERSIONS_HOTLINE/137761 b/VERSIONS_HOTLINE/137761 new file mode 100644 index 0000000000000000000000000000000000000000..40b7f563f5fe4eb875c21a17caabf33c57328c24 --- /dev/null +++ b/VERSIONS_HOTLINE/137761 @@ -0,0 +1 @@ + - ticket #137761 : Magasin de thèmes : Correction du filtrage des articles dans les caroussels \ No newline at end of file diff --git a/library/Class/Article.php b/library/Class/Article.php index a0fa8a5e4160c6b6be954125c7bc556ef28dde20..1818158dd85f64f2d9fe65769902fc1c783eede5 100644 --- a/library/Class/Article.php +++ b/library/Class/Article.php @@ -349,20 +349,59 @@ class ArticleLoader extends Storm_Model_Loader { * @return ArticleLoader */ protected function _filterByStatus() { - if (null === $this->_status) { + if (null === $this->_select) return $this; - } - if (null === $this->_select) { - return $this; - } + $filters = ($filters = $this->_getFilterByWorkflow()) + ? $filters + : $this->_status; - $this->_select->where('STATUS in (?)', $this->_status); + if ($filters) + $this->_select->where('STATUS in (?)', $filters); return $this; } + protected function _getFilterByWorkflow() { + if (! $this->_filter_by_workflow) + return null; + + if (!Class_AdminVar::isWorkflowEnabled()) + return null; + + $status_filter = [Class_Article::STATUS_VALIDATED]; + + if (Class_Users::isCurrentUserCanAccesBackend()) + $status_filter [] = Class_Article::STATUS_DRAFT; + + if ($this->_status) + $status_filter [] = $this->_status; + + return array_unique($status_filter); + } + + + protected function _filterByLocal() { + if (! $this->_filter_by_local) + return null; + + if (!Class_AdminVar::isTranslationEnabled()) + return null; + + if ($langue = Zend_Registry::get('translate')->getLocale()) + $this->_select + ->where('(cms_article.langue = "' . $langue . '" or exists (select \'x\' from cms_article as translation where translation.PARENT_ID = cms_article.ID_ARTICLE and trim(translation.langue) = "' . $langue . '") or cms_article.langue = "" or cms_article.langue is null)'); + + return function ($articles) use ($langue) { + return array_map(function ($article) use ($langue) + { + return $article->getTraductionLangue($langue); + }, + $articles); + }; + } + public function getArticlesByPreferencesDefaults() { return ['id_categorie' => '', // catégories d'article, ex: 12-2-8-1-89 @@ -383,7 +422,9 @@ class ArticleLoader extends Storm_Model_Loader { 'id_lieu' => null, // id du lieu Class_Lieu 'place_town' => null, 'display_mode' => 'Title', - 'custom_fields' => []]; + 'custom_fields' => [], + 'filter_by_workflow' => false, + 'filter_by_local' => false]; } @@ -420,6 +461,9 @@ class ArticleLoader extends Storm_Model_Loader { $this->_display_mode = $preferences['display_mode']; $this->_custom_fields = $preferences['custom_fields']; + $this->_filter_by_workflow = $preferences['filter_by_workflow']; + $this->_filter_by_local = $preferences['filter_by_local']; + if ($this->_sort_order == static::ORDER_SELECTION && !$this->_has_selection) return []; @@ -438,6 +482,8 @@ class ArticleLoader extends Storm_Model_Loader { ->_orderAndLimit() ->_getSelect(); + $filter_by_local_callback = $this->_filterByLocal(); + $articles = Class_Article::getLoader()->findAll($select); if ($this->_custom_fields) @@ -447,7 +493,11 @@ class ArticleLoader extends Storm_Model_Loader { if ((new ZendAfi_Validate_DateFormat)->isValid($this->_event_date)) $articles = $this->_filterByDay($this->_event_date, $articles); - $this->_all_articles = $articles = $this->_sortArticles($articles); + $articles = $this->_sortArticles($articles); + + $this->_all_articles = $articles = $filter_by_local_callback + ? $filter_by_local_callback($articles) + : $articles; if ( ($this->_sort_order == static::ORDER_SELECTION) diff --git a/library/templates/Intonation/Library/Widget/Carousel/Article/View.php b/library/templates/Intonation/Library/Widget/Carousel/Article/View.php index 2c234361b2d7340046374cc903dfb9a484ef5313..3b33b6cd6e58067ee78cd73e10a7bfac61185ad3 100644 --- a/library/templates/Intonation/Library/Widget/Carousel/Article/View.php +++ b/library/templates/Intonation/Library/Widget/Carousel/Article/View.php @@ -34,8 +34,10 @@ class Intonation_Library_Widget_Carousel_Article_View extends Intonation_Library if ( ! (isset($this->preferences['id_categorie']) && $this->preferences['id_categorie'])) $this->preferences['size'] = Intonation_Library_Widget_Carousel_Definition::LIMIT_SIZE; - $this->_articles = $loader->getArticlesByPreferences($this->preferences); - return Class_Article::filterByLocaleAndWorkflow($this->_articles); + $this->preferences['filter_by_workflow'] = true; + $this->preferences['filter_by_local'] = true; + + return $this->_articles = $loader->getArticlesByPreferences($this->preferences); } @@ -62,4 +64,4 @@ class Intonation_Library_Widget_Carousel_Article_View extends Intonation_Library protected function _extendedActions() { return [function() { return $this->view->Admin_TagAddNewArticle($this->id_module, $this->_articles); }]; } -} \ No newline at end of file +} diff --git a/tests/scenarios/Templates/TemplatesArticlesTest.php b/tests/scenarios/Templates/TemplatesArticlesTest.php index 8d409da4f3009e3c824c0efaa86012e82ed957fa..cbc6470dc045d9137360510f50359805446e1d9b 100644 --- a/tests/scenarios/Templates/TemplatesArticlesTest.php +++ b/tests/scenarios/Templates/TemplatesArticlesTest.php @@ -207,52 +207,6 @@ class TemplatesArticlesWidgetTest extends TemplatesArticlesWidgetTestCase { -class TemplatesArticlesWidgetWithWorklowTest extends TemplatesArticlesWidgetTestCase { - public function setUp() { - parent::setUp(); - Class_AdminVar::set('WORKFLOW', 1); - - Class_Article::find(4)->beValidated()->assertSave(); - Class_Article::find(6)->beDraft()->assertSave(); - Class_Article::find(7)->beValidationPending()->assertSave(); - - $this - ->onLoaderOfModel('Class_Article') - ->whenCalled('getArticlesByPreferences') - ->answers(Class_Article::findAllBy(['id_article' => [4, 6, 7]])); - - - - ZendAfi_Auth::getInstance()->clearIdentity(); - - $this->dispatch('/opac/index/index/id_profil/1', true); - } - - - /** @test */ - public function widgetCarouselShouldContainsValidatedArticleWinter() { - $this->assertXPathContentContains('//div[contains(@class, "boite news")]//div[@class="card-title"]', - 'Winter'); - } - - - /** @test */ - public function widgetCarouselShouldNotContainsDraftArticleSprechenSieDeutsh() { - $this->assertNotXPathContentContains('//div[contains(@class, "boite news")]//div[@class="card-title"]', - 'Sprechen Sie Deutsch ?'); - } - - - /** @test */ - public function widgetCarouselShouldNotContainsPendingArticleParlezVousFrancais() { - $this->assertNotXPathContentContains('//div[contains(@class, "boite news")]//div[@class="card-title"]', - 'Parlez-vous français ?'); - } -} - - - - class TemplatesArticlesCmsListActionTest extends TemplatesArticlesWidgetTestCase { /** @test */ public function dispatchShouldRenderSprechenSieDeutsh() { @@ -1092,4 +1046,4 @@ class TemplatesArticlesWidgetWithDescriptionHTMLTest extends AbstractControllerT $this->assertXPathContentContains('//div[contains(@class, "news widget")]//p[contains(@class, "model_description")]', '<a href="#">là </a><a href="#">ici</a>La description s\'arrête ici et pas plus loin.'); } -} \ No newline at end of file +} diff --git a/tests/scenarios/Templates/TemplatesWidgetCarouselTest.php b/tests/scenarios/Templates/TemplatesWidgetCarouselTest.php new file mode 100644 index 0000000000000000000000000000000000000000..58d89749e3245e2e3db08012e8f044b8948a3113 --- /dev/null +++ b/tests/scenarios/Templates/TemplatesWidgetCarouselTest.php @@ -0,0 +1,125 @@ +<?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 + */ + + +/* hotline: #137761 */ +abstract class TemplatesWidgetCarouselArticlesTestCase + extends AbstractControllerTestCase { + + protected + $_storm_default_to_volatile = true, + $_loader_article; + + public function setUp() { + parent::setUp(); + + $this->_buildTemplateProfil(['id' => 1, + 'libelle' => 'Carousel Articles']) + ->setBoiteOfTypeInDivision(2, + Intonation_Library_Widget_Carousel_Article_Definition::CODE, + ['all_layout' => Intonation_Library_Widget_Carousel_Definition::CAROUSEL, + 'size' => 3, + 'order' => 'Random', + 'id_categorie' => 1, + 'rss' => 1, + 'link_to_all' => 1, + 'embeded_code'=> 1, + 'IntonationShowFooter' => 1, + 'layout' => 'carousel' + ]) + ->assertSave(); + + $this->_loader_article = $this->onLoaderOfModel(Class_Article::class); + } +} + + + + +class TemplatesWidgetCarouselArticlesWithWorkflowTest + extends TemplatesWidgetCarouselArticlesTestCase { + + public function setUp() { + parent::setUp(); + + Class_AdminVar::set('WORKFLOW', 1); + $this->dispatch('/index'); + } + + + /** @test */ + public function getArticlesByPreferencesShouldBeCalledWithFilterByWorkflow() { + $prefs = $this->_loader_article + ->getFirstAttributeForLastCallOn('getArticlesByPreferences'); + + $this->assertTrue($prefs['filter_by_workflow']); + } + + + /** @test */ + public function getArticlesByPreferencesShouldBeCalledWithFilterByLocal() { + $prefs = $this->_loader_article + ->getFirstAttributeForLastCallOn('getArticlesByPreferences'); + + $this->assertTrue($prefs['filter_by_local']); + } +} + + + + +class TemplatesWidgetCarouselArticlesNotVolatileTest + extends TemplatesWidgetCarouselArticlesTestCase { + + protected $_storm_default_to_volatile = false; + + + /** @test */ + public function withWorkflowShouldDisplayThreeItems() { + Class_AdminVar::set('WORKFLOW', 1); + $this->dispatch('/index'); + $this->assertXpathCount('//div[contains(@class, "carousel-item")]', '3'); + } + + + /** @test */ + public function withoutWorkflowShouldDisplayThreeItems() { + Class_AdminVar::set('WORKFLOW', 0); + $this->dispatch('/index'); + $this->assertXpathCount('//div[contains(@class, "carousel-item")]', '3'); + } + + + /** @test */ + public function withLangueShouldDisplayThreeItems() { + Class_AdminVar::set('LANGUES', 'fr;en;es;ro;it'); + $this->dispatch('/index'); + $this->assertXpathCount('//div[contains(@class, "carousel-item")]', '3'); + } + + + /** @test */ + public function withoutLangueShouldDisplayThreeItems() { + Class_AdminVar::set('LANGUES', ''); + $this->dispatch('/index'); + $this->assertXpathCount('//div[contains(@class, "carousel-item")]', '3'); + } +}