Skip to content

Reinstated multivalue tests #153

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 2 commits into from
Feb 13, 2015
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
133 changes: 133 additions & 0 deletions fixtures/10_Writing/mixinreferenceable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?xml version="1.0" encoding="UTF-8"?>
<sv:node xmlns:crx="http://www.day.com/crx/1.0" xmlns:mix="http://www.jcp.org/jcr/mix/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:fn_old="http://www.w3.org/2004/10/xpath-functions" xmlns:vlt="http://www.day.com/jcr/vault/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:rep="internal" sv:name="tests_mixinreferencable">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
<sv:property sv:name="jcr:created" sv:type="Date">
<sv:value>2009-04-27T13:00:54.082+02:00</sv:value>
</sv:property>

<sv:node sv:name="testReferenceOnNonReferenceableNode">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
<sv:node sv:name="non-referenceable">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="node">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>
</sv:node>

<sv:node sv:name="testReferenceOnNewlyReferenceableNode">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>

<sv:node sv:name="other-node">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="node">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>
</sv:node>

<sv:node sv:name="testReferenceOnReferenceableNode">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>

<sv:node sv:name="referenceable">
<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>842e61c0-09ab-42a9-87c0-308ccc90e6f4</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="node">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>
</sv:node>

<sv:node sv:name="testUpdateReference">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>

<sv:node sv:name="referenceable">
<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>242e61c0-09ab-42a9-82c0-398cc690e6f4</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="node">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>
<sv:node sv:name="other-node">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>
</sv:node>

<sv:node sv:name="testMultiValueReference">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>

<sv:node sv:name="testMultiValueWeakReference">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>

<sv:node sv:name="testSetUuidNewReferenceable">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>

<sv:node sv:name="testSetUuidNewButNonreferenceable">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>

<sv:node sv:name="testSetUuidButNotReferenceableExisting">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>nt:unstructured</sv:value>
</sv:property>
</sv:node>

<sv:node sv:name="testSetUuidReferenceableButExisting">
<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:node>
</sv:node>

118 changes: 87 additions & 31 deletions tests/10_Writing/MixinReferenceableTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace PHPCR\Tests\Writing;

use PHPCR\PropertyType;

require_once(__DIR__ . '/../../inc/BaseCase.php');

/**
Expand All @@ -10,6 +12,11 @@
*/
class MixinReferenceableTest extends \PHPCR\Test\BaseCase
{
public static function setupBeforeClass($fixtures = '10_Writing/mixinreferenceable')
{
parent::setupBeforeClass($fixtures);
}

public function setUp()
{
$this->renewSession(); // discard changes
Expand All @@ -22,11 +29,11 @@ public function setUp()
public function testReferenceOnNonReferenceableNode()
{
// Load a non-referenceable node
$referenced_node = $this->session->getNode('/tests_general_base/emptyExample');
$nonReferenceableNode = $this->node->getNode('non-referenceable');

// Try to reference it
$source_node = $this->session->getNode('/tests_general_base/index.txt/jcr:content');
$source_node->setProperty('reference', $referenced_node, \PHPCR\PropertyType::WEAKREFERENCE);
$sourceNode = $this->node->getNode('node');
$sourceNode->setProperty('reference', $nonReferenceableNode, \PHPCR\PropertyType::WEAKREFERENCE);
$this->session->save();
}

Expand All @@ -36,24 +43,24 @@ public function testReferenceOnNonReferenceableNode()
public function testReferenceOnNewlyReferenceableNode()
{
// Load a non-referenceable node and make it referenceable
$referenced_node = $this->session->getNode('/tests_general_base/emptyExample');
$referenced_node->addMixin('mix:referenceable');
$referencedNode = $this->node->getNode('node');
$referencedNode->addMixin('mix:referenceable');

// Re-read the node to be sure it has a UUID
$this->saveAndRenewSession();
$referenced_node = $this->session->getNode('/tests_general_base/emptyExample');
$referencedNode = $this->node->getNode('node');

// Reference it from another node
$source_node = $this->session->getNode('/tests_general_base/index.txt/jcr:content');
$source_node->setProperty('reference', $referenced_node, \PHPCR\PropertyType::WEAKREFERENCE);
$sourceNode = $this->node->getNode('other-node');
$sourceNode->setProperty('reference', $referencedNode, \PHPCR\PropertyType::WEAKREFERENCE);

$this->session->save();

$this->assertInstanceOf('PHPCR\NodeInterface', $source_node->getPropertyValue('reference'));
$this->assertInstanceOf('PHPCR\NodeInterface', $sourceNode->getPropertyValue('reference'));

// referrers only required to work once save happened
$this->assertCount(0, $referenced_node->getReferences());
$this->assertCount(1, $referenced_node->getWeakReferences());
$this->assertCount(0, $referencedNode->getReferences());
$this->assertCount(1, $referencedNode->getWeakReferences());
}

/**
Expand All @@ -62,54 +69,105 @@ public function testReferenceOnNewlyReferenceableNode()
public function testReferenceOnReferenceableNode()
{
// Load a referenceable node
$referenced_node = $this->session->getNode('/tests_general_base/idExample');
$referencedNode = $this->node->getNode('referenceable');

// Reference it from another node
$source_node = $this->session->getNode('/tests_general_base/index.txt/jcr:content');
$source_node->setProperty('oreference', $referenced_node, \PHPCR\PropertyType::WEAKREFERENCE);
$sourceNode = $this->node->getNode('node');
$sourceNode->setProperty('oreference', $referencedNode, \PHPCR\PropertyType::WEAKREFERENCE);
$this->session->save();

$this->assertInstanceOf('PHPCR\NodeInterface', $source_node->getPropertyValue('oreference'));
$this->assertInstanceOf('PHPCR\NodeInterface', $sourceNode->getPropertyValue('oreference'));
}

/**
* Test that we can update a reference
*/
public function testUpdateReference()
{
$referenced1 = $this->session->getNode('/tests_general_base/emptyExample');
$referenced1 = $this->node->getNode('node');
$referenced1->addMixin('mix:referenceable');
$this->session->save();

// Load a referenceable node
$referenced2 = $this->session->getNode('/tests_general_base/idExample');
$referenced2 = $this->node->getNode('referenceable');

// Reference it from another node
$source_node = $this->session->getNode('/tests_general_base/index.txt/jcr:content');
$sourceNode = $this->node->getNode('other-node');

$source_node->setProperty('reference', $referenced1, \PHPCR\PropertyType::WEAKREFERENCE);
$sourceNode->setProperty('reference', $referenced1, \PHPCR\PropertyType::WEAKREFERENCE);
$this->session->save();
$source_node->setProperty('reference', $referenced2, \PHPCR\PropertyType::WEAKREFERENCE);
$sourceNode->setProperty('reference', $referenced2, \PHPCR\PropertyType::WEAKREFERENCE);
$this->session->save();
$this->assertSame($referenced2, $source_node->getPropertyValue('reference'));
$this->assertSame($referenced2, $sourceNode->getPropertyValue('reference'));

$session = $this->renewSession();
$referenced2 = $session->getNode('/tests_general_base/idExample');
$this->assertSame($referenced2, $session->getProperty('/tests_general_base/index.txt/jcr:content/reference')->getValue());
$this->renewSession();
$referenced2 = $this->node->getNode('referenceable');
$this->assertSame($referenced2, $this->node->getNode('other-node')->getProperty('reference')->getValue());
}

public function testMultiValueReference()
{
$this->doTestMultiValueReference(
array('one', 'two', 'three'),
array('one', 'two', 'one', 'one', 'two', 'three'),
PropertyType::REFERENCE
);
}

public function testMultiValueWeakReference()
{
$this->doTestMultiValueReference(
array('one', 'two', 'three'),
array('one', 'two', 'one', 'one', 'two', 'three'),
PropertyType::WEAKREFERENCE
);
}

private function doTestMultiValueReference($nodeNames, $nodeCollectionNames, $referenceType)
{
$baseNode = $this->node;
$nodes = array();
foreach ($nodeNames as $nodeName) {
$node = $baseNode->addNode($nodeName);
$node->addMixin('mix:referenceable');
$nodes[$nodeName] = $node;
}

$this->session->save();

$referrer = $baseNode->addNode('referrer');

$nodeCollection = array();

foreach ($nodeCollectionNames as $nodeCollectionName) {
$nodeCollection[] = $nodes[$nodeCollectionName];
}
$referrer->setProperty('references', $nodeCollection, $referenceType);

$this->session->save();

$this->renewSession();
$referrer = $this->node->getNode('referrer');
$values = $referrer->getProperty('references');

foreach ($values as $referencedNode) {
$name = array_shift($nodeCollectionNames);
$this->assertSame($name, $referencedNode->getName());
}
}

public function testSetUuidNewReferenceable()
{
$uuid = 'aaaa61c0-09ab-42a9-87c0-308ccc93aaaa';
$node = $this->session->getNode('/tests_general_base/index.txt/jcr:content')->addNode('newId', 'nt:unstructured');
$node = $this->node->addNode('newId', 'nt:unstructured');
$node->addMixin('mix:referenceable');
$node->setProperty('jcr:uuid', $uuid);
$this->session->save();
$this->assertSame($uuid, $node->getIdentifier());

$session = $this->renewSession();

$node = $session->getNode('/tests_general_base/index.txt/jcr:content/newId');
$node = $this->node->getNode('newId');
$this->assertSame($uuid, $node->getIdentifier());
}

Expand All @@ -118,7 +176,7 @@ public function testSetUuidNewReferenceable()
*/
public function testSetUuidNewButNonreferenceable()
{
$node = $this->session->getNode('/tests_general_base/index.txt/jcr:content')->addNode('newNonref', 'nt:unstructured');
$node = $this->node->addNode('newNonref', 'nt:unstructured');
$node->setProperty('jcr:uuid', 'bbbb61c0-09ab-42a9-87c0-308ccc93aaaa');
}

Expand All @@ -127,16 +185,14 @@ public function testSetUuidNewButNonreferenceable()
*/
public function testSetUuidReferenceableButExisting()
{
$node = $this->session->getNode('/tests_general_base/idExample');
$node->setProperty('jcr:uuid', 'cccc61c0-09ab-42a9-87c0-308ccc93aaaa');
$this->node->setProperty('jcr:uuid', 'cccc61c0-09ab-42a9-87c0-308ccc93aaaa');
}

/**
* @expectedException \PHPCR\NodeType\ConstraintViolationException
*/
public function testSetUuidButNotReferenceableExisting()
{
$node = $this->session->getNode('/tests_general_base/index.txt/jcr:content');
$node->setProperty('jcr:uuid', 'dddd61c0-09ab-42a9-87c0-308ccc93aaaa');
$this->node->setProperty('jcr:uuid', 'dddd61c0-09ab-42a9-87c0-308ccc93aaaa');
}
}