View source
<?php
namespace Drupal\Tests\jsonapi\Kernel\Normalizer;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\jsonapi\JsonApiResource\Link;
use Drupal\jsonapi\JsonApiResource\LinkCollection;
use Drupal\jsonapi\JsonApiResource\ResourceObject;
use Drupal\jsonapi\Normalizer\LinkCollectionNormalizer;
use Drupal\jsonapi\Normalizer\Value\CacheableNormalization;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class LinkCollectionNormalizerTest extends KernelTestBase {
use UserCreationTrait;
protected $serializer;
protected $normalizer;
protected $testUsers;
protected static $modules = [
'jsonapi',
'serialization',
'system',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('user', [
'users_data',
]);
$this->testUsers[] = $this
->createUser([], NULL, FALSE, [
'uid' => 2,
]);
$this->testUsers[] = $this
->createUser([], NULL, FALSE, [
'uid' => 3,
]);
$this->serializer = $this->container
->get('jsonapi.serializer');
}
public function testNormalize() {
$link_context = new ResourceObject(new CacheableMetadata(), new ResourceType('n/a', 'n/a', 'n/a'), 'n/a', NULL, [], new LinkCollection([]));
$link_collection = (new LinkCollection([]))
->withLink('related', new Link(new CacheableMetadata(), Url::fromUri('http://example.com/post/42'), 'related', [
'title' => 'Most viewed',
]))
->withLink('related', new Link(new CacheableMetadata(), Url::fromUri('http://example.com/post/42'), 'related', [
'title' => 'Top rated',
]))
->withContext($link_context);
$normalized = $this
->getNormalizer()
->normalize($link_collection)
->getNormalization();
$this
->assertIsArray($normalized);
foreach (array_keys($normalized) as $key) {
$this
->assertStringStartsWith('related', $key);
}
$this
->assertSame([
[
'href' => 'http://example.com/post/42',
'meta' => [
'title' => 'Most viewed',
],
],
[
'href' => 'http://example.com/post/42',
'meta' => [
'title' => 'Top rated',
],
],
], array_values($normalized));
}
public function testLinkAccess($current_user_id, $edit_form_uid, $expected_link_keys, $expected_cache_contexts) {
foreach ($this->testUsers as $user) {
$uid = (int) $user
->id();
if ($uid === $current_user_id) {
$current_user = $user;
}
if ($uid === $edit_form_uid) {
$edit_form_url = $user
->toUrl('edit-form');
}
}
assert(isset($current_user));
assert(isset($edit_form_url));
$mock_resource_object = $this
->createMock(ResourceObject::class);
$link_collection = new LinkCollection([
'edit-form' => new Link(new CacheableMetadata(), $edit_form_url, 'edit-form', [
'title' => 'Edit',
]),
]);
$link_collection = $link_collection
->withContext($mock_resource_object);
$actual_normalization = $this
->getNormalizer($current_user)
->normalize($link_collection);
$this
->assertInstanceOf(CacheableNormalization::class, $actual_normalization);
$actual_data = $actual_normalization
->getNormalization();
$this
->assertIsArray($actual_data);
$actual_link_keys = array_keys($actual_data);
sort($expected_link_keys);
sort($actual_link_keys);
$this
->assertSame($expected_link_keys, $actual_link_keys);
$actual_cache_contexts = $actual_normalization
->getCacheContexts();
sort($expected_cache_contexts);
sort($actual_cache_contexts);
$this
->assertSame($expected_cache_contexts, $actual_cache_contexts);
if (isset($actual_data['edit-form'])) {
$this
->assertSame($actual_data['edit-form'], [
'href' => $edit_form_url
->setAbsolute()
->toString(),
'meta' => [
'title' => 'Edit',
],
]);
}
}
public function linkAccessTestData() {
return [
'the edit-form link is present because uid 2 has access to the targeted resource (its own edit form)' => [
'uid' => 2,
'edit-form uid' => 2,
'expected link keys' => [
'edit-form',
],
'expected cache contexts' => [
'url.site',
'user',
],
],
"the edit-form link is omitted because uid 3 doesn't have access to the targeted resource (another account's edit form)" => [
'uid' => 3,
'edit-form uid' => 2,
'expected link keys' => [],
'expected cache contexts' => [
'url.site',
'user',
],
],
];
}
protected function getNormalizer(AccountInterface $current_user = NULL) {
if (is_null($current_user)) {
$current_user = $this
->setUpCurrentUser();
}
else {
$this
->setCurrentUser($current_user);
}
$normalizer = new LinkCollectionNormalizer($current_user);
$normalizer
->setSerializer($this->serializer);
return $normalizer;
}
}