Skip to content
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
106 changes: 106 additions & 0 deletions fixtures/10_Writing/mixinreferenceable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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>

106 changes: 82 additions & 24 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need this? i think the parent does that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not.

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,39 +69,90 @@ 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');

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

$this->renewSession();
$referenced2 = $this->node->getNode('referenceable');
$this->assertSame($referenced2, $this->node->getNode('other-node')->getProperty('reference')->getValue());
}

public function testMultiValueReference()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a data-provider rather than something to test separately, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.. it should be a data provider, but unfortunately there is some strange magic which happens in this test suite where the fixtures are automatically loaded depending on the "name" of the test method being called - when we use data provider the "name" is returned as testMutlivalueWeakReference data set (array(foo, bar)) or something similar.

And the fixtures are loaded according to that name..

{
$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;
}

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

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

$nodeCollection = array();

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

$this->session->save();
$this->assertSame($referenced2, $source_node->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();
$referrer = $this->node->getNode('referrer');
$values = $referrer->getProperty('references');

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