Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing isRouted and access check #1305

Merged
merged 6 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
run: composer --no-interaction --no-progress require \
webonyx/graphql-php:^14.8 \
drupal/typed_data:^1.0 \
drupal/redirect:^1.0 \
phpstan/phpstan:^1.7.14 \
mglaman/phpstan-drupal:^1.1.2 \
phpstan/phpstan-deprecation-rules:^1.0.0 \
Expand Down
12 changes: 6 additions & 6 deletions src/Plugin/GraphQL/DataProducer/Routing/RouteLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ public function __construct(
* @return \Drupal\Core\Url|null
*/
public function resolve($path, RefinableCacheableDependencyInterface $metadata) {
if ($this->redirectRepository) {
if ($this->redirectRepository && $redirect = $this->redirectRepository->findMatchingRedirect($path, [])) {
/** @var \Drupal\redirect\Entity\Redirect|null $redirect */
$redirect = $this->redirectRepository->findMatchingRedirect($path, []);
if ($redirect) {
return $redirect->getRedirectUrl();
}
$url = $redirect->getRedirectUrl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the type annotation on line 99. The @var annotation is not needed for getRedirectUrl.

Is it possible to take the $redirect = out of the if statement for legibility?

I've opened https://www.drupal.org/project/redirect/issues/3309603 to handle the return type annotation that's incorrect for findMatchingRedirect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can take out $redirect = but that gets otherwise ugly. Would you prefer something like this?

    if ($this->redirectRepository) {
      $redirect = $this->redirectRepository->findMatchingRedirect($path, []);
      if ($redirect){
        $url = $redirect->getRedirectUrl();
      }
    }

    if(empty($url)) {
      $url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path);
    }

    if ($url && $url->isRouted() && $url->access()) {
      return $url;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something along the lines of this is better

    if ($this->redirectRepository) {
      $redirect = $this->redirectRepository->findMatchingRedirect($path, []);
    }
    if (!empty($redirect)){
      $url = $redirect->getRedirectUrl();
    }
    else {
      $url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path);
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a ternary may be okay, I also tend to avoid empty checks if possible.

<?php

$redirect = $this->redirectRepository ? $this->redirectRepository->findMatchingRedirect($path, []) : NULL;
if ($redirect !== NULL) {
  $url = $redirect->getRedirectUrl();
}
else {
  $url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
else {
$url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path);
}

if (($url = $this->pathValidator->getUrlIfValidWithoutAccessCheck($path)) && $url->isRouted() && $url->access()) {
if ($url && $url->isRouted() && $url->access()) {
return $url;
}

Expand Down
57 changes: 57 additions & 0 deletions tests/src/Kernel/DataProducer/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\Tests\graphql\Kernel\DataProducer;

use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;

/**
Expand All @@ -11,6 +13,25 @@
*/
class RoutingTest extends GraphQLTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'redirect',
'views',
'path_alias',
];

/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();

$this->installEntitySchema('redirect');
$this->installConfig(['redirect']);
}

/**
* @covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Routing\RouteLoad::resolve
*/
Expand All @@ -21,6 +42,42 @@ public function testRouteLoad(): void {

$this->assertNotNull($result);
$this->assertEquals('user.logout', $result->getRouteName());

// Test route_load with redirect to an internal URL.
NodeType::create([
'type' => 'test',
'name' => 'Test',
])->save();
$node = Node::create([
'title' => 'Node',
'type' => 'test',
]);
$node->save();
$nodeUrl = $node->toUrl()->toString();

/** @var \Drupal\redirect\Entity\Redirect $redirect */
$redirect = $this->container->get('entity_type.manager')->getStorage('redirect')->create();
$redirect->setSource('internal-url');
$redirect->setRedirect($nodeUrl);
$redirect->save();

/** @var \Drupal\Core\Url $result */
$result = $this->executeDataProducer('route_load', [
'path' => 'internal-url',
]);

$this->assertNotNull($result);
$this->assertEquals($nodeUrl, $result->toString());

$redirect->setSource('external-url');
$redirect->setRedirect('https://example.com');
$redirect->save();

$result = $this->executeDataProducer('route_load', [
'path' => 'external-url',
]);

$this->assertNull($result, 'Route to external URL should not be found.');
}

/**
Expand Down