Skip to content

Commit a622750

Browse files
committed
tests: add tests for nested state leakage and transformRelated
1 parent 12888fb commit a622750

3 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\API;
15+
16+
use CodeIgniter\API\BaseTransformer;
17+
18+
/**
19+
* Child transformer for testing transformRelated() and Global State Leakage.
20+
*/
21+
class ChildTransformer extends BaseTransformer
22+
{
23+
public function toArray(mixed $resource): array
24+
{
25+
return [
26+
'child_id' => $resource['id'] ?? null,
27+
'status' => 'transformed',
28+
];
29+
}
30+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\API;
15+
16+
use CodeIgniter\API\BaseTransformer;
17+
18+
/**
19+
* Parent transformer for testing transformRelated() and Global State Leakage.
20+
*/
21+
class ParentTransformer extends BaseTransformer
22+
{
23+
public function toArray(mixed $resource): array
24+
{
25+
return [
26+
'parent_id' => $resource['id'] ?? null,
27+
];
28+
}
29+
30+
/**
31+
* Test include that triggers a child transformer.
32+
* If Global State leaks (?include=child), the child will try to find
33+
* includeChild() on itself and throw an ApiException.
34+
*/
35+
protected function includeChild(): array
36+
{
37+
$childData = ['id' => 99];
38+
39+
return $this->transformRelated(ChildTransformer::class, $childData);
40+
}
41+
42+
/**
43+
* Test include that returns a collection of items to verify smart routing.
44+
*/
45+
protected function includeChildrenCollection(): array
46+
{
47+
$collectionData = [
48+
['id' => 77],
49+
['id' => 88],
50+
];
51+
52+
return $this->transformRelated(ChildTransformer::class, $collectionData);
53+
}
54+
}

‎tests/system/API/TransformerTest.php‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Config\Services;
2323
use PHPUnit\Framework\Attributes\Group;
2424
use stdClass;
25+
use Tests\Support\API\ParentTransformer;
2526

2627
/**
2728
* @internal
@@ -641,4 +642,69 @@ protected function includePosts(): array
641642
$this->assertArrayHasKey('posts', $result);
642643
$this->assertSame([['id' => 1, 'title' => 'Post 1']], $result['posts']);
643644
}
645+
646+
public function testTransformRelatedPreventsGlobalStateLeakage(): void
647+
{
648+
$request = $this->createMockRequest('include=child');
649+
650+
$transformer = new ParentTransformer($request);
651+
$result = $transformer->transform(['id' => 1]);
652+
653+
$this->assertSame([
654+
'parent_id' => 1,
655+
'child' => [
656+
'child_id' => 99,
657+
'status' => 'transformed',
658+
],
659+
], $result);
660+
}
661+
662+
public function testTransformRelatedSmartRoutingForSingleItem(): void
663+
{
664+
$request = $this->createMockRequest('include=child');
665+
666+
$transformer = new ParentTransformer($request);
667+
$result = $transformer->transform(['id' => 1]);
668+
669+
$this->assertIsArray($result['child']);
670+
$this->assertArrayHasKey('child_id', $result['child']);
671+
$this->assertSame(99, $result['child']['child_id']);
672+
}
673+
674+
public function testTransformRelatedSmartRoutingForCollection(): void
675+
{
676+
$request = $this->createMockRequest('include=childrenCollection');
677+
678+
$transformer = new ParentTransformer($request);
679+
$result = $transformer->transform(['id' => 1]);
680+
681+
$this->assertIsArray($result['childrenCollection']);
682+
$this->assertCount(2, $result['childrenCollection']);
683+
$this->assertSame(77, $result['childrenCollection'][0]['child_id']);
684+
$this->assertSame(88, $result['childrenCollection'][1]['child_id']);
685+
}
686+
687+
public function testTransformRelatedWorksWhenParentUsesTransformMany(): void
688+
{
689+
$request = $this->createMockRequest('include=child');
690+
$transformer = new ParentTransformer($request);
691+
692+
$parents = [
693+
['id' => 1],
694+
['id' => 2],
695+
];
696+
697+
$result = $transformer->transformMany($parents);
698+
699+
$this->assertIsArray($result);
700+
$this->assertCount(2, $result);
701+
702+
$this->assertSame(1, $result[0]['parent_id']);
703+
$this->assertIsArray($result[0]['child']);
704+
$this->assertSame(99, $result[0]['child']['child_id']);
705+
706+
$this->assertSame(2, $result[1]['parent_id']);
707+
$this->assertIsArray($result[1]['child']);
708+
$this->assertSame(99, $result[1]['child']['child_id']);
709+
}
644710
}

0 commit comments

Comments
 (0)