Skip to content

Relativize path #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/PHPCR/Util/PathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ public static function absolutizePath($path, $context, $destination = false, $th
return self::normalizePath($path, $destination, $throw);
}

/**
* Make an absolute path relative to $context.
*
* ie. $context . '/' . PathHelper::relativePath($path, $context) === $path
*
* Input paths are assumed to be normalized.
*
* @param string $path The absolute path to a node
* @param string $context The absolute path to an ancestor of $path
* @param bool $throw Whether to throw exceptions on invalid data.
*
* @return string The relative path from $context to $path
*/
public static function relativizePath($path, $context, $throw = true)
{
if ($context !== substr($path, 0, strlen($context))) {
return self::error("$path is not within $context", $throw);
}

return ltrim(substr($path, strlen($context)), '/');
}

/**
* Get the parent path of a valid absolute path.
*
Expand Down
234 changes: 124 additions & 110 deletions tests/PHPCR/Tests/Util/PathHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,54 @@ class PathHelperTest extends \PHPUnit_Framework_TestCase
{
// assertValidPath tests

public function testAssertValidPath()
{
$this->assertTrue(PathHelper::assertValidAbsolutePath('/parent/child'));
}

public function testAssertValidPathRoot()
{
$this->assertTrue(PathHelper::assertValidAbsolutePath('/'));
}

public function testAssertValidPathNamespaced()
{
$this->assertTrue(PathHelper::assertValidAbsolutePath('/jcr:foo_/b-a/0^.txt'));
}

public function testAssertValidPathIndexed()
{
$this->assertTrue(PathHelper::assertValidAbsolutePath('/parent[7]/child'));
}

public function testAssertValidPathIndexedAtEnd()
{
$this->assertTrue(PathHelper::assertValidAbsolutePath('/parent[7]/child[3]'));
}

/**
* @expectedException \PHPCR\RepositoryException
* @dataProvider dataproviderValidAbsolutePaths
*/
public function testAssertValidTargetPathNoIndex()
public function testAssertValidAbsolutePath($path, $destination = false)
{
PathHelper::assertValidAbsolutePath('/parent/child[7]', true);
$this->assertTrue(PathHelper::assertValidAbsolutePath($path, $destination));
}

/**
* @expectedException \PHPCR\RepositoryException
*/
public function testAssertValidPathNotAbsolute()
public function dataproviderValidAbsolutePaths()
{
PathHelper::assertValidAbsolutePath('parent');
}

/**
* @expectedException \PHPCR\RepositoryException
*/
public function testAssertValidPathDouble()
{
PathHelper::assertValidAbsolutePath('/parent//child');
}

/**
* @expectedException \PHPCR\RepositoryException
*/
public function testAssertValidPathParent()
{
PathHelper::assertValidAbsolutePath('/parent/../child');
return array(
array('/parent/child'),
array('/'),
array('/jcr:foo_/b-a/0^.txt'),
array('/parent[7]/child'),
array('/parent[7]/child', true), // index is allowed in destination parent path, only not in last element
array('/parent[7]/child[3]'),
);
}

/**
* @dataProvider dataproviderInvalidAbsolutePaths
* @expectedException \PHPCR\RepositoryException
*/
public function testAssertValidPathSelf()
public function testAssertInvalidAbsolutePath($path, $destination = false)
{
PathHelper::assertValidAbsolutePath('/parent/./child');
PathHelper::assertValidAbsolutePath($path, $destination);
}

/**
* @expectedException \PHPCR\RepositoryException
* @dataProvider dataproviderInvalidAbsolutePaths
*/
public function testAssertValidPathTrailing()
public function testAssertInvalidAbsolutePathNoThrow($path, $destination = false)
{
PathHelper::assertValidAbsolutePath('/parent/child/');
$this->assertFalse(PathHelper::assertValidAbsolutePath($path, $destination, false));
}

public function testAssertValidPathNoThrow()
public function dataproviderInvalidAbsolutePaths()
{
$this->assertFalse(PathHelper::assertValidAbsolutePath('parent', false, false));
return array(
array('/parent/child[7]', true), // destination last element with index
array('parent'), // not absolute
array('/parent//child'),
array('//'),
array('/parent/../child'),
array('/parent/./child'),
array('/parent/child/'),
);
}

// assertValidLocalName tests
Expand All @@ -99,35 +71,22 @@ public function testAssertValidLocalNameRootnode()
}

/**
* @dataProvider dataproviderInvalidLocalNames
* @expectedException \PHPCR\RepositoryException
*/
public function testAssertValidLocalNameNamespaced()
public function testAssertInvalidLocalName($name)
{
$this->assertTrue(PathHelper::assertValidLocalName('jcr:nodename'));
PathHelper::assertValidLocalName($name);
}

/**
* @expectedException \PHPCR\RepositoryException
*/
public function testAssertValidLocalNamePath()
{
$this->assertTrue(PathHelper::assertValidLocalName('/path'));
}

/**
* @expectedException \PHPCR\RepositoryException
*/
public function testAssertValidLocalNameSelf()
public function dataproviderInvalidLocalNames()
{
PathHelper::assertValidLocalName('.');
}

/**
* @expectedException \PHPCR\RepositoryException
*/
public function testAssertValidLocalNameParent()
{
PathHelper::assertValidLocalName('..');
return array(
array('jcr:nodename'),
array('/path'),
array('.'),
array('..')
);
}

// normalizePath tests
Expand All @@ -151,17 +110,6 @@ public static function dataproviderNormalizePath()
);
}

public static function dataproviderNormalizePathInvalid()
{
return array(
array('foo/bar'),
array('bar'),
array('/foo/bar/'),
array(''),
array(new \stdClass()),
);
}

/**
* @dataProvider dataproviderNormalizePathInvalid
* @expectedException \PHPCR\RepositoryException
Expand All @@ -179,6 +127,17 @@ public function testNormalizePathInvalidNoThrow($input)
$this->assertFalse(PathHelper::normalizePath($input, true, false));
}

public static function dataproviderNormalizePathInvalid()
{
return array(
array('foo/bar'),
array('bar'),
array('/foo/bar/'),
array(''),
array(new \stdClass()),
);
}

// absolutizePath tests

/**
Expand Down Expand Up @@ -227,59 +186,114 @@ public static function dataproviderAbsolutizePathInvalid()
);
}

// getParentPath tests
// relativizePath tests

public function testGetParentPath()
/**
* @dataProvider dataproviderRelativizePath
*/
public function testRelativizePath($inputPath, $context, $outputPath)
{
$this->assertEquals('/parent', PathHelper::getParentPath('/parent/child'));
$this->assertSame($outputPath, PathHelper::relativizePath($inputPath, $context));
}

public function testGetParentPathNamespaced()
public static function dataproviderRelativizePath()
{
$this->assertEquals('/jcr:parent', PathHelper::getParentPath('/jcr:parent/ns:child'));
return array(
array('/parent/path/child', '/parent', 'path/child'),
array('/child', '/', 'child'),
);
}

public function testGetParentPathNodeAtRoot()
/**
* @expectedException \PHPCR\RepositoryException
* @dataProvider dataproviderRelativizePathInvalid
*/
public function testRelativizePathInvalidThrow($inputPath, $context)
{
$this->assertEquals('/', PathHelper::getParentPath('/parent'));
PathHelper::relativizePath($inputPath, $context);
}

public function testGetParentPathRoot()
/**
* @dataProvider dataproviderRelativizePathInvalid
*/
public function testRelativizePathInvalidNoThrow($inputPath, $context)
{
$this->assertEquals('/', PathHelper::getParentPath('/'));
$this->assertFalse(PathHelper::relativizePath($inputPath, $context, false));
}

public function provideGetNodeName()
public static function dataproviderRelativizePathInvalid()
{
return array(
array('/parent/child', 'child'),
array('/parent/ns:child', 'ns:child'),
array('/', ''),
array('/path', '/context'),
array('/parent', '/parent/child'),
);
}

// getParentPath tests

/**
* @dataProvider provideGetNodeName
* @dataProvider dataproviderParentPath
*/
public function testGetParentPath($path, $parent)
{
$this->assertEquals($parent, PathHelper::getParentPath($path));
}

public function dataproviderParentPath()
{
return array(
array('/parent/child', '/parent'),
array('/jcr:parent/ns:child', '/jcr:parent'),
array('/child', '/'),
array('/', '/'),
);
}

// getNodeName tests

/**
* @dataProvider dataproviderGetNodeName
*/
public function testGetNodeName($path, $expected = null)
{
$this->assertEquals($expected, PathHelper::getNodeName($path));
}

public function dataproviderGetNodeName()
{
return array(
array('/parent/child', 'child'),
array('/parent/ns:child', 'ns:child'),
array('/', ''),
);
}

/**
* @expectedException PHPCR\RepositoryException
* @expectedException \PHPCR\RepositoryException
* @expectedExceptionMessage must be an absolute path
*/
public function testGetNodeNameMustBeAbsolute()
{
PathHelper::getNodeName('foobar');
}

public function testGetPathDepth()
// getPathDepth tests

/**
* @dataProvider dataproviderPathDepth
*/
public function testGetPathDepth($path, $depth)
{
$this->assertEquals($depth, PathHelper::getPathDepth($path));
}

public function dataproviderPathDepth()
{
$this->assertEquals(0, PathHelper::getPathDepth('/'));
$this->assertEquals(1, PathHelper::getPathDepth('/foo'));
$this->assertEquals(2, PathHelper::getPathDepth('/foo/bar'));
$this->assertEquals(2, PathHelper::getPathDepth('/foo/bar/'));
return array(
array('/', 0),
array('/foo', 1),
array('/foo/bar', 2),
array('/foo/bar/', 2),
);
}
}