Skip to content
Snippets Groups Projects
Commit 0516c39d authored by Pierre Dureau's avatar Pierre Dureau
Browse files

Init LinksPropTypeNormalizationTest

parent 5ec4f471
No related branches found
No related tags found
No related merge requests found
<?php
declare(strict_types=1);
namespace Drupal\Tests\ui_patterns\Unit;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Drupal\ui_patterns\Plugin\UiPatterns\PropType\LinksPropType;
/**
* @coversDefaultClass \Drupal\ui_patterns\Plugin\UiPatterns\PropType\LinksPropType
*
* @group ui_patterns
*/
final class LinksPropTypeNormalizationTest extends UnitTestCase {
/**
* @covers ::canonicalize
*
* @dataProvider provideNormalizationData
*/
public function testNormalize(array $value, array $expected): void {
$normalized = LinksPropType::normalize($value);
self::assertEquals($normalized, $expected);
}
/**
* Provide data for testNormalize.
*/
public static function provideNormalizationData(): \Generator {
$data = [
"Empty value" => [
"value" => [],
"expected" => [],
],
"Standardized structure, flat, only primtives" => self::standardizedFlatPrimitives(),
"Standardized structure, flat, with objects" => self::standardizedFlatPrimitives(),
];
foreach ($data as $label => $test) {
yield $label => [
$test['value'],
$test['expected'],
];
};
}
/**
* Standardized structure, flat, only primtives.
*/
protected static function standardizedFlatPrimitives() {
$value = [
[
"title" => "With an absolute URL",
"url" => "http://wwww.example.org/foo/bar",
],
[
"title" => "With a relative URL",
"url" => "/foo/bar",
"attributes" => [
"foo" => "bar",
],
"link_attributes" => [
"foo" => "baz",
],
],
];
$expected = $value;
return [
"value" => $value,
"expected" => $expected,
];
}
/**
* Standardized structure, flat, with objects.
*/
protected static function standardizedFlatObjects() {
$value = [
[
"title" => "With an absolute URL",
"url" => Url::fromUri("http://wwww.example.org//foo/bar"),
],
[
"title" => "With a relative URL",
"url" => Url::fromUri("/foo/bar", ["attributes" => ["foo" => "baz"]]),
"attributes" => new Attribute([
"foo" => "bar",
]),
],
[
"title" => "With empty attributes",
"url" => Url::fromUri("/foo/bar", ["attributes" => []]),
"attributes" => new Attribute([]),
],
];
$expected = [
[
"title" => "With an absolute URL",
"url" => "http://wwww.example.org/foo/bar",
],
[
"title" => "With a relative URL",
"url" => "/foo/bar",
],
];
return [
"value" => $value,
"expected" => $expected,
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment