diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 0461aa4a..7e58b21b 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -368,16 +368,30 @@ public function thereShouldExistANodeAt($arg1) public function thereShouldExistANodeAtBefore($arg1, $arg2) { $session = $this->getSession(); + try { - $node = $session->getNode($arg1); + $node = $session->getNode($arg2); } catch (PathNotFoundException $e) { throw new \Exception('Node does at path ' . $arg1 . ' does not exist.'); } - $parent = $session->getNode(PathHelper::getParentPath($arg1)); - $index = $node->getIndex(); + + $parent = $session->getNode(PathHelper::getParentPath($arg2)); $parentChildren = array_values((array) $parent->getNodes()); - $beforeNode = $parentChildren[$index]; - PHPUnit_Framework_Assert::assertEquals($arg2, $beforeNode->getName()); + $targetNode = null; + + foreach ($parentChildren as $i => $parentChild) { + if ($parentChild->getPath() == $arg1) { + $targetNode = $parentChild; + $afterNode = $parentChildren[$i + 1]; + break; + } + } + + if (null === $targetNode) { + throw new \Exception('Could not find child node ' . $arg1); + } + + PHPUnit_Framework_Assert::assertEquals($arg2, $afterNode->getPath()); } /** diff --git a/features/node_reorder_before.feature b/features/node_reorder_before.feature index 892567cc..7508cf93 100644 --- a/features/node_reorder_before.feature +++ b/features/node_reorder_before.feature @@ -8,7 +8,8 @@ Feature: Reorder a node And the "session_data.xml" fixtures are loaded Scenario: Reorder a node - Given the cnp is "/tests_general_base" - And I execute the "node:order-before unversionable" command + Given the current node is "/tests_general_base" + And I execute the "node:order-before emptyExample idExample" command Then the command should not fail - And there should exist a node at "/tests_general_base/simpleVersioned" before "/tests_general_base/unversionable" + And I save the session + And there should exist a node at "/tests_general_base/emptyExample" before "/tests_general_base/idExample" diff --git a/src/PHPCR/Shell/Console/Application/ShellApplication.php b/src/PHPCR/Shell/Console/Application/ShellApplication.php index 67f591d6..4a9e9a3f 100644 --- a/src/PHPCR/Shell/Console/Application/ShellApplication.php +++ b/src/PHPCR/Shell/Console/Application/ShellApplication.php @@ -79,6 +79,7 @@ use PHPCR\Shell\Console\Command\NodeRenameCommand; use PHPCR\Shell\Console\Command\NodeMixinAddCommand; use PHPCR\Shell\Console\Command\NodeMixinRemoveCommand; +use PHPCR\Shell\Console\Command\NodeOrderBeforeCommand; use PHPCR\Shell\Console\Command\NodeInfoCommand; use PHPCR\Shell\Console\Command\NodeLifecycleFollowCommand; use PHPCR\Shell\Console\Command\NodeLifecycleListCommand; @@ -205,6 +206,7 @@ public function init() $this->add(new NodeRenameCommand()); $this->add(new NodeMixinAddCommand()); $this->add(new NodeMixinRemoveCommand()); + $this->add(new NodeOrderBeforeCommand()); $this->add(new NodeInfoCommand()); $this->add(new NodeLifecycleFollowCommand()); $this->add(new NodeLifecycleListCommand()); diff --git a/src/PHPCR/Shell/Console/Command/NodeOrderBeforeCommand.php b/src/PHPCR/Shell/Console/Command/NodeOrderBeforeCommand.php new file mode 100644 index 00000000..6d295f55 --- /dev/null +++ b/src/PHPCR/Shell/Console/Command/NodeOrderBeforeCommand.php @@ -0,0 +1,51 @@ +setName('node:order-before'); + $this->setDescription(''); + $this->addArgument('srcChildRelPath', null, InputArgument::REQUIRED, 'The relative path to the child node to be moved in the ordering'); + $this->addArgument('destChildRelPath', null, InputArgument::REQUIRED, 'The relative path to the child before which the node srcChildRelPath will be placed'); + $this->setHelp(<<srcChildRelPath into the child node list at the position +immediately before destChildRelPath + +To place the node srcChildRelPath at the end of the list, a +destChildRelPath of null is used. + +Note that (apart from the case where destChildRelPath is null) both of +these arguments must be relative paths of depth one, in other words they +are the names of the child nodes, possibly suffixed with an index. + +If srcChildRelPath and destChildRelPath are the same, then no change is +made. + +This is session-write method, meaning that a change made by this method +is dispatched on save. +HERE + ); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $session = $this->getHelper('phpcr')->getSession(); + $srcChildRelPath = $input->getArgument('srcChildRelPath'); + $destChildRelPath = $input->getArgument('destChildRelPath'); + $node = $session->getCurrentNode(); + $node->orderBefore($srcChildRelPath, $destChildRelPath); + } +}