From 924d1dc88c0037a50ccb559f3af5a8449baca2e2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 1 Oct 2020 17:16:44 -0700 Subject: [PATCH] (REF) Convert hidden function 'useRegion' to public 'fillDefaults' This mostly affects a hypothetical bundle defined outside of core's `Common.php`: * Before: You cannot re-use `useRegion()`. You'd have to write it again. * After: You can re-use and/or override `fillDefaults()`. --- CRM/Core/Resources/Bundle.php | 23 +++++++++++++++++++ CRM/Core/Resources/Common.php | 23 +++---------------- .../phpunit/CRM/Core/Resources/BundleTest.php | 16 +++++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/CRM/Core/Resources/Bundle.php b/CRM/Core/Resources/Bundle.php index 62c1a04269..7c89dd1aa3 100644 --- a/CRM/Core/Resources/Bundle.php +++ b/CRM/Core/Resources/Bundle.php @@ -38,4 +38,27 @@ class CRM_Core_Resources_Bundle implements CRM_Core_Resources_CollectionInterfac $this->types = $types ?: ['script', 'scriptFile', 'scriptUrl', 'settings', 'style', 'styleFile', 'styleUrl']; } + /** + * Fill in default values for the 'region' property. + * + * @return static + */ + public function fillDefaults() { + $this->filter(function ($s) { + if (!isset($s['region'])) { + if ($s['type'] === 'settings') { + $s['region'] = NULL; + } + elseif (preg_match(';^(markup|template|callback);', $s['type'])) { + $s['region'] = 'page-header'; + } + else { + $s['region'] = CRM_Core_Resources_Common::REGION; + } + } + return $s; + }); + return $this; + } + } diff --git a/CRM/Core/Resources/Common.php b/CRM/Core/Resources/Common.php index c85abeaec0..44b3687376 100644 --- a/CRM/Core/Resources/Common.php +++ b/CRM/Core/Resources/Common.php @@ -47,7 +47,7 @@ class CRM_Core_Resources_Common { ); CRM_Utils_Hook::alterBundle($bundle); - self::useRegion($bundle, self::REGION); + $bundle->fillDefaults(); return $bundle; } @@ -76,7 +76,7 @@ class CRM_Core_Resources_Common { $bundle->addStyleFile('civicrm', 'css/crm-i.css', -101); CRM_Utils_Hook::alterBundle($bundle); - self::useRegion($bundle, self::REGION); + $bundle->fillDefaults(); return $bundle; } @@ -133,7 +133,7 @@ class CRM_Core_Resources_Common { ]); CRM_Utils_Hook::alterBundle($bundle); - self::useRegion($bundle, self::REGION); + $bundle->fillDefaults(); return $bundle; } @@ -273,21 +273,4 @@ class CRM_Core_Resources_Common { return $items; } - /** - * Ensure that all elements of the bundle are in the same region. - * - * @param CRM_Core_Resources_Bundle $bundle - * @param string $region - * @return CRM_Core_Resources_Bundle - */ - protected static function useRegion($bundle, $region) { - $bundle->filter(function ($s) use ($region) { - if ($s['type'] !== 'settings' && !isset($s['region'])) { - $s['region'] = $region; - } - return $s; - }); - return $bundle; - } - } diff --git a/tests/phpunit/CRM/Core/Resources/BundleTest.php b/tests/phpunit/CRM/Core/Resources/BundleTest.php index 87630bf021..08490b9794 100644 --- a/tests/phpunit/CRM/Core/Resources/BundleTest.php +++ b/tests/phpunit/CRM/Core/Resources/BundleTest.php @@ -56,4 +56,20 @@ class CRM_Core_Resources_BundleTest extends CiviUnitTestCase { $this->assertEquals('http://example.com/region.css', $region->get('http://example.com/region.css')['styleUrl']); } + /** + * Add some resources - sometimes forgetting to set a 'region'. Fill in missing regions. + */ + public function testFillDefaults() { + $bundle = new CRM_Core_Resources_Bundle(__FUNCTION__, ['scriptUrl', 'styleUrl', 'markup']); + $bundle->addScriptUrl('http://example.com/myscript.js'); + $bundle->addStyleUrl('http://example.com/yonder-style.css', ['region' => 'yonder']); + $bundle->addMarkup('Cheese', ['name' => 'cheese']); + + $bundle->fillDefaults(); + + $this->assertEquals('html-header', $bundle->get('http://example.com/myscript.js')['region']); + $this->assertEquals('yonder', $bundle->get('http://example.com/yonder-style.css')['region']); + $this->assertEquals('page-header', $bundle->get('cheese')['region']); + } + } -- 2.25.1