Skip to content

Commit

Permalink
Merge pull request #203 from wjzijderveld/nodetypedefinition-fromxml
Browse files Browse the repository at this point in the history
Fixed NodeTypeDefinition::fromXml + added a test for it
  • Loading branch information
dbu committed Jan 20, 2014
2 parents 88fceac + a00c5ba commit 0206286
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/Jackalope/NodeType/NodeTypeDefinition.php
Expand Up @@ -163,41 +163,8 @@ protected function fromArray(array $data)
*/
protected function fromXml(DOMElement $node)
{
$this->name = $node->getAttribute('name');
$this->isAbstract = Helper::getBoolAttribute($node, 'isAbstract');
$this->isMixin = Helper::getBoolAttribute($node, 'isMixin');
$this->isQueryable = Helper::getBoolAttribute($node, 'isQueryable');
$this->hasOrderableChildNodes = Helper::getBoolAttribute($node, 'hasOrderableChildNodes');

$this->primaryItemName = $node->getAttribute('primaryItemName');
if (empty($this->primaryItemName)) {
$this->primaryItemName = null;
}

$this->declaredSuperTypeNames = array();
$xp = new DOMXPath($node->ownerDocument);
$supertypes = $xp->query('supertypes/supertype', $node);
foreach ($supertypes as $supertype) {
$this->declaredSuperTypeNames[] = $supertype->nodeValue;
}

$this->declaredPropertyDefinitions = new ArrayObject();
$properties = $xp->query('propertyDefinition', $node);
foreach ($properties as $property) {
$this->declaredPropertyDefinitions[] = $this->factory->get(
'NodeType\\PropertyDefinition',
array($property, $this->nodeTypeManager)
);
}

$this->declaredNodeDefinitions = new ArrayObject();
$declaredNodeDefinitions = $xp->query('childNodeDefinition', $node);
foreach ($declaredNodeDefinitions as $nodeDefinition) {
$this->declaredNodeDefinitions[] = $this->factory->get(
'NodeType\\NodeDefinition',
array($nodeDefinition, $this->nodeTypeManager)
);
}
$nodeTypeXmlConverter = new NodeTypeXmlConverter($this->factory);
$this->fromArray($nodeTypeXmlConverter->getNodeTypeDefinitionFromXml($node));
}

/**
Expand Down
81 changes: 81 additions & 0 deletions tests/Jackalope/NodeType/NodeTypeDefinitionTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Jackalope\NodeType;

use Jackalope\TestCase;
use PHPCR\PropertyType;

class NodeTypeDefinitionTest extends TestCase
{
Expand Down Expand Up @@ -58,4 +59,84 @@ public function testCreateFromArrayFalse()
$this->assertFalse($typeDef->isQueryable());
$this->assertFalse($typeDef->hasOrderableChildNodes());
}

public function testCreateFromXml()
{
$factory = new \Jackalope\Factory();
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" encoding="UTF-8"?>
<nodeType hasOrderableChildNodes="false" isAbstract="false" isMixin="true" isQueryable="true" name="mix:created">
<propertyDefinition autoCreated="true" declaringNodeType="mix:created" fullTextSearchable="true" mandatory="false" multiple="false" name="jcr:createdBy" onParentVersion="COPY" protected="true" queryOrderable="true" requiredType="String">
<valueConstraints/>
<availableQueryOperators>
<availableQueryOperator>jcr.operator.equal.to</availableQueryOperator>
<availableQueryOperator>jcr.operator.not.equal.to</availableQueryOperator>
<availableQueryOperator>jcr.operator.greater.than</availableQueryOperator>
<availableQueryOperator>jcr.operator.greater.than.or.equal.to</availableQueryOperator>
<availableQueryOperator>jcr.operator.less.than</availableQueryOperator>
<availableQueryOperator>jcr.operator.less.than.or.equal.to</availableQueryOperator>
<availableQueryOperator>jcr.operator.like</availableQueryOperator>
</availableQueryOperators>
</propertyDefinition>
<propertyDefinition autoCreated="true" declaringNodeType="mix:created" fullTextSearchable="true" mandatory="false" multiple="false" name="jcr:created" onParentVersion="COPY" protected="true" queryOrderable="true" requiredType="Date">
<valueConstraints/>
<availableQueryOperators>
<availableQueryOperator>jcr.operator.equal.to</availableQueryOperator>
<availableQueryOperator>jcr.operator.not.equal.to</availableQueryOperator>
<availableQueryOperator>jcr.operator.greater.than</availableQueryOperator>
<availableQueryOperator>jcr.operator.greater.than.or.equal.to</availableQueryOperator>
<availableQueryOperator>jcr.operator.less.than</availableQueryOperator>
<availableQueryOperator>jcr.operator.less.than.or.equal.to</availableQueryOperator>
<availableQueryOperator>jcr.operator.like</availableQueryOperator>
</availableQueryOperators>
</propertyDefinition>
</nodeType>');

$nodeTypeNode = $dom->childNodes->item(0);
$this->assertEquals('nodeType', $nodeTypeNode->nodeName);

$typeDef = new NodeTypeDefinition($factory, $this->getNodeTypeManagerMock(), $nodeTypeNode);
$this->assertEquals('mix:created', $typeDef->getName());
$this->assertFalse($typeDef->hasOrderableChildNodes());
$this->assertFalse($typeDef->isAbstract());
$this->assertTrue($typeDef->isMixin());
$this->assertTrue($typeDef->isQueryable());

// Property assertions
$propertyDefinitions = $typeDef->getDeclaredPropertyDefinitions();

$this->assertEquals('jcr:createdBy', $propertyDefinitions[0]->getName());
$this->assertEquals(PropertyType::STRING, $propertyDefinitions[0]->getRequiredType());
$this->assertTrue($propertyDefinitions[0]->isAutoCreated());
$this->assertFalse($propertyDefinitions[0]->isMandatory());
$this->assertFalse($propertyDefinitions[0]->isMultiple());
$this->assertTrue($propertyDefinitions[0]->isFullTextSearchable());
$this->assertTrue($propertyDefinitions[0]->isQueryOrderable());
$this->assertEquals(array(
'jcr.operator.equal.to',
'jcr.operator.not.equal.to',
'jcr.operator.greater.than',
'jcr.operator.greater.than.or.equal.to',
'jcr.operator.less.than',
'jcr.operator.less.than.or.equal.to',
'jcr.operator.like',
), $propertyDefinitions[0]->getAvailableQueryOperators());

$this->assertEquals('jcr:created', $propertyDefinitions[1]->getName());
$this->assertEquals(PropertyType::DATE, $propertyDefinitions[1]->getRequiredType());
$this->assertTrue($propertyDefinitions[1]->isAutoCreated());
$this->assertFalse($propertyDefinitions[1]->isMandatory());
$this->assertFalse($propertyDefinitions[1]->isMultiple());
$this->assertTrue($propertyDefinitions[1]->isFullTextSearchable());
$this->assertTrue($propertyDefinitions[1]->isQueryOrderable());
$this->assertEquals(array(
'jcr.operator.equal.to',
'jcr.operator.not.equal.to',
'jcr.operator.greater.than',
'jcr.operator.greater.than.or.equal.to',
'jcr.operator.less.than',
'jcr.operator.less.than.or.equal.to',
'jcr.operator.like',
), $propertyDefinitions[1]->getAvailableQueryOperators());
}
}

0 comments on commit 0206286

Please sign in to comment.