Skip to content

Commit

Permalink
SLB-281 path aliases 404 issues (#1532)
Browse files Browse the repository at this point in the history
- path alias update now triggers a build
  • Loading branch information
dspachos committed Jun 12, 2024
1 parent e97c31d commit 842449e
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules

# Local Netlify folder
.netlify

deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\node\Entity\Node;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\path_alias\Entity\PathAlias;
use Drupal\silverback_gatsby\Plugin\FeedBase;
use Drupal\silverback_gatsby\Plugin\GraphQL\DataProducer\GatsbyBuildId;
use GraphQL\Language\AST\DocumentNode;
Expand Down Expand Up @@ -53,6 +56,12 @@ class EntityFeed extends FeedBase implements ContainerFactoryPluginInterface {
*/
protected ?ContentTranslationManagerInterface $contentTranslationManager;

/**
* The path alias manager.
*
* @var \Drupal\path_alias\AliasManagerInterface
*/
protected $pathAliasManager;

/**
* {@inheritDoc}
Expand All @@ -67,22 +76,23 @@ public static function create(
$configuration,
$plugin_id,
$plugin_definition,
$container->has('content_translation.manager')
? $container->get('content_translation.manager')
: NULL
$container->has('content_translation.manager') ? $container->get('content_translation.manager') : NULL,
$container->get('path_alias.manager')
);
}

public function __construct(
$config,
$plugin_id,
$plugin_definition,
?ContentTranslationManagerInterface $contentTranslationManager
?ContentTranslationManagerInterface $contentTranslationManager,
AliasManagerInterface $path_alias_manager
) {
$this->type = $config['type'];
$this->bundle = $config['bundle'] ?? NULL;
$this->access = $config['access'] ?? true;
$this->access = $config['access'] ?? TRUE;
$this->contentTranslationManager = $contentTranslationManager;
$this->pathAliasManager = $path_alias_manager;

parent::__construct(
$config,
Expand All @@ -103,6 +113,19 @@ public function isTranslatable(): bool {
* {@inheritDoc}
*/
public function getUpdateIds($context, ?AccountInterface $account) : array {

// Special case for path alias.
if ($context instanceof PathAlias) {
$path = $this->pathAliasManager->getPathByAlias($context->alias->value);
if (preg_match('/node\/(\d+)/', $path, $matches)) {
$node = isset($matches[1]) ? Node::load($matches[1]) : NULL;
if ($node) {
// See SLB-281 for details.
$context = $node;
}
}
}

if (
$context instanceof EntityInterface
&& $context->getEntityTypeId() === $this->type
Expand All @@ -123,7 +146,7 @@ public function getUpdateIds($context, ?AccountInterface $account) : array {
/**
* {@inheritDoc}
*/
public function resolveItem(ResolverInterface $id, ?ResolverInterface $langcode = null): ResolverInterface {
public function resolveItem(ResolverInterface $id, ?ResolverInterface $langcode = NULL): ResolverInterface {
$resolver = $this->builder->produce('fetch_entity')
->map('type', $this->builder->fromValue($this->type))
->map('bundles', $this->builder->fromValue($this->bundle === NULL ? NULL : [$this->bundle]))
Expand Down Expand Up @@ -194,11 +217,17 @@ public function resolveTranslations(): ResolverInterface {
);
}

/**
*
*/
public function getExtensionDefinition(DocumentNode $parentAst): string {
$type = $this->typeName;
return "\nextend type Query { _load{$type}Revision(id: String!, revision: String!) : $type @deprecated }";
}

/**
*
*/
public function addExtensionResolvers(
ResolverRegistryInterface $registry,
ResolverBuilder $builder
Expand All @@ -209,15 +238,14 @@ public function addExtensionResolvers(
: $builder->fromArgument('id');
$langResolver = $this->isTranslatable()
? $builder->produce('gatsby_extract_langcode')->map('id', $builder->fromArgument('id'))
: $builder->fromValue(null);
: $builder->fromValue(NULL);
$resolver = $this->builder->produce('fetch_entity')
->map('type', $this->builder->fromValue($this->type))
->map('bundles', $this->builder->fromValue($this->bundle === NULL ? NULL : [$this->bundle]))
->map('access', $this->builder->fromValue($this->access))
->map('id', $idResolver)
->map('language', $langResolver)
->map('revision_id', $builder->fromArgument('revision'))
;
->map('revision_id', $builder->fromArgument('revision'));
$registry->addFieldResolver("Query", "_load{$type}Revision", $resolver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use GuzzleHttp\Psr7\Request;
use Prophecy\Argument;

/**
*
*/
class GatsbyBuildTriggerTest extends KernelTestBase {
use NotificationCheckTrait;

Expand All @@ -28,6 +31,7 @@ class GatsbyBuildTriggerTest extends KernelTestBase {
'silverback_gatsby',
'silverback_gatsby_example',
'menu_link_content',
'path_alias',
];

/**
Expand All @@ -40,6 +44,9 @@ class GatsbyBuildTriggerTest extends KernelTestBase {
*/
protected $trigger;

/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->setupClientProphecy();
Expand All @@ -58,12 +65,12 @@ protected function setUp() : void {
'schema_configuration' => [
'directable' => [
'extensions' => [
'silverback_gatsby' => 'silverback_gatsby'
'silverback_gatsby' => 'silverback_gatsby',
],
'schema_definition' => __DIR__ . '/../../../modules/silverback_gatsby_example/graphql/silverback_gatsby_example.graphqls',
'build_webhook' => 'http://localhost:8000/__refresh'
]
]
'build_webhook' => 'http://localhost:8000/__refresh',
],
],
])->save();

Server::create([
Expand All @@ -73,22 +80,28 @@ protected function setUp() : void {
'schema_configuration' => [
'directable' => [
'extensions' => [
'silverback_gatsby' => 'silverback_gatsby'
'silverback_gatsby' => 'silverback_gatsby',
],
'schema_definition' => __DIR__ . '/../../../modules/silverback_gatsby_example/graphql/silverback_gatsby_example.graphqls',
'build_webhook' => 'http://localhost:9000/__refresh'
]
]
'build_webhook' => 'http://localhost:9000/__refresh',
],
],
])->save();

}

/**
*
*/
public function testBeforeShutdown() {
$this->trigger->trigger('foo', 1);
// If _drupal_shutdown_function() is not called, no notifications go out.
$this->checkTotalNotifications(0);
}

/**
*
*/
public function testRequestException() {
$this->clientProphecy->post(Argument::any(), Argument::any())
->willThrow(new RequestException('Invalid!', new Request('post', 'http://localhost:8000/__refresh')));
Expand All @@ -99,13 +112,19 @@ public function testRequestException() {
$this->messengerProphecy->addError('Could not send build notification to server "http://localhost:8000/__refresh".')->shouldHaveBeenCalledTimes(1);
}

/**
*
*/
public function testSingleTrigger() {
$this->trigger->trigger('foo', 1);
_drupal_shutdown_function();
$this->checkTotalNotifications(1);
$this->checkBuildNotification('http://localhost:8000/__refresh', 1);
}

/**
*
*/
public function testMultipleTriggers() {
$this->trigger->trigger('foo', 1);
$this->trigger->trigger('foo', 2);
Expand All @@ -114,6 +133,9 @@ public function testMultipleTriggers() {
$this->checkBuildNotification('http://localhost:8000/__refresh', 2);
}

/**
*
*/
public function testMultipleServers() {
$this->trigger->trigger('foo', 1);
$this->trigger->trigger('bar', 2);
Expand All @@ -122,4 +144,5 @@ public function testMultipleServers() {
$this->checkBuildNotification('http://localhost:8000/__refresh', 1);
$this->checkBuildNotification('http://localhost:9000/__refresh', 2);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
use Drupal\node\Entity\Node;
use Drupal\silverback_gatsby\GatsbyUpdate;
use Drupal\silverback_gatsby\GatsbyUpdateTrigger;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;

/**
*
*/
class GatsbyUpdateHandlerTest extends EntityFeedTestBase {

use PathAliasTestTrait;
public static $modules = ['path_alias'];

/**
* @var \Drupal\silverback_gatsby\GatsbyUpdateTracker|object|null
*/
Expand All @@ -19,17 +26,27 @@ class GatsbyUpdateHandlerTest extends EntityFeedTestBase {
*/
protected $triggerProphecy;

/**
*
*/
public function register(ContainerBuilder $container) {
parent::register($container);
$this->triggerProphecy = $this->prophesize(GatsbyUpdateTrigger::class);
$container->set('silverback_gatsby.update_trigger', $this->triggerProphecy->reveal());
}

/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->installEntitySchema('path_alias');
$this->tracker = $this->container->get('silverback_gatsby.update_tracker');
}

/**
*
*/
public function testLogRelevantChanges() {
$node = Node::create([
'type' => 'page',
Expand All @@ -54,6 +71,9 @@ public function testLogRelevantChanges() {
], $diff);
}

/**
*
*/
public function testIgnoreIrrelevantChanges() {
$node = Node::create([
'type' => 'article',
Expand All @@ -68,6 +88,9 @@ public function testIgnoreIrrelevantChanges() {
$this->assertEmpty($diff);
}

/**
*
*/
public function testTriggerUpdates() {
$page = Node::create([
'type' => 'page',
Expand All @@ -86,4 +109,23 @@ public function testTriggerUpdates() {
$this->triggerProphecy
->trigger($this->server->id(), new GatsbyUpdate('Page', $page->uuid() . ':en'))->shouldHaveBeenCalledTimes(1);
}

/**
* Test case for testing the behavior when path alias triggers updates.
*/
public function testPathAliasTriggerUpdates() {
$node = Node::create([
'type' => 'page',
'title' => 'Test',
]);
$node->save();
$this->tracker->clear();
$this->createPathAlias('/node/1', '/test', 'en');
$this->tracker->clear();
// We expect two calls for the same page, as the path alias
// should also trigger a page build.
$this->triggerProphecy
->trigger($this->server->id(), new GatsbyUpdate('Page', $node->uuid() . ':en'))->shouldHaveBeenCalledTimes(2);
}

}
Loading

0 comments on commit 842449e

Please sign in to comment.