Skip to content
Snippets Groups Projects

add unit test to confirm core functionality

Merged Patrick Kenny requested to merge issue/jsonapi_links-3497804:unit_test into 1.1.x
Files
4
<?php
declare(strict_types=1);
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Component\Serialization\Json;
/**
* General functional test class.
*
* @group jsonapi_links
*/
class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'basic_auth',
'jsonapi_links',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests the GET method.
*/
public function testRead(): void {
// First, do testRead() from core's JSON:API and ensure that the output is
// exactly the same as core.
// JSON:API Links does not remove anything by default.
$this->createDefaultContent(61, 5, TRUE, TRUE, static::IS_NOT_MULTILINGUAL, FALSE);
// Unpublish the last entity, so we can check access.
$this->nodes[60]->setUnpublished()->save();
$uuid = $this->nodes[0]->uuid();
// 0. HEAD request allows a client to verify that JSON:API is installed.
$this->httpClient->request('HEAD', $this->buildUrl('/jsonapi/node/article'));
$this->assertSession()->statusCodeEquals(200);
// Confirm the default behavior (no change from core).
$this->checkForLinksPresent($uuid, FALSE);
// Enable link removal with JSON:API Links.
\Drupal::configFactory()->getEditable('jsonapi_links.settings')
->set('remove_links', TRUE)
->save();
// Verify that the configuration value is set.
$config = $this->config('jsonapi_links.settings');
$this->assertEquals(TRUE, $config->get('remove_links'));
// Ensure links are removed as expected.
$this->checkForLinksPresent($uuid, TRUE);
// Disable link removal with JSON:API Links.
\Drupal::configFactory()->getEditable('jsonapi_links.settings')
->set('remove_links', FALSE)
->save();
// Verify that the configuration value is set.
$config = $this->config('jsonapi_links.settings');
$this->assertEquals(FALSE, $config->get('remove_links'));
// Ensure that the prior behavior has been restored.
$this->checkForLinksPresent($uuid, FALSE);
}
/**
* Confirms that link attributes are present or absent.
*
* @param string $uuid
* The uuid of the article.
* @param bool $links_removed
* TRUE if links should be removed; FALSE otherwise.
*/
private function checkForLinksPresent(string $uuid, bool $links_removed): void {
// 6. Single relationship item.
$single_output = Json::decode($this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/node_type'));
$this->assertSession()->statusCodeEquals(200);
$this->assertArrayHasKey('type', $single_output['data']);
$this->assertArrayNotHasKey('attributes', $single_output['data']);
$this->assertLinksRemoved($single_output, $links_removed);
// 7. Single relationship image.
$single_output = Json::decode($this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/field_image'));
$this->assertSession()->statusCodeEquals(200);
$this->assertArrayHasKey('type', $single_output['data']);
$this->assertArrayNotHasKey('attributes', $single_output['data']);
$this->assertLinksRemoved($single_output, $links_removed);
// 8. Multiple relationship item.
$single_output = Json::decode($this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/field_tags'));
$this->assertSession()->statusCodeEquals(200);
$this->assertArrayHasKey('type', $single_output['data'][0]);
$this->assertArrayNotHasKey('attributes', $single_output['data'][0]);
$this->assertLinksRemoved($single_output, $links_removed);
// 10. Single article with includes.
$single_output = Json::decode($this->drupalGet('/jsonapi/node/article/' . $uuid, [
'query' => ['include' => 'uid,field_tags'],
]));
$this->assertSession()->statusCodeEquals(200);
$this->assertEquals('node--article', $single_output['data']['type']);
$first_include = reset($single_output['included']);
$this->assertEquals(
'user--user',
$first_include['type']
);
$last_include = end($single_output['included']);
$this->assertEquals(
'taxonomy_term--tags',
$last_include['type']
);
// 11. Includes with relationships.
$this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/uid');
$single_output = Json::decode($this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/uid', [
'query' => ['include' => 'uid'],
]));
$this->assertSession()->statusCodeEquals(200);
$this->assertEquals('user--user', $single_output['data']['type']);
$this->assertArrayHasKey('related', $single_output['links']);
$this->assertArrayHasKey('included', $single_output);
$first_include = reset($single_output['included']);
$this->assertEquals(
'user--user',
$first_include['type']
);
$this->assertNotEmpty($first_include['attributes']);
$this->assertArrayNotHasKey('mail', $first_include['attributes']);
$this->assertArrayNotHasKey('pass', $first_include['attributes']);
}
/**
* Asserts that links are removed or present depending on the module setting.
*
* @param array $array
* The array to check.
* @param bool $links_removed
* TRUE if links attributes should be removed.
*/
private function assertLinksRemoved(array $array, bool $links_removed): void {
if ($links_removed) {
$this->assertArrayNotHasKey('links', $array);
}
else {
$this->assertArrayHasKey('related', $array['links']);
}
}
}
Loading