11<?php
22namespace PHPCR \Tests \Writing ;
33
4+ use PHPCR \PropertyType ;
5+
46require_once (__DIR__ . '/../../inc/BaseCase.php ' );
57
68/**
1012 */
1113class MixinReferenceableTest extends \PHPCR \Test \BaseCase
1214{
15+ public static function setupBeforeClass ($ fixtures = '10_Writing/mixinreferenceable ' )
16+ {
17+ parent ::setupBeforeClass ($ fixtures );
18+ }
19+
1320 public function setUp ()
1421 {
1522 $ this ->renewSession (); // discard changes
@@ -22,11 +29,11 @@ public function setUp()
2229 public function testReferenceOnNonReferenceableNode ()
2330 {
2431 // Load a non-referenceable node
25- $ referenced_node = $ this ->session ->getNode ('/tests_general_base/emptyExample ' );
32+ $ nonReferenceableNode = $ this ->node ->getNode ('non-referenceable ' );
2633
2734 // Try to reference it
28- $ source_node = $ this ->session ->getNode ('/tests_general_base/index.txt/jcr:content ' );
29- $ source_node ->setProperty ('reference ' , $ referenced_node , \PHPCR \PropertyType::WEAKREFERENCE );
35+ $ sourceNode = $ this ->node ->getNode ('node ' );
36+ $ sourceNode ->setProperty ('reference ' , $ nonReferenceableNode , \PHPCR \PropertyType::WEAKREFERENCE );
3037 $ this ->session ->save ();
3138 }
3239
@@ -36,24 +43,24 @@ public function testReferenceOnNonReferenceableNode()
3643 public function testReferenceOnNewlyReferenceableNode ()
3744 {
3845 // Load a non-referenceable node and make it referenceable
39- $ referenced_node = $ this ->session ->getNode ('/tests_general_base/emptyExample ' );
40- $ referenced_node ->addMixin ('mix:referenceable ' );
46+ $ referencedNode = $ this ->node ->getNode ('node ' );
47+ $ referencedNode ->addMixin ('mix:referenceable ' );
4148
4249 // Re-read the node to be sure it has a UUID
4350 $ this ->saveAndRenewSession ();
44- $ referenced_node = $ this ->session ->getNode ('/tests_general_base/emptyExample ' );
51+ $ referencedNode = $ this ->node ->getNode ('node ' );
4552
4653 // Reference it from another node
47- $ source_node = $ this ->session ->getNode ('/tests_general_base/index.txt/jcr:content ' );
48- $ source_node ->setProperty ('reference ' , $ referenced_node , \PHPCR \PropertyType::WEAKREFERENCE );
54+ $ sourceNode = $ this ->node ->getNode ('other-node ' );
55+ $ sourceNode ->setProperty ('reference ' , $ referencedNode , \PHPCR \PropertyType::WEAKREFERENCE );
4956
5057 $ this ->session ->save ();
5158
52- $ this ->assertInstanceOf ('PHPCR\NodeInterface ' , $ source_node ->getPropertyValue ('reference ' ));
59+ $ this ->assertInstanceOf ('PHPCR\NodeInterface ' , $ sourceNode ->getPropertyValue ('reference ' ));
5360
5461 // referrers only required to work once save happened
55- $ this ->assertCount (0 , $ referenced_node ->getReferences ());
56- $ this ->assertCount (1 , $ referenced_node ->getWeakReferences ());
62+ $ this ->assertCount (0 , $ referencedNode ->getReferences ());
63+ $ this ->assertCount (1 , $ referencedNode ->getWeakReferences ());
5764 }
5865
5966 /**
@@ -62,54 +69,105 @@ public function testReferenceOnNewlyReferenceableNode()
6269 public function testReferenceOnReferenceableNode ()
6370 {
6471 // Load a referenceable node
65- $ referenced_node = $ this ->session ->getNode ('/tests_general_base/idExample ' );
72+ $ referencedNode = $ this ->node ->getNode ('referenceable ' );
6673
6774 // Reference it from another node
68- $ source_node = $ this ->session ->getNode ('/tests_general_base/index.txt/jcr:content ' );
69- $ source_node ->setProperty ('oreference ' , $ referenced_node , \PHPCR \PropertyType::WEAKREFERENCE );
75+ $ sourceNode = $ this ->node ->getNode ('node ' );
76+ $ sourceNode ->setProperty ('oreference ' , $ referencedNode , \PHPCR \PropertyType::WEAKREFERENCE );
7077 $ this ->session ->save ();
7178
72- $ this ->assertInstanceOf ('PHPCR\NodeInterface ' , $ source_node ->getPropertyValue ('oreference ' ));
79+ $ this ->assertInstanceOf ('PHPCR\NodeInterface ' , $ sourceNode ->getPropertyValue ('oreference ' ));
7380 }
7481
7582 /**
7683 * Test that we can update a reference
7784 */
7885 public function testUpdateReference ()
7986 {
80- $ referenced1 = $ this ->session ->getNode ('/tests_general_base/emptyExample ' );
87+ $ referenced1 = $ this ->node ->getNode ('node ' );
8188 $ referenced1 ->addMixin ('mix:referenceable ' );
8289 $ this ->session ->save ();
8390
8491 // Load a referenceable node
85- $ referenced2 = $ this ->session ->getNode ('/tests_general_base/idExample ' );
92+ $ referenced2 = $ this ->node ->getNode ('referenceable ' );
8693
8794 // Reference it from another node
88- $ source_node = $ this ->session ->getNode ('/tests_general_base/index.txt/jcr:content ' );
95+ $ sourceNode = $ this ->node ->getNode ('other-node ' );
8996
90- $ source_node ->setProperty ('reference ' , $ referenced1 , \PHPCR \PropertyType::WEAKREFERENCE );
97+ $ sourceNode ->setProperty ('reference ' , $ referenced1 , \PHPCR \PropertyType::WEAKREFERENCE );
9198 $ this ->session ->save ();
92- $ source_node ->setProperty ('reference ' , $ referenced2 , \PHPCR \PropertyType::WEAKREFERENCE );
99+ $ sourceNode ->setProperty ('reference ' , $ referenced2 , \PHPCR \PropertyType::WEAKREFERENCE );
93100 $ this ->session ->save ();
94- $ this ->assertSame ($ referenced2 , $ source_node ->getPropertyValue ('reference ' ));
101+ $ this ->assertSame ($ referenced2 , $ sourceNode ->getPropertyValue ('reference ' ));
95102
96- $ session = $ this ->renewSession ();
97- $ referenced2 = $ session ->getNode ('/tests_general_base/idExample ' );
98- $ this ->assertSame ($ referenced2 , $ session ->getProperty ('/tests_general_base/index.txt/jcr:content/reference ' )->getValue ());
103+ $ this ->renewSession ();
104+ $ referenced2 = $ this ->node ->getNode ('referenceable ' );
105+ $ this ->assertSame ($ referenced2 , $ this ->node ->getNode ('other-node ' )->getProperty ('reference ' )->getValue ());
106+ }
107+
108+ public function testMultiValueReference ()
109+ {
110+ $ this ->doTestMultiValueReference (
111+ array ('one ' , 'two ' , 'three ' ),
112+ array ('one ' , 'two ' , 'one ' , 'one ' , 'two ' , 'three ' ),
113+ PropertyType::REFERENCE
114+ );
115+ }
116+
117+ public function testMultiValueWeakReference ()
118+ {
119+ $ this ->doTestMultiValueReference (
120+ array ('one ' , 'two ' , 'three ' ),
121+ array ('one ' , 'two ' , 'one ' , 'one ' , 'two ' , 'three ' ),
122+ PropertyType::WEAKREFERENCE
123+ );
124+ }
125+
126+ private function doTestMultiValueReference ($ nodeNames , $ nodeCollectionNames , $ referenceType )
127+ {
128+ $ baseNode = $ this ->node ;
129+ $ nodes = array ();
130+ foreach ($ nodeNames as $ nodeName ) {
131+ $ node = $ baseNode ->addNode ($ nodeName );
132+ $ node ->addMixin ('mix:referenceable ' );
133+ $ nodes [$ nodeName ] = $ node ;
134+ }
135+
136+ $ this ->session ->save ();
137+
138+ $ referrer = $ baseNode ->addNode ('referrer ' );
139+
140+ $ nodeCollection = array ();
141+
142+ foreach ($ nodeCollectionNames as $ nodeCollectionName ) {
143+ $ nodeCollection [] = $ nodes [$ nodeCollectionName ];
144+ }
145+ $ referrer ->setProperty ('references ' , $ nodeCollection , $ referenceType );
146+
147+ $ this ->session ->save ();
148+
149+ $ this ->renewSession ();
150+ $ referrer = $ this ->node ->getNode ('referrer ' );
151+ $ values = $ referrer ->getProperty ('references ' );
152+
153+ foreach ($ values as $ referencedNode ) {
154+ $ name = array_shift ($ nodeCollectionNames );
155+ $ this ->assertSame ($ name , $ referencedNode ->getName ());
156+ }
99157 }
100158
101159 public function testSetUuidNewReferenceable ()
102160 {
103161 $ uuid = 'aaaa61c0-09ab-42a9-87c0-308ccc93aaaa ' ;
104- $ node = $ this ->session -> getNode ( ' /tests_general_base/index.txt/jcr:content ' ) ->addNode ('newId ' , 'nt:unstructured ' );
162+ $ node = $ this ->node ->addNode ('newId ' , 'nt:unstructured ' );
105163 $ node ->addMixin ('mix:referenceable ' );
106164 $ node ->setProperty ('jcr:uuid ' , $ uuid );
107165 $ this ->session ->save ();
108166 $ this ->assertSame ($ uuid , $ node ->getIdentifier ());
109167
110168 $ session = $ this ->renewSession ();
111169
112- $ node = $ session -> getNode ('/tests_general_base/index.txt/jcr:content/ newId ' );
170+ $ node = $ this -> node -> getNode ('newId ' );
113171 $ this ->assertSame ($ uuid , $ node ->getIdentifier ());
114172 }
115173
@@ -118,7 +176,7 @@ public function testSetUuidNewReferenceable()
118176 */
119177 public function testSetUuidNewButNonreferenceable ()
120178 {
121- $ node = $ this ->session -> getNode ( ' /tests_general_base/index.txt/jcr:content ' ) ->addNode ('newNonref ' , 'nt:unstructured ' );
179+ $ node = $ this ->node ->addNode ('newNonref ' , 'nt:unstructured ' );
122180 $ node ->setProperty ('jcr:uuid ' , 'bbbb61c0-09ab-42a9-87c0-308ccc93aaaa ' );
123181 }
124182
@@ -127,16 +185,14 @@ public function testSetUuidNewButNonreferenceable()
127185 */
128186 public function testSetUuidReferenceableButExisting ()
129187 {
130- $ node = $ this ->session ->getNode ('/tests_general_base/idExample ' );
131- $ node ->setProperty ('jcr:uuid ' , 'cccc61c0-09ab-42a9-87c0-308ccc93aaaa ' );
188+ $ this ->node ->setProperty ('jcr:uuid ' , 'cccc61c0-09ab-42a9-87c0-308ccc93aaaa ' );
132189 }
133190
134191 /**
135192 * @expectedException \PHPCR\NodeType\ConstraintViolationException
136193 */
137194 public function testSetUuidButNotReferenceableExisting ()
138195 {
139- $ node = $ this ->session ->getNode ('/tests_general_base/index.txt/jcr:content ' );
140- $ node ->setProperty ('jcr:uuid ' , 'dddd61c0-09ab-42a9-87c0-308ccc93aaaa ' );
196+ $ this ->node ->setProperty ('jcr:uuid ' , 'dddd61c0-09ab-42a9-87c0-308ccc93aaaa ' );
141197 }
142198}
0 commit comments