Merge pull request #15321 from yashodha/dev_1065
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomFieldTest.php
index 89bb6be83fd6c87aa0a94909a45f7a44fe0f6d64..53c1456ea6e112c36671bdd3c37cff6f4063a5d0 100644 (file)
@@ -11,10 +11,6 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
 
   protected $customFieldID;
 
-  public function setUp() {
-    parent::setUp();
-  }
-
   /**
    * Clean up after test.
    *
@@ -25,19 +21,22 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     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',
       'Database check for created CustomField.'
     );
-    $fields = array(
+    $fields = [
       'id' => $customFieldID,
       'label' => 'editTestFld',
       'is_active' => 1,
       'data_type' => 'String',
       'html_type' => 'Text',
       'custom_group_id' => $customGroup['id'],
-    );
+    ];
 
     CRM_Core_BAO_CustomField::create($fields);
     $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.');
@@ -51,15 +50,18 @@ 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(array('extends' => 'Individual'));
-    $fields = array(
+    $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
+    $fields = [
       'label' => 'testFld 2',
       'column_name' => 'special_colname',
       'data_type' => 'String',
       'html_type' => 'Text',
       'custom_group_id' => $customGroup['id'],
-    );
+    ];
     CRM_Core_BAO_CustomField::create($fields);
     $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
       'Database check for created CustomField.'
@@ -71,15 +73,18 @@ 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(array('extends' => 'Individual'));
-    $fields = array(
+    $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
+    $fields = [
       'label' => 'testFld 2',
       'name' => 'special_fldlname',
       'data_type' => 'String',
       'html_type' => 'Text',
       'custom_group_id' => $customGroup['id'],
-    );
+    ];
     CRM_Core_BAO_CustomField::create($fields);
     $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
       'Database check for created CustomField.'
@@ -91,26 +96,29 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     $this->customGroupDelete($customGroup['id']);
   }
 
+  /**
+   * Test get fields function.
+   */
   public function testGetFields() {
-    $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
-    $fields = array(
+    $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
+    $fields = [
       'label' => 'testFld1',
       'data_type' => 'String',
       'html_type' => 'Text',
       'is_active' => 1,
       'custom_group_id' => $customGroup['id'],
-    );
+    ];
     CRM_Core_BAO_CustomField::create($fields);
     $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
       'Database check for created CustomField.'
     );
-    $fields = array(
+    $fields = [
       'label' => 'testFld2',
       'data_type' => 'String',
       'html_type' => 'Text',
       'is_active' => 1,
       'custom_group_id' => $customGroup['id'],
-    );
+    ];
     CRM_Core_BAO_CustomField::create($fields);
     $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
       'Database check for created CustomField.'
@@ -119,69 +127,69 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     $this->customGroupDelete($customGroup['id']);
   }
 
+  /**
+   * @throws \Exception
+   */
   public function testGetDisplayedValues() {
-    $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
-    $fieldsToCreate = array(
-      array(
+    $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
+    $fieldsToCreate = [
+      [
         'data_type' => 'Country',
         'html_type' => 'Select Country',
-        'tests' => array(
+        'tests' => [
           'United States' => 1228,
           '' => NULL,
-        ),
-      ),
-      array(
+        ],
+      ],
+      [
         'data_type' => 'StateProvince',
         'html_type' => 'Multi-Select State/Province',
-        'tests' => array(
+        'tests' => [
           '' => 0,
           'Alabama' => 1000,
-          'Alabama, Alaska' => array(1000, 1001),
-        ),
-      ),
-      array(
+          'Alabama, Alaska' => [1000, 1001],
+        ],
+      ],
+      [
         'data_type' => 'String',
         'html_type' => 'Radio',
-        'option_values' => array(
+        'option_values' => [
           'key' => 'KeyLabel',
-        ),
-        'tests' => array(
+        ],
+        'tests' => [
           'KeyLabel' => 'key',
-        ),
-      ),
-      array(
+        ],
+      ],
+      [
         'data_type' => 'String',
         'html_type' => 'CheckBox',
-        'option_values' => array(
+        'option_values' => [
           'key1' => 'Label1',
           'key2' => 'Label2',
           'key3' => 'Label3',
           'key4' => 'Label4',
-        ),
-        'tests' => array(
-          'Label1' => array('key1'),
+        ],
+        'tests' => [
+          'Label1' => ['key1'],
           'Label2' => 'key2',
-          'Label2, Label3' => array('key2', 'key3'),
-          'Label3, Label4' => CRM_Utils_Array::implodePadded(array('key3', 'key4')),
-          'Label1, Label4' => array('key1' => 1, 'key4' => 1),
-        ),
-      ),
-      array(
+          'Label2, Label3' => ['key2', 'key3'],
+          'Label3, Label4' => CRM_Utils_Array::implodePadded(['key3', 'key4']),
+          'Label1, Label4' => ['key1' => 1, 'key4' => 1],
+        ],
+      ],
+      [
         'data_type' => 'Date',
         'html_type' => 'Select Date',
         'date_format' => 'd M yy',
         'time_format' => 1,
-        'tests' => array(
+        'tests' => [
           '1 Jun 1999 1:30PM' => '1999-06-01 13:30',
           '' => '',
-        ),
-      ),
-    );
+        ],
+      ],
+    ];
     foreach ($fieldsToCreate as $num => $field) {
-      $params = $field + array(
-        '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) {
@@ -192,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 = [
@@ -213,13 +227,13 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
   }
 
   public function testDeleteCustomField() {
-    $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
-    $fields = array(
+    $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
+    $fields = [
       'custom_group_id' => $customGroup['id'],
       'label' => 'Throwaway Field',
       'dataType' => 'Memo',
       'htmlType' => 'TextArea',
-    );
+    ];
 
     $customField = $this->customFieldCreate($fields);
     $fieldObject = new CRM_Core_BAO_CustomField();
@@ -236,79 +250,81 @@ 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));
     $this->assertTrue($countriesByName['Andorra'] > 0);
-    $groups = array(
-      'A' => $this->customGroupCreate(array(
+    $groups = [
+      'A' => $this->customGroupCreate([
         'title' => 'Test_Group A',
         'name' => 'test_group_a',
-        'extends' => array('Individual'),
+        'extends' => ['Individual'],
         'style' => 'Inline',
         'is_multiple' => 0,
         'is_active' => 1,
         'version' => 3,
-      )),
-      'B' => $this->customGroupCreate(array(
+      ]),
+      'B' => $this->customGroupCreate([
         'title' => 'Test_Group B',
         'name' => 'test_group_b',
-        'extends' => array('Individual'),
+        'extends' => ['Individual'],
         'style' => 'Inline',
         'is_multiple' => 0,
         'is_active' => 1,
         'version' => 3,
-      )),
-    );
+      ]),
+    ];
     $groupA = $groups['A']['values'][$groups['A']['id']];
     $groupB = $groups['B']['values'][$groups['B']['id']];
-    $countryA = $this->customFieldCreate(array(
+    $countryA = $this->customFieldCreate([
       'custom_group_id' => $groups['A']['id'],
       'label' => 'Country A',
       'dataType' => 'Country',
       'htmlType' => 'Select Country',
       'default_value' => NULL,
-    ));
-    $countryB = $this->customFieldCreate(array(
+    ]);
+    $countryB = $this->customFieldCreate([
       'custom_group_id' => $groups['A']['id'],
       'label' => 'Country B',
       'dataType' => 'Country',
       'htmlType' => 'Select Country',
       'default_value' => NULL,
-    ));
-    $countryC = $this->customFieldCreate(array(
+    ]);
+    $countryC = $this->customFieldCreate([
       'custom_group_id' => $groups['B']['id'],
       'label' => 'Country C',
       'dataType' => 'Country',
       'htmlType' => 'Select Country',
       'default_value' => NULL,
-    ));
+    ]);
 
-    $fields = array(
+    $fields = [
       'countryA' => $countryA['values'][$countryA['id']],
       'countryB' => $countryB['values'][$countryB['id']],
       'countryC' => $countryC['values'][$countryC['id']],
-    );
-    $contacts = array(
-      'alice' => $this->individualCreate(array(
+    ];
+    $contacts = [
+      'alice' => $this->individualCreate([
         'first_name' => 'Alice',
         'last_name' => 'Albertson',
         'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'],
         'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'],
-      )),
-      'bob' => $this->individualCreate(array(
+      ]),
+      'bob' => $this->individualCreate([
         'first_name' => 'Bob',
         'last_name' => 'Roberts',
         'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'],
         'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'],
         'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'],
-      )),
-      'carol' => $this->individualCreate(array(
+      ]),
+      'carol' => $this->individualCreate([
         'first_name' => 'Carol',
         'last_name' => 'Carolson',
         'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'],
-      )),
-    );
+      ]),
+    ];
 
     // Move!
     CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']);
@@ -329,10 +345,10 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
             WHERE entity_id = %1
             AND {$fields['countryB']['column_name']} = %3
             AND {$fields['countryC']['column_name']} is null",
-      array(
-        1 => array($contacts['alice'], 'Integer'),
-        3 => array($countriesByName['Barbados'], 'Integer'),
-      )
+      [
+        1 => [$contacts['alice'], 'Integer'],
+        3 => [$countriesByName['Barbados'], 'Integer'],
+      ]
     );
 
     // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
@@ -341,11 +357,11 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
             WHERE entity_id = %1
             AND {$fields['countryB']['column_name']} = %3
             AND {$fields['countryC']['column_name']} = %4",
-      array(
-        1 => array($contacts['bob'], 'Integer'),
-        3 => array($countriesByName['Bermuda'], 'Integer'),
-        4 => array($countriesByName['Chad'], 'Integer'),
-      )
+      [
+        1 => [$contacts['bob'], 'Integer'],
+        3 => [$countriesByName['Bermuda'], 'Integer'],
+        4 => [$countriesByName['Chad'], 'Integer'],
+      ]
     );
 
     // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
@@ -354,10 +370,10 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
             WHERE entity_id = %1
             AND {$fields['countryB']['column_name']} is null
             AND {$fields['countryC']['column_name']} = %4",
-      array(
-        1 => array($contacts['carol'], 'Integer'),
-        4 => array($countriesByName['Cambodia'], 'Integer'),
-      )
+      [
+        1 => [$contacts['carol'], 'Integer'],
+        4 => [$countriesByName['Cambodia'], 'Integer'],
+      ]
     );
 
     $this->customGroupDelete($groups['A']['id']);
@@ -429,7 +445,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'custom_field_id' => $this->getCustomFieldID('country'),
         'options_per_line' => NULL,
         'text_length' => NULL,
-        'data_type' => 'Int',
+        'data_type' => 'Country',
         'html_type' => 'Select Country',
         'is_search_range' => '0',
         'id' => $this->getCustomFieldID('country'),
@@ -451,6 +467,12 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('country'),
         'extends_table' => 'civicrm_contact',
         'search_table' => 'contact_a',
+        'pseudoconstant' => [
+          'table' => 'civicrm_country',
+          'keyColumn' => 'id',
+          'labelColumn' => 'name',
+          'nameColumn' => 'iso_code',
+        ],
       ],
       $this->getCustomFieldName('file') => [
         'name' => $this->getCustomFieldName('file'),
@@ -492,7 +514,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'import' => 1,
         'custom_field_id' => $this->getCustomFieldID('text'),
         'options_per_line' => NULL,
-        'text_length' => NULL,
+        'text_length' => 300,
         'data_type' => 'String',
         'html_type' => 'Text',
         'is_search_range' => '0',
@@ -515,6 +537,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
         'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'),
         'extends_table' => 'civicrm_contact',
         'search_table' => 'contact_a',
+        'maxlength' => 300,
       ],
       $this->getCustomFieldName('select_string') => [
         'name' => $this->getCustomFieldName('select_string'),
@@ -620,4 +643,60 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
     $this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport());
   }
 
+  /**
+   * Test the bulk create function works.
+   */
+  public function testBulkCreate() {
+    $customGroup = $this->customGroupCreate([
+      'extends' => 'Individual',
+      'title' => 'my bulk group',
+    ]);
+    CRM_Core_BAO_CustomField::bulkSave([
+      [
+        'label' => 'Test',
+        'data_type' => 'String',
+        'html_type' => 'Text',
+        'column_name' => 'my_text',
+      ],
+      [
+        'label' => 'test_link',
+        'data_type' => 'Link',
+        'html_type' => 'Link',
+        'is_search_range' => '0',
+      ],
+    ],
+    [
+      'custom_group_id' => $customGroup['id'],
+      'is_active' => 1,
+      'is_searchable' => 1,
+    ]);
+    $dao = CRM_Core_DAO::executeQuery(('SHOW CREATE TABLE ' . $customGroup['values'][$customGroup['id']]['table_name']));
+    $dao->fetch();
+    $this->assertContains('`test_link_2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL', $dao->Create_Table);
+    $this->assertContains('KEY `INDEX_my_text` (`my_text`)', $dao->Create_Table);
+  }
+
+  /**
+   * Check that outputting the display value for a file field with No description doesn't generate error
+   */
+  public function testFileDisplayValueNoDescription() {
+    $customGroup = $this->customGroupCreate([
+      'extends' => 'Individual',
+      'title' => 'Test Contact File Custom Group',
+    ]);
+    $fileField = $this->customFieldCreate([
+      'custom_group_id' => $customGroup['id'],
+      'data_type' => 'File',
+      'html_type' => 'File',
+      'default_value' => '',
+    ]);
+    $filePath = Civi::paths()->getPath('[civicrm.files]/custom/test_file.txt');
+    $file = $this->callAPISuccess('File', 'create', [
+      'uri' => $filePath,
+    ]);
+    $individual = $this->individualCreate(['custom_' . $fileField['id'] => $file['id']]);
+    $expectedDisplayValue = CRM_Core_BAO_File::paperIconAttachment('*', $file['id'])[$file['id']];
+    $this->assertEquals($expectedDisplayValue, CRM_Core_BAO_CustomField::displayValue($file['id'], $fileField['id']));
+  }
+
 }