示例#1
0
 /**
  * @param  \ServerGrove\KbBundle\Document\Article $article
  * @param  string                                 $locale
  * @return string
  */
 public function renderArticleLocale(Article $article, $locale)
 {
     try {
         $active = $this->manager->findTranslation(get_class($article), $article->getId(), $locale, false)->getIsActive();
     } catch (\InvalidArgumentException $e) {
         $active = false;
     }
     $this->manager->refresh($article);
     return $this->twig->renderBlock('article_locale', array('active' => $active, 'locale' => $locale, 'locale_name' => Locale::getDisplayLanguage($locale)));
 }
 public function testComputeChangesetTranslatableFind()
 {
     $this->dm->getEventManager()->addEventListener(array(Event::postUpdate), $this->listener);
     // Create initial user
     $user1 = new \Doctrine\Tests\Models\CMS\CmsUserTranslatable();
     $user1->name = 'david';
     $user1->username = '******';
     $user1->status = 'activ';
     $this->dm->persist($user1);
     $this->dm->bindTranslation($user1, 'en');
     $user1->status = 'actif';
     $this->dm->bindTranslation($user1, 'fr');
     $this->dm->flush();
     $this->assertEquals(0, $this->listener->count);
     $this->dm->clear();
     $user1 = $this->dm->findTranslation(null, $user1->id, 'en');
     $this->dm->findTranslation(null, $user1->id, 'fr');
     $this->dm->flush();
     $this->assertEquals(0, $this->listener->count);
     $user1 = $this->dm->findTranslation(null, $user1->id, 'en');
     $user1->status = 'active';
     $this->dm->findTranslation(null, $user1->id, 'fr');
     $this->dm->flush();
     $this->assertEquals(1, $this->listener->count);
     $this->dm->clear();
     $user1 = $this->dm->findTranslation(null, $user1->id, 'en');
     $this->assertEquals('active', $user1->status);
 }
示例#3
0
 public function testComputingBetweenEventsWithTranslation()
 {
     $this->dm->getEventManager()->addEventListener(array(Event::preCreateTranslation, Event::postLoadTranslation, Event::preRemoveTranslation, Event::postRemoveTranslation), $this->listener);
     $this->dm->setLocaleChooserStrategy(new LocaleChooser($this->localePrefs, 'en'));
     // Create initial user
     $user = new \Doctrine\Tests\Models\CMS\CmsUserTranslatable();
     $user->name = 'mdekrijger';
     $user->username = '******';
     $user->status = 'active';
     $this->dm->persist($user);
     $this->dm->flush();
     $this->dm->clear();
     $user = $this->dm->findTranslation('Doctrine\\Tests\\Models\\CMS\\CmsUserTranslatable', $user->id, 'en');
     // username should be changed after loading the translation
     $this->assertEquals('loadTranslation', $user->username);
     // name had been changed pre binding translation
     $this->assertEquals('preCreateTranslation', $user->name);
     $this->dm->name = 'neuer Name';
     $this->dm->bindTranslation($user, 'de');
     $this->dm->flush();
     $this->dm->clear();
     $user = $this->dm->findTranslation('Doctrine\\Tests\\Models\\CMS\\CmsUserTranslatable', $user->id, 'en');
     $this->dm->removeTranslation($user, 'en');
     $this->assertEquals('preRemoveTranslation', $user->name);
     $this->dm->flush();
     $this->dm->clear();
     $this->assertEquals('postRemoveTranslation', $user->username);
 }
示例#4
0
 public function testTriggerTranslationEvents()
 {
     $this->dm->getEventManager()->addEventListener(array(Event::preCreateTranslation, Event::postLoadTranslation, Event::preRemoveTranslation, Event::postRemoveTranslation), $this->listener);
     $this->dm->setLocaleChooserStrategy(new LocaleChooser($this->localePrefs, 'en'));
     $page = new CmsPageTranslatable();
     $page->title = "my-page";
     $page->content = "long story";
     $this->dm->persist($page);
     $this->assertFalse($this->listener->preCreateTranslation);
     $this->assertFalse($this->listener->postLoadTranslation);
     $this->assertFalse($this->listener->postRemoveTranslation);
     $this->assertFalse($this->listener->postRemoveTranslation);
     $this->dm->bindTranslation($page, 'en');
     $this->assertTrue($this->listener->preCreateTranslation);
     $this->assertFalse($this->listener->postLoadTranslation);
     $this->assertFalse($this->listener->postRemoveTranslation);
     $this->dm->flush();
     $this->dm->clear();
     $page = $this->dm->findTranslation('Doctrine\\Tests\\Models\\CMS\\CmsPageTranslatable', $page->id, 'en');
     $this->assertTrue($this->listener->postLoadTranslation);
     $page->title = 'neuer Titel';
     $this->dm->bindTranslation($page, 'de');
     $this->dm->flush();
     $this->dm->removeTranslation($page, 'en');
     $this->assertFalse($this->listener->postRemoveTranslation);
     $this->assertTrue($this->listener->preRemoveTranslation);
     $this->dm->flush();
     $this->assertTrue($this->listener->postRemoveTranslation);
 }
 public function testSecondLevelOverwrite()
 {
     $localePrefs = array('en' => array('en', 'de'), 'de' => array('de', 'en'));
     $this->dm->setLocaleChooserStrategy(new LocaleChooser($localePrefs, 'en'));
     $secondTrans = new SecondLevelWithDuplicateOverwrite();
     $secondTrans->id = '/functional/secondTrans';
     $secondTrans->text = 'deutsch';
     $this->dm->persist($secondTrans);
     $this->dm->bindTranslation($secondTrans, 'de');
     $secondTrans->text = 'english';
     $this->dm->bindTranslation($secondTrans, 'en');
     $this->dm->flush();
     $tmpDocDe = $this->dm->findTranslation(null, '/functional/secondTrans', 'de');
     $this->assertEquals($tmpDocDe->text, 'deutsch');
     $tmpDocEn = $this->dm->findTranslation(null, '/functional/secondTrans', 'en');
     $this->assertEquals($tmpDocEn->text, 'english');
 }
示例#6
0
 public function testMenuNode()
 {
     $data = array('name' => 'test-node', 'label' => 'label_foobar', 'uri' => 'http://www.example.com/foo', 'route' => 'foo_route', 'linkType' => 'route', 'content' => $this->content, 'publishable' => false, 'publishStartDate' => new \DateTime('2013-06-18'), 'publishEndDate' => new \DateTime('2013-06-18'), 'attributes' => array('attr_foobar_1' => 'barfoo', 'attr_foobar_2' => 'barfoo'), 'childrenAttributes' => array('child_foobar_1' => 'barfoo', 'child_foobar_2' => 'barfoo'), 'linkAttributes' => array('link_foobar_1' => 'barfoo', 'link_foobar_2' => 'barfoo'), 'labelAttributes' => array('label_foobar_1' => 'barfoo', 'label_foobar_2' => 'barfoo'), 'extras' => array('extra_foobar_1' => 'barfoo', 'extra_foobar_2' => 'barfoo'), 'routeParameters' => array('route_param_foobar_1' => 'barfoo', 'route_param_foobar_2' => 'barfoo'), 'routeAbsolute' => true, 'display' => false, 'displayChildren' => false);
     $startDateString = $data['publishStartDate']->format('Y-m-d');
     $endDateString = $data['publishEndDate']->format('Y-m-d');
     $menuNode = new MenuNode();
     $refl = new \ReflectionClass($menuNode);
     $menuNode->setParentDocument($this->rootDocument);
     foreach ($data as $key => $value) {
         $refl = new \ReflectionClass($menuNode);
         $prop = $refl->getProperty($key);
         $prop->setAccessible(true);
         $prop->setValue($menuNode, $value);
     }
     $menuNode->addChild($this->child1);
     $this->dm->persist($menuNode);
     $this->dm->flush();
     $this->dm->clear();
     $menuNode = $this->dm->find(null, '/test/test-node');
     $this->assertNotNull($menuNode);
     foreach ($data as $key => $value) {
         $prop = $refl->getProperty($key);
         $prop->setAccessible(true);
         $v = $prop->getValue($menuNode);
         if (!is_object($value)) {
             $this->assertEquals($value, $v);
         }
     }
     // test objects
     $prop = $refl->getProperty('content');
     $prop->setAccessible(true);
     $content = $prop->getValue($menuNode);
     $this->assertEquals('fake_weak_content', $content->getName());
     // test children
     $this->assertCount(1, $menuNode->getChildren());
     // test publish start and end
     $publishStartDate = $menuNode->getPublishStartDate();
     $publishEndDate = $menuNode->getPublishEndDate();
     $this->assertInstanceOf('\\DateTime', $publishStartDate);
     $this->assertInstanceOf('\\DateTime', $publishEndDate);
     $this->assertEquals($startDateString, $publishStartDate->format('Y-m-d'));
     $this->assertEquals($endDateString, $publishEndDate->format('Y-m-d'));
     // test multi-lang
     $menuNode->setLocale('fr');
     $this->dm->persist($menuNode);
     $this->dm->flush();
     $this->dm->clear();
     $menuNode = $this->dm->findTranslation(null, '/test/test-node', 'fr');
     $this->assertEquals('fr', $menuNode->getLocale());
     $child = $this->dm->find(null, '/test/test-node/child1');
     $menuNode = $child->getParent();
     $this->assertCount(1, $menuNode->getChildren());
     $menuNode->removeChild($child);
     $this->dm->flush();
     $this->dm->clear();
     $menuNode = $this->dm->find(null, '/test/test-node');
     $this->assertCount(0, $menuNode->getChildren());
 }
示例#7
0
 public function testPage()
 {
     $page = new Page(array('add_locale_pattern' => true));
     $page->setParentDocument($this->baseDocument);
     $page->setName('page-name');
     $page->setTitle('Page Title');
     $page->setLabel('Page Label');
     $page->setBody('This is body');
     $page->setPublishable(false);
     $page->setPublishStartDate(new \DateTime('2013-06-18'));
     $page->setPublishEndDate(new \DateTime('2013-06-18'));
     $page->setExtras(array('extra_1' => 'foobar', 'extra_2' => 'barfoo'));
     $this->dm->persist($page);
     $this->dm->flush();
     $this->dm->clear();
     $page = $this->dm->find(null, '/test/page-name');
     $this->assertNotNull($page);
     $this->assertTrue($page->getOption('add_locale_pattern'));
     $this->assertEquals('Page Title', $page->getTitle());
     $this->assertEquals('Page Label', $page->getLabel());
     $this->assertEquals('This is body', $page->getBody());
     $this->assertEquals(array('extra_1' => 'foobar', 'extra_2' => 'barfoo'), $page->getExtras());
     // test publish start and end
     $publishStartDate = $page->getPublishStartDate();
     $publishEndDate = $page->getPublishEndDate();
     $this->assertInstanceOf('\\DateTime', $publishStartDate);
     $this->assertInstanceOf('\\DateTime', $publishEndDate);
     $this->assertEquals('2013-06-18', $publishStartDate->format('Y-m-d'));
     $this->assertEquals('2013-06-18', $publishEndDate->format('Y-m-d'));
     // test multi-lang
     $page->setLocale('fr');
     $page->setTitle('french');
     $this->dm->persist($page);
     $this->dm->flush();
     $this->dm->clear();
     $page = $this->dm->findTranslation(null, '/test/page-name', 'fr');
     $this->assertEquals('fr', $page->getLocale());
     $this->assertEquals('french', $page->getTitle());
     // test node
     $node = $page->getNode();
     $this->assertInstanceOf('PHPCR\\NodeInterface', $node);
 }
 public function testAdditionalFindCallsDoNotRefresh()
 {
     $a = new Article();
     $a->id = '/functional/' . $this->testNodeName;
     $a->topic = 'Hello';
     $a->text = 'Some text';
     $this->dm->persist($a);
     $this->dm->flush();
     $a->topic = 'Guten tag';
     $trans = $this->dm->findTranslation(null, '/functional/' . $this->testNodeName, 'en');
     $this->assertEquals('Guten tag', $trans->topic);
 }
 public function testRefreshProxyUsesFallback()
 {
     $parent = new ParentObj();
     $parent->id = '/functional/thename/child';
     $this->dm->persist($parent);
     $child = new ChildObj();
     $child->parent = $parent;
     $child->name = 'c1';
     $child->text = 'french';
     $this->dm->persist($child);
     $this->dm->bindTranslation($child, 'fr');
     $this->dm->flush();
     $this->dm->clear();
     $doc = $this->dm->findTranslation(null, '/functional/thename/child', 'fr');
     $this->assertEquals('french', $doc->children['c1']->text);
     $this->dm->clear();
     $doc = $this->dm->find(null, '/functional/thename');
     $this->assertEquals('french', $doc->child->children['c1']->text);
 }
示例#10
0
 private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null)
 {
     $oid = spl_object_hash($document);
     if (isset($visited[$oid])) {
         return $document;
         // Prevent infinite recursion
     }
     $visited[$oid] = $document;
     // mark visited
     $class = $this->dm->getClassMetadata(get_class($document));
     $locale = $this->getCurrentLocale($document, $class);
     // First we assume DETACHED, although it can still be NEW but we can avoid
     // an extra db-roundtrip this way. If it is not MANAGED but has an identity,
     // we need to fetch it from the db anyway in order to merge.
     // MANAGED entities are ignored by the merge operation.
     if ($this->getDocumentState($document) == self::STATE_MANAGED) {
         $managedCopy = $document;
     } else {
         $id = $this->determineDocumentId($document, $class);
         $persist = false;
         if (!$id) {
             // document is new
             $managedCopy = $class->newInstance();
             $persist = true;
         } else {
             $managedCopy = $this->getDocumentById($id);
             if ($managedCopy) {
                 // We have the document in-memory already, just make sure its not removed.
                 if ($this->getDocumentState($managedCopy) == self::STATE_REMOVED) {
                     throw new InvalidArgumentException("Removed document detected during merge at '{$id}'. Cannot merge with a removed document.");
                 }
                 if (ClassUtils::getClass($managedCopy) != ClassUtils::getClass($document)) {
                     throw new InvalidArgumentException('Can not merge documents of different classes.');
                 }
                 if ($this->getCurrentLocale($managedCopy, $class) !== $locale) {
                     $this->doLoadTranslation($document, $class, $locale, true);
                 }
             } elseif ($locale) {
                 // We need to fetch the managed copy in order to merge.
                 $managedCopy = $this->dm->findTranslation($class->name, $id, $locale);
             } else {
                 // We need to fetch the managed copy in order to merge.
                 $managedCopy = $this->dm->find($class->name, $id);
             }
             if ($managedCopy === null) {
                 // If the identifier is ASSIGNED, it is NEW, otherwise an error
                 // since the managed document was not found.
                 if ($class->idGenerator !== ClassMetadata::GENERATOR_TYPE_ASSIGNED) {
                     throw new InvalidArgumentException("Document not found in merge operation: {$id}");
                 }
                 $managedCopy = $class->newInstance();
                 $class->setIdentifierValue($managedCopy, $id);
                 $persist = true;
             }
         }
         $managedOid = spl_object_hash($managedCopy);
         // Merge state of $document into existing (managed) document
         foreach ($class->reflFields as $fieldName => $prop) {
             $other = $prop->getValue($document);
             if ($other instanceof PersistentCollection && !$other->isInitialized() || $other instanceof Proxy && !$other->__isInitialized()) {
                 // do not merge fields marked lazy that have not been fetched.
                 // keep the lazy persistent collection of the managed copy.
                 continue;
             }
             $mapping = $class->mappings[$fieldName];
             if (ClassMetadata::MANY_TO_ONE === $mapping['type']) {
                 $this->doMergeSingleDocumentProperty($managedCopy, $other, $prop, $mapping);
             } elseif (ClassMetadata::MANY_TO_MANY === $mapping['type']) {
                 $managedCol = $prop->getValue($managedCopy);
                 if (!$managedCol) {
                     $managedCol = new ReferenceManyCollection($this->dm, $document, $mapping['property'], array(), isset($mapping['targetDocument']) ? $mapping['targetDocument'] : null, $locale);
                     $prop->setValue($managedCopy, $managedCol);
                     $this->originalData[$managedOid][$fieldName] = $managedCol;
                 }
                 $this->cascadeMergeCollection($managedCol, $mapping);
             } elseif ('child' === $mapping['type']) {
                 if (null !== $other) {
                     $this->doMergeSingleDocumentProperty($managedCopy, $other, $prop, $mapping);
                 }
             } elseif ('children' === $mapping['type']) {
                 $managedCol = $prop->getValue($managedCopy);
                 if (!$managedCol) {
                     $managedCol = new ChildrenCollection($this->dm, $managedCopy, $mapping['filter'], $mapping['fetchDepth'], $locale);
                     $prop->setValue($managedCopy, $managedCol);
                     $this->originalData[$managedOid][$fieldName] = $managedCol;
                 }
                 $this->cascadeMergeCollection($managedCol, $mapping);
             } elseif ('referrers' === $mapping['type']) {
                 $managedCol = $prop->getValue($managedCopy);
                 if (!$managedCol) {
                     $referringMeta = $this->dm->getClassMetadata($mapping['referringDocument']);
                     $referringField = $referringMeta->mappings[$mapping['referencedBy']];
                     $managedCol = new ReferrersCollection($this->dm, $managedCopy, $referringField['strategy'], $referringField['property'], $locale);
                     $prop->setValue($managedCopy, $managedCol);
                     $this->originalData[$managedOid][$fieldName] = $managedCol;
                 }
                 $this->cascadeMergeCollection($managedCol, $mapping);
             } elseif ('mixedreferrers' === $mapping['type']) {
                 $managedCol = $prop->getValue($managedCopy);
                 if (!$managedCol) {
                     $managedCol = new ImmutableReferrersCollection($this->dm, $managedCopy, $mapping['referenceType'], $locale);
                     $prop->setValue($managedCopy, $managedCol);
                     $this->originalData[$managedOid][$fieldName] = $managedCol;
                 }
                 $this->cascadeMergeCollection($managedCol, $mapping);
             } elseif ('parent' === $mapping['type']) {
                 $this->doMergeSingleDocumentProperty($managedCopy, $other, $prop, $mapping);
             } elseif (in_array($mapping['type'], array('locale', 'versionname', 'versioncreated', 'node', 'nodename'))) {
                 if (null !== $other) {
                     $prop->setValue($managedCopy, $other);
                 }
             } elseif (!$class->isIdentifier($fieldName)) {
                 $prop->setValue($managedCopy, $other);
             }
         }
         if ($persist) {
             $this->persistNew($class, $managedCopy);
         }
         // Mark the managed copy visited as well
         $visited[$managedOid] = true;
     }
     if ($prevManagedCopy !== null) {
         $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy));
         if ($assoc['type'] == ClassMetadata::MANY_TO_ONE) {
             $prevClass->reflFields[$assoc['fieldName']]->setValue($prevManagedCopy, $managedCopy);
         } else {
             $prevClass->reflFields[$assoc['fieldName']]->getValue($prevManagedCopy)->add($managedCopy);
         }
     }
     $this->cascadeMerge($class, $document, $managedCopy, $visited);
     return $managedCopy;
 }