Skip to content

Commit f9a3626

Browse files
authored
Merge pull request #57 from Flowpack/task/nodecreationCheckIfNodeIsAbstractOrNotAllowedExplicitly
TASK: NodeCreation: Check if node is abstract or if node is not allowed explicitly
2 parents 0ebcab1 + 86586a6 commit f9a3626

6 files changed

Lines changed: 48 additions & 6 deletions

File tree

Classes/Domain/NodeCreation/NodeCreationService.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ private function applyTemplateRecursively(Templates $templates, NodeInterface $p
8484
);
8585
continue;
8686
}
87+
88+
$nodeType = $this->nodeTypeManager->getNodeType($template->getType()->getValue());
89+
90+
if ($nodeType->isAbstract()) {
91+
$caughtExceptions->add(
92+
CaughtException::fromException(new \RuntimeException(sprintf('Template requires type to be a non abstract NodeType. Got: "%s".', $template->getType()->getValue()), 1686417628976))
93+
);
94+
continue;
95+
}
96+
97+
if (!$parentNode->getNodeType()->allowsChildNodeType($nodeType)) {
98+
$caughtExceptions->add(
99+
CaughtException::fromException(new \RuntimeException(sprintf('Node type "%s" is not allowed for child nodes of type %s', $template->getType()->getValue(), $parentNode->getNodeType()->getName()), 1686417627173))
100+
);
101+
continue;
102+
}
103+
104+
// todo maybe check also explicitly for allowsGrandchildNodeType (we do this currently like below)
87105
try {
88106
$node = $this->nodeOperations->create(
89107
$parentNode,

Configuration/Testing/NodeTypes.Malformed.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454
type: 'Flowpack.NodeTemplates:Content.Text'
5555
properties:
5656
text: bar
57+
abstractNodeAbort:
58+
type: 'Neos.Neos:Node'
5759
illegalNodeAbort:
58-
type: 'Neos.Neos:Document'
60+
type: 'Flowpack.NodeTemplates:Document.Page.Static'
5961
name: 'illegal'
6062
properties:
6163
text: huhu

Tests/Functional/Fixtures/WithEvaluationExceptions.messages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@
8080
"severity": "ERROR"
8181
},
8282
{
83-
"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)",
83+
"message": "RuntimeException(Template requires type to be a non abstract NodeType. Got: \"Neos.Neos:Node\"., 1686417628976)",
84+
"severity": "ERROR"
85+
},
86+
{
87+
"message": "RuntimeException(Node type \"Flowpack.NodeTemplates:Document.Page.Static\" is not allowed for child nodes of type Flowpack.NodeTemplates:Content.WithEvaluationExceptions, 1686417627173)",
8488
"severity": "ERROR"
8589
},
8690
{

Tests/Functional/Fixtures/WithEvaluationExceptions.template.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121
"childNodes": []
2222
},
2323
{
24-
"type": "Neos.Neos:Document",
24+
"type": "Neos.Neos:Node",
25+
"name": null,
26+
"properties": [],
27+
"childNodes": []
28+
},
29+
{
30+
"type": "Flowpack.NodeTemplates:Document.Page.Static",
2531
"name": "illegal",
2632
"properties": {
2733
"text": "huhu"

Tests/Functional/NodeTemplateTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function exceptionsAreCaughtAndPartialTemplateIsBuild(): void
229229

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

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

234234
$this->assertNodeDumpAndTemplateDumpMatchSnapshot('WithEvaluationExceptions', $createdNode);
235235
}
@@ -312,14 +312,14 @@ private function assertLastCreatedTemplateMatchesSnapshot(string $snapShotName):
312312
$lastCreatedTemplate = $this->serializeValuesInArray(
313313
$this->lastCreatedRootTemplate->jsonSerialize()
314314
);
315-
$this->assertStringEqualsFileOrCreateSnapshot(__DIR__ . '/Fixtures/' . $snapShotName . '.template.json', json_encode($lastCreatedTemplate, JSON_PRETTY_PRINT));
315+
$this->assertJsonStringEqualsJsonFileOrCreateSnapshot(__DIR__ . '/Fixtures/' . $snapShotName . '.template.json', json_encode($lastCreatedTemplate, JSON_PRETTY_PRINT));
316316
}
317317

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

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

Tests/Functional/SnapshotTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ private function assertStringEqualsFileOrCreateSnapshot(string $snapshotFileName
1212
if (getenv('CREATE_SNAPSHOT') === '1') {
1313
file_put_contents($snapshotFileName, $expectedString);
1414
$this->addWarning('Created snapshot.');
15+
return;
1516
}
1617
Assert::assertStringEqualsFile($snapshotFileName, $expectedString);
1718
}
19+
20+
private function assertJsonStringEqualsJsonFileOrCreateSnapshot(string $snapshotFileName, string $expectedJsonString): void
21+
{
22+
$expectedJsonString = rtrim($expectedJsonString, "\n") . "\n";
23+
if (getenv('CREATE_SNAPSHOT') === '1') {
24+
file_put_contents($snapshotFileName, $expectedJsonString);
25+
$this->addWarning('Created snapshot.');
26+
return;
27+
}
28+
Assert::assertJsonStringEqualsJsonFile($snapshotFileName, $expectedJsonString);
29+
}
1830
}

0 commit comments

Comments
 (0)