diff --git a/tests/unit/Grav/Common/MarkdownTest.php b/tests/unit/Grav/Common/MarkdownLinksTest.php similarity index 53% rename from tests/unit/Grav/Common/MarkdownTest.php rename to tests/unit/Grav/Common/MarkdownLinksTest.php index 3c5e7c664..9076a43d6 100644 --- a/tests/unit/Grav/Common/MarkdownTest.php +++ b/tests/unit/Grav/Common/MarkdownLinksTest.php @@ -34,20 +34,15 @@ class MarkdownTest extends \Codeception\TestCase\Test protected function _before() { $this->grav = Fixtures::get('grav'); - $this->pages = $this->grav['pages']; - $this->config = $this->grav['config']; - $this->uri = $this->grav['uri']; if (!self::$run) { - /** @var UniformResourceLocator $locator */ $locator = $this->grav['locator']; $locator->addPath('page', '', 'tests/fake/nested-site/user/pages', false); $this->pages->init(); - self::$run = true; } @@ -59,27 +54,13 @@ class MarkdownTest extends \Codeception\TestCase\Test 'special_chars' => ['>' => 'gt', '<' => 'lt'], ]; $page = $this->pages->dispatch('/item2/item2-2'); - $this->parsedown = new Parsedown($page, $defaults); - - - } protected function _after() { } - /** - * @param $string - * - * @return mixed - */ - public function stripLeadingWhitespace($string) - { - return preg_replace('/^\s*(.*)/', '', $string); - } - public function testAnchorLinksNoPortRelativeUrls() { $this->config->set('system.absolute_urls', false); @@ -170,6 +151,8 @@ class MarkdownTest extends \Codeception\TestCase\Test $this->parsedown->text('[Down a Level](item2-2-1)')); $this->assertSame('

Up a Level

', $this->parsedown->text('[Up a Level](..)')); +// $this->assertSame('

Up to Root Level

', +// $this->parsedown->text('[Up to Root Level](../..)')); $this->assertSame('

Up and Down

', $this->parsedown->text('[Up and Down](../../item3/item3-3)')); $this->assertSame('

Down a Level with Query

', @@ -184,6 +167,87 @@ class MarkdownTest extends \Codeception\TestCase\Test $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')); } + public function testSlugRelativeLinksAbsoluteUrls() + { + $this->config->set('system.absolute_urls', true); + $this->uri->initializeWithURL('http://localhost/item2/item-2-2')->init(); + + $this->assertSame('

Peer Page

', + $this->parsedown->text('[Peer Page](../item2-1)')); + $this->assertSame('

Down a Level

', + $this->parsedown->text('[Down a Level](item2-2-1)')); + $this->assertSame('

Up a Level

', + $this->parsedown->text('[Up a Level](..)')); +// $this->assertSame('

Up to Root Level

', +// $this->parsedown->text('[Up to Root Level](../..)')); + $this->assertSame('

Up and Down

', + $this->parsedown->text('[Up and Down](../../item3/item3-3)')); + $this->assertSame('

Down a Level with Query

', + $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')); +// $this->assertSame('

Up a Level with Query

', +// $this->parsedown->text('[Up a Level with Query](../?foo=bar)')); + $this->assertSame('

Up and Down with Query

', + $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')); + $this->assertSame('

Up and Down with Param

', + $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')); + $this->assertSame('

Up and Down with Anchor

', + $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')); + } + + public function testSlugRelativeLinksSubDir() + { + $this->config->set('system.absolute_urls', false); + $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item-2-2', '/subdir')->init(); + + $this->assertSame('

Peer Page

', + $this->parsedown->text('[Peer Page](../item2-1)')); + $this->assertSame('

Down a Level

', + $this->parsedown->text('[Down a Level](item2-2-1)')); + $this->assertSame('

Up a Level

', + $this->parsedown->text('[Up a Level](..)')); + $this->assertSame('

Up to Root Level

', + $this->parsedown->text('[Up to Root Level](../..)')); + $this->assertSame('

Up and Down

', + $this->parsedown->text('[Up and Down](../../item3/item3-3)')); + $this->assertSame('

Down a Level with Query

', + $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')); +// $this->assertSame('

Up a Level with Query

', +// $this->parsedown->text('[Up a Level with Query](../?foo=bar)')); + $this->assertSame('

Up and Down with Query

', + $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')); + $this->assertSame('

Up and Down with Param

', + $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')); + $this->assertSame('

Up and Down with Anchor

', + $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')); + } + + public function testSlugRelativeLinksSubDirAbsoluteUrls() + { + $this->config->set('system.absolute_urls', true); + $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item-2-2', '/subdir')->init(); + + $this->assertSame('

Peer Page

', + $this->parsedown->text('[Peer Page](../item2-1)')); + $this->assertSame('

Down a Level

', + $this->parsedown->text('[Down a Level](item2-2-1)')); + $this->assertSame('

Up a Level

', + $this->parsedown->text('[Up a Level](..)')); + $this->assertSame('

Up to Root Level

', + $this->parsedown->text('[Up to Root Level](../..)')); + $this->assertSame('

Up and Down

', + $this->parsedown->text('[Up and Down](../../item3/item3-3)')); + $this->assertSame('

Down a Level with Query

', + $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')); +// $this->assertSame('

Up a Level with Query

', +// $this->parsedown->text('[Up a Level with Query](../?foo=bar)')); + $this->assertSame('

Up and Down with Query

', + $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')); + $this->assertSame('

Up and Down with Param

', + $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')); + $this->assertSame('

Up and Down with Anchor

', + $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')); + } + public function testDirectoryRelativeLinks() { $this->config->set('system.absolute_urls', false); @@ -203,6 +267,7 @@ class MarkdownTest extends \Codeception\TestCase\Test // $this->parsedown->text('[Up and Down with Param](../../03.item3/03.item3-3/foo:bar)')); $this->assertSame('

Up and Down with Anchor

', $this->parsedown->text('[Up and Down with Anchor](../../03.item3/03.item3-3#foo)')); + } public function testDirectoryAbsoluteLinks() @@ -210,6 +275,8 @@ class MarkdownTest extends \Codeception\TestCase\Test $this->config->set('system.absolute_urls', false); $this->uri->initializeWithURL('http://localhost/item2/item-2-2')->init(); +// $this->assertSame('

Root

', +// $this->parsedown->text('[Root](/)')); $this->assertSame('

Peer Page

', $this->parsedown->text('[Peer Page](/item2/item2-1)')); $this->assertSame('

Down a Level

', @@ -222,6 +289,50 @@ class MarkdownTest extends \Codeception\TestCase\Test $this->parsedown->text('[With Param](/item2/foo:bar)')); $this->assertSame('

With Anchor

', $this->parsedown->text('[With Anchor](/item2#foo)')); + + } + + public function testDirectoryAbsoluteLinksSubDir() + { + $this->config->set('system.absolute_urls', false); + $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item-2-2', '/subdir')->init(); + + $this->assertSame('

Root

', + $this->parsedown->text('[Root](/)')); + $this->assertSame('

Peer Page

', + $this->parsedown->text('[Peer Page](/item2/item2-1)')); + $this->assertSame('

Down a Level

', + $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)')); + $this->assertSame('

Up a Level

', + $this->parsedown->text('[Up a Level](/item2)')); + $this->assertSame('

With Query

', + $this->parsedown->text('[With Query](/item2?foo=bar)')); + $this->assertSame('

With Param

', + $this->parsedown->text('[With Param](/item2/foo:bar)')); + $this->assertSame('

With Anchor

', + $this->parsedown->text('[With Anchor](/item2#foo)')); + + } + + public function testDirectoryAbsoluteLinksSubDirAbsoluteUrl() + { + $this->config->set('system.absolute_urls', true); + $this->uri->initializeWithUrlAndRootPath('http://localhost/subdir/item2/item-2-2', '/subdir')->init(); + + $this->assertSame('

Root

', + $this->parsedown->text('[Root](/)')); + $this->assertSame('

Peer Page

', + $this->parsedown->text('[Peer Page](/item2/item2-1)')); + $this->assertSame('

Down a Level

', + $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)')); + $this->assertSame('

Up a Level

', + $this->parsedown->text('[Up a Level](/item2)')); + $this->assertSame('

With Query

', + $this->parsedown->text('[With Query](/item2?foo=bar)')); + $this->assertSame('

With Param

', + $this->parsedown->text('[With Param](/item2/foo:bar)')); + $this->assertSame('

With Anchor

', + $this->parsedown->text('[With Anchor](/item2#foo)')); } public function testSpecialProtocols() @@ -268,4 +379,36 @@ class MarkdownTest extends \Codeception\TestCase\Test $this->parsedown->text('[google.com](https://www.google.com)')); } + public function testAttributeLinks() + { + $this->config->set('system.absolute_urls', false); + $this->uri->initializeWithURL('http://localhost/item2/item-2-2')->init(); + + $this->assertSame('

Relative Class

', + $this->parsedown->text('[Relative Class](../item2-3?classes=button)')); + $this->assertSame('

Relative ID

', + $this->parsedown->text('[Relative ID](../item2-3?id=unique)')); + $this->assertSame('

External

', + $this->parsedown->text('[External](https://github.com/getgrav/grav?classes=button,big)')); + $this->assertSame('

Relative Noprocess

', + $this->parsedown->text('[Relative Noprocess](../item2-3?id=unique&noprocess)')); + $this->assertSame('

Relative Target

', + $this->parsedown->text('[Relative Target](../item2-3?target=_blank)')); + $this->assertSame('

Relative Rel

', + $this->parsedown->text('[Relative Rel](../item2-3?rel=nofollow)')); + $this->assertSame('

Relative Mixed

', + $this->parsedown->text('[Relative Mixed](../item2-3?foo=bar&baz=qux&rel=nofollow&class=button)')); + } + + + /** + * @param $string + * + * @return mixed + */ + private function stripLeadingWhitespace($string) + { + return preg_replace('/^\s*(.*)/', '', $string); + } + }