Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
jsonapi_links
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
jsonapi_links
Merge requests
!27
add unit test to confirm core functionality
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
add unit test to confirm core functionality
issue/jsonapi_links-3497804:unit_test
into
1.1.x
Overview
0
Commits
24
Pipelines
13
Changes
4
Merged
Patrick Kenny
requested to merge
issue/jsonapi_links-3497804:unit_test
into
1.1.x
4 months ago
Overview
0
Commits
24
Pipelines
13
Changes
4
Expand
0
0
Merge request reports
Compare
1.1.x
version 12
8e327239
4 months ago
version 11
532bcf2e
4 months ago
version 10
4bdfbffd
4 months ago
version 9
a9520134
4 months ago
version 8
e119efae
4 months ago
version 7
5c85cd0a
4 months ago
version 6
cd235d50
4 months ago
version 5
62d0913d
4 months ago
version 4
2a617ce4
4 months ago
version 3
e0e95796
4 months ago
version 2
85d9bb2e
4 months ago
version 1
6b160b2c
4 months ago
1.1.x (base)
and
latest version
latest version
8e327239
24 commits,
4 months ago
version 12
8e327239
24 commits,
4 months ago
version 11
532bcf2e
23 commits,
4 months ago
version 10
4bdfbffd
20 commits,
4 months ago
version 9
a9520134
19 commits,
4 months ago
version 8
e119efae
18 commits,
4 months ago
version 7
5c85cd0a
16 commits,
4 months ago
version 6
cd235d50
14 commits,
4 months ago
version 5
62d0913d
10 commits,
4 months ago
version 4
2a617ce4
7 commits,
4 months ago
version 3
e0e95796
6 commits,
4 months ago
version 2
85d9bb2e
3 commits,
4 months ago
version 1
6b160b2c
1 commit,
4 months ago
4 files
+
194
−
2
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
tests/src/Functional/JsonApiLinksFunctionalTest.php
0 → 100644
+
191
−
0
Options
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\Tests\jsonapi\Functional
;
use
Drupal\jsonapi_links
\Exception\JsonApiLinksException
;
use
Drupal\user\Entity\User
;
/**
* General functional test class.
*
* @group jsonapi_links
*/
class
JsonApiLinksFunctionalTest
extends
JsonApiFunctionalTestBase
{
/**
* {@inheritdoc}
*/
protected
static
$modules
=
[
'basic_auth'
,
'jsonapi_links'
,
];
/**
* {@inheritdoc}
*/
protected
$defaultTheme
=
'stark'
;
/**
* The module config identifier.
*
* @var string
*/
protected
string
$configName
=
'jsonapi_links.settings'
;
/**
* The name of the config for removing links.
*
* @var string
*/
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
();
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
;
}
}
/**
* Tests link removal.
*
* Inspired by testRead() in JsonApiFunctionalTest.
*/
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.
$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'
));
$this
->
assertSession
()
->
statusCodeEquals
(
200
);
// Confirm the default behavior (no change from core).
$this
->
checkForLinksPresent
(
$uuid
,
FALSE
);
$this
->
drupalLogin
(
$this
->
adminUser
);
$this
->
drupalGet
(
'/admin/config/services/jsonapi/links'
);
$form_values
=
[
'edit-remove-links'
=>
1
,
];
$this
->
submitForm
(
$form_values
,
t
(
'Save configuration'
));
// Verify that the configuration value is set.
$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.
$this
->
drupalGet
(
'/admin/config/services/jsonapi/links'
);
$form_values
=
[
'edit-remove-links'
=>
0
,
];
$this
->
submitForm
(
$form_values
,
t
(
'Save configuration'
));
// 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
);
}
/**
* 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.
$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.
$drupal_response2
=
$this
->
drupalGet
(
'/jsonapi/node/article/'
.
$uuid
.
'/relationships/uid'
,
[
'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
);
}
/**
* Asserts that links are removed or present depending on the module setting.
*
* @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
$drupal_jsonapi_response
,
bool
$links_should_be_removed
):
void
{
// 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
);
}
else
{
$this
->
assertMatchesRegularExpression
(
'/links/'
,
$first_item
);
}
// Links attribute of response should never be removed.
$this
->
assertArrayHasKey
(
'links'
,
$drupal_jsonapi_response
);
}
}
Loading