@@ -330,26 +330,48 @@ public function testAttachBehavior(): void
330330 $ this ->assertTrue ($ component ->hasProperty ('p ' ));
331331 $ component ->test ();
332332 $ this ->assertTrue ($ component ->behaviorCalled );
333- }
334333
335- public function testAs ()
336- {
334+ $ this ->assertSame ($ behavior , $ component ->detachBehavior ('a ' ));
335+ $ this ->assertFalse ($ component ->hasProperty ('p ' ));
336+ try {
337+ $ component ->test ();
338+ $ this ->fail ('Expected exception ' . UnknownMethodException::class . " wasn't thrown " );
339+ } catch (UnknownMethodException $ e ) {
340+ // Expected
341+ }
342+
337343 $ component = new NewComponent ();
338- $ component ->{'as a ' } = new NewBehavior ();
344+ $ component ->{'as b ' } = ['class ' => NewBehavior::class];
345+ $ this ->assertInstanceOf (NewBehavior::class, $ component ->getBehavior ('b ' ));
339346 $ this ->assertTrue ($ component ->hasProperty ('p ' ));
340347 $ component ->test ();
341348 $ this ->assertTrue ($ component ->behaviorCalled );
342349
343- $ component ->{'as b ' } = ['class ' => NewBehavior::class];
344- $ this ->assertNotNull ($ component ->getBehavior ('b ' ));
345-
346350 $ component ->{'as c ' } = ['__class ' => NewBehavior::class];
347351 $ this ->assertNotNull ($ component ->getBehavior ('c ' ));
348352
349- $ component ->{'as d ' } = function () {
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+ }
369+
370+ $ component = new NewComponent ();
371+ $ component ->{'as f ' } = function () {
350372 return new NewBehavior ();
351373 };
352- $ this ->assertNotNull ($ component ->getBehavior ('d ' ));
374+ $ this ->assertNotNull ($ component ->getBehavior ('f ' ));
353375 }
354376
355377 public function testAttachBehaviors (): void
0 commit comments