Better unit testing for event copy
authorSamuel Vanhove <samuel@symbiotic.coop>
Fri, 28 May 2021 22:54:41 +0000 (18:54 -0400)
committerSamuel Vanhove <samuel@symbiotic.coop>
Fri, 28 May 2021 22:54:41 +0000 (18:54 -0400)
tests/phpunit/CRM/Core/CopyTest.php

index 52918d9d2deb4f77fb8007f562e48ecb24f331f9..76bff8c3590885e91c8e2ff480196252c45c65e1 100644 (file)
@@ -40,11 +40,11 @@ class CRM_Core_CopyTest extends CiviUnitTestCase {
 
   public function testI18nEventCopy() {
 
-    $otherLocale = 'fr_CA';
-    $locSuffix = " ({$otherLocale})";
+    $locales = ['en_US', 'fr_CA', 'nl_NL'];
 
     $this->enableMultilingual();
-    CRM_Core_I18n_Schema::addLocale($otherLocale, 'en_US');
+    CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');
+    CRM_Core_I18n_Schema::addLocale('nl_NL', 'en_US');
 
     CRM_Core_I18n::singleton()->setLocale('en_US');
 
@@ -56,19 +56,48 @@ class CRM_Core_CopyTest extends CiviUnitTestCase {
     $locParams = [
       'summary',
       'description',
+      'event_full_text',
+      'registration_link_text',
+      'fee_label',
+      'intro_text',
+      'footer_text',
+      'confirm_title',
+      'confirm_text',
+      'confirm_footer_text',
+      'confirm_email_text',
+      'confirm_from_name',
+      'thankyou_title',
+      'thankyou_text',
+      'thankyou_footer_text',
+      'pay_later_text',
+      'pay_later_receipt',
+      'initial_amount_label',
+      'initial_amount_help_text',
     ];
 
-    CRM_Core_I18n::singleton()->setLocale($otherLocale);
-    $ploc = ['id' => $eventId];
+    // init in case it's not defined
     foreach ($locParams as $field) {
-      $ploc[$field] = $eventData[$field] . $locSuffix;
+      $eventData[$field] = $eventData[$field] ?? '';
     }
-    $this->callAPISuccess('Event', 'create', $ploc);
 
+    // differencing the data in original content for each locales
+    foreach ($locales as $locale) {
+      CRM_Core_I18n::singleton()->setLocale($locale);
+      $locSuffix = " ({$locale})";
+      $ploc = ['id' => $eventId];
+      foreach ($locParams as $field) {
+        $ploc[$field] = $eventData[$field] . $locSuffix;
+      }
+
+      $res = $this->callAPISuccess('Event', 'create', $ploc);
+    }
+
+    // now that the data is different, do the copy
     CRM_Core_I18n::singleton()->setLocale('en_US');
     $eventCopy = CRM_Event_BAO_Event::copy($eventId);
     $eventCopyId = $eventCopy->id;
 
+    // define the fields that doesn't change
     $identicalParams = [
       'event_type_id',
       'is_public',
@@ -78,33 +107,24 @@ class CRM_Core_CopyTest extends CiviUnitTestCase {
       'registration_start_date',
       'registration_end_date',
       'max_participants',
-      'event_full_text',
       'is_monetary',
       'is_active',
       'is_show_location',
       'is_email_confirm',
     ];
 
-    // en_US should be as the original
-    CRM_Core_I18n::singleton()->setLocale('en_US');
-    $eventCopy = civicrm_api3('Event', 'getsingle', ['id' => $eventCopyId]);
-    foreach ($identicalParams as $name) {
-      $this->assertEquals($eventCopy[$name], $eventData[$name]);
-    }
-    // title is special
-    $this->assertEquals($eventCopy['title'], 'Copy of ' . $eventData['title']);
+    // check the data on the copy
+    foreach ($locales as $locale) {
+      CRM_Core_I18n::singleton()->setLocale($locale);
+      $locSuffix = " ({$locale})";
+      $eventCopy = civicrm_api3('Event', 'getsingle', ['id' => $eventCopyId]);
 
-    // localized fields should be different
-    CRM_Core_I18n::singleton()->setLocale($otherLocale);
-    $eventCopy = civicrm_api3('Event', 'getsingle', ['id' => $eventCopyId]);
-    foreach ($identicalParams as $name) {
-      $this->assertEquals($eventCopy[$name], $eventData[$name]);
-    }
-    foreach ($locParams as $name) {
-      $this->assertEquals($eventCopy[$name], $eventData[$name] . $locSuffix);
+      // title is special
+      $this->assertEquals($eventCopy['title'], 'Copy of ' . $eventData['title']);
+
+      // other fields
+      $this->compareLocalizedCopy($eventData, $eventCopy, $locParams, $identicalParams, $locSuffix);
     }
-    // title is special
-    $this->assertEquals($eventCopy['title'], 'Copy of ' . $eventData['title']);
 
     // reset to en_US only
     CRM_Core_I18n::singleton()->setLocale('en_US');
@@ -112,4 +132,15 @@ class CRM_Core_CopyTest extends CiviUnitTestCase {
 
   }
 
+  protected function compareLocalizedCopy($source, $dest, $locParams, $identicalParams, $locSuffix) {
+
+    foreach ($identicalParams as $name) {
+      $this->assertEquals($dest[$name], $source[$name]);
+    }
+    foreach ($locParams as $name) {
+      $this->assertEquals($dest[$name], $source[$name] . $locSuffix);
+    }
+
+  }
+
 }