Path: blob/master/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
12242 views
<?php12/**3* Test cases for @{class:PhutilRemarkupEngine}.4*/5final class PhutilRemarkupEngineTestCase extends PhutilTestCase {67public function testEngine() {8$root = dirname(__FILE__).'/remarkup/';9foreach (Filesystem::listDirectory($root, $hidden = false) as $file) {10$this->markupText($root.$file);11}12}1314private function markupText($markup_file) {15$contents = Filesystem::readFile($markup_file);16$file = basename($markup_file);1718$parts = explode("\n~~~~~~~~~~\n", $contents);19$this->assertEqual(3, count($parts), $markup_file);2021list($input_remarkup, $expected_output, $expected_text) = $parts;2223$input_remarkup = $this->unescapeTrailingWhitespace($input_remarkup);24$expected_output = $this->unescapeTrailingWhitespace($expected_output);25$expected_text = $this->unescapeTrailingWhitespace($expected_text);2627$engine = $this->buildNewTestEngine();2829switch ($file) {30case 'raw-escape.txt':3132// NOTE: Here, we want to test PhutilRemarkupEscapeRemarkupRule and33// PhutilRemarkupBlockStorage, which are triggered by "\1". In the34// test, "~" is used as a placeholder for "\1" since it's hard to type35// "\1".3637$input_remarkup = str_replace('~', "\1", $input_remarkup);38$expected_output = str_replace('~', "\1", $expected_output);39$expected_text = str_replace('~', "\1", $expected_text);40break;41case 'toc.txt':42$engine->setConfig('header.generate-toc', true);43break;44case 'link-same-window.txt':45$engine->setConfig('uri.same-window', true);46break;47case 'link-square.txt':48$engine->setConfig('uri.base', 'http://www.example.com/');49$engine->setConfig('uri.here', 'http://www.example.com/page/');50break;51}5253$actual_output = (string)$engine->markupText($input_remarkup);5455switch ($file) {56case 'toc.txt':57$table_of_contents =58PhutilRemarkupHeaderBlockRule::renderTableOfContents($engine);59$actual_output = $table_of_contents."\n\n".$actual_output;60break;61}6263$this->assertEqual(64$expected_output,65$actual_output,66pht("Failed to markup HTML in file '%s'.", $file));6768$engine->setMode(PhutilRemarkupEngine::MODE_TEXT);69$actual_output = (string)$engine->markupText($input_remarkup);7071$this->assertEqual(72$expected_text,73$actual_output,74pht("Failed to markup text in file '%s'.", $file));75}7677private function buildNewTestEngine() {78$engine = new PhutilRemarkupEngine();7980$engine->setConfig(81'uri.allowed-protocols',82array(83'http' => true,84'mailto' => true,85'tel' => true,86));8788$rules = array();89$rules[] = new PhutilRemarkupEscapeRemarkupRule();90$rules[] = new PhutilRemarkupMonospaceRule();91$rules[] = new PhutilRemarkupDocumentLinkRule();92$rules[] = new PhutilRemarkupHyperlinkRule();93$rules[] = new PhutilRemarkupBoldRule();94$rules[] = new PhutilRemarkupItalicRule();95$rules[] = new PhutilRemarkupDelRule();96$rules[] = new PhutilRemarkupUnderlineRule();97$rules[] = new PhutilRemarkupHighlightRule();9899$blocks = array();100$blocks[] = new PhutilRemarkupQuotesBlockRule();101$blocks[] = new PhutilRemarkupReplyBlockRule();102$blocks[] = new PhutilRemarkupHeaderBlockRule();103$blocks[] = new PhutilRemarkupHorizontalRuleBlockRule();104$blocks[] = new PhutilRemarkupCodeBlockRule();105$blocks[] = new PhutilRemarkupLiteralBlockRule();106$blocks[] = new PhutilRemarkupNoteBlockRule();107$blocks[] = new PhutilRemarkupTableBlockRule();108$blocks[] = new PhutilRemarkupSimpleTableBlockRule();109$blocks[] = new PhutilRemarkupDefaultBlockRule();110$blocks[] = new PhutilRemarkupListBlockRule();111$blocks[] = new PhutilRemarkupInterpreterBlockRule();112113foreach ($blocks as $block) {114if (!($block instanceof PhutilRemarkupCodeBlockRule)) {115$block->setMarkupRules($rules);116}117}118119$engine->setBlockRules($blocks);120121return $engine;122}123124125private function unescapeTrailingWhitespace($input) {126// Remove up to one "~" at the end of each line so trailing whitespace may127// be written in tests as " ~".128return preg_replace('/~$/m', '', $input);129}130131}132133134