Skip to content
Snippets Groups Projects
Commit 4db4f4ee authored by Patrick Kenny's avatar Patrick Kenny
Browse files

Merge branch 'unit_test' into '1.1.x'

add unit test to confirm core functionality

See merge request !27
parents 15934104 85d9bb2e
No related branches found
No related tags found
No related merge requests found
Pipeline #388224 failed
<?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('related', $array['links']);
} else {
$this->assertArrayHasKey('related', $array['links']);
}
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\jsonapi_links\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\UserInterface;
/**
* Simple test to ensure that main page loads with module enabled.
*
* @group jsonapi_links
*/
class LoadTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['jsonapi_links'];
/**
* The default theme when not relying on core markup.
*
* @var string
*/
protected $defaultTheme = 'stark';
/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected UserInterface $user;
/**
* {@inheritdoc}
*/
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}
/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad(): void {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertSession()->statusCodeEquals(200);
}
}
......@@ -11,7 +11,7 @@ use Drupal\KernelTests\KernelTestBase;
*
* @group jsonapi_links
*/
class JsonapiLinksInstallTest extends KernelTestBase {
class JsonApiLinksInstallTest extends KernelTestBase {
private const string MODULE_NAME = 'jsonapi_links';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment