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

Add $_GET['q'] #113

Merged
merged 7 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions src/Path/CurrentPathStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Retrofit\Drupal\Path;

use Drupal\Core\Path\CurrentPathStack as CoreCurrentPathStack;
use Symfony\Component\HttpFoundation\Request;

class CurrentPathStack extends CoreCurrentPathStack
{
public function getPath(?Request $request = null): string
{
$request ??= $this->requestStack->getCurrentRequest();
assert($request instanceof Request);
$this->paths[$request] ??= ['path' => $request->getPathInfo()];
assert(is_array($this->paths[$request]) && is_string($this->paths[$request]['path']));
return $this->paths[$request]['path'];
}
mglaman marked this conversation as resolved.
Show resolved Hide resolved

public function setPath($path, ?Request $request = null): static
{
$request ??= $this->requestStack->getCurrentRequest();
assert($request instanceof Request);
$_GET['q'] = $path;
$this->paths[$request] = ['path' => &$_GET['q']];
return $this;
}
}
7 changes: 7 additions & 0 deletions src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Retrofit\Drupal\Menu\MenuLinkManager;
use Retrofit\Drupal\Extension\ModuleHandler;
use Retrofit\Drupal\ParamConverter\PageArgumentsConverter;
use Retrofit\Drupal\Path\CurrentPathStack;
use Retrofit\Drupal\Render\AttachmentResponseSubscriber;
use Retrofit\Drupal\Render\RetrofitHtmlResponseAttachmentsProcessor;
use Retrofit\Drupal\Routing\HookMenuRegistry;
Expand Down Expand Up @@ -131,6 +132,12 @@ public function register(ContainerBuilder $container)
(new ChildDefinition('form_builder'))
->setDecoratedService('form_builder')
);

$container->setDefinition(
CurrentPathStack::class,
(new ChildDefinition('path.current'))
->setDecoratedService('path.current')
);
}

public function alter(ContainerBuilder $container)
Expand Down
43 changes: 43 additions & 0 deletions tests/src/Integration/Path/CurrentPathStackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Retrofit\Drupal\Tests\Integration\Path;

use mglaman\DrupalTestHelpers\RequestTrait;
use Retrofit\Drupal\Tests\Integration\IntegrationTestCase;
use Symfony\Component\HttpFoundation\Request;

final class CurrentPathStackTest extends IntegrationTestCase
{
use RequestTrait;

protected static $modules = [
'system',
'user',
];

protected function tearDown(): void
{
// phpcs:ignore
unset($_GET['q']);
parent::tearDown();
}

public function testIntegration(): void
{
self::assertArrayNotHasKey('q', $_GET);
$this->doRequest(Request::create('/user/login'));
self::assertEquals(
'/user/login',
$this->container->get('path.current')->getPath()
);
self::assertArrayHasKey('q', $_GET);
self::assertEquals('/user/login', $_GET['q']);
$_GET['q'] = '/login';
self::assertEquals(
'/login',
$this->container->get('path.current')->getPath()
);
}
}
74 changes: 74 additions & 0 deletions tests/src/Unit/Path/CurrentPathStackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Retrofit\Drupal\Tests\Unit\Path;

use PHPUnit\Framework\TestCase;
use Retrofit\Drupal\Path\CurrentPathStack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @coversDefaultClass \Retrofit\Drupal\Path\CurrentPathStack
*/
final class CurrentPathStackTest extends TestCase
{
protected function tearDown(): void
{
// phpcs:ignore
unset($_GET['q']);
}

/**
* @covers ::getPath
*/
public function testGetPath(): void
{
$request1 = Request::create('/foo');
$request2 = Request::create('/bar');
$requestStack = new RequestStack();
$requestStack->push($request1);
$sut = new CurrentPathStack($requestStack);
self::assertEquals('/foo', $sut->getPath());
self::assertEquals('/bar', $sut->getPath($request2));
$requestStack->push($request2);
self::assertEquals('/foo', $sut->getPath($request1));
self::assertEquals('/bar', $sut->getPath());
}

/**
* @covers ::setPath
*/
public function testSetPath(): void
{
$path = '/node/1';
$request = Request::create('/test-page');
$requestStack = new RequestStack();
$requestStack->push($request);

$sut = new CurrentPathStack($requestStack);

self::assertArrayNotHasKey('q', $_GET);
$sut->setPath($path, $request);
self::assertArrayHasKey('q', $_GET);
self::assertEquals($path, $_GET['q']);
}

/**
* @covers ::setPath
* @covers ::getPath
*/
public function testMutable(): void
{
$request = Request::create('/test-page');
$requestStack = new RequestStack();
$requestStack->push($request);

$sut = new CurrentPathStack($requestStack);
$sut->setPath('/node/1', $request);
$_GET['q'] = '/node/2';
self::assertEquals('/node/2', $_GET['q']);
self::assertEquals('/node/2', $sut->getPath());
}
}
Loading