fix filter & escaping

This commit is contained in:
DavidBadura 2015-04-23 16:49:41 +00:00
parent 682561a58e
commit d392b5871e
2 changed files with 45 additions and 10 deletions

View File

@ -117,7 +117,7 @@ class Taskwarrior
$file = tempnam(sys_get_temp_dir(), 'task') . '.json'; $file = tempnam(sys_get_temp_dir(), 'task') . '.json';
$fs->dumpFile($file, $json); $fs->dumpFile($file, $json);
$output = $this->command('import', $file); $output = $this->command('import', null, [$file]);
$fs->remove($file); $fs->remove($file);
@ -153,11 +153,7 @@ class Taskwarrior
} }
if ($filter) { if ($filter) {
if (strpos($filter, '(') !== false) { $parts[] = "( " . $filter . ' )';
$parts[] = "'" . $filter . "'";
} else {
$parts[] = $filter;
}
} }
$parts[] = $command; $parts[] = $command;
@ -166,7 +162,7 @@ class Taskwarrior
$parts[] = $param; $parts[] = $param;
} }
$process = new Process(implode(' ', $parts)); $process = new Process($this->createCommandLine($parts));
$process->run(); $process->run();
if (!$process->isSuccessful()) { if (!$process->isSuccessful()) {
@ -181,11 +177,11 @@ class Taskwarrior
*/ */
public function version() public function version()
{ {
if ($this->version) { if (!$this->version) {
return $this->version; $this->version = trim($this->command('_version'));
} }
return $this->version = trim($this->command('_version')); return $this->version;
} }
/** /**
@ -261,6 +257,19 @@ class Taskwarrior
return array_filter(explode("\n", $string), 'strlen'); return array_filter(explode("\n", $string), 'strlen');
} }
/**
* @param array $parts
* @return string
*/
private function createCommandLine(array $parts)
{
$parts = array_map(function($part) {
return "'" . str_replace("'", "'\\''", $part) . "'";
}, $parts);
return implode(' ', $parts);
}
/** /**
* @param string $string * @param string $string
* @return string|null * @return string|null

View File

@ -69,6 +69,32 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($task, $result); $this->assertEquals($task, $result);
} }
public function testUrlInDescription()
{
$task = new Task();
$task->setDescription('hier http://google.de ');
$this->taskManager->save($task);
$this->taskManager->clear();
$result = $this->taskManager->find($task->getUuid());
$this->assertEquals('hier http://google.de ', $result->getDescription());
}
public function testQuoteInDescription()
{
$task = new Task();
$task->setDescription(" test ' foo ");
$this->taskManager->save($task);
$this->taskManager->clear();
$result = $this->taskManager->find($task->getUuid());
$this->assertEquals(" test ' foo ", $result->getDescription());
}
public function testSaveTaskWithoutDescription() public function testSaveTaskWithoutDescription()
{ {
$this->setExpectedException('DavidBadura\Taskwarrior\Exception\TaskwarriorException'); $this->setExpectedException('DavidBadura\Taskwarrior\Exception\TaskwarriorException');