diff --git a/src/Entity/Transformer.php b/src/Entity/Transformer.php index 4cc61ce3a..79200e960 100644 --- a/src/Entity/Transformer.php +++ b/src/Entity/Transformer.php @@ -181,8 +181,8 @@ public function loadBlock(array $content, PageInterface $page): PageBlockInterfa $block->setEnabled($content['enabled']); - if (isset($content['position'])) { - $block->setPosition($content['position']); + if (isset($content['position']) && is_numeric($content['position'])) { + $block->setPosition((int) $content['position']); } $block->setSettings($content['settings']); diff --git a/src/Model/TransformerInterface.php b/src/Model/TransformerInterface.php index 49ff307f7..65430c150 100644 --- a/src/Model/TransformerInterface.php +++ b/src/Model/TransformerInterface.php @@ -22,7 +22,7 @@ * id: int|string|null, * name?: string|null, * enabled: boolean, - * position: int|null, + * position: int|string|null, * settings: array, * type: string|null, * created_at: int|null, @@ -32,7 +32,7 @@ * id: int|string|null, * name?: string|null, * enabled: boolean, - * position: int|null, + * position: int|string|null, * settings: array, * type: string|null, * created_at: int|null, diff --git a/tests/Entity/TransformerTest.php b/tests/Entity/TransformerTest.php index 7e7351c3b..78e616384 100644 --- a/tests/Entity/TransformerTest.php +++ b/tests/Entity/TransformerTest.php @@ -36,19 +36,19 @@ final class TransformerTest extends TestCase /** * @var MockObject&SnapshotManagerInterface */ - protected $snapshotManager; + private $snapshotManager; /** * @var MockObject&PageManagerInterface */ - protected $pageManager; + private $pageManager; /** * @var MockObject&ManagerInterface */ - protected $blockManager; + private $blockManager; - protected TransformerInterface $transformer; + private TransformerInterface $transformer; protected function setUp(): void { @@ -147,6 +147,7 @@ public function testLoadSnapshotToPage(): void $snapshot = new SonataPageSnapshot(); $snapshot->setContent($this->getTestContent($dateTime)); $snapshot->setUrl('/get-child'); + $page = $this->transformer->load($snapshot); static::assertSame('page_child', $page->getId()); @@ -155,23 +156,94 @@ public function testLoadSnapshotToPage(): void static::assertSame('/get-child', $page->getUrl()); } - public function testLoadBlock(): void + /** + * @dataProvider blockProvider + * + * @param array $content + * + * @phpstan-param BlockContent $content + */ + public function testLoadBlock(array $content): void { $this->blockManager->method('create')->willReturnCallback(static fn () => new SonataPageBlock()); - $dateTime = new \DateTime(); + $block = $this->transformer->loadBlock($content, new SonataPagePage()); - $page = new SonataPagePage(); + static::assertSame($content['id'], $block->getId()); + } + + /** + * @phpstan-return iterable + */ + public function blockProvider(): iterable + { + $datetime = new \DateTime(); - $block = $this->transformer->loadBlock($this->getTestBlockArray($dateTime), $page); + // Normal block + yield [[ + 'id' => 1, + 'name' => 'block1', + 'enabled' => true, + 'position' => 0, + 'settings' => [], + 'type' => 'type', + 'created_at' => (int) $datetime->format('U'), + 'updated_at' => (int) $datetime->format('U'), + 'blocks' => [[ + 'id' => 2, + 'name' => 'block2', + 'enabled' => true, + 'position' => 1, + 'settings' => [], + 'type' => 'type', + 'created_at' => (int) $datetime->format('U'), + 'updated_at' => (int) $datetime->format('U'), + 'blocks' => [], + ]], + ]]; + + // Minimal block block + yield [[ + 'id' => null, + 'enabled' => false, + 'position' => null, + 'settings' => [], + 'type' => null, + 'created_at' => null, + 'updated_at' => null, + 'blocks' => [], + ]]; + + // Weird block data + yield [[ + 'id' => 'random_string', + 'name' => 'block1', + 'enabled' => true, + 'position' => '', + 'settings' => [], + 'type' => 'type', + 'created_at' => null, + 'updated_at' => null, + 'blocks' => [], + ]]; - static::assertSame('block123', $block->getId()); + // Numeric string position block data + yield [[ + 'id' => 'block123', + 'enabled' => false, + 'position' => '0', + 'settings' => [], + 'type' => null, + 'created_at' => null, + 'updated_at' => null, + 'blocks' => [], + ]]; } /** * @phpstan-return PageContent */ - protected function getTestContent(\DateTimeInterface $datetime): array + private function getTestContent(\DateTimeInterface $datetime): array { return [ 'id' => 'page_child', @@ -188,37 +260,29 @@ protected function getTestContent(\DateTimeInterface $datetime): array 'updated_at' => (int) $datetime->format('U'), 'slug' => null, 'parent_id' => 'page_parent', - 'blocks' => [ - $this->getTestBlockArray($datetime), - ], - ]; - } - - /** - * @phpstan-return BlockContent - */ - protected function getTestBlockArray(\DateTimeInterface $datetime): array - { - return [ - 'id' => 'block123', - 'name' => 'block1', - 'enabled' => false, - 'position' => 0, - 'settings' => [], - 'type' => 'type', - 'created_at' => (int) $datetime->format('U'), - 'updated_at' => (int) $datetime->format('U'), 'blocks' => [ [ - 'id' => 'block234', - 'name' => 'block2', + 'id' => 'block123', + 'name' => 'block1', 'enabled' => false, 'position' => 0, 'settings' => [], 'type' => 'type', 'created_at' => (int) $datetime->format('U'), 'updated_at' => (int) $datetime->format('U'), - 'blocks' => [], + 'blocks' => [ + [ + 'id' => 'block234', + 'name' => 'block2', + 'enabled' => false, + 'position' => 0, + 'settings' => [], + 'type' => 'type', + 'created_at' => (int) $datetime->format('U'), + 'updated_at' => (int) $datetime->format('U'), + 'blocks' => [], + ], + ], ], ], ];