Skip to content

Commit 56b9b91

Browse files
committed
Merge pull request #101 from phpcr/fix_reference_edit
Correctly set type and serialize correct value for References
2 parents 0b35a4a + c9a7ab3 commit 56b9b91

File tree

7 files changed

+92
-19
lines changed

7 files changed

+92
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dev-master
77
### Bug fixes
88

99
- [mixin] Node mxin remove does not accept a path
10+
- [node:edit] Cannot edit nodes with multivalue References
1011

1112
### Enhancements
1213

features/bootstrap/FeatureContext.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,22 @@ public function theNodeAtShouldHaveThePropertyWithValueAtIndex($arg1, $arg2, $ar
738738
PHPUnit_Framework_Assert::assertEquals($arg3, $propertyType[$index]);
739739
}
740740

741+
/**
742+
* @Given /^the property "([^"]*)" should have type "([^"]*)"$/
743+
*/
744+
public function thePropertyShouldHaveType($arg1, $arg2)
745+
{
746+
$session = $this->getSession();
747+
$property = $session->getItem($arg1);
748+
if (!$property instanceof PropertyInterface) {
749+
throw new \InvalidArgumentException(sprintf(
750+
'Item at "%s" is not a property', $arg1
751+
));
752+
}
753+
754+
PHPUnit_Framework_Assert::assertEquals($arg2, PropertyType::nameFromValue($property->getType()));
755+
}
756+
741757

742758
/**
743759
* @Given /^the property "([^"]*)" should have type "([^"]*)" and value "([^"]*)"$/

features/fixtures/cms.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<sv:value>two</sv:value>
5151
<sv:value>three</sv:value>
5252
</sv:property>
53+
<sv:property sv:name="articles" sv:type="Reference" multiple="true">
54+
<sv:value>66666fc6-1abf-4708-bfcc-e49511754b40</sv:value>
55+
<sv:value>77777777-1abf-4708-bfcc-e49511754b40</sv:value>
56+
</sv:property>
5357
</sv:node>
5458
</sv:node>
5559

@@ -87,6 +91,28 @@
8791
<sv:value>Planes</sv:value>
8892
</sv:property>
8993
</sv:node>
94+
<sv:node sv:name="article2">
95+
<sv:property sv:name="jcr:primaryType" sv:type="Name">
96+
<sv:value>nt:unstructured</sv:value>
97+
</sv:property>
98+
<sv:property sv:name="jcr:mixinTypes" sv:type="Name">
99+
<sv:value>mix:referenceable</sv:value>
100+
</sv:property>
101+
<sv:property sv:name="jcr:uuid" sv:type="String">
102+
<sv:value>77777777-1abf-4708-bfcc-e49511754b40</sv:value>
103+
</sv:property>
104+
<sv:property sv:name="title" sv:type="String">
105+
<sv:value>Article 1</sv:value>
106+
</sv:property>
107+
<sv:property sv:name="tags" sv:type="String" sv:multiple="true">
108+
<sv:value>Planes</sv:value>
109+
<sv:value>Trains</sv:value>
110+
<sv:value>Automobiles</sv:value>
111+
</sv:property>
112+
<sv:property sv:name="tag" sv:type="String" sv:multiple="true">
113+
<sv:value>Planes</sv:value>
114+
</sv:property>
115+
</sv:node>
90116
</sv:node>
91117
</sv:node>
92118

features/phpcr_node_edit.feature

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@ Feature: Edit a node
2525
tags:
2626
type: String
2727
value: [one, two, three]
28+
articles:
29+
type: Reference
30+
value: [ 66666fc6-1abf-4708-bfcc-e49511754b40, 77777777-1abf-4708-bfcc-e49511754b40 ]
2831
'jcr:primaryType':
2932
type: Name
3033
value: 'nt:unstructured'
3134
"""
32-
And I execute the "node:edit cms/products/product1" command
35+
And I execute the "node:edit cms/products/product1 --no-interaction" command
3336
Then the command should not fail
3437
And the property "/cms/products/product1/weight" should have type "Long" and value "10"
3538
And the property "/cms/products/product1/cost" should have type "Double" and value "12.13"
3639
And the property "/cms/products/product1/size" should have type "String" and value "XL"
3740
And the property "/cms/products/product1/name" should have type "String" and value "Product One"
41+
And the property "/cms/products/product1/articles" should have type "Reference"
3842
And the property "/cms/products/product1/jcr:primaryType" should have type "Name" and value "nt:unstructured"
3943

4044
Scenario: Remove some properties
@@ -171,3 +175,27 @@ Feature: Edit a node
171175
And I save the session
172176
Then the command should not fail
173177
And the property "/cms/articles/article1/foobar" should have type "String" and value "FOOOOOOO"
178+
179+
Scenario: Change a property type (cost from Double to Long
180+
Given I have an editor which produces the following:
181+
""""
182+
cost:
183+
type: Long
184+
value: 100
185+
weight:
186+
type: String
187+
value: 10
188+
size:
189+
value: 10
190+
'jcr:primaryType':
191+
type: Name
192+
value: 'nt:unstructured'
193+
"""
194+
And I execute the "node:edit cms/products/product1" command
195+
Then the command should not fail
196+
And I save the session
197+
Then the command should not fail
198+
And the property "/cms/products/product1/cost" should have type "Long" and value "100"
199+
And the property "/cms/products/product1/weight" should have type "String" and value "10"
200+
And the property "/cms/products/product1/size" should have type "Long" and value "10"
201+

features/phpcr_query_delete.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ Feature: Execute a a raw DELETE query in JCR_SQL2
1919
"""
2020
Examples:
2121
| query | path |
22-
| DELETE FROM [nt:unstructured] AS a WHERE localname() = 'article1' | /cms/articles/article1 |
23-
| delete FROM [nt:unstructured] as a where localname() = 'article1' | /cms/articles/article1 |
24-
| DELETE FROM nt:unstructured AS a WHERE localname() = 'article1' | /cms/articles/article1 |
22+
| DELETE FROM [nt:unstructured] AS a WHERE localname() = 'product1' | /cms/products/product1 |
23+
| delete FROM [nt:unstructured] as a where localname() = 'product1' | /cms/products/product1 |
24+
| DELETE FROM nt:unstructured AS a WHERE localname() = 'product1' | /cms/products/product1 |

features/phpcr_query_update.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ Feature: Execute a a raw UPDATE query in JCR_SQL2
3939
Then the command should not fail
4040
And I should see the following:
4141
"""
42-
1 row(s) affected
42+
2 row(s) affected
4343
"""
4444
And the node at "/cms/articles/article1" should have the property "tag" with value "Rockets" at index "0"

src/PHPCR/Shell/Serializer/NodeNormalizer.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ public function normalize($node, $format = null, array $context = array())
4242
}
4343

4444
$propertyType = $property->getType();
45+
4546
$propertyValue = $property->getValue();
4647
$propertyName = $property->getName();
4748

49+
if (in_array($property->getType(), array(PropertyType::REFERENCE, PropertyType::WEAKREFERENCE))) {
50+
$propertyValue = array_keys($propertyValue);
51+
}
52+
4853
$res[$propertyName] = array(
4954
'type' => PropertyType::nameFromValue($propertyType),
5055
'value' => $propertyValue
@@ -97,22 +102,19 @@ public function denormalize($data, $class, $format = null, array $context = arra
97102
}
98103

99104
$datum = $this->normalizeDatum($data[$property->getName()]);
105+
$typeValue = isset($datum['type']) ? PropertyType::valueFromName($datum['type']) : null;
100106

101-
if (isset($datum['type'])) {
102-
$typeName = $datum['type'];
107+
if (isset($datum['value'])) {
103108

104-
if ($datum['type'] != $typeName) {
105-
throw new \InvalidArgumentException(sprintf(
106-
'Cannot currently change a properties type for property "%s" (trying to change from "%s" to "%s")',
107-
$property->getPath(),
108-
$typeName, $datum['type']
109-
));
110-
}
111-
}
109+
// if the type or the value is differnet, update the property
110+
if ($datum['value'] != $property->getValue() || $typeValue != $property->getType()) {
112111

113-
if (isset($datum['value'])) {
114-
if ($datum['value'] != $property->getValue()) {
115-
$property->setValue($datum['value']);
112+
// setValue doesn't like being passed a null value as a type ...
113+
if ($typeValue !== null) {
114+
$property->setValue($datum['value'], $typeValue);
115+
} else {
116+
$property->setValue($datum['value']);
117+
}
116118
}
117119
}
118120
} catch (\Exception $e) {
@@ -162,7 +164,7 @@ private function normalizeDatum($value)
162164
if (is_scalar($value)) {
163165
return array(
164166
'value' => $value,
165-
'type' => 'String'
167+
'type' => null
166168
);
167169
}
168170

0 commit comments

Comments
 (0)