Browse Source

Merge pull request #5164 from TamiasSibiricus/widget-config-fix

3.x Fix for config loader in FormHelper And WidgetRegistry
Mark Story 11 years ago
parent
commit
ae15809a7d

+ 3 - 0
src/View/Helper/FormHelper.php

@@ -202,6 +202,9 @@ class FormHelper extends Helper {
 			unset($config['registry']);
 		}
 		if (isset($config['widgets'])) {
+			if (is_string($config['widgets'])) {
+				$config['widgets'] = (array)$config['widgets'];
+			}
 			$widgets = $config['widgets'] + $widgets;
 			unset($config['widgets']);
 		}

+ 6 - 4
src/View/Widget/WidgetRegistry.php

@@ -62,10 +62,12 @@ class WidgetRegistry {
 	public function __construct(StringTemplate $templates, View $view, $widgets = []) {
 		$this->_templates = $templates;
 		if (!empty($widgets)) {
-			if (is_string($widgets)) {
-				$this->load($widgets);
-			} else {
-				$this->add($widgets);
+			$this->add($widgets);
+			foreach ($this->_widgets as $key => $widget) {
+				if (is_string($widget) && !class_exists($widget)) {
+					$this->load($widget);
+					unset($this->_widgets[$key]);
+				}
 			}
 		}
 		$this->_widgets['_view'] = $view;

+ 39 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -244,6 +244,45 @@ class FormHelperTest extends TestCase {
 	}
 
 /**
+ * Test that when specifying custom widgets config file and it should be
+ * added to widgets array. WidgetRegistry will load widgets in constructor.
+ *
+ * @return void
+ */
+	public function testConstructWithWidgetsConfig() {
+		$expected = [
+			'button' => ['Cake\View\Widget\ButtonWidget'],
+			'checkbox' => ['Cake\View\Widget\CheckboxWidget'],
+			'file' => ['Cake\View\Widget\FileWidget'],
+			'label' => ['Cake\View\Widget\LabelWidget'],
+			'nestingLabel' => ['Cake\View\Widget\NestingLabelWidget'],
+			'multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'nestingLabel'],
+			'radio' => ['Cake\View\Widget\RadioWidget', 'nestingLabel'],
+			'select' => ['Cake\View\Widget\SelectBoxWidget'],
+			'textarea' => ['Cake\View\Widget\TextareaWidget'],
+			'datetime' => ['Cake\View\Widget\DateTimeWidget', 'select'],
+			'_default' => ['Cake\View\Widget\BasicWidget'],
+			0 => 'test_widgets',
+		];
+
+		$helper = $this->getMock(
+			'Cake\View\Helper\FormHelper',
+			['widgetRegistry'],
+			[],
+			'',
+			false
+		);
+		$helper->expects($this->once())
+			->method('widgetRegistry')
+			->with(null, $expected);
+
+		$config = [
+			'widgets' => 'test_widgets'
+		];
+		$helper->__construct($this->View, $config);
+	}
+
+/**
  * Test registering a new widget class and rendering it.
  *
  * @return void

+ 12 - 4
tests/TestCase/View/Widget/WidgetRegistryTest.php

@@ -71,8 +71,12 @@ class WidgetRegistryTestCase extends TestCase {
  *
  * @return void
  */
-	public function testLoadInConstructor() {
-		$inputs = new WidgetRegistry($this->templates, $this->view, 'test_widgets');
+	public function testAddWidgetsFromConfigInConstuctor() {
+		$widgets = [
+			'text' => ['Cake\View\Widget\BasicWidget'],
+			'test_widgets',
+		];
+		$inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
 		$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
 	}
 
@@ -81,9 +85,13 @@ class WidgetRegistryTestCase extends TestCase {
  *
  * @return void
  */
-	public function testLoadPluginInConstuctor() {
+	public function testAddPluginWidgetsFromConfigInConstuctor() {
 		Plugin::load('TestPlugin');
-		$inputs = new WidgetRegistry($this->templates, $this->view, 'TestPlugin.test_widgets');
+		$widgets = [
+			'text' => ['Cake\View\Widget\BasicWidget'],
+			'TestPlugin.test_widgets',
+		];
+		$inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
 		$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
 	}