From 11300b688e19645960e25597c8f7c62c6f531746 Mon Sep 17 00:00:00 2001 From: pbarroca <pbarroca@afi-sa.fr> Date: Fri, 25 Jul 2014 19:05:39 +0200 Subject: [PATCH] fix test failures --- .../admin/controllers/UsergroupController.php | 2 +- library/Class/CustomField/Model.php | 61 ++++++++++++++++-- library/Class/CustomField/NullModelValues.php | 62 +++++++++++++++++++ library/ZendAfi/Controller/Action.php | 2 +- .../CustomFieldsMetaControllerTest.php | 42 +++++++------ .../controllers/UserGroupControllerTest.php | 2 +- .../library/Class/UserGroupCategorieTest.php | 19 ++++-- 7 files changed, 156 insertions(+), 34 deletions(-) create mode 100644 library/Class/CustomField/NullModelValues.php diff --git a/application/modules/admin/controllers/UsergroupController.php b/application/modules/admin/controllers/UsergroupController.php index 2cc6664490c..bfb616b0c52 100644 --- a/application/modules/admin/controllers/UsergroupController.php +++ b/application/modules/admin/controllers/UsergroupController.php @@ -30,7 +30,7 @@ class Admin_UsergroupController extends ZendAfi_Controller_Action { 'messages' => [ 'successful_save' => $this->_('Groupe "%s" sauvegardé'), - 'successful_add' => $this->_('Groupe "%s" ajouté')], + 'successful_add' => $this->_('Le groupe "%s" a été sauvegardé')], 'actions' => [ 'add' => ['title' => $this->_("Ajouter un groupe d'utilisateurs")], diff --git a/library/Class/CustomField/Model.php b/library/Class/CustomField/Model.php index 5357c62dc81..35a0d3f4a0a 100644 --- a/library/Class/CustomField/Model.php +++ b/library/Class/CustomField/Model.php @@ -32,13 +32,23 @@ class Class_CustomField_Model { public static function registerAll($array_of_models_configuration) { static::$_models = []; foreach ($array_of_models_configuration as $configuration) - static::register($configuration); + static::register($configuration); } - public static function register($configuration) { - static::$_models[$configuration->getClassName()] = new Class_CustomField_Model($configuration); - } + public static function register($configuration) { + static::$_models[$configuration->getClassName()] = new Class_CustomField_Model($configuration); + } + + + public static function isRegistered($name) { + return array_key_exists(static::sanitizeName($name), static::$_models); + } + + + public static function sanitizeName($name) { + return str_replace(self::CLASS_PREFIX, '', $name); + } public static function getModels() { @@ -47,8 +57,9 @@ class Class_CustomField_Model { public static function getModel($model_name) { - $model_name = str_replace(self::CLASS_PREFIX, '', $model_name); - return static::$_models[$model_name]; + return (static::isRegistered($model_name)) ? + static::$_models[static::sanitizeName($model_name)] : + new Class_CustomField_NullModel(); } @@ -104,4 +115,42 @@ class Class_CustomField_Model { return []; } } + + + +class Class_CustomField_NullModel extends Class_CustomField_Model { + public function __construct() { + + } + + + public function getLabel() { + return $this->_configuration->getLabel(); + } + + + public function getLibelle() { + return $this->getLabel(); + } + + + public function getEditUrl() { + return $this->_configuration->getEditUrl(); + } + + + public function getId() { + return 'Null'; + } + + + public function getFields() { + return []; + } + + + public function find($id) { + return new Class_CustomField_NullModelValues(); + } +} ?> \ No newline at end of file diff --git a/library/Class/CustomField/NullModelValues.php b/library/Class/CustomField/NullModelValues.php new file mode 100644 index 00000000000..fea46e07312 --- /dev/null +++ b/library/Class/CustomField/NullModelValues.php @@ -0,0 +1,62 @@ +<?php +/** + * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved. + * + * AFI-OPAC 2.0 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). + * + * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class Class_CustomField_NullModelValues extends Class_CustomField_ModelValues{ + public function __construct() { + } + + + public function customizedModelIsNew() { + return true; + } + + + public function getLabel() { + return $this->_customized_model->getLabel(); + } + + + public function getEditURL() { + return []; + } + + + public function getFields() { + return []; + } + + + public function setFieldValue($custom_field_id, $value) { + return $this; + } + + + public function getFieldValues() { + return []; + } + + + public function save() { + return $this; + } +} +?> \ No newline at end of file diff --git a/library/ZendAfi/Controller/Action.php b/library/ZendAfi/Controller/Action.php index df5da84ae26..f18cb999180 100644 --- a/library/ZendAfi/Controller/Action.php +++ b/library/ZendAfi/Controller/Action.php @@ -190,7 +190,7 @@ class ZendAfi_Controller_Action extends Zend_Controller_Action { $post = $this->_request->getPost(); $custom_values = []; foreach ($post as $k=>$v) - if ('field_' == substr($k, 0, 6)) { + if (preg_match('/field_[0-9]+/', $k)) { $custom_values[$k] = $v; unset($post[$k]); } diff --git a/tests/application/modules/admin/controllers/CustomFieldsMetaControllerTest.php b/tests/application/modules/admin/controllers/CustomFieldsMetaControllerTest.php index 9e0a3cec6f7..1eadc41e0af 100644 --- a/tests/application/modules/admin/controllers/CustomFieldsMetaControllerTest.php +++ b/tests/application/modules/admin/controllers/CustomFieldsMetaControllerTest.php @@ -23,24 +23,23 @@ abstract class CustomFieldsMetaControllerTestCase extends AbstractControllerTest public function setUp() { parent::setUp(); - $this->fixture('Class_CustomField_Meta', - ['id' => 1, - 'label' => 'Address', - 'field_type' => Class_CustomField_Meta::TEXT_INPUT, - 'options_list' => '']); - + $this->fixture('Class_CustomField_Meta', [ + 'id' => 1, + 'label' => 'Address', + 'field_type' => Class_CustomField_Meta::TEXT_INPUT, + 'options_list' => '']); - $this->fixture('Class_CustomField_Meta', - ['id' => 2, - 'label' => 'Status', - 'field_type' => Class_CustomField_Meta::SELECT]); + $this->fixture('Class_CustomField_Meta', [ + 'id' => 2, + 'label' => 'Status', + 'field_type' => Class_CustomField_Meta::SELECT]); - $this->fixture('Class_CustomField', - ['id' => 1, - 'meta_id' => 1, - 'priority' => 2, - 'model' => 'Article']); + $this->fixture('Class_CustomField', [ + 'id' => 1, + 'meta_id' => 1, + 'priority' => 2, + 'model' => 'Article']); } } @@ -135,10 +134,11 @@ class CustomFieldsMetaControllerPostEditActionTest extends CustomFieldsMetaContr $this->postDispatch('admin/custom-fields-meta/edit/id/1', ['label' => 'public', - 'field_type' => 'SELECT', + 'field_type' => Class_CustomField_Meta::SELECT, 'options_list' => 'teens; parents ; all public ; ']); } - + + /** @test */ public function labelShouldBePublic() { $this->assertEquals('public', Class_CustomField_Meta::find(1)->getLabel()); @@ -147,13 +147,17 @@ class CustomFieldsMetaControllerPostEditActionTest extends CustomFieldsMetaContr /** @test */ public function fieldTypeShouldBeSelect() { - $this->assertEquals('SELECT', Class_CustomField_Meta::find(1)->getFieldType()); + $this->assertEquals( + Class_CustomField_Meta::SELECT, + Class_CustomField_Meta::find(1)->getFieldType()); } /** @test */ public function optionsListShouldBeTeensAndParents() { - $this->assertEquals(['teens','parents', 'all public'], Class_CustomField_Meta::find(1)->getOptionsListAsArray()); + $this->assertEquals( + ['teens', 'parents', 'all public'], + Class_CustomField_Meta::find(1)->getOptionsListAsArray()); } } diff --git a/tests/application/modules/admin/controllers/UserGroupControllerTest.php b/tests/application/modules/admin/controllers/UserGroupControllerTest.php index 0498afe450f..787dc5612a7 100644 --- a/tests/application/modules/admin/controllers/UserGroupControllerTest.php +++ b/tests/application/modules/admin/controllers/UserGroupControllerTest.php @@ -372,7 +372,7 @@ class Admin_UserGroupControllerAddPostTest extends Admin_UserGroupControllerTest /** @test */ public function responseShouldRedirectToDefaultAction() { - $this->assertRedirectTo('/admin/usergroup'); + $this->assertRedirect(); } diff --git a/tests/library/Class/UserGroupCategorieTest.php b/tests/library/Class/UserGroupCategorieTest.php index 43e63739d35..84fe0f8dc0d 100644 --- a/tests/library/Class/UserGroupCategorieTest.php +++ b/tests/library/Class/UserGroupCategorieTest.php @@ -26,12 +26,19 @@ class UserGroupCategorieWithNoGroupTest extends Storm_Test_ModelTestCase { public function setup() { parent::setup(); - $this->_etablissement = Class_UserGroupCategorie::newInstanceWithId(1,['libelle'=>'Etablissement']); - $this->_association = - Class_UserGroupCategorie::newInstanceWithId(2,['libelle'=>'Association', - 'parent_categorie' => $this->_etablissement, - 'user_groups' => []]); - $this->_etablissement->setSousCategories([$this->_association]); + + $this->_etablissement = $this->fixture('Class_UserGroupCategorie', [ + 'id' => 1, 'libelle'=>'Etablissement' + ]); + + $this->_association = $this->fixture('Class_UserGroupCategorie', [ + 'id' => 2, 'libelle'=>'Association', + 'parent_categorie' => $this->_etablissement, + 'user_groups' => []]); + + $this->_etablissement + ->setSousCategories([$this->_association]) + ->save(); } -- GitLab