Merge pull request #15321 from yashodha/dev_1065
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomFieldTest.php
index 70449f8d6ccb9254ab35721f375adea896ca2306..53c1456ea6e112c36671bdd3c37cff6f4063a5d0 100644 (file)
@@ -445,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'),
@@ -467,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'),
@@ -508,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',
@@ -531,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'),
@@ -636,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']));
+  }
+
 }