@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * Test cases for @{class:PhutilRemarkupEngine}.
5 */
6final class PhutilRemarkupEngineTestCase extends PhabricatorTestCase {
7
8 public function testEngine() {
9 $root = dirname(__FILE__).'/remarkup/';
10 foreach (Filesystem::listDirectory($root, $hidden = false) as $file) {
11 $this->markupText($root.$file);
12 }
13 }
14
15 private function markupText($markup_file) {
16 $contents = Filesystem::readFile($markup_file);
17 $file = basename($markup_file);
18
19 $parts = explode("\n~~~~~~~~~~\n", $contents);
20 $this->assertEqual(3, count($parts), $markup_file);
21
22 list($input_remarkup, $expected_output, $expected_text) = $parts;
23
24 $input_remarkup = $this->unescapeTrailingWhitespace($input_remarkup);
25 $expected_output = $this->unescapeTrailingWhitespace($expected_output);
26 $expected_text = $this->unescapeTrailingWhitespace($expected_text);
27
28 $engine = $this->buildNewTestEngine();
29
30 switch ($file) {
31 case 'raw-escape.txt':
32
33 // NOTE: Here, we want to test PhutilRemarkupEscapeRemarkupRule and
34 // PhutilRemarkupBlockStorage, which are triggered by "\1". In the
35 // test, "~" is used as a placeholder for "\1" since it's hard to type
36 // "\1".
37
38 $input_remarkup = str_replace('~', "\1", $input_remarkup);
39 $expected_output = str_replace('~', "\1", $expected_output);
40 $expected_text = str_replace('~', "\1", $expected_text);
41 break;
42 case 'toc.txt':
43 $engine->setConfig('header.generate-toc', true);
44 break;
45 case 'link-same-window.txt':
46 $engine->setConfig('uri.same-window', true);
47 break;
48 case 'link-square.txt':
49 $engine->setConfig('uri.base', 'http://www.example.com/');
50 $engine->setConfig('uri.here', 'http://www.example.com/page/');
51 break;
52 }
53
54 $actual_output = (string)$engine->markupText($input_remarkup);
55
56 switch ($file) {
57 case 'toc.txt':
58 $table_of_contents =
59 PhutilRemarkupHeaderBlockRule::renderTableOfContents($engine);
60 $actual_output = $table_of_contents."\n\n".$actual_output;
61 break;
62 }
63
64 $this->assertEqual(
65 $expected_output,
66 $actual_output,
67 pht("Failed to markup HTML in file '%s'.", $file));
68
69 $engine->setMode(PhutilRemarkupEngine::MODE_TEXT);
70 $actual_output = (string)$engine->markupText($input_remarkup);
71
72 $this->assertEqual(
73 $expected_text,
74 $actual_output,
75 pht("Failed to markup text in file '%s'.", $file));
76 }
77
78 private function buildNewTestEngine() {
79 $engine = new PhutilRemarkupEngine();
80
81 $engine->setConfig(
82 'uri.allowed-protocols',
83 array(
84 'http' => true,
85 'mailto' => true,
86 'tel' => true,
87 ));
88
89 $rules = array();
90 $rules[] = new PhutilRemarkupEscapeRemarkupRule();
91 $rules[] = new PhutilRemarkupMonospaceRule();
92 $rules[] = new PhutilRemarkupHexColorCodeRule();
93 $rules[] = new PhutilRemarkupDocumentLinkRule();
94 $rules[] = new PhutilRemarkupHyperlinkRule();
95 $rules[] = new PhutilRemarkupBoldRule();
96 $rules[] = new PhutilRemarkupItalicRule();
97 $rules[] = new PhutilRemarkupDelRule();
98 $rules[] = new PhutilRemarkupUnderlineRule();
99 $rules[] = new PhutilRemarkupHighlightRule();
100
101 $blocks = array();
102 $blocks[] = new PhutilRemarkupQuotesBlockRule();
103 $blocks[] = new PhutilRemarkupReplyBlockRule();
104 $blocks[] = new PhutilRemarkupHeaderBlockRule();
105 $blocks[] = new PhutilRemarkupHorizontalRuleBlockRule();
106 $blocks[] = new PhutilRemarkupCodeBlockRule();
107 $blocks[] = new PhutilRemarkupLiteralBlockRule();
108 $blocks[] = new PhutilRemarkupNoteBlockRule();
109 $blocks[] = new PhutilRemarkupTableBlockRule();
110 $blocks[] = new PhutilRemarkupSimpleTableBlockRule();
111 $blocks[] = new PhutilRemarkupDefaultBlockRule();
112 $blocks[] = new PhutilRemarkupListBlockRule();
113 $blocks[] = new PhutilRemarkupInterpreterBlockRule();
114
115 foreach ($blocks as $block) {
116 if (!($block instanceof PhutilRemarkupCodeBlockRule)) {
117 $block->setMarkupRules($rules);
118 }
119 }
120
121 $engine->setBlockRules($blocks);
122
123 return $engine;
124 }
125
126
127 private function unescapeTrailingWhitespace($input) {
128 // Remove up to one "~" at the end of each line so trailing whitespace may
129 // be written in tests as " ~".
130 return preg_replace('/~$/m', '', $input);
131 }
132
133}