Skip to content

Commit 369abf6

Browse files
committed
Always check for references when running node:remove
- Help prevent transaction failure when calling save - Also allow the removal of node by UUID
1 parent a7876a9 commit 369abf6

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ dev-master
77
### Bug fixes
88

99
- [config] Do not override CLI options with profile options
10+
- [node:remove] Cannot `node:remove` by UUID
1011

1112
### Features
1213

14+
- [node:remove] Immediately fail when trying to delete a node which has a
15+
(hard) referrer
1316
- [cli] Specify workspace with first argument
1417
- [global] Refactored to use DI container and various general improvements
1518
- [node:property:set] Allow setting reference property type by path

features/all/phpcr_node_remove.feature

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ Feature: Remove a node
55

66
Background:
77
Given that I am logged in as "testuser"
8-
And the "session_data.xml" fixtures are loaded
8+
And the "cms.xml" fixtures are loaded
99

1010
Scenario: Remove the current node
11-
Given the current node is "/tests_general_base"
11+
Given the current node is "/cms/test"
1212
And I execute the "node:remove ." command
1313
Then the command should not fail
1414
And I save the session
15-
And there should not exist a node at "/tests_general_base"
16-
And the current node should be "/"
15+
And there should not exist a node at "/cms/test"
16+
And the current node should be "/cms"
1717

1818
Scenario: Remove a non-current node
19-
Given the current node is "/tests_general_base"
20-
And I execute the "node:remove daniel" command
19+
Given the current node is "/cms"
20+
And I execute the "node:remove /cms/users/daniel" command
2121
Then the command should not fail
2222
And I save the session
23-
And there should not exist a node at "/tests_general_base/daniel"
24-
And the current node should be "/tests_general_base"
23+
And there should not exist a node at "/cms/users/daniel"
24+
And the current node should be "/cms"
2525

2626
Scenario: Delete root node
2727
Given the current node is "/"
@@ -37,3 +37,20 @@ Feature: Remove a node
3737
Then the command should not fail
3838
And I save the session
3939
And there should not exist a node at "/tests_general_base/daniel"
40+
41+
Scenario: Delete node by UUID
42+
Given the current node is "/"
43+
And I execute the "node:remove 88888888-1abf-4708-bfcc-e49511754b40" command
44+
Then the command should not fail
45+
46+
Scenario: Delete referenced node
47+
Given I execute the "node:remove /cms/articles/article1" command
48+
Then the command should fail
49+
And I should see the following:
50+
"""
51+
The node "/cms/articles/article1" is referenced by the following properties
52+
"""
53+
54+
Scenario: Delete weak referenced node
55+
Given I execute the "node:remove /cms/articles/article3" command
56+
Then the command should not fail

features/fixtures/cms.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@
5454
<sv:value>66666fc6-1abf-4708-bfcc-e49511754b40</sv:value>
5555
<sv:value>77777777-1abf-4708-bfcc-e49511754b40</sv:value>
5656
</sv:property>
57+
<sv:property sv:name="article-weak" sv:type="WeakReference">
58+
<sv:value>99999999-1abf-4708-bfcc-e49511754b40</sv:value>
59+
</sv:property>
60+
</sv:node>
61+
</sv:node>
62+
63+
<sv:node sv:name="users">
64+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
65+
<sv:value>nt:unstructured</sv:value>
66+
</sv:property>
67+
<sv:node sv:name="daniel">
68+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
69+
<sv:value>nt:unstructured</sv:value>
70+
</sv:property>
71+
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
72+
<sv:value>mix:referenceable</sv:value>
73+
</sv:property>
74+
<sv:property sv:name="jcr:uuid" sv:type="String">
75+
<sv:value>88888888-1abf-4708-bfcc-e49511754b40</sv:value>
76+
</sv:property>
5777
</sv:node>
5878
</sv:node>
5979

@@ -113,6 +133,20 @@
113133
<sv:value>Planes</sv:value>
114134
</sv:property>
115135
</sv:node>
136+
<sv:node sv:name="article3">
137+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
138+
<sv:value>nt:unstructured</sv:value>
139+
</sv:property>
140+
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
141+
<sv:value>mix:referenceable</sv:value>
142+
</sv:property>
143+
<sv:property sv:name="jcr:uuid" sv:type="String">
144+
<sv:value>99999999-1abf-4708-bfcc-e49511754b40</sv:value>
145+
</sv:property>
146+
<sv:property sv:name="title" sv:type="String">
147+
<sv:value>Article 3</sv:value>
148+
</sv:property>
149+
</sv:node>
116150
</sv:node>
117151
</sv:node>
118152

Binary file not shown.

src/PHPCR/Shell/Console/Command/Phpcr/NodeRemoveCommand.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function execute(InputInterface $input, OutputInterface $output)
2525
$session = $this->get('phpcr.session');
2626
$path = $input->getArgument('path');
2727
$currentPath = $session->getCwd();
28+
$nodePaths = array();
2829

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

40+
$references = $node->getReferences();
41+
42+
if (count($references) > 0) {
43+
$paths = array();
44+
foreach ($references as $reference) {
45+
$paths[] = $reference->getPath();
46+
}
47+
48+
throw new \InvalidArgumentException(sprintf(
49+
'The node "%s" is referenced by the following properties: "%s"',
50+
$node->getPath(),
51+
implode('", "', $paths)
52+
));
53+
}
54+
55+
$nodePaths[] = $node->getPath();
3956
$node->remove();
4057
}
4158

4259
// if we deleted the current path, switch back to the parent node
43-
if ($currentPath == $session->getAbsPath($path)) {
60+
if (in_array($currentPath, $nodePaths)) {
4461
$session->chdir('..');
4562
}
4663
}

0 commit comments

Comments
 (0)