Skip to content

Commit 60aa7cc

Browse files
committed
test: increase coverage and fix CI
1 parent 3f44c47 commit 60aa7cc

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

.github/workflows/drupal-module.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
SIMPLETEST_DB: "sqlite://localhost/sites/default/files/db.sqlite"
5050

5151
steps:
52-
- uses: actions/checkout@v6
52+
- uses: actions/checkout@v4
5353

5454
- uses: shivammathur/setup-php@v2
5555
with:
@@ -88,29 +88,29 @@ jobs:
8888
chmod -R 777 drupal/web/sites/simpletest/browser_output
8989
9090
- name: Run PHPUnit
91-
if: ${{ !matrix.coverage || github.ref != 'refs/heads/master' }}
91+
if: ${{ !matrix.coverage }}
9292
run: |
9393
cd drupal/web
9494
../vendor/bin/phpunit -c core modules/contrib/jsonapi_frontend_menu/tests
9595
9696
- name: Run PHPUnit (coverage)
97-
if: ${{ matrix.coverage && github.ref == 'refs/heads/master' }}
97+
if: ${{ matrix.coverage }}
9898
run: |
9999
cd drupal/web
100100
../vendor/bin/phpunit -c core modules/contrib/jsonapi_frontend_menu/tests \
101101
--coverage-clover "$GITHUB_WORKSPACE/coverage.xml" \
102102
--coverage-filter modules/contrib/jsonapi_frontend_menu
103103
104104
- name: Upload coverage artifact
105-
if: ${{ matrix.coverage && github.ref == 'refs/heads/master' }}
105+
if: ${{ matrix.coverage }}
106106
uses: actions/upload-artifact@v4
107107
with:
108108
name: coverage-jsonapi_frontend_menu
109109
path: coverage.xml
110110
if-no-files-found: error
111111

112112
- name: Upload coverage to Codecov
113-
if: ${{ matrix.coverage && github.ref == 'refs/heads/master' }}
113+
if: ${{ matrix.coverage }}
114114
continue-on-error: true
115115
uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de
116116
with:

.github/workflows/semgrep.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
security-events: write
1818

1919
steps:
20-
- uses: actions/checkout@v6
20+
- uses: actions/checkout@v4
2121

2222
- name: Run Semgrep
2323
run: |

codecov.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: 0.9
6+
threshold: 0.01
7+
patch:
8+
default:
9+
target: 0.9
10+
threshold: 0.01
11+

tests/src/Kernel/MenuEndpointTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ protected function setUp(): void {
153153
$login->save();
154154
$this->ids['login'] = 'menu_link_content:' . $login->uuid();
155155

156+
$query = MenuLinkContent::create([
157+
'title' => 'With Query',
158+
'menu_name' => 'main',
159+
'link' => [
160+
'uri' => 'internal:/about-us',
161+
'options' => [
162+
'query' => [
163+
'utm' => '1',
164+
],
165+
'fragment' => 'frag',
166+
],
167+
],
168+
]);
169+
$query->save();
170+
$this->ids['query'] = 'menu_link_content:' . $query->uuid();
171+
156172
$this->config('jsonapi_frontend.settings')
157173
->set('drupal_base_url', 'https://cms.example.com')
158174
->save();
@@ -272,6 +288,67 @@ public function testResolveCanBeDisabled(): void {
272288
$this->assertNull($about['resolve']);
273289
}
274290

291+
public function testMenuQueryDepthAndParentOptionsAreHandled(): void {
292+
$this->config('jsonapi_frontend.settings')
293+
->set('resolver.cache_max_age', 60)
294+
->set('resolver.langcode_fallback', 'current')
295+
->save();
296+
297+
$this->container->get('current_user')->setAccount(new AnonymousUserSession());
298+
299+
$controller = \Drupal\jsonapi_frontend_menu\Controller\MenuController::create($this->container);
300+
$request = Request::create('/jsonapi/menu/main', 'GET', [
301+
'_format' => 'json',
302+
'path' => 'https://www.example.com/about-us/team?utm=1#frag',
303+
'min_depth' => '2',
304+
'max_depth' => '3',
305+
'parent' => $this->ids['about'],
306+
]);
307+
308+
$response = $controller->menu($request, 'main');
309+
$this->assertSame(200, $response->getStatusCode());
310+
$this->assertStringContainsString('public', (string) $response->headers->get('Cache-Control'));
311+
312+
$payload = json_decode((string) $response->getContent(), TRUE);
313+
$this->assertIsArray($payload);
314+
$this->assertSame([$this->ids['about'], $this->ids['team']], $payload['meta']['active_trail']);
315+
316+
$team = $this->findItemById($payload['data'], $this->ids['team']);
317+
$this->assertNotNull($team);
318+
}
319+
320+
public function testAuthenticatedUserDisablesCaching(): void {
321+
$this->config('jsonapi_frontend.settings')
322+
->set('resolver.cache_max_age', 60)
323+
->save();
324+
325+
$controller = \Drupal\jsonapi_frontend_menu\Controller\MenuController::create($this->container);
326+
$request = Request::create('/jsonapi/menu/main', 'GET', [
327+
'_format' => 'json',
328+
]);
329+
330+
$response = $controller->menu($request, 'main');
331+
$this->assertSame(200, $response->getStatusCode());
332+
$this->assertStringContainsString('no-store', (string) $response->headers->get('Cache-Control'));
333+
}
334+
335+
public function testInternalUrlsAreNormalizedForResolve(): void {
336+
$this->container->get('current_user')->setAccount(new AnonymousUserSession());
337+
338+
$controller = \Drupal\jsonapi_frontend_menu\Controller\MenuController::create($this->container);
339+
$request = Request::create('/jsonapi/menu/main', 'GET', [
340+
'_format' => 'json',
341+
]);
342+
343+
$response = $controller->menu($request, 'main');
344+
$payload = json_decode((string) $response->getContent(), TRUE);
345+
346+
$query = $this->findItemById($payload['data'], $this->ids['query']);
347+
$this->assertNotNull($query);
348+
$this->assertSame('/about-us?utm=1', $query['url']);
349+
$this->assertTrue($query['resolve']['resolved']);
350+
}
351+
275352
/**
276353
* Find an item by id in a nested menu tree.
277354
*

0 commit comments

Comments
 (0)