From 6b160b2c4eb55091d3701a87a0d31de85ca24c4c Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 16:35:02 +0900 Subject: [PATCH 01/24] add basic load test --- tests/src/Functional/LoadTest.php | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/src/Functional/LoadTest.php diff --git a/tests/src/Functional/LoadTest.php b/tests/src/Functional/LoadTest.php new file mode 100644 index 0000000..e85f34a --- /dev/null +++ b/tests/src/Functional/LoadTest.php @@ -0,0 +1,57 @@ +<?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); + } + +} -- GitLab From 10a199ebd986863e38a62561fe14d5c4d9fcef2c Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 16:49:11 +0900 Subject: [PATCH 02/24] rename to be consistent with core --- .../{JsonapiLinksInstallTest.php => JsonApiLinksInstallTest.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/src/Kernel/{JsonapiLinksInstallTest.php => JsonApiLinksInstallTest.php} (100%) diff --git a/tests/src/Kernel/JsonapiLinksInstallTest.php b/tests/src/Kernel/JsonApiLinksInstallTest.php similarity index 100% rename from tests/src/Kernel/JsonapiLinksInstallTest.php rename to tests/src/Kernel/JsonApiLinksInstallTest.php -- GitLab From 85d9bb2ea15bf1d214ed294d331fe448c6da00e4 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:12:10 +0900 Subject: [PATCH 03/24] add basic test to check whether links are removed --- .../Functional/JsonApiLinksFunctionalTest.php | 150 ++++++++++++++++++ tests/src/Kernel/JsonApiLinksInstallTest.php | 2 +- 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 tests/src/Functional/JsonApiLinksFunctionalTest.php diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php new file mode 100644 index 0000000..79fbec6 --- /dev/null +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -0,0 +1,150 @@ +<?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']); + } + } +} diff --git a/tests/src/Kernel/JsonApiLinksInstallTest.php b/tests/src/Kernel/JsonApiLinksInstallTest.php index ac44201..39a5fa9 100644 --- a/tests/src/Kernel/JsonApiLinksInstallTest.php +++ b/tests/src/Kernel/JsonApiLinksInstallTest.php @@ -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'; -- GitLab From 7d06fe4cb386d3e303ac245b774b5777b66487a9 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:14:47 +0900 Subject: [PATCH 04/24] test PREVIOUS major not PREV major --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9f273de..a7ba662 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -38,7 +38,7 @@ phpstan: variables: _CSPELL_WORDS: 'Kravchuk,ptmkenny,Taras,tarik' OPT_IN_TEST_NEXT_MINOR: '1' - OPT_IN_TEST_PREV_MAJOR: '1' + OPT_IN_TEST_PREVIOUS_MAJOR: '1' # SKIP_ESLINT: '1' # OPT_IN_TEST_NEXT_MAJOR: '1' # _CURL_TEMPLATES_REF: 'main' -- GitLab From 861d3f7e67e08309578bbbbe63a96f3edcf1a615 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:16:17 +0900 Subject: [PATCH 05/24] phpcs --- .../src/Functional/JsonApiLinksFunctionalTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 79fbec6..283e0cc 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -30,7 +30,8 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { * 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. + // 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. @@ -47,24 +48,24 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Enable link removal with JSON:API Links. \Drupal::configFactory()->getEditable('jsonapi_links.settings') - ->set('remove_links', true) + ->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')); + $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) + ->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')); + $this->assertEquals(FALSE, $config->get('remove_links')); // Ensure that the prior behavior has been restored. $this->checkForLinksPresent($uuid, FALSE); @@ -143,8 +144,10 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { private function assertLinksRemoved(array $array, bool $links_removed): void { if ($links_removed) { $this->assertArrayNotHasKey('related', $array['links']); - } else { + } + else { $this->assertArrayHasKey('related', $array['links']); } } + } -- GitLab From e0e95796b93cdf3833c8fc670c2f52dc3f6ab5ad Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:18:36 +0900 Subject: [PATCH 06/24] fix check for link removal --- tests/src/Functional/JsonApiLinksFunctionalTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 283e0cc..3b16100 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -143,7 +143,7 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { */ private function assertLinksRemoved(array $array, bool $links_removed): void { if ($links_removed) { - $this->assertArrayNotHasKey('related', $array['links']); + $this->assertArrayNotHasKey('links', $array); } else { $this->assertArrayHasKey('related', $array['links']); -- GitLab From 2a617ce4207e96f5a383df951c0a589fe3f42074 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:26:24 +0900 Subject: [PATCH 07/24] try busting config cache --- tests/src/Functional/JsonApiLinksFunctionalTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 3b16100..c8e4ef3 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -54,6 +54,7 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Verify that the configuration value is set. $config = $this->config('jsonapi_links.settings'); $this->assertEquals(TRUE, $config->get('remove_links')); + \Drupal::service('config.storage')->clear(); // Ensure links are removed as expected. $this->checkForLinksPresent($uuid, TRUE); -- GitLab From 21bb03a69afc705e024221d6b9c4f103ae2b5d2f Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:31:49 +0900 Subject: [PATCH 08/24] use assertTrue + assertFalse --- tests/src/Functional/JsonApiLinksFunctionalTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index c8e4ef3..abd07ae 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -53,7 +53,7 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Verify that the configuration value is set. $config = $this->config('jsonapi_links.settings'); - $this->assertEquals(TRUE, $config->get('remove_links')); + $this->assertTrue($config->get('remove_links')); \Drupal::service('config.storage')->clear(); // Ensure links are removed as expected. @@ -66,7 +66,7 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Verify that the configuration value is set. $config = $this->config('jsonapi_links.settings'); - $this->assertEquals(FALSE, $config->get('remove_links')); + $this->assertFalse($config->get('remove_links')); // Ensure that the prior behavior has been restored. $this->checkForLinksPresent($uuid, FALSE); -- GitLab From 17a860f8d1fa2f4addac6e681f2002830729c0f7 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:33:12 +0900 Subject: [PATCH 09/24] use variable for configName --- .../src/Functional/JsonApiLinksFunctionalTest.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index abd07ae..f487e4d 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -26,6 +26,13 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { */ protected $defaultTheme = 'stark'; + /** + * The module config identifier. + * + * @var string + */ + protected string $configName = 'jsonapi_links.settings'; + /** * Tests the GET method. */ @@ -47,12 +54,12 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { $this->checkForLinksPresent($uuid, FALSE); // Enable link removal with JSON:API Links. - \Drupal::configFactory()->getEditable('jsonapi_links.settings') + \Drupal::configFactory()->getEditable($this->configName) ->set('remove_links', TRUE) ->save(); // Verify that the configuration value is set. - $config = $this->config('jsonapi_links.settings'); + $config = $this->config($this->configName); $this->assertTrue($config->get('remove_links')); \Drupal::service('config.storage')->clear(); @@ -60,12 +67,12 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { $this->checkForLinksPresent($uuid, TRUE); // Disable link removal with JSON:API Links. - \Drupal::configFactory()->getEditable('jsonapi_links.settings') + \Drupal::configFactory()->getEditable($this->configName) ->set('remove_links', FALSE) ->save(); // Verify that the configuration value is set. - $config = $this->config('jsonapi_links.settings'); + $config = $this->config($this->configName); $this->assertFalse($config->get('remove_links')); // Ensure that the prior behavior has been restored. -- GitLab From 62d0913d679a53fbc6d43bcbb550724d481ae36a Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:36:29 +0900 Subject: [PATCH 10/24] invalidate all cache --- tests/src/Functional/JsonApiLinksFunctionalTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index f487e4d..4591878 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -61,7 +61,8 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Verify that the configuration value is set. $config = $this->config($this->configName); $this->assertTrue($config->get('remove_links')); - \Drupal::service('config.storage')->clear(); + // Clear cache to ensure fresh data. + \Drupal::cache()->invalidateAll(); // Ensure links are removed as expected. $this->checkForLinksPresent($uuid, TRUE); -- GitLab From 8c1e1629a2743c3bf79d1552925a86dac688afb7 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 17:41:01 +0900 Subject: [PATCH 11/24] use variable for 'remove_links' --- .../src/Functional/JsonApiLinksFunctionalTest.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 4591878..cee7f99 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -33,6 +33,13 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { */ protected string $configName = 'jsonapi_links.settings'; + /** + * The name of the config for removing links. + * + * @var string + */ + protected string $configRemoveLinks = 'remove_links'; + /** * Tests the GET method. */ @@ -55,12 +62,12 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Enable link removal with JSON:API Links. \Drupal::configFactory()->getEditable($this->configName) - ->set('remove_links', TRUE) + ->set($this->configRemoveLinks, TRUE) ->save(); // Verify that the configuration value is set. $config = $this->config($this->configName); - $this->assertTrue($config->get('remove_links')); + $this->assertTrue($config->get($this->configRemoveLinks)); // Clear cache to ensure fresh data. \Drupal::cache()->invalidateAll(); @@ -69,12 +76,12 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Disable link removal with JSON:API Links. \Drupal::configFactory()->getEditable($this->configName) - ->set('remove_links', FALSE) + ->set($this->configRemoveLinks, FALSE) ->save(); // Verify that the configuration value is set. $config = $this->config($this->configName); - $this->assertFalse($config->get('remove_links')); + $this->assertFalse($config->get($this->configRemoveLinks)); // Ensure that the prior behavior has been restored. $this->checkForLinksPresent($uuid, FALSE); -- GitLab From 4bfe67faf9506ad1dc2526b02d3d71ab5e1ac7d8 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 18:02:39 +0900 Subject: [PATCH 12/24] set config on module page instead of modifying config --- .../Functional/JsonApiLinksFunctionalTest.php | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index cee7f99..cc6c3c6 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -60,28 +60,32 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Confirm the default behavior (no change from core). $this->checkForLinksPresent($uuid, FALSE); - // Enable link removal with JSON:API Links. - \Drupal::configFactory()->getEditable($this->configName) - ->set($this->configRemoveLinks, TRUE) - ->save(); + $this->drupalLogin($this->adminUser); + $this->drupalGet('/admin/config/services/jsonapi/links'); + $form_values = [ + 'edit-remove-links' => 1, + ]; + $this->submitForm($form_values, t('Save configuration')); + $this->drupalLogout(); // Verify that the configuration value is set. - $config = $this->config($this->configName); - $this->assertTrue($config->get($this->configRemoveLinks)); - // Clear cache to ensure fresh data. - \Drupal::cache()->invalidateAll(); + $this->assertTrue($this->config($this->configName)->get($this->configRemoveLinks)); // Ensure links are removed as expected. $this->checkForLinksPresent($uuid, TRUE); // Disable link removal with JSON:API Links. - \Drupal::configFactory()->getEditable($this->configName) - ->set($this->configRemoveLinks, FALSE) - ->save(); + + $this->drupalLogin($this->adminUser); + $this->drupalGet('/admin/config/services/jsonapi/links'); + $form_values = [ + 'edit-remove-links' => 0, + ]; + $this->submitForm($form_values, t('Save configuration')); + $this->drupalLogout(); // Verify that the configuration value is set. - $config = $this->config($this->configName); - $this->assertFalse($config->get($this->configRemoveLinks)); + $this->assertFalse($this->config($this->configName)->get($this->configRemoveLinks)); // Ensure that the prior behavior has been restored. $this->checkForLinksPresent($uuid, FALSE); @@ -97,7 +101,8 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { */ 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')); + $drupal_response = $this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/node_type'); + $single_output = Json::decode($drupal_response); $this->assertSession()->statusCodeEquals(200); $this->assertArrayHasKey('type', $single_output['data']); $this->assertArrayNotHasKey('attributes', $single_output['data']); -- GitLab From ef83c47000f4224a1b3eaf060d07f96e23f6b758 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 18:25:25 +0900 Subject: [PATCH 13/24] simplify links test --- .../Functional/JsonApiLinksFunctionalTest.php | 56 +++++-------------- 1 file changed, 15 insertions(+), 41 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index cc6c3c6..115eaa1 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -101,45 +101,15 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { */ private function checkForLinksPresent(string $uuid, bool $links_removed): void { // 6. Single relationship item. - $drupal_response = $this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/node_type'); - $single_output = Json::decode($drupal_response); + $drupal_response = $this->drupalGet('/jsonapi/node/article/' . $uuid); + $single_output = json_decode($drupal_response, TRUE); $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', [ + $single_output = json_decode($this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/uid', [ 'query' => ['include' => 'uid'], - ])); + ]), TRUE); $this->assertSession()->statusCodeEquals(200); $this->assertEquals('user--user', $single_output['data']['type']); $this->assertArrayHasKey('related', $single_output['links']); @@ -157,18 +127,22 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { /** * Asserts that links are removed or present depending on the module setting. * - * @param array $array - * The array to check. - * @param bool $links_removed + * @param array $drupal_jsonapi_response + * The Drupal JSON:API response to check. + * @param bool $links_should_be_removed * TRUE if links attributes should be removed. */ - private function assertLinksRemoved(array $array, bool $links_removed): void { - if ($links_removed) { - $this->assertArrayNotHasKey('links', $array); + private function assertLinksRemoved(array $drupal_jsonapi_response, bool $links_should_be_removed): void { + $first_item = $drupal_jsonapi_response['data']; + if ($links_should_be_removed) { + // Links attribute should be removed from each item. + $this->assertArrayNotHasKey('links', $first_item); } else { - $this->assertArrayHasKey('related', $array['links']); + $this->assertArrayHasKey('links', $first_item); } + // Links attribute of response should never be removed. + $this->assertArrayHasKey('links', $drupal_jsonapi_response); } } -- GitLab From cd235d504e0e77f23408e80696dd8b3f80e5e8a1 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 18:45:10 +0900 Subject: [PATCH 14/24] first working version --- .../Functional/JsonApiLinksFunctionalTest.php | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 115eaa1..aa179c5 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -4,8 +4,6 @@ declare(strict_types=1); namespace Drupal\Tests\jsonapi\Functional; -use Drupal\Component\Serialization\Json; - /** * General functional test class. * @@ -66,7 +64,6 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { 'edit-remove-links' => 1, ]; $this->submitForm($form_values, t('Save configuration')); - $this->drupalLogout(); // Verify that the configuration value is set. $this->assertTrue($this->config($this->configName)->get($this->configRemoveLinks)); @@ -76,16 +73,16 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Disable link removal with JSON:API Links. - $this->drupalLogin($this->adminUser); $this->drupalGet('/admin/config/services/jsonapi/links'); $form_values = [ 'edit-remove-links' => 0, ]; $this->submitForm($form_values, t('Save configuration')); - $this->drupalLogout(); // Verify that the configuration value is set. $this->assertFalse($this->config($this->configName)->get($this->configRemoveLinks)); + // Logout to refresh cache. + $this->drupalLogout(); // Ensure that the prior behavior has been restored. $this->checkForLinksPresent($uuid, FALSE); @@ -101,27 +98,17 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { */ private function checkForLinksPresent(string $uuid, bool $links_removed): void { // 6. Single relationship item. - $drupal_response = $this->drupalGet('/jsonapi/node/article/' . $uuid); - $single_output = json_decode($drupal_response, TRUE); + $drupal_response1 = $this->drupalGet('/jsonapi/node/article/' . $uuid); + $single_output = json_decode($drupal_response1, TRUE); $this->assertSession()->statusCodeEquals(200); $this->assertLinksRemoved($single_output, $links_removed); // 11. Includes with relationships. - $this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/uid'); - $single_output = json_decode($this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/uid', [ + $drupal_response2 = $this->drupalGet('/jsonapi/node/article/' . $uuid . '/relationships/uid', [ 'query' => ['include' => 'uid'], - ]), TRUE); + ]); + $output_with_relationships = json_decode($drupal_response2, TRUE); $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']); + $this->assertLinksRemoved($output_with_relationships, $links_removed); } /** @@ -133,13 +120,19 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { * TRUE if links attributes should be removed. */ private function assertLinksRemoved(array $drupal_jsonapi_response, bool $links_should_be_removed): void { - $first_item = $drupal_jsonapi_response['data']; + // Merge the includes into the response data because we want to make sure + // that the links attribute has also been removed from includes. + $response_content = $drupal_jsonapi_response['data']; + if (isset($drupal_jsonapi_response['included'])) { + $response_content = array_merge($response_content, $drupal_jsonapi_response['included']); + } + $first_item = json_encode($response_content); if ($links_should_be_removed) { // Links attribute should be removed from each item. - $this->assertArrayNotHasKey('links', $first_item); + $this->assertDoesNotMatchRegularExpression('/links/', $first_item); } else { - $this->assertArrayHasKey('links', $first_item); + $this->assertMatchesRegularExpression('/links/', $first_item); } // Links attribute of response should never be removed. $this->assertArrayHasKey('links', $drupal_jsonapi_response); -- GitLab From ad6693e4e81e45336439cfdf1a4f88097c99b398 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 18:48:16 +0900 Subject: [PATCH 15/24] phpcs --- tests/src/Functional/JsonApiLinksFunctionalTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index aa179c5..00ba819 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -72,7 +72,6 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { $this->checkForLinksPresent($uuid, TRUE); // Disable link removal with JSON:API Links. - $this->drupalGet('/admin/config/services/jsonapi/links'); $form_values = [ 'edit-remove-links' => 0, -- GitLab From 5c85cd0ac4ff58c7147d6587a9ac805f746c3278 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 18:52:26 +0900 Subject: [PATCH 16/24] phpstan fixes --- .../Functional/JsonApiLinksFunctionalTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 00ba819..8e102b6 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Drupal\Tests\jsonapi\Functional; +use Drupal\jsonapi_links\Exception\JsonApiLinksException; + /** * General functional test class. * @@ -50,6 +52,9 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { $this->nodes[60]->setUnpublished()->save(); $uuid = $this->nodes[0]->uuid(); + if (!is_string($uuid)) { + throw new JsonApiLinksException("Failed to get UUID for node!"); + } // 0. HEAD request allows a client to verify that JSON:API is installed. $this->httpClient->request('HEAD', $this->buildUrl('/jsonapi/node/article')); @@ -99,6 +104,9 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // 6. Single relationship item. $drupal_response1 = $this->drupalGet('/jsonapi/node/article/' . $uuid); $single_output = json_decode($drupal_response1, TRUE); + if (!is_array($single_output)) { + throw new JsonApiLinksException("Failed to decode JSON:API response!"); + } $this->assertSession()->statusCodeEquals(200); $this->assertLinksRemoved($single_output, $links_removed); // 11. Includes with relationships. @@ -106,6 +114,9 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { 'query' => ['include' => 'uid'], ]); $output_with_relationships = json_decode($drupal_response2, TRUE); + if (!is_array($output_with_relationships)) { + throw new JsonApiLinksException("Failed to decode JSON:API response!"); + } $this->assertSession()->statusCodeEquals(200); $this->assertLinksRemoved($output_with_relationships, $links_removed); } @@ -122,10 +133,16 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { // Merge the includes into the response data because we want to make sure // that the links attribute has also been removed from includes. $response_content = $drupal_jsonapi_response['data']; + if (!is_array($response_content)) { + throw new JsonApiLinksException("Failed to parse 'data' in JSON:API response!"); + } if (isset($drupal_jsonapi_response['included'])) { $response_content = array_merge($response_content, $drupal_jsonapi_response['included']); } $first_item = json_encode($response_content); + if (!is_string($first_item)) { + throw new JsonApiLinksException("Failed to encode first item!"); + } if ($links_should_be_removed) { // Links attribute should be removed from each item. $this->assertDoesNotMatchRegularExpression('/links/', $first_item); -- GitLab From c24affb7f10873780201610769070057fd24cc16 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 18:58:18 +0900 Subject: [PATCH 17/24] ignore translatableMarkup phpstan errors --- phpstan.neon | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 6db079d..aa2de20 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,3 +5,5 @@ parameters: ignoreErrors: # Drupal does not define its own arrays. - '#no value type specified in iterable type array#' + # TranslatableMarkup is equivalent to string + - '#TranslatableMarkup given#' -- GitLab From e119efae9fb92a9dc4a3c7227f59041872f158aa Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 18:58:43 +0900 Subject: [PATCH 18/24] remove load test --- tests/src/Functional/LoadTest.php | 57 ------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 tests/src/Functional/LoadTest.php diff --git a/tests/src/Functional/LoadTest.php b/tests/src/Functional/LoadTest.php deleted file mode 100644 index e85f34a..0000000 --- a/tests/src/Functional/LoadTest.php +++ /dev/null @@ -1,57 +0,0 @@ -<?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); - } - -} -- GitLab From a95201346f94953d5b5386df951f513cd5169e78 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 19:02:09 +0900 Subject: [PATCH 19/24] backwards compatibilitly for 11.1 --- .../Functional/JsonApiLinksFunctionalTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 8e102b6..6dc1eba 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -40,6 +40,33 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { */ protected string $configRemoveLinks = 'remove_links'; + /** + * Test admin user. + * + * @var \Drupal\user\Entity\User + * + * @todo Remove when 11.2 is released. + */ + protected $adminUser; + + /** + * {@inheritdoc} + * + * @todo Remove when 11.2 is released. + */ + protected function setUp(): void { + parent::setUp(); + + $this->adminUser = $this->drupalCreateUser([ + 'create article content', + 'edit any article content', + 'delete any article content', + ], + 'jsonapi_admin_user', + TRUE, + ); + } + /** * Tests the GET method. */ -- GitLab From 4bdfbffd7f3e99e2834e2128fe24bc62161e44b5 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 19:04:22 +0900 Subject: [PATCH 20/24] rename method --- tests/src/Functional/JsonApiLinksFunctionalTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 6dc1eba..2529080 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -68,11 +68,11 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { } /** - * Tests the GET method. + * Tests link removal. + * + * Inspired by testRead() in JsonApiFucntionalTest. */ - public function testRead(): void { - // First, do testRead() from core's JSON:API and ensure that the output is - // exactly the same as core. + public function testLinkRemoval(): void { // 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. -- GitLab From 49910131bbb136451b1cff982ca294b746735ad9 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 19:07:53 +0900 Subject: [PATCH 21/24] cspell --- tests/src/Functional/JsonApiLinksFunctionalTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 2529080..9834ce1 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -70,7 +70,7 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { /** * Tests link removal. * - * Inspired by testRead() in JsonApiFucntionalTest. + * Inspired by testRead() in JsonApiFunctionalTest. */ public function testLinkRemoval(): void { // JSON:API Links does not remove anything by default. -- GitLab From d08ff1b6141ab995a6cdd8c7569b806b7762dad2 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 19:08:52 +0900 Subject: [PATCH 22/24] phpstan --- tests/src/Functional/JsonApiLinksFunctionalTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 9834ce1..4b38e72 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -57,7 +57,7 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { protected function setUp(): void { parent::setUp(); - $this->adminUser = $this->drupalCreateUser([ + $admin_user = $this->drupalCreateUser([ 'create article content', 'edit any article content', 'delete any article content', @@ -65,6 +65,10 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { 'jsonapi_admin_user', TRUE, ); + if ($admin_user === FALSE) { + throw new JsonApiLinksException("Failed to create admin user!"); + } + $this->adminUser = $admin_user; } /** -- GitLab From 532bcf2ed8c72cbaa3b4f3152206edc75f1cc15f Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 19:09:24 +0900 Subject: [PATCH 23/24] ci: do not test previous major because unit tests don't run correctly --- .gitlab-ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a7ba662..5af1449 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -38,7 +38,6 @@ phpstan: variables: _CSPELL_WORDS: 'Kravchuk,ptmkenny,Taras,tarik' OPT_IN_TEST_NEXT_MINOR: '1' - OPT_IN_TEST_PREVIOUS_MAJOR: '1' # SKIP_ESLINT: '1' # OPT_IN_TEST_NEXT_MAJOR: '1' # _CURL_TEMPLATES_REF: 'main' -- GitLab From 8e32723925e00c8c63143c03de707dab9bb2f0d7 Mon Sep 17 00:00:00 2001 From: Patrick Kenny <33362-ptmkenny@users.noreply.drupalcode.org> Date: Tue, 7 Jan 2025 19:14:27 +0900 Subject: [PATCH 24/24] fix for Drupal 11.2 --- .../Functional/JsonApiLinksFunctionalTest.php | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/src/Functional/JsonApiLinksFunctionalTest.php b/tests/src/Functional/JsonApiLinksFunctionalTest.php index 4b38e72..c996c42 100644 --- a/tests/src/Functional/JsonApiLinksFunctionalTest.php +++ b/tests/src/Functional/JsonApiLinksFunctionalTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Drupal\Tests\jsonapi\Functional; use Drupal\jsonapi_links\Exception\JsonApiLinksException; +use Drupal\user\Entity\User; /** * General functional test class. @@ -57,18 +58,20 @@ class JsonApiLinksFunctionalTest extends JsonApiFunctionalTestBase { protected function setUp(): void { parent::setUp(); - $admin_user = $this->drupalCreateUser([ - 'create article content', - 'edit any article content', - 'delete any article content', - ], - 'jsonapi_admin_user', - TRUE, - ); - if ($admin_user === FALSE) { - throw new JsonApiLinksException("Failed to create admin user!"); + if (!($this->adminUser instanceof User)) { + $admin_user = $this->drupalCreateUser([ + 'create article content', + 'edit any article content', + 'delete any article content', + ], + 'jsonapi_admin_user', + TRUE, + ); + if ($admin_user === FALSE) { + throw new JsonApiLinksException("Failed to create admin user!"); + } + $this->adminUser = $admin_user; } - $this->adminUser = $admin_user; } /** -- GitLab