Forward port https://github.com/civicrm/civicrm-core/pull/18141
authorTim Otten <totten@civicrm.org>
Tue, 25 Aug 2020 04:26:23 +0000 (21:26 -0700)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 3 Sep 2020 22:02:17 +0000 (08:02 +1000)
CRM/Core/Resources.php
tests/phpunit/CRM/Core/ResourcesTest.php

index 5b7765a33a6c1c2d4bab98040310da4ef547462f..795789474c8a0234dc23ea86c273bf1b92fb2dc6 100644 (file)
@@ -224,8 +224,8 @@ class CRM_Core_Resources implements CRM_Core_Resources_CollectionAdderInterface
   /**
    * Helper fn for addSettingsFactory.
    */
-  public function getSettings() {
-    return $this->getSettingRegion()->getSettings();
+  public function getSettings($region = NULL) {
+    return $this->getSettingRegion($region)->getSettings();
   }
 
   /**
index f39a7a329fd6c32e6301de6a9da5cc51ff58001d..e3e8627bd847a90e15c93252d422138aa8dd2285 100644 (file)
@@ -138,7 +138,7 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
           ],
         ],
       ],
-      $this->res->getSettings()
+      $this->res->getSettings('html-header')
     );
   }
 
@@ -148,10 +148,23 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
       ->addSetting(['fruit' => ['yours' => 'orange']]);
     $this->assertTreeEquals(
       ['fruit' => ['yours' => 'orange', 'mine' => 'apple']],
-      $this->res->getSettings()
+      $this->res->getSettings('html-header')
     );
     $actual = CRM_Core_Region::instance('html-header')->render('');
-    $expected = json_encode(['fruit' => ['yours' => 'orange', 'mine' => 'apple']]);
+    $expected = 'var CRM = ' . json_encode(['fruit' => ['yours' => 'orange', 'mine' => 'apple']]) . ';';
+    $this->assertTrue(strpos($actual, $expected) !== FALSE);
+  }
+
+  public function testAddSettingToBillingBlock() {
+    $this->res
+      ->addSetting(['cheese' => ['cheddar' => 'yellow']], 'billing-block')
+      ->addSetting(['cheese' => ['edam' => 'red']], 'billing-block');
+    $this->assertTreeEquals(
+      ['cheese' => ['edam' => 'red', 'cheddar' => 'yellow']],
+      $this->res->getSettings('billing-block')
+    );
+    $actual = CRM_Core_Region::instance('billing-block')->render('');
+    $expected = 'CRM.$.extend(true, CRM, ' . json_encode(['cheese' => ['edam' => 'red', 'cheddar' => 'yellow']]) . ');';
     $this->assertTrue(strpos($actual, $expected) !== FALSE);
   }
 
@@ -162,7 +175,7 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
       $event->data['fruit']['mine'] = 'banana';
     });
     $this->res->addSetting(['fruit' => ['mine' => 'apple']]);
-    $settings = $this->res->getSettings();
+    $settings = $this->res->getSettings('html-header');
     $this->assertTreeEquals(['fruit' => ['mine' => 'banana']], $settings);
   }
 
@@ -174,7 +187,7 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
       return ['fruit' => ['mine' => 'apple']];
     });
 
-    $actual = $this->res->getSettings();
+    $actual = $this->res->getSettings('html-header');
     $expected = ['fruit' => ['yours' => 'orange', 'mine' => 'apple']];
     $this->assertTreeEquals($expected, $actual);
   }
@@ -186,14 +199,14 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
     $this->res->addSettingsFactory(function () use (&$muckableValue) {
       return $muckableValue;
     });
-    $actual = $this->res->getSettings();
+    $actual = $this->res->getSettings('html-header');
     $expected = ['fruit' => ['mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot']];
     $this->assertTreeEquals($expected, $actual);
 
     // note: the setting is not fixed based on what the factory returns when registered; it's based
     // on what the factory returns when getSettings is called
     $muckableValue = ['fruit' => ['yours' => 'banana']];
-    $actual = $this->res->getSettings();
+    $actual = $this->res->getSettings('html-header');
     $expected = ['fruit' => ['mine' => 'apple', 'yours' => 'banana']];
     $this->assertTreeEquals($expected, $actual);
   }