-
-
Save sbruggmann/e0b106fc8b148d7cedb8e84a74748ab5 to your computer and use it in GitHub Desktop.
Neos.RedirectHandler.Test RedirectCommandController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Neos\RedirectHandler\Test\Command; | |
use Neos\Flow\Annotations as Flow; | |
use Neos\Flow\Cli\CommandController; | |
use Neos\ContentRepository\Domain\Model\NodeInterface; | |
use Neos\Neos\Domain\Service\SiteService; | |
use Neos\Neos\Domain\Service\ContentDimensionPresetSourceInterface; | |
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface; | |
use Neos\Eel\FlowQuery\FlowQuery; | |
use Neos\ContentRepository\Domain\Service\NodeTypeManager; | |
use Neos\Flow\Persistence\PersistenceManagerInterface; | |
use Neos\Neos\Service\PublishingService; | |
use Neos\ContentRepository\Domain\Model\Workspace; | |
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository; | |
/** | |
* @Flow\Scope("singleton") | |
*/ | |
class RedirectCommandController extends CommandController | |
{ | |
/** | |
* @Flow\Inject | |
* @var ContentDimensionPresetSourceInterface | |
*/ | |
protected $contentDimensionPresetSource; | |
/** | |
* @Flow\Inject | |
* @var ContextFactoryInterface | |
*/ | |
protected $contextFactory; | |
/** | |
* @Flow\Inject | |
* @var NodeTypeManager | |
*/ | |
protected $nodeTypeManager; | |
/** | |
* @Flow\Inject | |
* @var PersistenceManagerInterface | |
*/ | |
protected $persistenceManager; | |
/** | |
* @Flow\Inject | |
* @var PublishingService | |
*/ | |
protected $publishingService; | |
/** | |
* @Flow\Inject | |
* @var WorkspaceRepository | |
*/ | |
protected $workspaceRepository; | |
/** | |
* Neos RedirectHandler Test | |
* | |
* @param string $sitePathSegmentName | |
* @param string $workspaceName | |
* @param string $nodeTypeName | |
* @return void | |
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException | |
* @throws \Neos\Eel\Exception | |
*/ | |
public function testCommand($sitePathSegmentName, $workspaceName = 'import', $nodeTypeName = 'Neos.Demo:Chapter') | |
{ | |
/** @var NodeInterface $siteNode */ | |
$siteNode = $this->getStorageNode($sitePathSegmentName, $workspaceName); | |
$flowQuery = new FlowQuery([$siteNode]); | |
$nodeNodeType = $this->nodeTypeManager->getNodeType($nodeTypeName); | |
/** @var NodeInterface $node */ | |
$node = $flowQuery->find('[instanceof ' . $nodeTypeName . ']')->get(0); | |
$isNew = false; | |
if (!$node) { | |
$isNew = true; | |
$node = $siteNode->createSingleNode(uniqid('node-'), $nodeNodeType); | |
$node->setProperty('title', 'A'); | |
$this->outputLine('Created Node "A"'); | |
} else { | |
$node = $node->createVariantForContext($siteNode->getContext()); | |
$node->setProperty('title', $node->getProperty('title') . 'a'); | |
$this->outputLine('Renamed Node "' . substr($node->getProperty('title'), 0, | |
-1) . '" to "' . $node->getProperty('title') . '"'); | |
} | |
$this->persistenceManager->persistAll(); | |
/** @var Workspace $workspace */ | |
$workspace = $this->workspaceRepository->findByIdentifier($workspaceName); | |
$this->publishingService->publishNodes($this->publishingService->getUnpublishedNodes($workspace)); | |
return; | |
} | |
/** | |
* @param $sitePathSegmentName | |
* @param $workspaceName | |
* @return mixed | |
* @throws \Neos\Eel\Exception | |
*/ | |
private function getStorageNode($sitePathSegmentName, $workspaceName = 'import') | |
{ | |
$defaultLanguage = $this->contentDimensionPresetSource->getDefaultPreset('language'); | |
$contextProperties = array( | |
'workspaceName' => 'live', | |
'dimensions' => $defaultLanguage['values'], | |
'invisibleContentShown' => true, | |
'inaccessibleContentShown' => true, | |
); | |
$baseContext = $this->contextFactory->create($contextProperties); | |
$baseContextSitesNode = $baseContext->getNode(SiteService::SITES_ROOT_PATH); | |
$flowQuery = new FlowQuery([$baseContextSitesNode]); | |
/** @var NodeInterface $node */ | |
$node = $flowQuery->children($sitePathSegmentName)->get(0); | |
if ($workspaceName !== 'live') { | |
$liveContextProperties = array( | |
'workspaceName' => $workspaceName, | |
'dimensions' => $defaultLanguage['values'], | |
'invisibleContentShown' => true, | |
'inaccessibleContentShown' => true, | |
); | |
$liveBaseContext = $this->contextFactory->create($liveContextProperties); | |
return $node->createVariantForContext($liveBaseContext); | |
} else { | |
return $node; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment