Skip to content

Commit 743aa9e

Browse files
committed
test: add functional smoke coverage
Run BrowserTestBase smoke and ensure custom /jsonapi endpoints return application/json to avoid JSON:API response validation asserts.
1 parent fa86022 commit 743aa9e

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

.github/workflows/drupal-module.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
coverage: false
4646

4747
env:
48-
SIMPLETEST_BASE_URL: "http://127.0.0.1"
48+
SIMPLETEST_BASE_URL: "http://127.0.0.1:8888"
4949
SIMPLETEST_DB: "sqlite://localhost/sites/default/files/db.sqlite"
5050

5151
steps:
@@ -82,6 +82,12 @@ jobs:
8282
chmod -R 777 drupal/web/sites/default/files
8383
chmod -R 777 drupal/web/sites/simpletest/browser_output
8484
85+
- name: Start test webserver
86+
run: |
87+
cd drupal/web
88+
php -S 127.0.0.1:8888 .ht.router.php >/tmp/php-server.log 2>&1 &
89+
echo $! > /tmp/php-server.pid
90+
8591
- name: Run PHPUnit
8692
if: ${{ !matrix.coverage }}
8793
run: |
@@ -115,3 +121,10 @@ jobs:
115121
flags: phpunit,drupal
116122
fail_ci_if_error: false
117123
verbose: true
124+
125+
- name: Stop test webserver
126+
if: always()
127+
run: |
128+
if [ -f /tmp/php-server.pid ]; then
129+
kill "$(cat /tmp/php-server.pid)" || true
130+
fi

src/Controller/PathResolverController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
final class PathResolverController extends ControllerBase {
2121

22-
private const CONTENT_TYPE = 'application/vnd.api+json; charset=utf-8';
22+
private const CONTENT_TYPE = 'application/json; charset=utf-8';
2323

2424
public function __construct(
2525
private readonly PathResolverInterface $resolver,

src/Controller/RoutesFeedController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
final class RoutesFeedController extends ControllerBase {
2222

23-
private const CONTENT_TYPE = 'application/vnd.api+json; charset=utf-8';
23+
private const CONTENT_TYPE = 'application/json; charset=utf-8';
2424

2525
public function __construct(
2626
private readonly RoutesFeedBuilder $routesFeedBuilder,

tests/src/Functional/SmokeTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\Tests\jsonapi_frontend\Functional;
6+
7+
use Drupal\node\Entity\Node;
8+
use Drupal\node\Entity\NodeType;
9+
use Drupal\Tests\BrowserTestBase;
10+
use Drupal\user\Entity\Role;
11+
use Drupal\user\RoleInterface;
12+
13+
/**
14+
* Smoke tests for the settings form + resolver endpoint.
15+
*
16+
* @group jsonapi_frontend
17+
*/
18+
#[\PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses]
19+
final class SmokeTest extends BrowserTestBase {
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected static $modules = [
25+
'system',
26+
'user',
27+
'field',
28+
'text',
29+
'filter',
30+
'file',
31+
'node',
32+
'path',
33+
'path_alias',
34+
'serialization',
35+
'jsonapi',
36+
'jsonapi_frontend',
37+
];
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected $defaultTheme = 'stark';
43+
44+
public function testSettingsSaveAndResolverResponse(): void {
45+
$anonymous = Role::load(RoleInterface::ANONYMOUS_ID);
46+
$anonymous?->grantPermission('access content');
47+
$anonymous?->save();
48+
49+
NodeType::create([
50+
'type' => 'page',
51+
'name' => 'Page',
52+
])->save();
53+
54+
$node = Node::create([
55+
'type' => 'page',
56+
'title' => 'About Us',
57+
'status' => 1,
58+
'path' => ['alias' => '/about-us'],
59+
]);
60+
$node->save();
61+
62+
$admin = $this->createUser(['administer jsonapi frontend']);
63+
$this->drupalLogin($admin);
64+
65+
$this->drupalGet('/admin/config/services/jsonapi-frontend');
66+
$this->assertSession()->statusCodeEquals(200);
67+
68+
$this->submitForm([
69+
'enable_all' => FALSE,
70+
'headless_bundles[node][page]' => TRUE,
71+
'resolver_cache_max_age' => 60,
72+
'resolver_langcode_fallback' => 'site_default',
73+
], 'Save configuration');
74+
75+
$this->assertSession()->statusCodeEquals(200);
76+
$this->assertSession()->pageTextContains('The configuration options have been saved.');
77+
78+
$this->drupalLogout();
79+
80+
$this->drupalGet('/jsonapi/resolve', [
81+
'query' => [
82+
'path' => '/about-us',
83+
'_format' => 'json',
84+
],
85+
]);
86+
$this->assertSession()->statusCodeEquals(200);
87+
88+
$payload = json_decode($this->getSession()->getPage()->getContent(), TRUE);
89+
$this->assertIsArray($payload);
90+
$this->assertTrue($payload['resolved']);
91+
$this->assertSame('entity', $payload['kind']);
92+
$this->assertSame('node--page', $payload['entity']['type']);
93+
$this->assertSame($node->uuid(), $payload['entity']['id']);
94+
$this->assertSame(TRUE, $payload['headless']);
95+
$this->assertIsString($payload['jsonapi_url']);
96+
$this->assertStringContainsString('/jsonapi/node/page/', $payload['jsonapi_url']);
97+
}
98+
99+
}

0 commit comments

Comments
 (0)