public function testCommandIdentifier()
 {
     $uuid = UUIDHelper::generateUUID();
     $this->dumperHelper->expects($this->once())->method('getTreeWalker')->will($this->returnValue($this->treeWalker));
     $this->session->expects($this->once())->method('getNodeByIdentifier')->with($uuid)->will($this->returnValue($this->node1));
     $this->treeWalker->expects($this->once())->method('traverse')->with($this->node1);
     $this->executeCommand('phpcr:node:dump', array('identifier' => $uuid));
 }
 /**
  * {@inheritdoc}
  */
 public function createNodeForDocument($document, NodeInterface $parentNode, $name)
 {
     $metadata = $this->metadataFactory->getMetadataForClass(get_class($document));
     $node = $parentNode->addNode($name);
     $node->addMixin($metadata->getPhpcrType());
     $node->setProperty('jcr:uuid', UUIDHelper::generateUUID());
     return $node;
 }
Example #3
0
 private function processNode(Node $node)
 {
     if (false === $node->hasProperty(Storage::JCR_UUID)) {
         return;
     }
     $jcrUuid = UUIDHelper::generateUUID();
     $node->setProperty(Storage::JCR_UUID, $jcrUuid, 'String');
 }
 /**
  * @covers Doctrine\ODM\PHPCR\DocumentManager::findTranslation
  */
 public function testFindTranslation()
 {
     $fakeUuid = \PHPCR\Util\UUIDHelper::generateUUID();
     $session = $this->getMockForAbstractClass('PHPCR\\SessionInterface', array('getNodeByIdentifier'));
     $session->expects($this->once())->method('getNodeByIdentifier')->will($this->throwException(new \PHPCR\ItemNotFoundException(sprintf('403: %s', $fakeUuid))));
     $config = new \Doctrine\ODM\PHPCR\Configuration();
     $dm = DocumentManager::create($session, $config);
     $nonExistent = $dm->findTranslation(null, $fakeUuid, 'en');
     $this->assertNull($nonExistent);
 }
Example #5
0
 private function getOrCreateInternalUuid($path)
 {
     if (!$this->pathRegistry->hasPath($path)) {
         $internalUuid = UUIDHelper::generateUUID();
     } else {
         $internalUuid = $this->pathRegistry->getUuid($path);
     }
     if (!$internalUuid) {
         throw new \RuntimeException('Failed to determine internal UUID');
     }
     return $internalUuid;
 }
 public function testFindManyWithNonExistingUuuid()
 {
     $user = new TestUser();
     $user->username = '******';
     $user->id = '/functional/test';
     $this->dm->persist($user);
     $this->dm->flush();
     $this->dm->clear();
     $actualUuid = $user->uuid;
     $unusedUuid = UUIDHelper::generateUUID();
     $this->assertNotNull($this->dm->find(get_class($user), $user->id));
     $this->assertNotNull($this->dm->find(get_class($user), $actualUuid));
     $this->assertNull($this->dm->find(get_class($user), $unusedUuid));
     $uuids = array($actualUuid, $unusedUuid);
     $documents = $this->dm->findMany(get_class($user), $uuids);
     $this->assertEquals(1, count($documents));
 }
 /**
  * Get the closure for the UUID generation.
  *
  * @since 1.1
  * @return callable a UUID generator
  */
 public function getUuidGenerator()
 {
     return isset($this->attributes['uuidGenerator']) ? $this->attributes['uuidGenerator'] : function () {
         return UUIDHelper::generateUUID();
     };
 }
Example #8
0
 /**
  * @return string a universally unique id.
  */
 protected function generateUuid()
 {
     return UUIDHelper::generateUUID();
 }
 public function testGetRoutesByNamesUuid()
 {
     $uuid1 = UUIDHelper::generateUUID();
     $uuid2 = UUIDHelper::generateUUID();
     $paths = array($uuid1, $uuid2);
     $route1 = new Route('/test-route');
     $route2 = new Route('/other-route');
     $routes = new ArrayCollection();
     $routes->set($uuid1, $route1);
     $routes->set($uuid2, $route2);
     $this->dmMock->expects($this->once())->method('findMany')->with(null, $paths)->will($this->returnValue($routes));
     $uow = $this->buildMock('Doctrine\\ODM\\PHPCR\\UnitOfWork');
     $this->dmMock->expects($this->any())->method('getUnitOfWork')->will($this->returnValue($uow));
     $uow->expects($this->at(0))->method('getDocumentId')->with($route1)->will($this->returnValue('/cms/routes/test-route'));
     $uow->expects($this->at(1))->method('getDocumentId')->with($route2)->will($this->returnValue('/cms/routes/other-route'));
     $this->candidatesMock->expects($this->at(0))->method('isCandidate')->with('/cms/routes/test-route')->will($this->returnValue(true));
     $this->candidatesMock->expects($this->at(1))->method('isCandidate')->with('/cms/routes/other-route')->will($this->returnValue(false));
     $routeProvider = new RouteProvider($this->managerRegistryMock, $this->candidatesMock);
     $routeProvider->setManagerName('default');
     $routes = $routeProvider->getRoutesByNames($paths);
     $this->assertCount(1, $routes);
 }
Example #10
0
 /**
  * TODO: we should move that into the common Jackalope BaseTransport or as new method of NodeType
  * it will be helpful for other implementations
  *
  * Validate this node with the nodetype and generate not yet existing
  * autogenerated properties as necessary
  *
  * @param Node     $node
  * @param NodeType $def
  */
 private function validateNode(Node $node, NodeType $def)
 {
     foreach ($def->getDeclaredChildNodeDefinitions() as $childDef) {
         /* @var $childDef \PHPCR\NodeType\NodeDefinitionInterface */
         if (!$node->hasNode($childDef->getName())) {
             if ('*' === $childDef->getName()) {
                 continue;
             }
             if ($childDef->isMandatory() && !$childDef->isAutoCreated()) {
                 throw new RepositoryException("Child " . $childDef->getName() . " is mandatory, but is not present while " . "saving " . $def->getName() . " at " . $node->getPath());
             }
             if ($childDef->isAutoCreated()) {
                 throw new NotImplementedException("Auto-creation of child node '" . $def->getName() . "#" . $childDef->getName() . "' is not yet supported in DoctrineDBAL transport.");
             }
         }
     }
     foreach ($def->getDeclaredPropertyDefinitions() as $propertyDef) {
         /* @var $propertyDef \PHPCR\NodeType\PropertyDefinitionInterface */
         if ('*' == $propertyDef->getName()) {
             continue;
         }
         if (!$node->hasProperty($propertyDef->getName())) {
             if ($propertyDef->isMandatory() && !$propertyDef->isAutoCreated()) {
                 throw new RepositoryException("Property " . $propertyDef->getName() . " is mandatory, but is not present while " . "saving " . $def->getName() . " at " . $node->getPath());
             }
             if ($propertyDef->isAutoCreated()) {
                 switch ($propertyDef->getName()) {
                     case 'jcr:uuid':
                         $value = UUIDHelper::generateUUID();
                         break;
                     case 'jcr:createdBy':
                     case 'jcr:lastModifiedBy':
                         $value = $this->credentials->getUserID();
                         break;
                     case 'jcr:created':
                     case 'jcr:lastModified':
                         $value = new \DateTime();
                         break;
                     case 'jcr:etag':
                         // TODO: http://www.day.com/specs/jcr/2.0/3_Repository_Model.html#3.7.12.1%20mix:etag
                         $value = 'TODO: generate from binary properties of this node';
                         break;
                     default:
                         $defaultValues = $propertyDef->getDefaultValues();
                         if ($propertyDef->isMultiple()) {
                             $value = $defaultValues;
                         } elseif (isset($defaultValues[0])) {
                             $value = $defaultValues[0];
                         } else {
                             // When implementing versionable or activity, we need to handle more properties explicitly
                             throw new RepositoryException('No default value for autocreated property ' . $propertyDef->getName() . ' at ' . $node->getPath());
                         }
                 }
                 $node->setProperty($propertyDef->getName(), $value, $propertyDef->getRequiredType());
             }
         }
     }
     foreach ($node->getProperties() as $property) {
         $this->assertValidProperty($property);
     }
 }
 /**
  * Create a path.
  *
  * @param string $path
  *
  * @return NodeInterface
  */
 public function createPath($path)
 {
     $current = $this->session->getRootNode();
     $segments = preg_split('#/#', $path, null, PREG_SPLIT_NO_EMPTY);
     foreach ($segments as $segment) {
         if ($current->hasNode($segment)) {
             $current = $current->getNode($segment);
         } else {
             $current = $current->addNode($segment);
             $current->addMixin('mix:referenceable');
             $current->setProperty('jcr:uuid', UUIDHelper::generateUUID());
         }
     }
     return $current;
 }
Example #12
0
 private function setMixins(Mapping\ClassMetadata $metadata, NodeInterface $node)
 {
     if ($metadata->versionable === 'full') {
         $node->addMixin('mix:versionable');
     } else {
         if ($metadata->versionable === 'simple') {
             $node->addMixin('mix:simpleVersionable');
         }
         if ($metadata->referenceable) {
             $node->addMixin('mix:referenceable');
         }
     }
     // we manually set the uuid to allow creating referenced and referencing document without flush in between.
     if ($node->isNodeType('mix:referenceable') && !$node->hasProperty('jcr:uuid')) {
         // TODO do we need to check with the storage backend if the generated id really is unique?
         $node->setProperty('jcr:uuid', UUIDHelper::generateUUID());
     }
 }
         $parentPath = implode('/', array_slice(explode('/', $path), 0, -1));
         $parentPath = $parentPath == '' ? '/' : $parentPath;
         $dataSet[] = array('_id' => $id, 'path' => $path, 'parent' => $parentPath, 'w_id' => array('$oid' => '4e00e8fea381601b08000000'), 'type' => $type, 'props' => $attributes);
         $pos++;
     }
 } else {
     $nodes = $srcDom->getElementsByTagName('*');
     foreach ($nodes as $node) {
         if ($node instanceof DOMElement) {
             $parent = $node;
             $path = '';
             do {
                 $path = '/' . $parent->tagName . $path;
                 $parent = $parent->parentNode;
             } while ($parent instanceof DOMElement);
             $uuid = \PHPCR\Util\UUIDHelper::generateUUID();
             $type = 'nt:unstructured';
             $attributes = array();
             foreach ($node->attributes as $attr) {
                 $name = $attr->prefix ? $attr->prefix . ':' . $attr->name : $attr->name;
                 if ($name == 'jcr:uuid') {
                     $uuid = $attr->value;
                 } elseif ($name == 'jcr:primaryType') {
                     $type = $attr->value;
                 } else {
                     $attributes[] = array('name' => $name, 'value' => $attr->value);
                 }
             }
             $id = new \MongoBinData($uuid, MongoBinData::UUID);
             $id = array('$binary' => $id->__toString(), '$type' => MongoBinData::UUID);
             if (!isset($seenPaths[$path])) {
Example #14
0
 /**
  * Validate this node with the nodetype and generate not yet existing
  * autogenerated properties as necessary.
  *
  * @param NodeInterface $node
  * @param NodeType      $nodeTypeDefinition
  *
  * @return AddNodeOperation[] Additional operations to handle autocreated nodes.
  *
  * @throws \InvalidArgumentException
  * @throws RepositoryException
  * @throws ItemExistsException
  * @throws LockException
  * @throws ConstraintViolationException
  * @throws PathNotFoundException
  * @throws VersionException
  * @throws ValueFormatException
  */
 private function processNodeWithType(NodeInterface $node, NodeType $nodeTypeDefinition)
 {
     $additionalOperations = array();
     foreach ($nodeTypeDefinition->getDeclaredChildNodeDefinitions() as $childDef) {
         /* @var $childDef NodeDefinitionInterface */
         if (!$node->hasNode($childDef->getName())) {
             if ('*' === $childDef->getName()) {
                 continue;
             }
             if ($childDef->isMandatory() && !$childDef->isAutoCreated()) {
                 throw new RepositoryException(sprintf('Child "%s" is mandatory, but is not present while saving "%s" at path "%s"', $childDef->getName(), $nodeTypeDefinition->getName(), $node->getPath()));
             }
             if ($childDef->isAutoCreated()) {
                 $requiredPrimaryTypeNames = $childDef->getRequiredPrimaryTypeNames();
                 $primaryType = count($requiredPrimaryTypeNames) ? current($requiredPrimaryTypeNames) : null;
                 $newNode = $node->addNode($childDef->getName(), $primaryType);
                 $absPath = $node->getPath() . '/' . $childDef->getName();
                 $operation = new AddNodeOperation($absPath, $newNode);
                 $additionalOperations[] = $operation;
             }
         }
     }
     foreach ($nodeTypeDefinition->getDeclaredPropertyDefinitions() as $propertyDef) {
         /* @var $propertyDef PropertyDefinitionInterface */
         if ('*' === $propertyDef->getName()) {
             continue;
         }
         if (!$node->hasProperty($propertyDef->getName())) {
             if ($propertyDef->isMandatory() && !$propertyDef->isAutoCreated()) {
                 throw new RepositoryException(sprintf('Property "%s" is mandatory, but is not present while saving "%s" at "%s"', $propertyDef->getName(), $nodeTypeDefinition->getName(), $node->getPath()));
             }
             if ($propertyDef->isAutoCreated()) {
                 switch ($propertyDef->getName()) {
                     case 'jcr:uuid':
                         $value = UUIDHelper::generateUUID();
                         break;
                     case 'jcr:createdBy':
                     case 'jcr:lastModifiedBy':
                         $value = $this->userId;
                         break;
                     case 'jcr:created':
                     case 'jcr:lastModified':
                         $value = new \DateTime();
                         break;
                     case 'jcr:etag':
                         // TODO: http://www.day.com/specs/jcr/2.0/3_Repository_Model.html#3.7.12.1%20mix:etag
                         $value = 'TODO: generate from binary properties of this node';
                         break;
                     default:
                         $defaultValues = $propertyDef->getDefaultValues();
                         if ($propertyDef->isMultiple()) {
                             $value = $defaultValues;
                         } elseif (isset($defaultValues[0])) {
                             $value = $defaultValues[0];
                         } else {
                             // When implementing versionable or activity, we need to handle more properties explicitly
                             throw new RepositoryException(sprintf('No default value for autocreated property "%s" at "%s"', $propertyDef->getName(), $node->getPath()));
                         }
                 }
                 $node->setProperty($propertyDef->getName(), $value, $propertyDef->getRequiredType());
             }
         } elseif ($propertyDef->isAutoCreated()) {
             $prop = $node->getProperty($propertyDef->getName());
             if (!$prop->isModified() && !$prop->isNew()) {
                 switch ($propertyDef->getName()) {
                     case 'jcr:lastModified':
                         if ($this->autoLastModified) {
                             $prop->setValue(new \DateTime());
                         }
                         break;
                     case 'jcr:lastModifiedBy':
                         if ($this->autoLastModified) {
                             $prop->setValue($this->userId);
                         }
                         break;
                     case 'jcr:etag':
                         // TODO: update etag if needed
                         break;
                 }
             }
         }
     }
     foreach ($nodeTypeDefinition->getDeclaredSupertypes() as $superType) {
         $this->processNodeWithType($node, $superType);
     }
     foreach ($node->getProperties() as $property) {
         $this->assertValidProperty($property);
     }
     return $additionalOperations;
 }
 public function addNode($workspaceName, \DOMElement $node)
 {
     $properties = $this->getAttributes($node);
     $uuid = isset($properties['jcr:uuid']['value'][0]) ? (string) $properties['jcr:uuid']['value'][0] : UUIDHelper::generateUUID();
     $this->ids[$uuid] = $id = isset($this->expectedNodes[$uuid]) ? $this->expectedNodes[$uuid] : self::$idCounter++;
     $dom = new \DOMDocument('1.0', 'UTF-8');
     $phpcrNode = $dom->createElement('sv:node');
     foreach ($this->namespaces as $namespace => $uri) {
         $phpcrNode->setAttribute('xmlns:' . $namespace, $uri);
     }
     $dom->appendChild($phpcrNode);
     foreach ($properties as $propertyName => $propertyData) {
         if ($propertyName == 'jcr:uuid') {
             continue;
         }
         if (!isset($this->jcrTypes[$propertyData['type']])) {
             throw new \InvalidArgumentException('"' . $propertyData['type'] . '" is not a valid JCR type.');
         }
         $phpcrNode->appendChild($this->createPropertyNode($workspaceName, $propertyName, $propertyData, $id, $dom, $phpcrNode));
     }
     list($parentPath, $childPath) = $this->getPath($node);
     $namespace = '';
     $name = $node->getAttributeNS($this->namespaces['sv'], 'name');
     if (count($parts = explode(':', $name, 2)) == 2) {
         list($namespace, $name) = $parts;
     }
     if ($namespace == 'jcr' && $name == 'root') {
         $id = 1;
         $childPath = '/';
         $parentPath = '';
         $name = '';
         $namespace = '';
     }
     $this->addRow('phpcr_nodes', array('id' => $id, 'path' => $childPath, 'parent' => $parentPath, 'local_name' => $name, 'namespace' => $namespace, 'workspace_name' => $workspaceName, 'identifier' => $uuid, 'type' => $properties['jcr:primaryType']['value'][0], 'props' => $dom->saveXML(), 'depth' => PathHelper::getPathDepth($childPath), 'sort_order' => $id - 2));
     return $this;
 }
 /**
  * @return callable a uuid generator function.
  */
 protected function getUuidGenerator()
 {
     if (!$this->uuidGenerator) {
         $this->uuidGenerator = function () {
             return UUIDHelper::generateUUID();
         };
     }
     return $this->uuidGenerator;
 }
 public function testGenerateUUID()
 {
     $id = UUIDHelper::generateUUID();
     $this->assertEquals(1, preg_match('/^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}$/', $id));
 }