Skip to content

Commit

Permalink
Merge pull request #5281 from dlubitz/bugfix/5280-similarize-on-node-…
Browse files Browse the repository at this point in the history
…materialize

BUGFIX: Force direct access on setting node properties in node data similarize
  • Loading branch information
dlubitz authored Jan 20, 2025
2 parents 6c3ad10 + d96fbb0 commit eb37739
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Neos.ContentRepository/Classes/Domain/Model/NodeData.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Neos\ContentRepository\Domain\Model;

/*
Expand Down Expand Up @@ -475,9 +476,9 @@ public function createNodeData($name, ?NodeType $nodeType = null, $identifier =
* @param string $identifier The identifier of the node, unique within the workspace, optional(!)
* @param Workspace $workspace
* @param array $dimensions An array of dimension name to dimension values
* @throws NodeExistsException if a node with this path already exists.
* @throws \InvalidArgumentException if the node name is not accepted.
* @return NodeData
* @throws \InvalidArgumentException if the node name is not accepted.
* @throws NodeExistsException if a node with this path already exists.
*/
public function createSingleNodeData($name, ?NodeType $nodeType = null, $identifier = null, ?Workspace $workspace = null, ?array $dimensions = null)
{
Expand Down Expand Up @@ -767,14 +768,21 @@ public function similarize(AbstractNodeData $sourceNode, $isCopy = false)
];
if (!$isCopy) {
$propertyNames[] = 'creationDateTime';
$propertyNames[] = 'lastModificationDateTime';
}
if ($sourceNode instanceof NodeData) {
$propertyNames[] = 'index';
$propertyNames[] = 'removed';
}

// We need to force direct access for the following properties, as they don't have a setter in AbstractNodeData
$propertyNamesToForceDirectAccess = ['creationDateTime'];
foreach ($propertyNames as $propertyName) {
ObjectAccess::setProperty($this, $propertyName, ObjectAccess::getProperty($sourceNode, $propertyName));
ObjectAccess::setProperty(
$this,
$propertyName,
ObjectAccess::getProperty($sourceNode, $propertyName),
in_array($propertyName, $propertyNamesToForceDirectAccess)
);
}

$contentObject = $sourceNode->getContentObject();
Expand Down
18 changes: 18 additions & 0 deletions Neos.ContentRepository/Tests/Unit/Domain/Model/NodeDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,24 @@ public function similarizeClearsPropertiesBeforeAddingNewOnes()
self::assertEquals($expectedProperties, $this->nodeData->getProperties());
}

/**
* @test
*/
public function similarizeCopiesCreationAndLastModificationDateTimes()
{
$creationDateTime = \DateTime::createFromFormat('Y-m-d', '2000-01-01 12:00:00');

/** @var $sourceNode NodeData */
$sourceNode = $this->getAccessibleMock(NodeData::class, ['addOrUpdate'], ['/foo/bar', $this->mockWorkspace]);
$this->inject($sourceNode, 'nodeTypeManager', $this->mockNodeTypeManager);
$sourceNode->_set('nodeDataRepository', $this->createMock(RepositoryInterface::class));
$sourceNode->_set('creationDateTime', $creationDateTime);

$this->nodeData->similarize($sourceNode);

self::assertSame($creationDateTime, $this->nodeData->getCreationDateTime());
}

/**
* @test
*/
Expand Down

0 comments on commit eb37739

Please sign in to comment.