Skip to content

Commit

Permalink
Merge pull request #93 from netglue/bug/image-constraint-richtext
Browse files Browse the repository at this point in the history
Fix missing image constraint in Rich Text
  • Loading branch information
gsteel authored May 20, 2022
2 parents 1d273a4 + c68bf0b commit 9671133
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ public static function richText(
'allowTargetBlank' => $allowTargetBlank,
'useAsTitle' => $isTitle,
'labels' => $labels === [] ? null : $labels,
'imageConstraint' => $imgX && $imgY ? ['width' => $imgX, 'height' => $imgY] : null,
'imageConstraint' => $imgX !== null || $imgY !== null
? array_filter(['width' => $imgX, 'height' => $imgY], [self::class, 'filterNull'])
: null,
];

return [
Expand Down
34 changes: 34 additions & 0 deletions test/Unit/TypeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,38 @@ public function testThatNumbersWillIncludeTheMinWhenZero(): void
];
self::assertEquals($expect, $data);
}

/** @return array<array-key, array{0: int|null, 1: int|null, 2: array{width?:int, height?:int}}> */
public function richTextImageConstraintProvider(): array
{
return [
[999, null, ['width' => 999]],
[null, 999, ['height' => 999]],
[123, 456, ['width' => 123, 'height' => 456]],
];
}

/**
* @param array{width?:int, height?:int} $expect
*
* @dataProvider richTextImageConstraintProvider
*/
public function testWidthOrHeightWillYieldImageConstraintInRichText(?int $x, ?int $y, array $expect): void
{
$data = T::richText('Foo', 'Foo', [], true, true, true, [], $x, $y);
self::assertArrayHasKey('config', $data);
$config = $data['config'];
self::assertIsArray($config);
self::assertArrayHasKey('imageConstraint', $config);
self::assertEquals($expect, $config['imageConstraint']);
}

public function testImageConstraintIsAbsentInRichTextWhenNeitherXNorYAreProvided(): void
{
$data = T::richText('Foo', null, []);
self::assertArrayHasKey('config', $data);
$config = $data['config'];
self::assertIsArray($config);
self::assertArrayNotHasKey('imageConstraint', $config);
}
}

0 comments on commit 9671133

Please sign in to comment.