Skip to content

Always check for references when running node:remove #110

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

Merged
merged 1 commit into from
Dec 21, 2014
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ dev-master
### Bug fixes

- [config] Do not override CLI options with profile options
- [node:remove] Cannot `node:remove` by UUID
- [node:edit] Serialization of single value references doesn't work

### Features

- [node:remove] Immediately fail when trying to delete a node which has a
(hard) referrer
- [cli] Specify workspace with first argument
- [global] Refactored to use DI container and various general improvements
- [node:property:set] Allow setting reference property type by path
Expand Down
3 changes: 3 additions & 0 deletions features/all/phpcr_node_edit.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Feature: Edit a node
articles:
type: Reference
value: [ 66666fc6-1abf-4708-bfcc-e49511754b40, 77777777-1abf-4708-bfcc-e49511754b40 ]
article-weak:
type: WeakReference
value: 99999999-1abf-4708-bfcc-e49511754b40
'jcr:primaryType':
type: Name
value: 'nt:unstructured'
Expand Down
33 changes: 25 additions & 8 deletions features/all/phpcr_node_remove.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ Feature: Remove a node

Background:
Given that I am logged in as "testuser"
And the "session_data.xml" fixtures are loaded
And the "cms.xml" fixtures are loaded

Scenario: Remove the current node
Given the current node is "/tests_general_base"
Given the current node is "/cms/test"
And I execute the "node:remove ." command
Then the command should not fail
And I save the session
And there should not exist a node at "/tests_general_base"
And the current node should be "/"
And there should not exist a node at "/cms/test"
And the current node should be "/cms"

Scenario: Remove a non-current node
Given the current node is "/tests_general_base"
And I execute the "node:remove daniel" command
Given the current node is "/cms"
And I execute the "node:remove /cms/users/daniel" command
Then the command should not fail
And I save the session
And there should not exist a node at "/tests_general_base/daniel"
And the current node should be "/tests_general_base"
And there should not exist a node at "/cms/users/daniel"
And the current node should be "/cms"

Scenario: Delete root node
Given the current node is "/"
Expand All @@ -37,3 +37,20 @@ Feature: Remove a node
Then the command should not fail
And I save the session
And there should not exist a node at "/tests_general_base/daniel"

Scenario: Delete node by UUID
Given the current node is "/"
And I execute the "node:remove 88888888-1abf-4708-bfcc-e49511754b40" command
Then the command should not fail

Scenario: Delete referenced node
Given I execute the "node:remove /cms/articles/article1" command
Then the command should fail
And I should see the following:
"""
The node "/cms/articles/article1" is referenced by the following properties
"""

Scenario: Delete weak referenced node
Given I execute the "node:remove /cms/articles/article3" command
Then the command should not fail
34 changes: 34 additions & 0 deletions features/fixtures/cms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@
<sv:value>66666fc6-1abf-4708-bfcc-e49511754b40</sv:value>
<sv:value>77777777-1abf-4708-bfcc-e49511754b40</sv:value>
</sv:property>
<sv:property sv:name="article-weak" sv:type="WeakReference">
<sv:value>99999999-1abf-4708-bfcc-e49511754b40</sv:value>
</sv:property>
</sv:node>
</sv:node>

<sv:node sv:name="users">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
<sv:node sv:name="daniel">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
<sv:value>mix:referenceable</sv:value>
</sv:property>
<sv:property sv:name="jcr:uuid" sv:type="String">
<sv:value>88888888-1abf-4708-bfcc-e49511754b40</sv:value>
</sv:property>
</sv:node>
</sv:node>

Expand Down Expand Up @@ -113,6 +133,20 @@
<sv:value>Planes</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="article3">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
<sv:value>mix:referenceable</sv:value>
</sv:property>
<sv:property sv:name="jcr:uuid" sv:type="String">
<sv:value>99999999-1abf-4708-bfcc-e49511754b40</sv:value>
</sv:property>
<sv:property sv:name="title" sv:type="String">
<sv:value>Article 3</sv:value>
</sv:property>
</sv:node>
</sv:node>
</sv:node>

Expand Down
Binary file not shown.
19 changes: 18 additions & 1 deletion src/PHPCR/Shell/Console/Command/Phpcr/NodeRemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function execute(InputInterface $input, OutputInterface $output)
$session = $this->get('phpcr.session');
$path = $input->getArgument('path');
$currentPath = $session->getCwd();
$nodePaths = array();

// verify that node exists by trying to get it..
$nodes = $session->findNodes($path);
Expand All @@ -36,11 +37,27 @@ public function execute(InputInterface $input, OutputInterface $output)
);
}

$references = $node->getReferences();

if (count($references) > 0) {
$paths = array();
foreach ($references as $reference) {
$paths[] = $reference->getPath();
}

throw new \InvalidArgumentException(sprintf(
'The node "%s" is referenced by the following properties: "%s"',
$node->getPath(),
implode('", "', $paths)
));
}

$nodePaths[] = $node->getPath();
$node->remove();
}

// if we deleted the current path, switch back to the parent node
if ($currentPath == $session->getAbsPath($path)) {
if (in_array($currentPath, $nodePaths)) {
$session->chdir('..');
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/PHPCR/Shell/Serializer/NodeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public function normalize($node, $format = null, array $context = array())

if (in_array($property->getType(), array(PropertyType::REFERENCE, PropertyType::WEAKREFERENCE))) {
$nodesUuids = array();
foreach ((array) $propertyValue as $node) {

if (false === is_array($propertyValue)) {
$propertyValue = array($propertyValue);
}

foreach ($propertyValue as $node) {
$nodeUuids[] = $node->getIdentifier();
}
$propertyValue = $nodeUuids;
Expand Down