2010-08-01 06:46:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-08 11:36:16 +02:00
|
|
|
* @package docsviewer
|
2010-08-01 06:46:26 +02:00
|
|
|
*/
|
|
|
|
class DocumentationParserTest extends SapphireTest {
|
2010-08-01 06:46:37 +02:00
|
|
|
|
2012-05-17 23:48:57 +02:00
|
|
|
function testGenerateHtmlId() {
|
|
|
|
$this->assertEquals('title-one', DocumentationParser::generate_html_id('title one'));
|
|
|
|
$this->assertEquals('title-one', DocumentationParser::generate_html_id('Title one'));
|
|
|
|
$this->assertEquals('title-and-one', DocumentationParser::generate_html_id('Title & One'));
|
|
|
|
$this->assertEquals('title-and-one', DocumentationParser::generate_html_id('Title & One'));
|
|
|
|
$this->assertEquals('title-one', DocumentationParser::generate_html_id(' Title one '));
|
|
|
|
$this->assertEquals('title-one', DocumentationParser::generate_html_id('Title--one'));
|
|
|
|
}
|
|
|
|
|
2011-01-10 10:18:44 +01:00
|
|
|
function testRewriteCodeBlocks() {
|
2011-01-14 04:29:14 +01:00
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('test.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('mymodule', '2.4', DOCSVIEWER_PATH . '/tests/docs/'));
|
2011-01-14 04:29:14 +01:00
|
|
|
$page->setLang('en');
|
|
|
|
$page->setVersion('2.4');
|
|
|
|
$result = DocumentationParser::rewrite_code_blocks($page->getMarkdown());
|
|
|
|
$expected = <<<HTML
|
2011-01-10 10:18:44 +01:00
|
|
|
<pre class="brush: php">
|
|
|
|
code block
|
|
|
|
with multiple
|
|
|
|
lines
|
|
|
|
and tab indent
|
2011-01-10 11:59:22 +01:00
|
|
|
and escaped < brackets
|
2011-01-10 10:18:44 +01:00
|
|
|
</pre>
|
2011-01-10 11:59:22 +01:00
|
|
|
|
|
|
|
Normal text after code block
|
2011-01-10 10:18:44 +01:00
|
|
|
HTML;
|
|
|
|
|
2011-01-14 04:29:14 +01:00
|
|
|
$this->assertContains($expected, $result, 'Custom code blocks with ::: prefix');
|
|
|
|
|
|
|
|
$expected = <<<HTML
|
2011-01-10 11:59:22 +01:00
|
|
|
<pre>
|
|
|
|
code block
|
|
|
|
without formatting prefix
|
|
|
|
</pre>
|
|
|
|
HTML;
|
2011-01-14 04:29:14 +01:00
|
|
|
$this->assertContains($expected, $result, 'Traditional markdown code blocks');
|
|
|
|
}
|
2010-08-01 23:13:40 +02:00
|
|
|
|
2010-08-01 10:25:00 +02:00
|
|
|
function testImageRewrites() {
|
|
|
|
// Page on toplevel
|
2010-10-21 22:27:23 +02:00
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('subfolder/subpage.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('mymodule', '2.4', DOCSVIEWER_PATH . '/tests/docs/'));
|
2010-10-21 22:27:23 +02:00
|
|
|
$page->setLang('en');
|
|
|
|
$page->setVersion('2.4');
|
|
|
|
|
2010-08-01 10:25:00 +02:00
|
|
|
$result = DocumentationParser::rewrite_image_links($page->getMarkdown(), $page, 'mycontroller/cms/2.4/en/');
|
2010-12-22 09:21:49 +01:00
|
|
|
|
2010-08-01 10:25:00 +02:00
|
|
|
$this->assertContains(
|
2012-04-08 11:36:16 +02:00
|
|
|
'[relative image link](' . Director::absoluteBaseURL() .'/'. DOCSVIEWER_DIR . '/tests/docs/en/subfolder/_images/image.png)',
|
2010-08-01 10:25:00 +02:00
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
2012-04-08 11:36:16 +02:00
|
|
|
'[parent image link](' . Director::absoluteBaseURL() . '/'. DOCSVIEWER_DIR. '/tests/docs/en/_images/image.png)',
|
2010-08-01 10:25:00 +02:00
|
|
|
$result
|
|
|
|
);
|
2012-04-08 11:23:49 +02:00
|
|
|
|
2010-08-01 10:25:00 +02:00
|
|
|
// $this->assertContains(
|
2012-04-08 11:36:16 +02:00
|
|
|
// '[absolute image link](' . Director::absoluteBaseURL() . '/'. DOCSVIEWER_DIR. '/tests/docs/en/_images/image.png)',
|
2012-04-08 11:23:49 +02:00
|
|
|
// $result
|
2010-08-01 10:25:00 +02:00
|
|
|
// );
|
|
|
|
}
|
|
|
|
|
2010-08-01 06:46:37 +02:00
|
|
|
function testApiLinks() {
|
|
|
|
// Page on toplevel
|
2010-10-21 22:27:23 +02:00
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('test.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('mymodule', '2.4', DOCSVIEWER_PATH .'/tests/docs/'));
|
2010-10-21 22:27:23 +02:00
|
|
|
$page->setLang('en');
|
|
|
|
$page->setVersion('2.4');
|
|
|
|
|
|
|
|
|
2010-08-01 06:46:37 +02:00
|
|
|
$result = DocumentationParser::rewrite_api_links($page->getMarkdown(), $page, 'mycontroller/cms/2.4/en/');
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: api](http://api.silverstripe.org/search/lookup/?q=DataObject&version=2.4&module=mymodule)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains( '[DataObject::$has_one](http://api.silverstripe.org/search/lookup/?q=DataObject::$has_one&version=2.4&module=mymodule)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
}
|
2010-09-03 07:29:15 +02:00
|
|
|
|
|
|
|
function testHeadlineAnchors() {
|
2010-10-21 22:27:23 +02:00
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('test.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('mymodule', '2.4', DOCSVIEWER_PATH . '/tests/docs/'));
|
2010-10-21 22:27:23 +02:00
|
|
|
$page->setLang('en');
|
|
|
|
$page->setVersion('2.4');
|
2010-09-03 07:29:15 +02:00
|
|
|
|
|
|
|
$result = DocumentationParser::rewrite_heading_anchors($page->getMarkdown(), $page);
|
|
|
|
|
|
|
|
/*
|
|
|
|
# Heading one {#Heading-one}
|
|
|
|
|
|
|
|
# Heading with custom anchor {#custom-anchor} {#Heading-with-custom-anchor-custom-anchor}
|
|
|
|
|
|
|
|
## Heading two {#Heading-two}
|
|
|
|
|
|
|
|
### Heading three {#Heading-three}
|
|
|
|
|
|
|
|
## Heading duplicate {#Heading-duplicate}
|
|
|
|
|
|
|
|
## Heading duplicate {#Heading-duplicate-2}
|
|
|
|
|
|
|
|
## Heading duplicate {#Heading-duplicate-3}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-01-15 22:47:43 +01:00
|
|
|
$this->assertContains('# Heading one {#heading-one}', $result);
|
2010-09-03 07:29:15 +02:00
|
|
|
$this->assertContains('# Heading with custom anchor {#custom-anchor}', $result);
|
2011-01-15 22:47:43 +01:00
|
|
|
$this->assertNotContains('# Heading with custom anchor {#custom-anchor} {#heading', $result);
|
|
|
|
$this->assertContains('# Heading two {#heading-two}', $result);
|
|
|
|
$this->assertContains('# Heading three {#heading-three}', $result);
|
|
|
|
$this->assertContains('## Heading duplicate {#heading-duplicate}', $result);
|
|
|
|
$this->assertContains('## Heading duplicate {#heading-duplicate-2}', $result);
|
|
|
|
$this->assertContains('## Heading duplicate {#heading-duplicate-3}', $result);
|
2010-09-03 07:29:15 +02:00
|
|
|
|
|
|
|
}
|
2010-08-01 06:46:26 +02:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
function testRelativeLinks() {
|
|
|
|
// Page on toplevel
|
2010-10-21 22:27:23 +02:00
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('test.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('mymodule', '2.4', DOCSVIEWER_PATH . '/tests/docs/'));
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
$result = DocumentationParser::rewrite_relative_links($page->getMarkdown(), $page, 'mycontroller/cms/2.4/en/');
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'[link: subfolder index](mycontroller/cms/2.4/en/subfolder/)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: subfolder page](mycontroller/cms/2.4/en/subfolder/subpage)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: http](http://silverstripe.org)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: api](api:DataObject)',
|
|
|
|
$result
|
|
|
|
);
|
2011-01-14 02:03:46 +01:00
|
|
|
$this->assertContains(
|
|
|
|
'[link: relative](mycontroller/cms/2.4/a-relative-file.md)',
|
|
|
|
$result
|
|
|
|
);
|
2010-08-01 06:46:32 +02:00
|
|
|
|
|
|
|
// Page in subfolder
|
2010-10-21 22:27:23 +02:00
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('subfolder/subpage.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('mymodule', '2.4', DOCSVIEWER_PATH . '/tests/docs/'));
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
$result = DocumentationParser::rewrite_relative_links($page->getMarkdown(), $page, 'mycontroller/cms/2.4/en/');
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2011-01-14 02:03:46 +01:00
|
|
|
$this->assertContains(
|
|
|
|
'[link: relative](mycontroller/cms/2.4/en/subfolder/subpage.md)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'[link: absolute index](mycontroller/cms/2.4/en/)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: absolute index with name](mycontroller/cms/2.4/en/index)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: relative index](mycontroller/cms/2.4/en/)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: relative parent page](mycontroller/cms/2.4/en/test)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: absolute parent page](mycontroller/cms/2.4/en/test)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
|
|
|
|
// Page in nested subfolder
|
2010-10-21 22:27:23 +02:00
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('subfolder/subsubfolder/subsubpage.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('mymodule', '2.4', DOCSVIEWER_PATH . '/tests/docs/'));
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
$result = DocumentationParser::rewrite_relative_links($page->getMarkdown(), $page, 'mycontroller/cms/2.4/en/');
|
2010-10-21 22:27:23 +02:00
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'[link: absolute index](mycontroller/cms/2.4/en/)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: relative index](mycontroller/cms/2.4/en/subfolder/)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: relative parent page](mycontroller/cms/2.4/en/subfolder/subpage)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: relative grandparent page](mycontroller/cms/2.4/en/test)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'[link: absolute page](mycontroller/cms/2.4/en/test)',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
}
|
2011-07-04 02:05:22 +02:00
|
|
|
|
2011-01-14 04:29:14 +01:00
|
|
|
function testRetrieveMetaData() {
|
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('MetaDataTest.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('parser', '2.4', DOCSVIEWER_PATH . '/tests/docs-parser/'));
|
2011-01-14 04:29:14 +01:00
|
|
|
|
|
|
|
DocumentationParser::retrieve_meta_data($page);
|
|
|
|
|
|
|
|
$this->assertEquals('Dr. Foo Bar.', $page->Author);
|
|
|
|
$this->assertEquals("Foo Bar's Test page.", $page->getTitle());
|
|
|
|
$this->assertEquals("Foo Bar's Test page.", $page->Title);
|
|
|
|
}
|
2011-04-05 04:13:51 +02:00
|
|
|
|
|
|
|
function testParserConvertsSpecialCharacters() {
|
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('CodeSnippets.md');
|
2012-04-08 11:36:16 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('parser', '2.4', DOCSVIEWER_PATH . '/tests/docs-parser/'));
|
2011-04-05 04:13:51 +02:00
|
|
|
|
2012-04-08 11:36:16 +02:00
|
|
|
$parsed = DocumentationParser::parse($page, '/'.DOCSVIEWER_DIR .'/tests/docs-parser/');
|
2011-04-05 04:13:51 +02:00
|
|
|
|
|
|
|
// header elements parsed
|
|
|
|
$this->assertContains(
|
|
|
|
'<% control Foo %>',
|
|
|
|
$parsed
|
|
|
|
);
|
|
|
|
|
|
|
|
// paragraphs
|
|
|
|
$this->assertContains(
|
|
|
|
'<% foo %>',
|
|
|
|
$parsed
|
|
|
|
);
|
|
|
|
}
|
2011-07-04 06:58:15 +02:00
|
|
|
|
|
|
|
function testRewritingRelativeLinksToFiles() {
|
2012-04-08 11:36:16 +02:00
|
|
|
$folder = DOCSVIEWER_PATH . '/tests/docs-parser/';
|
2011-07-04 06:58:15 +02:00
|
|
|
|
|
|
|
$page = new DocumentationPage();
|
|
|
|
$page->setRelativePath('file-download.md');
|
2012-04-08 11:23:49 +02:00
|
|
|
$page->setEntity(new DocumentationEntity('parser', '2.4', $folder));
|
2011-07-04 06:58:15 +02:00
|
|
|
|
|
|
|
$parsed = DocumentationParser::parse($page, $folder);
|
2012-04-08 11:23:49 +02:00
|
|
|
|
2011-07-04 06:58:15 +02:00
|
|
|
$this->assertContains(
|
2012-04-08 11:36:16 +02:00
|
|
|
DOCSVIEWER_DIR .'/tests/docs-parser/en/_images/external_link.png',
|
2011-07-04 06:58:15 +02:00
|
|
|
$parsed
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertContains(
|
2012-04-08 11:36:16 +02:00
|
|
|
DOCSVIEWER_DIR .'/tests/docs-parser/en/_images/test.tar.gz',
|
2011-07-04 06:58:15 +02:00
|
|
|
$parsed
|
|
|
|
);
|
|
|
|
}
|
2010-08-01 06:46:26 +02:00
|
|
|
}
|