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

TASK: NodeCreation: Check if node is abstract or if node is not allowed explicitly #57

Merged
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
18 changes: 18 additions & 0 deletions Classes/Domain/NodeCreation/NodeCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ private function applyTemplateRecursively(Templates $templates, NodeInterface $p
);
continue;
}

$nodeType = $this->nodeTypeManager->getNodeType($template->getType()->getValue());

if ($nodeType->isAbstract()) {
$caughtExceptions->add(
CaughtException::fromException(new \RuntimeException(sprintf('Template requires type to be a non abstract NodeType. Got: "%s".', $template->getType()->getValue()), 1686417628976))
);
continue;
}

if (!$parentNode->getNodeType()->allowsChildNodeType($nodeType)) {
$caughtExceptions->add(
CaughtException::fromException(new \RuntimeException(sprintf('Node type "%s" is not allowed for child nodes of type %s', $template->getType()->getValue(), $parentNode->getNodeType()->getName()), 1686417627173))
);
continue;
}

// todo maybe check also explicitly for allowsGrandchildNodeType (we do this currently like below)
try {
$node = $this->nodeOperations->create(
$parentNode,
Expand Down
4 changes: 3 additions & 1 deletion Configuration/Testing/NodeTypes.Malformed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
type: 'Flowpack.NodeTemplates:Content.Text'
properties:
text: bar
abstractNodeAbort:
type: 'Neos.Neos:Node'
illegalNodeAbort:
type: 'Neos.Neos:Document'
type: 'Flowpack.NodeTemplates:Document.Page.Static'
name: 'illegal'
properties:
text: huhu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@
"severity": "ERROR"
},
{
"message": "NodeConstraintException(Cannot create new node \"illegal\" of Type \"Neos.Neos:Document\" in Node \/sites\/test-site\/homepage\/main\/new-node@live[Flowpack.NodeTemplates:Content.WithEvaluationExceptions], 1400782413)",
"message": "RuntimeException(Template requires type to be a non abstract NodeType. Got: \"Neos.Neos:Node\"., 1686417628976)",
"severity": "ERROR"
},
{
"message": "RuntimeException(Node type \"Flowpack.NodeTemplates:Document.Page.Static\" is not allowed for child nodes of type Flowpack.NodeTemplates:Content.WithEvaluationExceptions, 1686417627173)",
"severity": "ERROR"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
"childNodes": []
},
{
"type": "Neos.Neos:Document",
"type": "Neos.Neos:Node",
"name": null,
"properties": [],
"childNodes": []
},
{
"type": "Flowpack.NodeTemplates:Document.Page.Static",
"name": "illegal",
"properties": {
"text": "huhu"
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/NodeTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function exceptionsAreCaughtAndPartialTemplateIsBuild(): void

$createdNode = $targetNode->getChildNodes($toBeCreatedNodeTypeName->getValue())[0];

$this->assertStringEqualsFileOrCreateSnapshot(__DIR__ . '/Fixtures/WithEvaluationExceptions.messages.json', json_encode($this->getMessagesOfFeedbackCollection(), JSON_PRETTY_PRINT));
$this->assertJsonStringEqualsJsonFileOrCreateSnapshot(__DIR__ . '/Fixtures/WithEvaluationExceptions.messages.json', json_encode($this->getMessagesOfFeedbackCollection(), JSON_PRETTY_PRINT));

$this->assertNodeDumpAndTemplateDumpMatchSnapshot('WithEvaluationExceptions', $createdNode);
}
Expand Down Expand Up @@ -312,14 +312,14 @@ private function assertLastCreatedTemplateMatchesSnapshot(string $snapShotName):
$lastCreatedTemplate = $this->serializeValuesInArray(
$this->lastCreatedRootTemplate->jsonSerialize()
);
$this->assertStringEqualsFileOrCreateSnapshot(__DIR__ . '/Fixtures/' . $snapShotName . '.template.json', json_encode($lastCreatedTemplate, JSON_PRETTY_PRINT));
$this->assertJsonStringEqualsJsonFileOrCreateSnapshot(__DIR__ . '/Fixtures/' . $snapShotName . '.template.json', json_encode($lastCreatedTemplate, JSON_PRETTY_PRINT));
}

private function assertNodeDumpAndTemplateDumpMatchSnapshot(string $snapShotName, NodeInterface $node): void
{
$serializedNodes = $this->jsonSerializeNodeAndDescendents($node);
unset($serializedNodes['nodeTypeName']);
$this->assertStringEqualsFileOrCreateSnapshot(__DIR__ . '/Fixtures/' . $snapShotName . '.nodes.json', json_encode($serializedNodes, JSON_PRETTY_PRINT));
$this->assertJsonStringEqualsJsonFileOrCreateSnapshot(__DIR__ . '/Fixtures/' . $snapShotName . '.nodes.json', json_encode($serializedNodes, JSON_PRETTY_PRINT));

$dumpedYamlTemplate = $this->nodeTemplateDumper->createNodeTemplateYamlDumpFromSubtree($node);

Expand Down
12 changes: 12 additions & 0 deletions Tests/Functional/SnapshotTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ private function assertStringEqualsFileOrCreateSnapshot(string $snapshotFileName
if (getenv('CREATE_SNAPSHOT') === '1') {
file_put_contents($snapshotFileName, $expectedString);
$this->addWarning('Created snapshot.');
return;
}
Assert::assertStringEqualsFile($snapshotFileName, $expectedString);
}

private function assertJsonStringEqualsJsonFileOrCreateSnapshot(string $snapshotFileName, string $expectedJsonString): void
{
$expectedJsonString = rtrim($expectedJsonString, "\n") . "\n";
if (getenv('CREATE_SNAPSHOT') === '1') {
file_put_contents($snapshotFileName, $expectedJsonString);
$this->addWarning('Created snapshot.');
return;
}
Assert::assertJsonStringEqualsJsonFile($snapshotFileName, $expectedJsonString);
}
}