[REF] [Import] [Test] switch test to use full form flow
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 17 Apr 2022 22:49:57 +0000 (10:49 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 18 Apr 2022 00:41:54 +0000 (12:41 +1200)
CRM/Contact/Import/Form/DataSource.php
tests/phpunit/CRM/Import/DataSource/CsvTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 8a76bb05f321938d057048a2e9a379e119c3fec0..4123c70d46b370bc14df025f57c2cc8b35842f14 100644 (file)
@@ -236,7 +236,7 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Import_Forms {
       }
       $this->set('disableUSPS', !empty($this->_params['disableUSPS']));
 
-      $this->set('dataSource', $this->_params['dataSource']);
+      $this->set('dataSource', $this->getSubmittedValue('dataSource'));
       $this->set('skipColumnHeader', CRM_Utils_Array::value('skipColumnHeader', $this->_params));
 
       CRM_Core_Session::singleton()->set('dateTypes', $storeParams['dateFormats']);
index f7d1dda343656a4cd266b8ffc01c103bff97ada9..41626941ee8e343dcede2ca5bfe7254b87e1456b 100644 (file)
@@ -20,25 +20,9 @@ class CRM_Import_DataSource_CsvTest extends CiviUnitTestCase {
    * @param array $fileData
    *
    * @dataProvider getCsvFiles
-   * @throws \CRM_Core_Exception
    */
-  public function testToCsv(array $fileData) {
-    $dataSource = new CRM_Import_DataSource_CSV();
-    $params = [
-      'uploadFile' => [
-        'name' => __DIR__ . '/' . $fileData['filename'],
-      ],
-      'skipColumnHeader' => TRUE,
-    ];
-
-    // Get the PEAR::DB object
-    $dao = new CRM_Core_DAO();
-    $db = $dao->getDatabaseConnection();
-
-    $form = new CRM_Contact_Import_Form_DataSource();
-    $form->controller = new CRM_Contact_Import_Controller();
-
-    $dataSource->postProcess($params, $db, $form);
+  public function testToCsv(array $fileData): void {
+    $form = $this->submitDatasourceForm($fileData['filename']);
     $tableName = $form->get('importTableName');
     foreach (['first_name', 'last_name', 'email'] as $field) {
       $json = json_encode(CRM_Core_DAO::singleValueQuery("SELECT $field FROM $tableName"));
@@ -52,7 +36,7 @@ class CRM_Import_DataSource_CsvTest extends CiviUnitTestCase {
    *
    * @return array
    */
-  public function getCsvFiles() {
+  public function getCsvFiles(): array {
     return [
       // import.csv is utf8-encoded, with no BOM
       [
@@ -95,17 +79,20 @@ class CRM_Import_DataSource_CsvTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test the trim function
+   * Test the trim function.
+   *
    * @dataProvider trimDataProvider
+   *
    * @param string $input
    * @param string $expected
    */
-  public function testTrim(string $input, string $expected) {
+  public function testTrim(string $input, string $expected): void {
     $this->assertSame($expected, CRM_Import_DataSource_CSV::trimNonBreakingSpaces($input));
   }
 
   /**
-   * Dataprovider for testTrim
+   * DataProvider for testTrim.
+   *
    * @return array
    */
   public function trimDataProvider(): array {
@@ -132,27 +119,33 @@ class CRM_Import_DataSource_CsvTest extends CiviUnitTestCase {
    * skipped because of some checking for column-count matches in the import,
    * and so you don't hit the current fail.
    */
-  public function testBlankLineAtEnd() {
-    $dataSource = new CRM_Import_DataSource_CSV();
-    $params = [
-      'uploadFile' => [
-        'name' => __DIR__ . '/blankLineAtEnd.csv',
-      ],
-      'skipColumnHeader' => TRUE,
-    ];
-
-    // Get the PEAR::DB object
-    $dao = new CRM_Core_DAO();
-    $db = $dao->getDatabaseConnection();
-
-    $form = new CRM_Contact_Import_Form_DataSource();
-    $form->controller = new CRM_Contact_Import_Controller();
-
-    $dataSource->postProcess($params, $db, $form);
+  public function testBlankLineAtEnd(): void {
+    $form = $this->submitDatasourceForm('blankLineAtEnd.csv');
     $tableName = $form->get('importTableName');
     $json = json_encode(CRM_Core_DAO::singleValueQuery("SELECT email FROM $tableName"));
     $this->assertEquals('"yogi@yellowstone.park"', $json);
     CRM_Core_DAO::executeQuery("DROP TABLE $tableName");
   }
 
+  /**
+   * Test submitting the datasource form.
+   *
+   * @param string $csvFileName
+   *
+   * @return \CRM_Contact_Import_Form_DataSource
+   */
+  protected function submitDatasourceForm(string $csvFileName): CRM_Contact_Import_Form_DataSource {
+    $_GET['dataSource'] = 'CRM_Import_DataSource_CSV';
+    /* @var CRM_Contact_Import_Form_DataSource $form */
+    $form = $this->getFormObject('CRM_Contact_Import_Form_DataSource', [
+      'uploadFile' => [
+        'name' => __DIR__ . '/' . $csvFileName,
+      ],
+      'skipColumnHeader' => TRUE,
+    ]);
+    $form->buildForm();
+    $form->postProcess();
+    return $form;
+  }
+
 }
index eff00f48b3a9ca3bfb8f0a12f251bad7b454eb94..903dfb47bd6c61784ae8cc5901210e7d712f03a1 100644 (file)
@@ -3218,6 +3218,9 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
       case 'CRM_Event_Form_Registration_Confirm':
         $form->controller = new CRM_Event_Controller_Registration();
         break;
+      case 'CRM_Contact_Import_Form_DataSource':
+        $form->controller = new CRM_Contact_Import_Controller();
+        break;
 
       case strpos($class, '_Form_') !== FALSE:
         $form->controller = new CRM_Core_Controller_Simple($class, $pageName);