|
10 | 10 | use yii\base\Behavior;
|
11 | 11 | use yii\base\Component;
|
12 | 12 | use yii\base\Event;
|
| 13 | +use yii\base\InvalidConfigException; |
| 14 | +use yii\base\UnknownMethodException; |
13 | 15 | use yiiunit\TestCase;
|
14 | 16 |
|
15 | 17 | function globalEventHandler($event)
|
@@ -331,19 +333,39 @@ public function testAttachBehavior()
|
331 | 333 |
|
332 | 334 | $this->assertSame($behavior, $component->detachBehavior('a'));
|
333 | 335 | $this->assertFalse($component->hasProperty('p'));
|
334 |
| - $this->expectException('yii\base\UnknownMethodException'); |
335 |
| - $component->test(); |
| 336 | + try { |
| 337 | + $component->test(); |
| 338 | + $this->fail('Expected exception ' . UnknownMethodException::class . " wasn't thrown"); |
| 339 | + } catch (UnknownMethodException $e) { |
| 340 | + // Expected |
| 341 | + } |
336 | 342 |
|
337 |
| - $p = 'as b'; |
338 | 343 | $component = new NewComponent();
|
339 |
| - $component->$p = ['class' => 'NewBehavior']; |
340 |
| - $this->assertSame($behavior, $component->getBehavior('a')); |
| 344 | + $component->{'as b'} = ['class' => NewBehavior::class]; |
| 345 | + $this->assertInstanceOf(NewBehavior::class, $component->getBehavior('b')); |
341 | 346 | $this->assertTrue($component->hasProperty('p'));
|
342 | 347 | $component->test();
|
343 | 348 | $this->assertTrue($component->behaviorCalled);
|
344 | 349 |
|
345 | 350 | $component->{'as c'} = ['__class' => NewBehavior::class];
|
346 | 351 | $this->assertNotNull($component->getBehavior('c'));
|
| 352 | + |
| 353 | + $component->{'as d'} = [ |
| 354 | + '__class' => NewBehavior2::class, |
| 355 | + 'class' => NewBehavior::class, |
| 356 | + ]; |
| 357 | + $this->assertInstanceOf(NewBehavior2::class, $component->getBehavior('d')); |
| 358 | + |
| 359 | + // CVE-2024-4990 |
| 360 | + try { |
| 361 | + $component->{'as e'} = [ |
| 362 | + '__class' => 'NotExistsBehavior', |
| 363 | + 'class' => NewBehavior::class, |
| 364 | + ]; |
| 365 | + $this->fail('Expected exception ' . InvalidConfigException::class . " wasn't thrown"); |
| 366 | + } catch (InvalidConfigException $e) { |
| 367 | + $this->assertSame('Class is not of type yii\base\Behavior or its subclasses', $e->getMessage()); |
| 368 | + } |
347 | 369 | }
|
348 | 370 |
|
349 | 371 | public function testAttachBehaviors()
|
@@ -546,6 +568,10 @@ public function test()
|
546 | 568 | }
|
547 | 569 | }
|
548 | 570 |
|
| 571 | +class NewBehavior2 extends Behavior |
| 572 | +{ |
| 573 | +} |
| 574 | + |
549 | 575 | class NewComponent2 extends Component
|
550 | 576 | {
|
551 | 577 | public $a;
|
|
0 commit comments