Skip to content

Commit 66939f6

Browse files
committed
Node type creation and proper editor testing
1 parent e7529fc commit 66939f6

File tree

6 files changed

+138
-41
lines changed

6 files changed

+138
-41
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,34 @@ public function theNodeTypeIsLoaded($arg1)
489489
$nodeTypeManager->registerNodeTypesCnd($cnd, true);
490490
$session->save();
491491
}
492+
493+
/**
494+
* @Given /^there should exist a node type called "([^"]*)"$/
495+
*/
496+
public function thereShouldExistANodeTypeCalled($arg1)
497+
{
498+
$session = $this->getSession();
499+
$workspace = $session->getWorkspace();
500+
$nodeTypeManager = $workspace->getNodeTypeManager();
501+
$nodeTypeManager->getNodeType($arg1);
502+
}
503+
504+
/**
505+
* @Given /^I have an editor which produces the following:$/
506+
*/
507+
public function iHaveAnEditorWhichProducesTheFollowing(PyStringNode $string)
508+
{
509+
$tmpFile = $this->workingDir . DIRECTORY_SEPARATOR . 'fake-editor-file';
510+
$editorFile = $this->workingDir . DIRECTORY_SEPARATOR . 'fakeed';
511+
file_put_contents($tmpFile, $string->getRaw());
512+
chmod($tmpFile, 0777);
513+
$script = array();
514+
$script[] = '#!/bin/bash';
515+
$script[] = 'FILE=$1';
516+
$script[] = 'cat ' . $tmpFile . ' > $FILE';
517+
518+
file_put_contents($editorFile, implode("\n", $script));
519+
chmod($editorFile, 0777);
520+
putenv('EDITOR=' . $editorFile);
521+
}
492522
}

features/node_type_edit.feature

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,41 @@ Feature: Edit a node type
1616
| command |
1717
| node-type:edit ns:NodeType |
1818

19-
Scenario: Make an invalid CND file
20-
Given the "EDITOR" environment variable is set to "tac"
19+
Scenario: Edit an existing node type
20+
Given the "EDITOR" environment variable is set to "cat"
2121
And I execute the "node-type:edit rep:versionStorage --no-interaction" command
2222
Then the command should fail
2323
And I should see the following:
2424
"""
2525
can't reregister built-in node type
2626
"""
27+
28+
Scenario: Make an invalid edit
29+
Given I have an editor which produces the following:
30+
""""
31+
asdf asdf asdf
32+
"""
33+
And I execute the "node-type:edit ns:NodeType --no-interaction" command
34+
Then the command should fail
35+
And I should see the following:
36+
"""
37+
PARSER ERROR
38+
"""
39+
40+
Scenario: Create a new node type
41+
Given I have an editor which produces the following:
42+
"""
43+
<ns ='http://namespace.com/ns'>
44+
[ns:somenewtype] > nt:unstructured
45+
"""
46+
And I execute the "node-type:edit ns:somenewtype --no-interaction" command
47+
Then the command should not fail
48+
And there should exist a node type called "ns:somenewtype"
49+
50+
Scenario: Empty the node type
51+
Given I have an editor which produces the following:
52+
"""
53+
"""
54+
And I execute the "node-type:edit ns:NodeType --no-interaction" command
55+
Then the command should not fail
56+
And there should exist a node type called "ns:somenewtype"

src/PHPCR/Shell/Console/Command/NodeTypeEditCommand.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,24 @@
99
use PHPCR\Util\CND\Writer\CndWriter;
1010
use PHPCR\NodeType\NoSuchNodeTypeException;
1111
use PHPCR\Util\CND\Parser\CndParser;
12+
use PHPCR\NamespaceException;
1213

1314
class NodeTypeEditCommand extends Command
1415
{
1516
protected function configure()
1617
{
1718
$this->setName('node-type:edit');
18-
$this->setDescription('Edit the current node');
19-
$this->addArgument('nodeTypeName', null, InputArgument::REQUIRED, 'The name of the node type to edit');
19+
$this->setDescription('Edit or create a node type');
20+
$this->addArgument('nodeTypeName', null, InputArgument::REQUIRED, 'The name of the node type to edit or create');
2021
$this->setHelp(<<<HERE
2122
Edit the given node type name with the editor defined in the EDITOR environment variable.
23+
24+
If the node type does not exist, it will be created. All node types must be prefixed with
25+
a namespace prefix as shown in the <info>session:namespace:list</info> command
26+
27+
$ node-type:edit nt:examplenode
28+
29+
Will open an editor with a new node type.
2230
HERE
2331
);
2432
}
@@ -35,22 +43,49 @@ public function execute(InputInterface $input, OutputInterface $output)
3543

3644
try {
3745
$nodeType = $nodeTypeManager->getNodeType($nodeTypeName);
46+
$cndWriter = new CndWriter($namespaceRegistry);
47+
$out = $cndWriter->writeString(array($nodeType));
48+
$message = null;
3849
} catch (NoSuchNodeTypeException $e) {
39-
throw new \Exception(sprintf(
40-
'The node type "%s" does not exist'
41-
, $nodeTypeName));
50+
$parts = explode(':', $nodeTypeName);
51+
52+
if (count($parts) != 2) {
53+
throw new \InvalidArgumentException(
54+
'Node type names must be prefixed with a namespace, e.g. ns:foobar'
55+
);
56+
}
57+
list($namespace, $name) = $parts;
58+
$uri = $session->getNamespaceURI($namespace);
59+
60+
// so we will create one ..
61+
$out = <<<EOT
62+
<$namespace ='$uri'>
63+
[$namespace:$name] > nt:unstructured
64+
65+
EOT
66+
;
67+
$message = <<<EOT
68+
Creating a new node type: $nodeTypeName
69+
EOT
70+
;
4271
}
43-
$cndWriter = new CndWriter($namespaceRegistry);
44-
$out = $cndWriter->writeString(array($nodeType));
45-
$res = $editor->fromString($out);
72+
4673

4774
$valid = false;
48-
while (false === $valid) {
75+
$prefix = '# ';
76+
do {
77+
$res = $editor->fromStringWithMessage($out, $message);
78+
79+
if (empty($res)) {
80+
$output->writeln('<info>Editor emptied the CND file, doing nothing. Use node-type:delete to remove node types.</info>');
81+
return 0;
82+
}
83+
4984
try {
5085
$cndParser = new CndParser($nodeTypeManager);
51-
$res = $cndParser->parseString($res);
86+
$namespacesAndNodeTypes = $cndParser->parseString($res);
5287

53-
foreach ($res['nodeTypes'] as $nodeType) {
88+
foreach ($namespacesAndNodeTypes['nodeTypes'] as $nodeType) {
5489
$nodeTypeManager->registerNodeType($nodeType, true);
5590
}
5691
$valid = true;
@@ -66,13 +101,12 @@ public function execute(InputInterface $input, OutputInterface $output)
66101
return 1;
67102
}
68103

69-
$prefix = '# ';
70104
$message = 'The following errors were encountered (all lines starting with ' . $prefix . ' will be ignored):';
71105
$message .= PHP_EOL;
72106
$message .= PHP_EOL;
73107
$message .= $e->getMessage();
74-
$editor->fromStringWithMessage($res, $message, $prefix);
108+
$out = $res;
75109
}
76-
}
110+
} while (false === $valid);
77111
}
78112
}

src/PHPCR/Shell/Console/Command/RetentionPolicyRemoveCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ protected function configure()
2323

2424
public function execute(InputInterface $input, OutputInterface $output)
2525
{
26-
$session = $this->removeHelper('phpcr')->removeSession();
27-
$retentionManager = $session->removeRetentionManager();
26+
$session = $this->getHelper('phpcr')->getSession();
27+
$retentionManager = $session->getRetentionManager();
2828
$absPath = $input->removeArgument('absPath');
2929

30-
$policy = $retentionManager->removeRetentionPolicy($absPath);
30+
$policy = $retentionManager->getRetentionPolicy($absPath);
3131
if (!$policy) {
3232
$output->writeln('No retention policy');
3333
} else {
34-
$output->writeln($policy->removeName());
34+
$output->writeln($policy->remove());
3535
}
3636
}
3737
}

src/PHPCR/Shell/Console/Helper/EditorHelper.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ public function fromString($string)
4747

4848
public function fromStringWithMessage($string, $message, $messagePrefix = '# ')
4949
{
50-
$message = explode("\n", $message);
51-
52-
foreach ($message as $line) {
53-
$source[] = $messagePrefix.$line;
50+
if (null !== $message) {
51+
$message = explode("\n", $message);
52+
53+
foreach ($message as $line) {
54+
$source[] = $messagePrefix.$line;
55+
}
56+
$source = implode("\n", $source).PHP_EOL;
57+
} else {
58+
$source = '';
5459
}
5560

56-
$source = implode("\n", $source);
57-
$source .= PHP_EOL.$string;
61+
$source .= $string;
5862

5963
$res = $this->fromString($source);
6064
$res = explode("\n", $res);
@@ -72,7 +76,6 @@ public function fromStringWithMessage($string, $message, $messagePrefix = '# ')
7276
$line = next($res);
7377
}
7478

75-
7679
return implode("\n", $out);
7780
}
7881

tests/PHPCR/Shell/Helper/TextHelperTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,51 @@ public function setUp()
1515

1616
public function provideTruncate()
1717
{
18-
return [
19-
[
18+
return array(
19+
array(
2020
'this is some text',
2121
5,
2222
null,
2323
null,
2424
'th...'
25-
],
26-
[
25+
),
26+
array(
2727
'this is some text',
2828
5,
2929
'right',
3030
null,
3131
'...xt',
32-
],
33-
[
32+
),
33+
array(
3434
'this is some text',
3535
5,
3636
'right',
3737
'-',
3838
'-text',
39-
],
40-
[
39+
),
40+
array(
4141
'th',
4242
5,
4343
'right',
4444
'-',
4545
'th',
46-
],
47-
[
46+
),
47+
array(
4848
'this is some more text',
4949
5,
5050
'right',
5151
'-----',
5252
'-----',
53-
],
54-
[
53+
),
54+
array(
5555
'this is some more text',
5656
5,
5757
'right',
5858
'--------',
5959
'-----',
6060
'Delimiter length "8" cannot be greater',
61-
],
62-
];
61+
),
62+
);
6363
}
6464

6565
/**

0 commit comments

Comments
 (0)