From 40a2cf3c7ae919e18ce5ac970ccf2699271a7095 Mon Sep 17 00:00:00 2001 From: llaffont <llaffont@afi-sa.fr> Date: Fri, 12 Feb 2016 17:33:02 +0100 Subject: [PATCH] start dev #32668 refacto --- cosmogramme/php/_init.php | 2 +- cosmogramme/sql/patch/patch_287.php | 7 --- library/Class/Article.php | 44 +++++++++++--- library/Class/CustomField/ModelValues.php | 5 +- library/Trait/CustomFields.php | 5 +- tests/db/UpgradeDBTest.php | 18 ------ tests/library/Class/ArticleLoaderTest.php | 74 +++++++++++++++++++++-- 7 files changed, 109 insertions(+), 46 deletions(-) delete mode 100644 cosmogramme/sql/patch/patch_287.php diff --git a/cosmogramme/php/_init.php b/cosmogramme/php/_init.php index 0f5feab41d9..783024abbb2 100644 --- a/cosmogramme/php/_init.php +++ b/cosmogramme/php/_init.php @@ -1,7 +1,7 @@ <?php error_reporting(E_ERROR | E_PARSE); -define("PATCH_LEVEL","287"); +define("PATCH_LEVEL","286"); define("APPLI","cosmogramme"); define("COSMOPATH", "/var/www/html/vhosts/opac2/www/htdocs"); diff --git a/cosmogramme/sql/patch/patch_287.php b/cosmogramme/sql/patch/patch_287.php deleted file mode 100644 index be89ca51784..00000000000 --- a/cosmogramme/sql/patch/patch_287.php +++ /dev/null @@ -1,7 +0,0 @@ -<?php -$adapter = Zend_Db_Table::getDefaultAdapter(); - -try { - $adapter->query(' alter table custom_field_values add index (value(50))'); -} catch(Exception $e) {} -?> diff --git a/library/Class/Article.php b/library/Class/Article.php index 4577a0236ae..7edd67e92e7 100644 --- a/library/Class/Article.php +++ b/library/Class/Article.php @@ -270,8 +270,8 @@ class ArticleLoader extends Storm_Model_Loader { return $this; } - protected function _byCustomFields($custom_fields) { + protected function _byCustomFields($custom_fields) { foreach ($custom_fields as $id => $value) { $this->_select ->join( @@ -279,14 +279,8 @@ class ArticleLoader extends Storm_Model_Loader { "cms_article.ID_ARTICLE = cfv$id.model_id AND cfv$id.custom_field_id = $id", [] ); - if (Class_CustomField::findTypeForCustomFieldId($id) == Class_CustomField_Meta::MULTI_CHECKBOX) { - - $this->_select - ->where(" cfv$id.value like ?", "%;".$value.";%"); - continue; - } - $this->_select - ->where(" cfv$id.value = ? ", $value); +// $this->_select +// ->where(" cfv$id.value = ? ", $value); } return $this; @@ -367,7 +361,10 @@ class ArticleLoader extends Storm_Model_Loader { ->_getSelect(); - $articles = $this->_sortArticles($this->findAll($select)); + $articles = $this->_sortArticles( + $this->_filterByCustomFields( + Class_Article::getLoader()->findAll($select), + $this->_custom_fields)); if ( ($this->_sort_order == 'Selection') or !$this->_nb_aff @@ -378,6 +375,33 @@ class ArticleLoader extends Storm_Model_Loader { } + protected function _filterByCustomFields($articles, $custom_fields) { + if (!$custom_fields) + return $articles; + + $cfs = []; + foreach($custom_fields as $id => $value) { + $cfs[Class_CustomField::find($id)->getLabel()] = $value; + } + + Class_CustomField::find($id)->isAValueMatch($value, $article); + + + (new Class_CustomField_ModelValues($article, Class_CustomField::find($id)))->isOneMatching($value) + + return array_filter($articles, + function ($article) use ($cfs) + { + foreach($cfs as $name => $value) { + if (!$article->findCustomFieldValueMatching($value)) + return false; + } + return true; + } + ); + } + + /** * @param array $articles * @return array diff --git a/library/Class/CustomField/ModelValues.php b/library/Class/CustomField/ModelValues.php index 11f7dbd55dc..27b100b3050 100644 --- a/library/Class/CustomField/ModelValues.php +++ b/library/Class/CustomField/ModelValues.php @@ -26,7 +26,10 @@ class Class_CustomField_ModelValues { $_model, $_values; - + /** + * @param $customized_model Storm_Model_Abstract + * @param $model Class_CustomField_Model + */ public function __construct($customized_model, $model) { $this->_customized_model = $customized_model; $this->_model = $model; diff --git a/library/Trait/CustomFields.php b/library/Trait/CustomFields.php index 3dcd68c267f..3281bb99e92 100644 --- a/library/Trait/CustomFields.php +++ b/library/Trait/CustomFields.php @@ -45,9 +45,8 @@ trait Trait_CustomFields { return $this->custom_fields[$name]; if ($this->isNew()) return ''; - $model_values = Class_CustomField_Model::getModel($this->getModelName())->find($this->getId()); - if ($value = $model_values->getFieldValueByName($name)) + if ($value = $this->getAllCustomFields()->getFieldValueByName($name)) return $value->getValue(); return ''; } @@ -71,7 +70,7 @@ trait Trait_CustomFields { if (!Class_CustomField_Model::isRegistered($this->getModelName())) return true; - $model_values = Class_CustomField_Model::getModel($this->getModelName())->find($this->getId()); + $model_values = $this->getAllCustomFields(); foreach ($this->custom_fields as $key => $value) $model_values->setFieldValueByName($key, $value); diff --git a/tests/db/UpgradeDBTest.php b/tests/db/UpgradeDBTest.php index 4f2ba0e77cb..daba6f5e222 100644 --- a/tests/db/UpgradeDBTest.php +++ b/tests/db/UpgradeDBTest.php @@ -443,22 +443,4 @@ class UpgradeDB_286_Test extends UpgradeDBTestCase { public function typeDocShouldExist() { $this->assertColumn('notices_avis', 'type_doc'); } -} - - -class UpgradeDB_287_Test extends UpgradeDBTestCase { - - public function prepare() { - try { - $this->query(' drop index value on custom_field_values'); - } catch(Exception $e) {} - } - - - /** @test */ - public function indexOnCustomFieldShouldExist() { - $this->assertNotNull($this->query("SELECT INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS WHERE `TABLE_SCHEMA` = DATABASE() AND `TABLE_NAME` = 'custom_field_values' AND `INDEX_NAME` ='value';")); - - } - } \ No newline at end of file diff --git a/tests/library/Class/ArticleLoaderTest.php b/tests/library/Class/ArticleLoaderTest.php index 64664a53c11..80040dfb3ac 100644 --- a/tests/library/Class/ArticleLoaderTest.php +++ b/tests/library/Class/ArticleLoaderTest.php @@ -256,12 +256,12 @@ class ArticleLoaderGetArticlesByPreferencesTest extends ModelTestCase { 'options_list' => 'wifi;jardinage;projection', 'model' => 'Article']); - $articles = $this->getArticles(array('display_order' => 'EventDebut', - 'custom_fields' => [5 => 'value1', - 12 => 'value2'], - 'id_categorie' => '4', - 'nb_aff' => 3)); - $expected = sprintf("INNER JOIN `custom_field_values` AS `cfv5` ON cms_article.ID_ARTICLE = cfv5.model_id AND cfv5.custom_field_id = 5 INNER JOIN `custom_field_values` AS `cfv12` ON cms_article.ID_ARTICLE = cfv12.model_id AND cfv12.custom_field_id = 12 WHERE %s AND ( cfv5.value like '%s') AND ( cfv12.value = 'value2' ) AND (`cms_article`.ID_CAT in (4)) ORDER BY FIELD(`cms_article`.ID_CAT, 4) ASC", self::WHERE_VISIBLE_CLAUSE,'%;value1;%'); + $articles = $this->getArticles(['display_order' => 'EventDebut', + 'custom_fields' => [5 => 'value1', + 12 => 'value2'], + 'id_categorie' => '4', + 'nb_aff' => 3]); + $expected = sprintf("INNER JOIN `custom_field_values` AS `cfv5` ON cms_article.ID_ARTICLE = cfv5.model_id AND cfv5.custom_field_id = 5 INNER JOIN `custom_field_values` AS `cfv12` ON cms_article.ID_ARTICLE = cfv12.model_id AND cfv12.custom_field_id = 12 WHERE %s AND (`cms_article`.ID_CAT in (4)) ORDER BY FIELD(`cms_article`.ID_CAT, 4) ASC", self::WHERE_VISIBLE_CLAUSE); $this->assertSelect($expected); } @@ -381,6 +381,68 @@ abstract class ArticleLoaderGroupByBibTestCase extends ModelTestCase { +class ArticleLoaderWithCustomFieldsTest extends ModelTestCase { + protected $_storm_default_to_volatile = true; + + public function setUp() { + parent::setUp(); + + $this->fixture('Class_CustomField', + ['id' => 5, + 'priority' => 3, + 'label' => 'Options', + 'field_type' => Class_CustomField_Meta::MULTI_CHECKBOX, + 'options_list' => 'wifi;jardinage;projection', + 'model' => 'Article']); + + $soiree_net = $this->fixture('Class_Article', + ['id' => 1, + 'titre' => 'Soirée internet', + 'contenu' => 'Ondes power', + ]); + $soiree_net + ->setCustomField('Options', ['wifi', 'projection']) + ->saveWithCustomFields(); + + $journee_potager = $this->fixture('Class_Article', + ['id' => 2, + 'titre' => 'Massacre de limaces', + 'contenu' => '50 façons de tuer les limaces']); + $journee_potager + ->setCustomField('Options', ['jardinage', 'projection']) + ->saveWithCustomFields(); + + + $table = $this->mock(); + $table + ->whenCalled('select')->with('cms_article.*')->answers($table) + ->whenCalled('setIntegrityCheck')->with(false)->answers($table) + ->whenCalled('from')->with('cms_article')->answers($table) + ->whenCalled('where')->answers($table) + ->whenCalled('join')->answers($table) + ->whenCalled('order')->answers($table) + ->whenCalled('limit')->answers($table); + + Class_Article::setTable($table); + + $this->onLoaderOfModel('Class_Article') + ->whenCalled('findAll') + ->answers([$soiree_net, $journee_potager]); + } + + + /** @test */ + public function articlesByPrefsWithWifiShouldReturnSoireeNet() { + $articles = Class_Article::getArticlesByPreferences(['custom_fields' => [5 => 'wifi']]); + + $this->assertEquals(['Soirée internet'], + (new Storm_Model_Collection($articles))->collect('titre')->getArrayCopy()); + } +} + + + + class ArticleLoaderGroupByBibWithoutBibTest extends ArticleLoaderGroupByBibTestCase { /** @test */ public function sizeOfArrayShouldBeOne() { -- GitLab