From c62c37c72fa1c0de6c957b5c634939ecf46b5fee Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 21 Jul 2016 09:54:33 +1000 Subject: [PATCH] Move getting dataType into own function and add test of loading data type also minor code changes as suggested on PR --- CRM/Admin/Form/OptionGroup.php | 7 ++++++ CRM/Admin/Form/Options.php | 23 +++++++++++++---- CRM/Utils/Check/Component/OptionGroups.php | 4 +-- sql/civicrm_generated.mysql | 1 - tests/phpunit/CRM/Core/OptionGroupTest.php | 29 ++++++++++++++++++++++ 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/CRM/Admin/Form/OptionGroup.php b/CRM/Admin/Form/OptionGroup.php index 05a504ec5e..7fbe798178 100644 --- a/CRM/Admin/Form/OptionGroup.php +++ b/CRM/Admin/Form/OptionGroup.php @@ -36,6 +36,13 @@ */ class CRM_Admin_Form_OptionGroup extends CRM_Admin_Form { + /** + * Explicitly declare the entity api name. + */ + public function getDefaultEntity() { + return 'OptionGroup'; + } + /** * Build the form object. */ diff --git a/CRM/Admin/Form/Options.php b/CRM/Admin/Form/Options.php index 6d82d38c07..3ff8e98ec3 100644 --- a/CRM/Admin/Form/Options.php +++ b/CRM/Admin/Form/Options.php @@ -376,11 +376,7 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { } } - $optionGroup = civicrm_api3('OptionGroup', 'get', array( - 'sequential' => 1, - 'name' => $self->_gName, - )); - $dataType = CRM_Core_BAO_OptionGroup::getDataType($optionGroup['id']); + $optionGroup = $this->getOptionGroupDataType($self->_gName); if ($dataType) { $validate = CRM_Utils_Type::validate($fields['value'], $dataType, FALSE); if (!$validate) { @@ -392,6 +388,23 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { return $errors; } + /** + * Get the DataType for a specified Option Group. + * + * @param string $optionGroupname name of the option group + * + * @return string|bool + */ + public static function getOptionGroupDataType($optionGroupName) { + $optionGroup = civicrm_api3('OptionGroup', 'get', array( + 'sequential' => 1, + 'name' => $optionGroupName, + )); + + $dataType = CRM_Core_BAO_OptionGroup::getDataType($optionGroup['id']); + return $dataType; + } + /** * Process the form submission. */ diff --git a/CRM/Utils/Check/Component/OptionGroups.php b/CRM/Utils/Check/Component/OptionGroups.php index e4e2b997e8..077e551e96 100644 --- a/CRM/Utils/Check/Component/OptionGroups.php +++ b/CRM/Utils/Check/Component/OptionGroups.php @@ -53,7 +53,7 @@ class CRM_Utils_Check_Component_OptionGroups extends CRM_Utils_Check_Component { $validate = CRM_Utils_Type::validate($value['value'], $optionGroup['data_type'], FALSE); if (!$validate) { $problemValues[] = array( - 'group_name' => $optionGroup['label'], + 'group_name' => $optionGroup['title'], 'value_name' => $value['label'], ); } @@ -74,7 +74,7 @@ class CRM_Utils_Check_Component_OptionGroups extends CRM_Utils_Check_Component { __FUNCTION__, ts('The Following Option Values contain value fields that do not match the Data Type of the Option Group

') . - explode('\n', $strings) . ts('
Option GroupOption Value

'), + implode('\n', $strings) . ts('

'), ts('Option Values with problematic Values'), \Psr\Log\LogLevel::NOTICE, 'fa-server' diff --git a/sql/civicrm_generated.mysql b/sql/civicrm_generated.mysql index 8740f20336..56124804bf 100644 --- a/sql/civicrm_generated.mysql +++ b/sql/civicrm_generated.mysql @@ -918,7 +918,6 @@ LOCK TABLES `civicrm_membership_log` WRITE; /*!40000 ALTER TABLE `civicrm_membership_log` DISABLE KEYS */; INSERT INTO `civicrm_membership_log` (`id`, `membership_id`, `status_id`, `start_date`, `end_date`, `modified_id`, `modified_date`, `membership_type_id`, `max_related`) VALUES (1,9,1,'2016-08-05','2018-08-04',11,'2016-08-13',1,NULL),(2,10,4,'2015-08-04','2016-08-03',15,'2016-08-13',2,NULL),(3,6,1,'2016-08-08','2017-08-07',35,'2016-08-13',2,NULL),(4,7,1,'2016-08-07','2018-08-06',36,'2016-08-13',1,NULL),(5,11,1,'2016-08-03',NULL,39,'2016-08-13',3,NULL),(6,5,4,'2015-08-09','2016-08-08',42,'2016-08-13',2,NULL),(7,2,1,'2016-08-12','2017-08-11',48,'2016-08-13',2,NULL),(8,24,1,'2016-07-21','2017-07-20',51,'2016-08-13',2,NULL),(9,19,1,'2016-07-26','2018-07-25',56,'2016-08-13',1,NULL),(10,23,1,'2016-07-22','2018-07-21',59,'2016-08-13',1,NULL),(11,22,1,'2016-07-23',NULL,70,'2016-08-13',3,NULL),(12,20,3,'2014-03-14','2016-03-13',76,'2016-08-13',1,NULL),(13,3,1,'2016-08-11','2018-08-10',79,'2016-08-13',1,NULL),(14,4,1,'2016-08-10','2017-08-09',94,'2016-08-13',2,NULL),(15,15,3,'2014-04-23','2016-04-22',99,'2016-08-13',1,NULL),(16,26,1,'2016-07-19','2017-07-18',103,'2016-08-13',2,NULL),(17,1,1,'2016-08-13','2018-08-12',104,'2016-08-13',1,NULL),(18,13,1,'2016-08-01','2018-07-31',105,'2016-08-13',1,NULL),(19,18,1,'2016-07-27','2017-07-26',109,'2016-08-13',2,NULL),(20,17,1,'2016-07-28','2018-07-27',133,'2016-08-13',1,NULL),(21,27,1,'2016-07-18','2018-07-17',139,'2016-08-13',1,NULL),(22,21,1,'2016-07-24','2018-07-23',145,'2016-08-13',1,NULL),(23,16,1,'2016-07-29','2017-07-28',148,'2016-08-13',2,NULL),(24,14,1,'2016-07-31','2017-07-30',161,'2016-08-13',2,NULL),(25,30,3,'2013-12-24','2015-12-23',172,'2016-08-13',1,NULL),(26,28,1,'2016-07-17','2017-07-16',174,'2016-08-13',2,NULL),(27,25,4,'2015-07-20','2016-07-19',179,'2016-08-13',2,NULL),(28,12,1,'2016-08-02','2017-08-01',197,'2016-08-13',2,NULL),(29,8,1,'2016-08-06','2017-08-05',198,'2016-08-13',2,NULL),(30,29,1,'2016-07-16','2018-07-15',201,'2016-08-13',1,NULL); /*!40000 ALTER TABLE `civicrm_membership_log` ENABLE KEYS */; -UNLOCK TABLES; -- -- Dumping data for table `civicrm_membership_payment` diff --git a/tests/phpunit/CRM/Core/OptionGroupTest.php b/tests/phpunit/CRM/Core/OptionGroupTest.php index 73d7c404c7..dad218314a 100644 --- a/tests/phpunit/CRM/Core/OptionGroupTest.php +++ b/tests/phpunit/CRM/Core/OptionGroupTest.php @@ -63,4 +63,33 @@ class CRM_Core_OptionGroupTest extends CiviUnitTestCase { $this->assertTrue(($values === $options2), "The arrays retrieved should be the same and in the same order"); } + /** + * optionGroupTests + * + * @return array + */ + public function optionGroupTests() { + $tests = array(); + $tests[] = array('event_type', 'Integer'); + $tests[] = array('addressee', 'null'); + $tests[] = array('activity_status', 'Integer'); + return $tests; + } + + /** + * Test Returning DataType of Option Group + * + * + * @dataProvider optionGroupTests + */ + public function testsOptionGroupDataType($optionGroup, $expectedDataType) { + $dataType = CRM_Admin_Form_Options::getOptionGroupDataType($optionGroup); + if ($expectedDataType == 'null') { + $this->assertNull($dataType); + } + else { + $this->assertEquals($dataType, $expectedDataType); + } + } + } -- 2.25.1