Skip to content

Commit

Permalink
WIP: Fix basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Feb 21, 2024
1 parent 23b2056 commit ad7df6c
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions Neos.Neos/Tests/Behavior/Features/Bootstrap/RoutingTrait.php
Expand Up @@ -19,16 +19,14 @@
use GuzzleHttp\Psr7\Uri;
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeIdentity;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepository\TestSuite\Behavior\Features\Bootstrap\CRTestSuiteRuntimeVariables;
use Neos\Flow\Http\ServerRequestAttributes;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\Exception\NoMatchingRouteException;
use Neos\Flow\Mvc\Routing\Dto\RouteContext;
use Neos\Flow\Mvc\Routing\Dto\RouteParameters;
use Neos\Flow\Mvc\Routing\RouterInterface;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\Tests\FunctionalTestRequestHandler;
Expand All @@ -43,9 +41,8 @@
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\FrontendRouting\DimensionResolution\DimensionResolverFactoryInterface;
use Neos\Neos\FrontendRouting\DimensionResolution\RequestToDimensionSpacePointContext;
use Neos\Neos\FrontendRouting\NodeAddress;
use Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\Neos\FrontendRouting\NodeUriBuilder;
use Neos\Neos\FrontendRouting\NodeUriBuilderFactory;
use Neos\Neos\FrontendRouting\NodeUriSpecification;
use Neos\Neos\FrontendRouting\Projection\DocumentUriPathProjectionFactory;
use Neos\Neos\FrontendRouting\SiteDetection\SiteDetectionMiddleware;
use Neos\Neos\FrontendRouting\SiteDetection\SiteDetectionResult;
Expand Down Expand Up @@ -195,17 +192,19 @@ public function iAmOnUrl(string $url): void
*/
public function theMatchedNodeShouldBeInContentStreamAndOriginDimension(string $nodeAggregateId, string $contentStreamId, string $dimensionSpacePoint): void
{
$nodeAddress = $this->match($this->requestUrl);
Assert::assertNotNull($nodeAddress, 'Routing result does not have "node" key - this probably means that the FrontendNodeRoutePartHandler did not properly resolve the result.');
Assert::assertTrue($nodeAddress->isInLiveWorkspace());
Assert::assertSame($nodeAggregateId, $nodeAddress->nodeAggregateId->value);
Assert::assertSame($contentStreamId, $nodeAddress->contentStreamId->value);
$matchedNodeIdentity = $this->match($this->requestUrl);
Assert::assertNotNull($matchedNodeIdentity, 'Routing result does not have "node" key - this probably means that the FrontendNodeRoutePartHandler did not properly resolve the result.');
Assert::assertTrue($matchedNodeIdentity->workspaceName->isLive());
Assert::assertSame($nodeAggregateId, $matchedNodeIdentity->nodeAggregateId->value);
// todo useless check?
$workspace = $this->currentContentRepository->getWorkspaceFinder()->findOneByCurrentContentStreamId(ContentStreamId::fromString($contentStreamId));
Assert::assertSame($contentStreamId, $workspace?->currentContentStreamId->value);
Assert::assertSame(
DimensionSpacePoint::fromJsonString($dimensionSpacePoint),
$nodeAddress->dimensionSpacePoint,
$matchedNodeIdentity->dimensionSpacePoint,
sprintf(
'Dimension space point "%s" did not match the expected "%s"',
$nodeAddress->dimensionSpacePoint->toJson(),
$matchedNodeIdentity->dimensionSpacePoint->toJson(),
$dimensionSpacePoint
)
);
Expand All @@ -216,26 +215,29 @@ public function theMatchedNodeShouldBeInContentStreamAndOriginDimension(string $
*/
public function noNodeShouldMatchUrl(string $url): void
{
$matchedNodeAddress = $this->match(new Uri($url));
Assert::assertNull($matchedNodeAddress, 'Expected no node to be found, but instead the following node address was matched: ' . $matchedNodeAddress ?? '- none -');
$matchedNodeIdentity = $this->match(new Uri($url));
Assert::assertNull($matchedNodeIdentity, 'Expected no node to be found, but instead the following node address was matched: ' . $matchedNodeIdentity ?? '- none -');
}

/**
* @Then The URL :url should match the node :nodeAggregateId in content stream :contentStreamId and dimension :dimensionSpacePoint
*/
public function theUrlShouldMatchTheNodeInContentStreamAndDimension(string $url, $nodeAggregateId, $contentStreamId, $dimensionSpacePoint): void
{
$matchedNodeAddress = $this->match(new Uri($url));
$matchedNodeIdentity = $this->match(new Uri($url));

Assert::assertNotNull($matchedNodeAddress, 'Expected node to be found, but instead nothing was found.');
Assert::assertEquals(NodeAggregateId::fromString($nodeAggregateId), $matchedNodeAddress->nodeAggregateId, 'Expected nodeAggregateId doesn\'t match.');
Assert::assertEquals(ContentStreamId::fromString($contentStreamId), $matchedNodeAddress->contentStreamId, 'Expected contentStreamId doesn\'t match.');
Assert::assertTrue($matchedNodeAddress->dimensionSpacePoint->equals(DimensionSpacePoint::fromJsonString($dimensionSpacePoint)), 'Expected dimensionSpacePoint doesn\'t match.');
Assert::assertNotNull($matchedNodeIdentity, 'Expected node to be found, but instead nothing was found.');
Assert::assertEquals(NodeAggregateId::fromString($nodeAggregateId), $matchedNodeIdentity->nodeAggregateId, 'Expected nodeAggregateId doesn\'t match.');

// todo use workspace name instead here:
$workspace = $this->currentContentRepository->getWorkspaceFinder()->findOneByCurrentContentStreamId(ContentStreamId::fromString($contentStreamId));
Assert::assertEquals($workspace->workspaceName, $matchedNodeIdentity->workspaceName, 'Expected workspace doesn\'t match.');
Assert::assertTrue($matchedNodeIdentity->dimensionSpacePoint->equals(DimensionSpacePoint::fromJsonString($dimensionSpacePoint)), 'Expected dimensionSpacePoint doesn\'t match.');
}

private $eventListenerRegistered = false;

private function match(UriInterface $uri): ?NodeAddress
private function match(UriInterface $uri): ?NodeIdentity
{
$router = $this->getObject(RouterInterface::class);
$serverRequestFactory = $this->getObject(ServerRequestFactoryInterface::class);
Expand All @@ -253,8 +255,7 @@ private function match(UriInterface $uri): ?NodeAddress
return null;
}

$nodeAddressFactory = NodeAddressFactory::create($this->currentContentRepository);
return $nodeAddressFactory->createFromUriString($routeValues['node']);
return NodeIdentity::fromJsonString($routeValues['node']);
}


Expand Down Expand Up @@ -308,18 +309,22 @@ private function resolveUrl(string $nodeAggregateId, string $contentStreamId, st
if ($this->requestUrl === null) {
$this->iAmOnUrl('/');
}
$nodeAddress = new NodeAddress(
ContentStreamId::fromString($contentStreamId),
$workspace = $this->currentContentRepository->getWorkspaceFinder()->findOneByCurrentContentStreamId(ContentStreamId::fromString($contentStreamId));

$nodeIdentity = NodeIdentity::create(
$this->currentContentRepository->id,
$workspace->workspaceName, // todo always live?
DimensionSpacePoint::fromJsonString($dimensionSpacePoint),
\str_starts_with($nodeAggregateId, '$')
? $this->rememberedNodeAggregateIds[\mb_substr($nodeAggregateId, 1)]
: NodeAggregateId::fromString($nodeAggregateId),
WorkspaceName::forLive()
: NodeAggregateId::fromString($nodeAggregateId)
);
$httpRequest = $this->getObject(ServerRequestFactoryInterface::class)->createServerRequest('GET', $this->requestUrl);
$httpRequest = $this->addRoutingParameters($httpRequest);
$actionRequest = ActionRequest::fromHttpRequest($httpRequest);
return NodeUriBuilder::fromRequest($actionRequest)->uriFor($nodeAddress);

return $this->getObject(NodeUriBuilderFactory::class)
->forRequest($httpRequest)
->uriFor(NodeUriSpecification::create($nodeIdentity));
}

private function addRoutingParameters(ServerRequestInterface $httpRequest): ServerRequestInterface
Expand Down

0 comments on commit ad7df6c

Please sign in to comment.