Skip to content

Commit 31c4753

Browse files
authored
fix(dataproducer): missing isRouted and access check (#1305)
1 parent ffed89b commit 31c4753

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

.github/workflows/testing.yml

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ jobs:
8585
run: composer --no-interaction --no-progress require \
8686
webonyx/graphql-php:^14.8 \
8787
drupal/typed_data:^1.0 \
88+
drupal/redirect:^1.0 \
8889
phpstan/phpstan:^1.7.14 \
8990
mglaman/phpstan-drupal:^1.1.2 \
9091
phpstan/phpstan-deprecation-rules:^1.0.0 \

src/Plugin/GraphQL/DataProducer/Routing/RouteLoad.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ public function __construct(
9595
* @return \Drupal\Core\Url|null
9696
*/
9797
public function resolve($path, RefinableCacheableDependencyInterface $metadata) {
98-
if ($this->redirectRepository) {
99-
/** @var \Drupal\redirect\Entity\Redirect|null $redirect */
100-
$redirect = $this->redirectRepository->findMatchingRedirect($path, []);
101-
if ($redirect) {
102-
return $redirect->getRedirectUrl();
103-
}
98+
$redirect = $this->redirectRepository ? $this->redirectRepository->findMatchingRedirect($path, []) : NULL;
99+
if ($redirect !== NULL) {
100+
$url = $redirect->getRedirectUrl();
101+
}
102+
else {
103+
$url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path);
104104
}
105105

106-
if (($url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path)) && $url->isRouted() && $url->access()) {
106+
if ($url && $url->isRouted() && $url->access()) {
107107
return $url;
108108
}
109109

tests/src/Kernel/DataProducer/RoutingTest.php

+57
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Drupal\Tests\graphql\Kernel\DataProducer;
44

5+
use Drupal\node\Entity\Node;
6+
use Drupal\node\Entity\NodeType;
57
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
68

79
/**
@@ -11,6 +13,25 @@
1113
*/
1214
class RoutingTest extends GraphQLTestBase {
1315

16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected static $modules = [
20+
'redirect',
21+
'views',
22+
'path_alias',
23+
];
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function setUp(): void {
29+
parent::setUp();
30+
31+
$this->installEntitySchema('redirect');
32+
$this->installConfig(['redirect']);
33+
}
34+
1435
/**
1536
* @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Routing\RouteLoad::resolve
1637
*/
@@ -21,6 +42,42 @@ public function testRouteLoad(): void {
2142

2243
$this->assertNotNull($result);
2344
$this->assertEquals('user.logout', $result->getRouteName());
45+
46+
// Test route_load with redirect to an internal URL.
47+
NodeType::create([
48+
'type' => 'test',
49+
'name' => 'Test',
50+
])->save();
51+
$node = Node::create([
52+
'title' => 'Node',
53+
'type' => 'test',
54+
]);
55+
$node->save();
56+
$nodeUrl = $node->toUrl()->toString();
57+
58+
/** @var \Drupal\redirect\Entity\Redirect $redirect */
59+
$redirect = $this->container->get('entity_type.manager')->getStorage('redirect')->create();
60+
$redirect->setSource('internal-url');
61+
$redirect->setRedirect($nodeUrl);
62+
$redirect->save();
63+
64+
/** @var \Drupal\Core\Url $result */
65+
$result = $this->executeDataProducer('route_load', [
66+
'path' => 'internal-url',
67+
]);
68+
69+
$this->assertNotNull($result);
70+
$this->assertEquals($nodeUrl, $result->toString());
71+
72+
$redirect->setSource('external-url');
73+
$redirect->setRedirect('https://example.com');
74+
$redirect->save();
75+
76+
$result = $this->executeDataProducer('route_load', [
77+
'path' => 'external-url',
78+
]);
79+
80+
$this->assertNull($result, 'Route to external URL should not be found.');
2481
}
2582

2683
/**

0 commit comments

Comments
 (0)