diff --git a/library/ZendAfi/Controller/Action/Helper/ArticleListViewMode.php b/library/ZendAfi/Controller/Action/Helper/ArticleListViewMode.php index e0d528c96ea1fd24cdff59d64e4a247666fdefe9..9f9d7efe77f07132a8b81eb761b7aa9df03c98d6 100644 --- a/library/ZendAfi/Controller/Action/Helper/ArticleListViewMode.php +++ b/library/ZendAfi/Controller/Action/Helper/ArticleListViewMode.php @@ -122,10 +122,84 @@ class ZendAfi_Controller_Action_Helper_ArticleListViewMode extends ZendAfi_Contr } + public function buildWhereParams() { + $terms = explode(' ', $this->getSearch()); + + $where = []; + $second_where = []; + $user_where = []; + + foreach($terms as $term) { + $search_term = Zend_Db_Table::getDefaultAdapter()->quote('%' . $term . '%'); + + if(strtotime($term)) { + $where[] = sprintf('date_maj like %s', + $search_term); + + $second_where[] = sprintf('titre like %s', + $search_term, + $search_term); + continue; + } + + $criteria = (new Class_User_SearchCriteria(['search_search_for' => $term, + 'search_role_level' => [ZendAfi_Acl_AdminControllerRoles::MODO_BIB, + ZendAfi_Acl_AdminControllerRoles::MODO_PORTAIL, + ZendAfi_Acl_AdminControllerRoles::ADMIN_PORTAIL, + ZendAfi_Acl_AdminControllerRoles::ADMIN_BIB, + ZendAfi_Acl_AdminControllerRoles::SUPER_ADMIN]])); + + + if((!is_numeric($term))&& (3 < strlen($term)) && ($users_ids = (new Storm_Model_Collection($criteria->findPage(1)))->collect('id')->getArrayCopy())) { + $user_where[] = sprintf('id_user in (%s)', + implode(',', $users_ids)); + + $where[] = sprintf('titre like %s', + $search_term, + $search_term); + continue; + } + + $where[] = $second_where[] = sprintf('titre like %s', + $search_term); + } + + if(!$second_where && !$user_where) + return ''; + + if(!$second_where) + return sprintf('(((%s) or (%s)) or (%s))', + implode(' and ', $where), + implode(' or ', $where), + implode(' or ', $user_where)); + + if(!$where) + return implode(' or ', $user_where); + + if(!$user_where) + return sprintf('(%s) or (%s)', + implode(' and ', $where), + implode(' and ', $second_where)); + + return sprintf('(((%s) or (%s)) and (%s))', + implode(' and ', $where), + implode(' and ', $second_where), + implode(' or ', $user_where)); + + } + + + protected function getMultipleSearchParams() { + return ['where' => $this->buildWhereParams(), + 'order' => 'titre']; + } + + protected function getSearchParams() { if (count($filtered_ids=$this->getFilteredCategoriesIds())>0) - return array_merge(parent::getSearchParams(),['id_cat' => $filtered_ids]); - return parent::getSearchParams(); + return array_merge($this->getMultipleSearchParams(), + ['id_cat' => $filtered_ids]); + return $this->getMultipleSearchParams(); } diff --git a/tests/application/modules/admin/controllers/CmsControllerListModeTest.php b/tests/application/modules/admin/controllers/CmsControllerListModeTest.php index d7e81827a50e77818e59741296d92081b2db3548..d922ba1e97374a617c9a000379c222afd78c066d 100644 --- a/tests/application/modules/admin/controllers/CmsControllerListModeTest.php +++ b/tests/application/modules/admin/controllers/CmsControllerListModeTest.php @@ -273,6 +273,7 @@ class CmsControllerListModeSearchTest 'titre' => 'News from portail', 'contenu' => 'Welcome', 'categorie' => Class_ArticleCategorie::find(23)]); + $this->fixture('Class_ArticleCategorie', ['id' => 3400, 'libelle' => 'Racine CRan', @@ -324,6 +325,8 @@ class CmsControllerListModeSearchTest } } + + class CmsControllerListModeAdminBibSearchTest extends CmsControllerListModeTestCase { public function setUp() { parent::setUp(); @@ -366,9 +369,10 @@ class CmsControllerListModeAdminBibSearchTest extends CmsControllerListModeTestC public function newsFromPortailShouldNotBePresent() { $this->assertNotXPathContentContains('//td', 'News from portail', $this->_response->getBody()); } - } + + class CmsControllerListModeEditWithPaginationTest extends CmsControllerListModeTestCase { public function setUp() { @@ -421,6 +425,4 @@ class CmsControllerListModeEditPostTest extends CmsControllerListModeTestCase { $this->assertNotContains('id_cat', $this->getResponseLocation()); } -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/tests/library/ZendAfi/Controller/Action/Helper/ArticleListViewModeTest.php b/tests/library/ZendAfi/Controller/Action/Helper/ArticleListViewModeTest.php new file mode 100644 index 0000000000000000000000000000000000000000..14a5c37fb4ea0888abf4348092a0fd5d041124dd --- /dev/null +++ b/tests/library/ZendAfi/Controller/Action/Helper/ArticleListViewModeTest.php @@ -0,0 +1,89 @@ +<?php +/** + * Copyright (c) 2012-2014, 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 ArticleListViewModeTest extends ModelTestCase { + + protected $_storm_default_to_volatile = true, + $_list, + $_bib; + + + public function setUp() { + parent::setUp(); + $this->_bib = $this->fixture('Class_Bib', + ['id' => 1, + 'libelle' => 'Library' + ]); + + $user = $this->fixture('Class_Users', + ['id' => 1, + 'login' => 'redac', + 'password' => 'redac', + 'role_level' => ZendAfi_Acl_AdminControllerRoles::MODO_BIB, + 'id_site' => 1]); + + ZendAfi_Auth::getInstance()->logUser($user); + + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Users') + ->whenCalled('getIdentity') + ->answers($user) + + ->whenCalled('findAllBy') + ->with(['role_level' => [3,5,6,4,7], + 'where' => '(login LIKE "%news%" OR nom LIKE "%news%" OR prenom LIKE "%news%" OR pseudo LIKE "%news%" OR mail LIKE "%news%" OR idabon LIKE "%news%")', + 'limitPage' => [1, 20]]) + ->answers([]) + + ->beStrict(); + } + + + public function search() { + return [ + ['news', "(titre like '%news%')"], + /* ['news 2016', ""], */ + /* ['news redac', ""], */ + /* ['news 2016 redac', ""], */ + /* ['news top 5', ""], */ + /* ['top 5', ""], */ + /* ['top 5 2016', ""], */ + /* ['top 5 2016 redac', ""], */ + /* ['top 5 2016 2015-10 redac', ""], */ + ]; + } + + + /** + * @test + * @dataProvider search + */ + public function searchRequestShouldBeAsExpected($search, $where) { + $this->_list = (new ZendAfi_Controller_Action_Helper_ArticleListViewMode()) + ->articleListViewMode(['page' => 0, + 'search_value' => $search, + 'bib' => $this->_bib, + 'id_bib' => null, + 'id_cat' => 0]); + + $this->assertEquals($where, $this->_list->buildWhereParams()); + } +}