Merge pull request #14669 from civicrm/5.15
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomFieldTest.php
index 83284700f5d840f4e74c624d7d0cd72c7628274c..70449f8d6ccb9254ab35721f375adea896ca2306 100644 (file)
@@ -11,10 +11,19 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
 
   protected $customFieldID;
 
-  public function setUp() {
-    parent::setUp();
+  /**
+   * Clean up after test.
+   *
+   * @throws \Exception
+   */
+  public function tearDown() {
+    $this->quickCleanup([], TRUE);
+    parent::tearDown();
   }
 
+  /**
+   * Test creating a custom field.
+   */
   public function testCreateCustomField() {
     $customGroup = $this->createCustomField();
     $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
@@ -41,6 +50,9 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     $this->customGroupDelete($customGroup['id']);
   }
 
+  /**
+   * Test custom field create accepts passed column name.
+   */
   public function testCreateCustomFieldColumnName() {
     $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
     $fields = [
@@ -61,6 +73,9 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     $this->customGroupDelete($customGroup['id']);
   }
 
+  /**
+   * Test that name is used for the column.
+   */
   public function testCreateCustomFieldName() {
     $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
     $fields = [
@@ -81,6 +96,9 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     $this->customGroupDelete($customGroup['id']);
   }
 
+  /**
+   * Test get fields function.
+   */
   public function testGetFields() {
     $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
     $fields = [
@@ -109,6 +127,9 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     $this->customGroupDelete($customGroup['id']);
   }
 
+  /**
+   * @throws \Exception
+   */
   public function testGetDisplayedValues() {
     $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
     $fieldsToCreate = [
@@ -168,10 +189,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
       ],
     ];
     foreach ($fieldsToCreate as $num => $field) {
-      $params = $field + [
-          'label' => 'test field ' . $num,
-          'custom_group_id' => $customGroup['id'],
-        ];
+      $params = $field + ['label' => 'test field ' . $num, 'custom_group_id' => $customGroup['id']];
       unset($params['tests']);
       $createdField = $this->callAPISuccess('customField', 'create', $params);
       foreach ($field['tests'] as $expected => $input) {
@@ -182,6 +200,12 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     $this->customGroupDelete($customGroup['id']);
   }
 
+  /**
+   * Test CRM_Core_BAO_CustomField::displayValue.
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \Exception
+   */
   public function testGetDisplayedValuesContactRef() {
     $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
     $params = [
@@ -226,6 +250,8 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
    * Move a custom field from $groupA to $groupB.
    *
    * Make sure that data records are correctly matched and created.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testMoveField() {
     $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
@@ -356,6 +382,8 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
 
   /**
    * Test get custom field id function.
+   *
+   * @throws \CiviCRM_API3_Exception
    */
   public function testGetCustomFieldID() {
     $this->createCustomField();
@@ -399,14 +427,17 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
   }
 
   /**
-   * Tet the getFieldsForImport function.
+   * Test the getFieldsForImport function.
+   *
+   * @throws \Exception
    */
   public function testGetFieldsForImport() {
     $this->entity = 'Contact';
     $this->createCustomGroupWithFieldsOfAllTypes();
+    $customGroupID = $this->ids['CustomGroup']['Custom Group'];
     $expected = [
       $this->getCustomFieldName('country') => [
-        'name' => $this->getCustomFieldName('country') ,
+        'name' => $this->getCustomFieldName('country'),
         'type' => 1,
         'title' => 'Country',
         'headerPattern' => '//',
@@ -417,8 +448,27 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'data_type' => 'Int',
         'html_type' => 'Select Country',
         'is_search_range' => '0',
+        'id' => $this->getCustomFieldID('country'),
+        'label' => 'Country',
+        'groupTitle' => 'Custom Group',
+        'default_value' => NULL,
+        'custom_group_id' => $customGroupID,
+        'extends' => 'Contact',
+        'extends_entity_column_value' => NULL,
+        'extends_entity_column_id' => NULL,
+        'is_view' => '0',
+        'is_multiple' => '0',
+        'option_group_id' => NULL,
+        'date_format' => NULL,
+        'time_format' => NULL,
+        'is_required' => '0',
+        'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
+        'column_name' => 'country_' . $this->getCustomFieldID('country'),
+        'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('country'),
+        'extends_table' => 'civicrm_contact',
+        'search_table' => 'contact_a',
       ],
-      $this->getCustomFieldName('file')  => [
+      $this->getCustomFieldName('file') => [
         'name' => $this->getCustomFieldName('file'),
         'type' => 2,
         'title' => 'Custom Field',
@@ -430,6 +480,25 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'data_type' => 'File',
         'html_type' => 'File',
         'is_search_range' => '0',
+        'id' => $this->getCustomFieldID('file'),
+        'label' => 'Custom Field',
+        'groupTitle' => 'Custom Group',
+        'default_value' => NULL,
+        'custom_group_id' => $customGroupID,
+        'extends' => 'Contact',
+        'extends_entity_column_value' => NULL,
+        'extends_entity_column_id' => NULL,
+        'is_view' => '0',
+        'is_multiple' => '0',
+        'option_group_id' => NULL,
+        'date_format' => NULL,
+        'time_format' => NULL,
+        'is_required' => '0',
+        'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
+        'column_name' => 'custom_field_' . $this->getCustomFieldID('file'),
+        'where' => 'civicrm_value_custom_group_' . $customGroupID . '.custom_field_' . $this->getCustomFieldID('file'),
+        'extends_table' => 'civicrm_contact',
+        'search_table' => 'contact_a',
       ],
       $this->getCustomFieldName('text') => [
         'name' => $this->getCustomFieldName('text'),
@@ -443,9 +512,28 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'data_type' => 'String',
         'html_type' => 'Text',
         'is_search_range' => '0',
+        'id' => $this->getCustomFieldID('text'),
+        'label' => 'Enter text here',
+        'groupTitle' => 'Custom Group',
+        'default_value' => 'xyz',
+        'custom_group_id' => '1',
+        'extends' => 'Contact',
+        'extends_entity_column_value' => NULL,
+        'extends_entity_column_id' => NULL,
+        'is_view' => '0',
+        'is_multiple' => '0',
+        'option_group_id' => NULL,
+        'date_format' => NULL,
+        'time_format' => NULL,
+        'is_required' => '1',
+        'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
+        'column_name' => 'enter_text_here_' . $this->getCustomFieldID('text'),
+        'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'),
+        'extends_table' => 'civicrm_contact',
+        'search_table' => 'contact_a',
       ],
       $this->getCustomFieldName('select_string') => [
-        'name' =>  $this->getCustomFieldName('select_string'),
+        'name' => $this->getCustomFieldName('select_string'),
         'type' => 2,
         'title' => 'Pick Color',
         'headerPattern' => '//',
@@ -456,6 +544,29 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'data_type' => 'String',
         'html_type' => 'Select',
         'is_search_range' => '0',
+        'id' => $this->getCustomFieldID('select_string'),
+        'label' => 'Pick Color',
+        'groupTitle' => 'Custom Group',
+        'default_value' => NULL,
+        'custom_group_id' => $customGroupID,
+        'extends' => 'Contact',
+        'extends_entity_column_value' => NULL,
+        'extends_entity_column_id' => NULL,
+        'is_view' => '0',
+        'is_multiple' => '0',
+        'option_group_id' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id']),
+        'date_format' => NULL,
+        'time_format' => NULL,
+        'is_required' => '1',
+        'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
+        'column_name' => 'pick_color_' . $this->getCustomFieldID('select_string'),
+        'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'),
+        'extends_table' => 'civicrm_contact',
+        'search_table' => 'contact_a',
+        'pseudoconstant' => [
+          'optionGroupName' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
+          'optionEditPath' => 'civicrm/admin/options/' . $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
+        ],
       ],
       $this->getCustomFieldName('select_date') => [
         'name' => $this->getCustomFieldName('select_date'),
@@ -471,6 +582,23 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'is_search_range' => '0',
         'date_format' => 'mm/dd/yy',
         'time_format' => '1',
+        'id' => $this->getCustomFieldID('select_date'),
+        'label' => 'test_date',
+        'groupTitle' => 'Custom Group',
+        'default_value' => '20090711',
+        'custom_group_id' => $customGroupID,
+        'extends' => 'Contact',
+        'extends_entity_column_value' => NULL,
+        'extends_entity_column_id' => NULL,
+        'is_view' => '0',
+        'is_multiple' => '0',
+        'option_group_id' => NULL,
+        'is_required' => '0',
+        'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
+        'column_name' => 'test_date_' . $this->getCustomFieldID('select_date'),
+        'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_date_' . $this->getCustomFieldID('select_date'),
+        'extends_table' => 'civicrm_contact',
+        'search_table' => 'contact_a',
       ],
       $this->getCustomFieldName('link') => [
         'name' => $this->getCustomFieldName('link'),
@@ -484,6 +612,25 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'data_type' => 'Link',
         'html_type' => 'Link',
         'is_search_range' => '0',
+        'id' => $this->getCustomFieldID('link'),
+        'label' => 'test_link',
+        'groupTitle' => 'Custom Group',
+        'default_value' => 'http://civicrm.org',
+        'custom_group_id' => $customGroupID,
+        'extends' => 'Contact',
+        'extends_entity_column_value' => NULL,
+        'extends_entity_column_id' => NULL,
+        'is_view' => '0',
+        'is_multiple' => '0',
+        'option_group_id' => NULL,
+        'date_format' => NULL,
+        'time_format' => NULL,
+        'is_required' => '1',
+        'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
+        'column_name' => 'test_link_' . $this->getCustomFieldID('link'),
+        'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'),
+        'extends_table' => 'civicrm_contact',
+        'search_table' => 'contact_a',
       ],
     ];
     $this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport());