mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-01 11:39:41 +08:00
Merge branch 'master' into feature/attach-behaviors-with-closure
# Conflicts: # framework/base/Component.php # tests/framework/base/ComponentTest.php
This commit is contained in:
@ -10,6 +10,8 @@ namespace yiiunit\framework\base;
|
||||
use yii\base\Behavior;
|
||||
use yii\base\Component;
|
||||
use yii\base\Event;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\base\UnknownMethodException;
|
||||
use yiiunit\TestCase;
|
||||
|
||||
function globalEventHandler($event)
|
||||
@ -328,26 +330,48 @@ class ComponentTest extends TestCase
|
||||
$this->assertTrue($component->hasProperty('p'));
|
||||
$component->test();
|
||||
$this->assertTrue($component->behaviorCalled);
|
||||
}
|
||||
|
||||
public function testAs()
|
||||
{
|
||||
$this->assertSame($behavior, $component->detachBehavior('a'));
|
||||
$this->assertFalse($component->hasProperty('p'));
|
||||
try {
|
||||
$component->test();
|
||||
$this->fail('Expected exception ' . UnknownMethodException::class . " wasn't thrown");
|
||||
} catch (UnknownMethodException $e) {
|
||||
// Expected
|
||||
}
|
||||
|
||||
$component = new NewComponent();
|
||||
$component->{'as a'} = new NewBehavior();
|
||||
$component->{'as b'} = ['class' => NewBehavior::class];
|
||||
$this->assertInstanceOf(NewBehavior::class, $component->getBehavior('b'));
|
||||
$this->assertTrue($component->hasProperty('p'));
|
||||
$component->test();
|
||||
$this->assertTrue($component->behaviorCalled);
|
||||
|
||||
$component->{'as b'} = ['class' => NewBehavior::class];
|
||||
$this->assertNotNull($component->getBehavior('b'));
|
||||
|
||||
$component->{'as c'} = ['__class' => NewBehavior::class];
|
||||
$this->assertNotNull($component->getBehavior('c'));
|
||||
|
||||
$component->{'as d'} = function () {
|
||||
$component->{'as d'} = [
|
||||
'__class' => NewBehavior2::class,
|
||||
'class' => NewBehavior::class,
|
||||
];
|
||||
$this->assertInstanceOf(NewBehavior2::class, $component->getBehavior('d'));
|
||||
|
||||
// CVE-2024-4990
|
||||
try {
|
||||
$component->{'as e'} = [
|
||||
'__class' => 'NotExistsBehavior',
|
||||
'class' => NewBehavior::class,
|
||||
];
|
||||
$this->fail('Expected exception ' . InvalidConfigException::class . " wasn't thrown");
|
||||
} catch (InvalidConfigException $e) {
|
||||
$this->assertSame('Class is not of type yii\base\Behavior or its subclasses', $e->getMessage());
|
||||
}
|
||||
|
||||
$component = new NewComponent();
|
||||
$component->{'as f'} = function () {
|
||||
return new NewBehavior();
|
||||
};
|
||||
$this->assertNotNull($component->getBehavior('d'));
|
||||
$this->assertNotNull($component->getBehavior('f'));
|
||||
}
|
||||
|
||||
public function testAttachBehaviors()
|
||||
@ -380,9 +404,6 @@ class ComponentTest extends TestCase
|
||||
|
||||
$detachedBehavior = $component->detachBehavior('z');
|
||||
$this->assertNull($detachedBehavior);
|
||||
|
||||
$this->expectException('yii\base\UnknownMethodException');
|
||||
$component->test();
|
||||
}
|
||||
|
||||
public function testDetachBehaviors()
|
||||
@ -553,6 +574,10 @@ class NewBehavior extends Behavior
|
||||
}
|
||||
}
|
||||
|
||||
class NewBehavior2 extends Behavior
|
||||
{
|
||||
}
|
||||
|
||||
class NewComponent2 extends Component
|
||||
{
|
||||
public $a;
|
||||
|
||||
Reference in New Issue
Block a user