From 63918d376faf25f699f54e61122c36ade0828f40 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 24 Aug 2020 21:26:23 -0700 Subject: [PATCH] Forward port https://github.com/civicrm/civicrm-core/pull/18141 --- CRM/Core/Resources.php | 4 ++-- tests/phpunit/CRM/Core/ResourcesTest.php | 27 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 5b7765a33a..795789474c 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -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(); } /** diff --git a/tests/phpunit/CRM/Core/ResourcesTest.php b/tests/phpunit/CRM/Core/ResourcesTest.php index f39a7a329f..e3e8627bd8 100644 --- a/tests/phpunit/CRM/Core/ResourcesTest.php +++ b/tests/phpunit/CRM/Core/ResourcesTest.php @@ -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); } -- 2.25.1