Browse Source

Fix illegal offset caused by TranslateBehavior.

If you load TranslateBehavior at runtime in a disabled state, the enabled
flag would be interpreted as an association and cause errors.

Fixes #2443
mark_story 14 years ago
parent
commit
beced84d2d

+ 3 - 1
lib/Cake/Model/BehaviorCollection.php

@@ -106,6 +106,9 @@ class BehaviorCollection extends ObjectCollection {
 			$alias = $behavior;
 			$behavior = $config['className'];
 		}
+		$configDisabled = isset($config['enabled']) && $config['enabled'] === false;
+		unset($config['enabled'], $config['className']);
+
 		list($plugin, $name) = pluginSplit($behavior, true);
 		if (!isset($alias)) {
 			$alias = $name;
@@ -165,7 +168,6 @@ class BehaviorCollection extends ObjectCollection {
 			}
 		}
 
-		$configDisabled = isset($config['enabled']) && $config['enabled'] === false;
 		if (!in_array($alias, $this->_enabled) && !$configDisabled) {
 			$this->enable($alias);
 		} elseif ($configDisabled) {

+ 14 - 1
lib/Cake/Test/Case/Model/BehaviorCollectionTest.php

@@ -419,10 +419,23 @@ class BehaviorCollectionTest extends CakeTestCase {
  */
 	public $fixtures = array(
 		'core.apple', 'core.sample', 'core.article', 'core.user', 'core.comment',
-		'core.attachment', 'core.tag', 'core.articles_tag'
+		'core.attachment', 'core.tag', 'core.articles_tag', 'core.translate'
 	);
 
 /**
+ * Test load() with enabled => false
+ *
+ */
+	public function testLoadDisabled() {
+		$Apple = new Apple();
+		$this->assertSame($Apple->Behaviors->attached(), array());
+
+		$Apple->Behaviors->load('Translate', array('enabled' => false));
+		$this->assertTrue($Apple->Behaviors->attached('Translate'));
+		$this->assertFalse($Apple->Behaviors->enabled('Translate'));
+	}
+
+/**
  * Tests loading aliased behaviors
  */
 	public function testLoadAlias() {