-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAddMethodsTest.php
More file actions
427 lines (349 loc) · 15.2 KB
/
Copy pathAddMethodsTest.php
File metadata and controls
427 lines (349 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/*
* This file is part of the PHPCR API Tests package
*
* Copyright (c) 2015 Liip and others
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPCR\Tests\Writing;
use PHPCR\NodeType\ConstraintViolationException;
use PHPCR\PropertyType;
use PHPCR\RepositoryInterface;
use PHPCR\ValueFormatException;
/**
* Covering jcr-283 spec $10.4.
*/
class AddMethodsTest extends \PHPCR\Test\BaseCase
{
public static function setupBeforeClass($fixtures = '10_Writing/add')
{
parent::setupBeforeClass($fixtures);
}
public function setUp()
{
$this->renewSession();
parent::setUp();
//all tests in this suite rely on the trick to have the node populated from the fixtures
$this->assertInstanceOf('PHPCR\NodeInterface', $this->node, 'Something went wrong with fixture loading');
}
public function testAddNode()
{
$this->markTestSkipped('TODO: Find a case where the parent type specifies the type for this node'); //with nt:folder, this is also not working with the java jackrabbit, so it seems not to be an implementation issue
// should take the primaryType
$new = $this->node->addNode('newNode');
$this->assertNotNull($this->session->getNode($this->node->getPath().'/newNode'), 'Node newNode was not created');
$this->session->save();
$this->assertFalse($new->isNew(), 'Node was not saved');
$this->renewSession();
$this->assertNotNull($this->session->getNode($this->node->getPath().'/newNode'), 'Node newNode was not properly saved');
}
public function testAddNodeWithPath()
{
$new = $this->node->addNode('test:namespacedNode/newNode', 'nt:unstructured');
$this->assertNotNull($this->session->getNode($this->node->getPath().'/test:namespacedNode/newNode'), 'Node newNode was not created');
$this->session->save();
$this->assertFalse($new->isNew(), 'Node was not saved');
$this->renewSession();
$this->assertNotNull($this->session->getNode($this->node->getPath().'/test:namespacedNode/newNode'), 'Node newNode was not properly saved');
}
public function testAddNodeFileType()
{
$path = $this->node->getPath();
$newNode = $this->node->addNode('newFileNode', 'nt:file');
$contentNode = $newNode->addNode('jcr:content', 'nt:resource');
$contentNode->setProperty('jcr:mimeType', 'text/plain', PropertyType::STRING);
$contentNode->setProperty('jcr:data', 'Hello', PropertyType::BINARY);
$contentNode->setProperty('jcr:lastModified', new \DateTime('2010-12-12'), PropertyType::DATE);
$this->assertNotNull($newNode, 'Node newFileNode was not created');
$this->assertTrue($newNode->isNew(), 'Node newFileNode is not marked dirty');
$this->session->save();
$this->assertFalse($newNode->isNew(), 'Node newFileNode was not saved');
$this->renewSession();
$newNode = $this->session->getNode($path.'/newFileNode');
$this->assertNotNull($newNode, 'Node newFileNode was not created');
$this->assertEquals('nt:file', $newNode->getPrimaryNodeType()->getName(), 'Node newFileNode was not created');
$lastModified = $newNode->getNode('jcr:content')->getPropertyValue('jcr:lastModified');
$this->assertInstanceOf('\DateTime', $lastModified);
$this->assertEquals('2010-12-12', $lastModified->format('Y-m-d'));
}
public function testAddNodeUnstructuredType()
{
$new = $this->node->addNode('newUnstructuredNode', 'nt:unstructured');
$this->assertNotNull($this->session->getNode($this->node->getPath().'/newUnstructuredNode'), 'Node newUnstructuredNode was not created');
$this->session->save();
$this->assertFalse($new->isNew(), 'Node was not saved');
$this->renewSession();
$this->assertNotNull($this->session->getNode($this->node->getPath().'/newUnstructuredNode'), 'Node newUnstructuredNode was not created');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testAddNodeNoNameException()
{
$this->node->addNode(null, 'nt:folder');
}
public function testAddNodeAutoNamedEmptyNamehint()
{
$node = $this->node->getNode('jcr:content');
$new = $node->addNodeAutoNamed('', 'nt:unstructured');
$this->assertInstanceOf('PHPCR\\NodeInterface', $new);
$nodes = $node->getNodes();
$this->assertCount(1, $nodes);
$newnode = $nodes->current();
$name = $newnode->getName();
$this->assertEquals(0, substr_count(':', $name));
$this->session->save();
$this->assertFalse($new->isNew(), 'Node was not saved');
$this->renewSession();
$this->assertNotNull($this->session->getNode($this->node->getPath().'/jcr:content/'.$name), 'Node newNode was not properly saved');
}
public function testAddNodeAutoNamedNullNamehint()
{
$node = $this->node->getNode('jcr:content');
$new = $node->addNodeAutoNamed(null, 'nt:unstructured');
$this->assertInstanceOf('PHPCR\\NodeInterface', $new);
$nodes = $node->getNodes();
$this->assertCount(1, $nodes);
$newnode = $nodes->current();
$name = $newnode->getName();
$this->session->save();
$this->assertFalse($new->isNew(), 'Node was not saved');
$this->renewSession();
$this->assertNotNull($this->session->getNode($this->node->getPath().'/jcr:content/'.$name), 'Node newNode was not properly saved');
}
public function testAddNodeAutoNamedValidNamespaceNamehint()
{
$node = $this->node->getNode('jcr:content');
$new = $node->addNodeAutoNamed('jcr:', 'nt:unstructured');
$this->assertInstanceOf('PHPCR\\NodeInterface', $new);
$nodes = $node->getNodes();
$this->assertCount(1, $nodes);
$newnode = $nodes->current();
$name = $newnode->getName();
$this->assertEquals('jcr:', substr($name, 0, 4));
$this->session->save();
$this->assertFalse($new->isNew(), 'Node was not saved');
$this->renewSession();
$this->assertNotNull($this->session->getNode($this->node->getPath().'/jcr:content/'.$name), 'Node newNode was not properly saved');
}
public function testAddPropertyOnUnstructured()
{
$path = $this->node->getPath();
$node = $this->node->addNode('unstructuredNode', 'nt:unstructured');
$node->setProperty('testprop', 'val');
$node->setProperty('refprop', $this->node->getNode('ref'));
$this->session->save();
$this->assertFalse($node->isNew(), 'Node was not saved');
$this->renewSession();
$node = $this->session->getNode($path.'/unstructuredNode');
$this->assertNotNull($node, 'Node was not created');
$this->assertEquals('val', $node->getPropertyValue('testprop'), 'Property was not saved correctly');
$node->setProperty('test2', 'val2');
$this->session->save();
$this->assertFalse($node->isNew(), 'Node was not saved');
$this->assertFalse($node->getProperty('test2')->isNew(), 'Property was not saved');
$this->renewSession();
$node = $this->session->getNode($path.'/unstructuredNode');
$this->assertEquals('val2', $node->getPropertyValue('test2'), 'Property was not added correctly');
}
public function testAddMultiValuePropertyOnUnstructured()
{
$path = $this->node->getPath();
$node = $this->node->addNode('unstructuredNode2', 'nt:unstructured');
$node->setProperty('test', array('val', 'val2'));
$this->session->save();
$this->assertFalse($node->isNew(), 'Node was not saved');
$this->renewSession();
$node = $this->session->getNode($path.'/unstructuredNode2');
$this->assertNotNull($node, 'Node was not created');
$this->assertEquals(array('val', 'val2'), $node->getPropertyValue('test'), 'Property was not saved correctly');
$node->setProperty('test2', array('val3', 'val4'));
$this->session->save();
$this->assertFalse($node->isNew(), 'Node was not saved');
$this->assertFalse($node->getProperty('test2')->isNew(), 'Property was not saved');
$this->renewSession();
$node = $this->session->getNode($path.'/unstructuredNode2');
$this->assertEquals(array('val3', 'val4'), $node->getPropertyValue('test2'), 'Property was not added correctly');
}
/**
* @expectedException \PHPCR\NodeType\ConstraintViolationException
*/
public function testAddNodeMissingType()
{
$this->node->addNode('newNode');
}
/**
* @expectedException \PHPCR\NodeType\ConstraintViolationException
*/
public function testAddNodeIllegalType()
{
$this->node->addNode('newNode', 'nt:unstructured');
$this->saveAndRenewSession();
}
/**
* @expectedException \PHPCR\NodeType\NoSuchNodeTypeException
*/
public function testAddNodeWithInexistingType()
{
$this->node->addNode('newFileNode', 'inexistenttype');
}
/**
* Test adding an already existing child.
*
* nt:folder do not allow same-name siblings
*
* @expectedException \PHPCR\ItemExistsException
*/
public function testAddNodeExisting()
{
$this->node->addNode('child', 'nt:file');
}
/**
* Tests adding the same child to the same node in 2 different sessions.
*
* @expectedException \PHPCR\ItemExistsException
*/
public function testAddNodeInParallel()
{
$path = $this->node->getPath();
$session1 = $this->session;
$session2 = self::$loader->getSession();
$node1 = $session1->getNode($path);
$c1 = $node1->addNode('test', 'nt:file');
$c1->addNode('jcr:content', 'nt:unstructured');
$node2 = $session2->getNode($path);
$c2 = $node2->addNode('test', 'nt:file');
$c2->addNode('jcr:content', 'nt:unstructured');
$session1->save();
$session2->save();
}
/**
* try to add a node below a not existing node.
*
* @expectedException \PHPCR\PathNotFoundException
*/
public function testAddNodePathNotFound()
{
$this->node->addNode('nonExistent/newNode', 'nt:unstructured');
}
/**
* try to add a node below a property.
*
* @expectedException \PHPCR\NodeType\ConstraintViolationException
*/
public function testAddNodeToProperty()
{
$this->node->addNode('prop/failNode', 'nt:unstructured');
}
/**
* try to add a property of the wrong type.
*/
public function testAddPropertyWrongType()
{
$file = $this->node->addNode('file', 'nt:file');
$data = $file->addNode('jcr:content', 'nt:resource');
try {
$data->setProperty('jcr:lastModified', true);
$this->saveAndRenewSession();
} catch (ValueFormatException $e) {
//correct according to JSR-287 3.6.4 Property Type Conversion
return;
} catch (ConstraintViolationException $e) {
//also correct
return;
}
$this->fail('Expected PHPCR\\NodeType\\ConstraintViolationException or PHPCR\\ValueFormatException');
}
/**
* @expectedException \PHPCR\RepositoryException
*/
public function testAddNodeWithIndex()
{
$this->node->addNode('name[3]', 'nt:unstructured');
}
/**
* Add a node and a child node to it.
*/
public function testAddNodeChild()
{
$newNode = $this->node->addNode('parent', 'nt:unstructured');
$newChild = $newNode->addNode('child', 'nt:unstructured');
$path = $newChild->getPath();
$this->assertTrue($this->session->nodeExists('/tests_write_manipulation_add/testAddNodeChild/parent/child'), 'Child node not found [Session]');
$this->session->save();
$this->assertFalse($newChild->isNew(), 'Node was not saved');
// dispatch to backend
$session = $this->saveAndRenewSession();
$this->assertTrue($session->nodeExists($path));
$this->assertInstanceOf('PHPCR\\NodeInterface', $session->getNode($path));
}
/**
* Add a node and a child node with some properties.
*/
public function testAddNodeChildProperties()
{
$parent = $this->node->addNode('parent', 'nt:folder');
$child = $parent->addNode('child', 'nt:file');
$content = $child->addNode('jcr:content', 'nt:resource');
$content->setProperty('jcr:data', '1234', PropertyType::BINARY);
$path = $child->getPath();
$this->saveAndRenewSession();
$child = $this->session->getNode($path);
$this->assertInstanceOf('PHPCR\NodeInterface', $child);
}
/**
* try to add a node with an unregistered namespace.
*
* @expectedException \PHPCR\RepositoryException
*/
public function testAddNodeWithUnregisteredNamespace()
{
$namespace = 'testUnregisteredNamespace';
$nodeName = 'child';
//add the node with an unregistered namespace, should throw a RepositoryException
$this->node->addNode($namespace.':'.$nodeName, 'nt:unstructured');
//save the changes
$this->saveAndRenewSession();
}
/**
* try to add a property with an unregistered namespace.
*
* @expectedException \PHPCR\RepositoryException
*/
public function testAddPropertyWithUnregisteredNamespace()
{
$namespace = 'testUnregisteredNamespace';
$propertyName = 'prop';
//add a property with an unregistered namespace, should throw a RepositoryException
$this->node->setProperty($namespace.':'.$propertyName, 'some value');
//save the changes
$this->saveAndRenewSession();
}
public function testAddNodeWithAutoCreatedNode()
{
if ($this->skipIfNotSupported(RepositoryInterface::NODE_TYPE_MANAGEMENT_AUTOCREATED_DEFINITIONS_SUPPORTED)) {
return;
}
$workspace = $this->session->getWorkspace();
$cnd = file_get_contents(__DIR__.'/../../fixtures/10_Writing/add_auto_create.cnd');
$workspace->getNodeTypeManager()->registerNodeTypesCnd($cnd, true);
$this->node->addNode('foo', 'test:testautocreate');
$this->session->save();
$childNode = $this->session->getNode($this->node->getPath().'/foo');
$this->assertNotNull($childNode);
$childNode = $this->session->getNode($this->node->getPath().'/foo/autocreated');
$this->assertNotNull($childNode);
$primaryType = $childNode->getPrimaryNodeType();
$this->assertEquals('nt:unstructured', $primaryType->getName());
$childNode = $this->session->getNode($this->node->getPath().'/foo/autocreatedwithchild');
$this->assertNotNull($childNode);
$primaryType = $childNode->getPrimaryNodeType();
$this->assertEquals('test:testAutoCreateChild', $primaryType->getName());
$childNode = $this->session->getNode($this->node->getPath().'/foo/autocreatedwithchild/foo');
$this->assertNotNull($childNode);
$primaryType = $childNode->getPrimaryNodeType();
$this->assertEquals('nt:unstructured', $primaryType->getName());
}
}