/** * @covers BackBee\NestedNode\Page::replaceRootContentSet */ public function testReplaceRootContentSet() { $oldContentSet = $this->page->getContentSet()->last(); $newContentSet = new ContentSet(); $this->assertEquals($newContentSet, $this->page->replaceRootContentSet($this->page->getContentSet()->last(), $newContentSet)); $this->assertEquals($oldContentSet, $this->page->getContentSet()->last()); $this->assertEquals($newContentSet, $this->page->replaceRootContentSet($this->page->getContentSet()->last(), $newContentSet, true)); $this->assertEquals($oldContentSet, $this->page->getContentSet()->last()); $this->assertEquals($newContentSet, $this->page->replaceRootContentSet($this->page->getContentSet()->last(), $newContentSet, false)); $this->assertEquals($newContentSet, $this->page->getContentSet()->last()); $this->page->replaceRootContentSet($this->page->getContentSet()->last(), $oldContentSet, false); $this->assertEquals($newContentSet, $this->page->replaceRootContentSet($this->page->getContentSet()->last(), $newContentSet, null)); $this->assertEquals($newContentSet, $this->page->getContentSet()->last()); $this->page->replaceRootContentSet($this->page->getContentSet()->last(), $oldContentSet, false); $this->assertEquals($newContentSet, $this->page->replaceRootContentSet($this->page->getContentSet()->last(), $newContentSet, 'fake')); $this->assertEquals($newContentSet, $this->page->getContentSet()->last()); $child = new Page('child', array('title' => 'child', 'url' => 'url')); $child->setParent($this->page)->setLayout($this->page->getLayout()); $this->assertEquals($newContentSet, $child->replaceRootContentSet($child->getContentSet()->last(), $newContentSet)); $this->assertEquals($newContentSet, $child->getContentSet()->last()); }
/** * @covers \BackBee\NestedNode\Repository\PageRepository::getOnlineSiblingsByLayout */ public function testGetOnlineSiblingsByLayout() { $section1 = $this->repository->find('section1'); $section2 = $this->repository->find('section2'); $page1 = $this->repository->find('page1'); $page2 = $this->repository->find('page2'); $page3 = $this->repository->find('page3'); $this->assertEquals(array(), $this->repository->getOnlineSiblingsByLayout($section1, $this->root->getLayout())); $this->assertEquals(array(), $this->repository->getOnlineSiblingsByLayout($section2, $this->root->getLayout())); $this->assertEquals(array(), $this->repository->getOnlineSiblingsByLayout($page1, $this->root->getLayout())); $this->assertEquals(array(), $this->repository->getOnlineSiblingsByLayout($page2, $this->root->getLayout())); $this->assertEquals(array(), $this->repository->getOnlineSiblingsByLayout($page3, $this->root->getLayout())); $section2->setState(Page::STATE_ONLINE); $section1->setState(Page::STATE_ONLINE); $page1->setState(Page::STATE_ONLINE); $page2->setState(Page::STATE_ONLINE); $page3->setState(Page::STATE_ONLINE); self::$em->flush(); $this->assertEquals(array($section2, $page3), $this->repository->getOnlineSiblingsByLayout($section1, $this->root->getLayout(), false, array('_position' => 'ASC', '_leftnode' => 'ASC'))); $this->assertEquals(array($section1, $page3), $this->repository->getOnlineSiblingsByLayout($section2, $this->root->getLayout(), false, array('_position' => 'ASC', '_leftnode' => 'ASC'))); $this->assertEquals(array($page2), $this->repository->getOnlineSiblingsByLayout($page1, $this->root->getLayout(), false, array('_position' => 'ASC', '_leftnode' => 'ASC'))); $this->assertEquals(array($page1), $this->repository->getOnlineSiblingsByLayout($page2, $this->root->getLayout(), false, array('_position' => 'ASC', '_leftnode' => 'ASC'))); $this->assertEquals(array($section2, $section1), $this->repository->getOnlineSiblingsByLayout($page3, $this->root->getLayout(), false, array('_position' => 'ASC', '_leftnode' => 'ASC'))); }
/** * Page workflow state setter. * * @param Page $page * @param State $workflow */ private function trySetPageWorkflowState(Page $page, State $workflow = null) { $page->setWorkflowState(null); if (null !== $workflow) { if (null === $workflow->getLayout() || $workflow->getLayout()->getUid() === $page->getLayout()->getUid()) { $page->setWorkflowState($workflow); } } }
/** * Copy a page to a new one. * * @param Page $page The page to be copied. * @param string|null $title Optional, the title of the copy, if null (default) the title of the copied page. * @param Page|null $parent Optional, the parent of the copy, if null (default) the parent of the copied page. * * @return Page The copy of the page. * * @throws InvalidArgumentException Raises if the page is deleted. */ private function copy(Page $page, $title = null, Page $parent = null) { if (Page::STATE_DELETED & $page->getState()) { throw new InvalidArgumentException('Cannot duplicate a deleted page.'); } // Cloning the page $new_page = clone $page; $new_page->setTitle(null === $title ? $page->getTitle() : $title)->setLayout($page->getLayout()); // Setting the clone as first child of the parent if (null !== $parent) { $new_page = $this->insertNodeAsFirstChildOf($new_page, $parent, $new_page->hasMainSection()); } // Persisting entities $this->_em->persist($new_page); return $new_page; }
/** * Updates the associated definition of the set of metadata. * * @param array $definitions * @param \BackBee\NestedNode\Page $page */ public function update(array $definitions = null, Page $page = null) { $content = null === $page ? null : $page->getContentSet(); if (null !== $definitions) { foreach ($definitions as $name => $definition) { if (false === is_array($definition)) { continue; } if (null === ($metadata = $this->get($name))) { $metadata = new MetaData($name); $this->add($metadata); } foreach ($definition as $attrname => $attrvalue) { if (false === is_array($attrvalue)) { $attrvalue = '' === $metadata->getAttribute($attrname) ? $attrvalue : $metadata->getAttribute($attrname); $metadata->setAttribute($attrname, $attrvalue, $content); continue; } if (true === $metadata->hasAttribute($attrname)) { if (null !== $page && true === array_key_exists('layout', $attrvalue)) { $layout_uid = $page->getLayout()->getUid(); if (true === array_key_exists($layout_uid, $attrvalue['layout'])) { $scheme = is_array($attrvalue['layout'][$layout_uid]) ? reset($attrvalue['layout'][$layout_uid]) : $attrvalue['layout'][$layout_uid]; $metadata->updateAttributeScheme($attrname, $scheme, $content); } } continue; } if (true === array_key_exists('default', $attrvalue)) { $value = is_array($attrvalue['default']) ? reset($attrvalue['default']) : $attrvalue['default']; $metadata->setAttribute($attrname, $value, $content); } if (null !== $page && true === array_key_exists('layout', $attrvalue)) { $layout_uid = $page->getLayout()->getUid(); if (true === array_key_exists($layout_uid, $attrvalue['layout'])) { $value = is_array($attrvalue['layout'][$layout_uid]) ? reset($attrvalue['layout'][$layout_uid]) : $attrvalue['layout'][$layout_uid]; $metadata->setAttribute($attrname, $value, $content); } } } } } }
/** * Parses definition and resolve value for $metadata. * * @param array $definition * @param MetaData $metadata * @param Page|NULL $page */ private function parseDefinition(array $definition, MetaData $metadata, Page $page = null) { foreach ($definition as $attrname => $attrvalue) { if (!is_array($attrvalue)) { $attrvalue = [$attrvalue]; } if (!$metadata->isComputed($attrname)) { continue; } $scheme = '' === $metadata->getAttribute($attrname) ? reset($attrvalue) : $metadata->getAttribute($attrname); if ('title' === $metadata->getName() && null !== $page && null !== $page->getTitle() && array_key_exists('default', $attrvalue)) { $scheme = $page->getTitle(); } elseif (array_key_exists('default', $attrvalue)) { $scheme = $attrvalue['default']; } if (null !== $page && null !== $page->getLayout() && array_key_exists('layout', $attrvalue) && array_key_exists($page->getLayout()->getUid(), $attrvalue['layout'])) { $scheme = $attrvalue['layout'][$page->getLayout()->getUid()]; } if (null !== $page && null !== $page->getLayout() && array_key_exists('layout', $attrvalue) && array_key_exists($page->getLayout()->getLabel(), $attrvalue['layout'])) { $scheme = $attrvalue['layout'][$page->getLayout()->getLabel()]; } $matches = []; $isComputed = true; if (null !== $page && preg_match(self::MATCH_PATTERN, $scheme)) { $value = $this->resolveScheme($scheme, $page); } elseif (preg_match(self::CONST_PATTERN, $scheme, $matches)) { $value = $this->resolveConst($matches[1], $page); } else { $value = $scheme; $scheme = null; $isComputed = false; } $metadata->setAttribute($attrname, $value, $scheme, $isComputed); } }