Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
12242 views
1
<?php
2
3
/**
4
* Test cases for @{class:PhutilRemarkupEngine}.
5
*/
6
final class PhutilRemarkupEngineTestCase extends PhutilTestCase {
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 PhutilRemarkupDocumentLinkRule();
93
$rules[] = new PhutilRemarkupHyperlinkRule();
94
$rules[] = new PhutilRemarkupBoldRule();
95
$rules[] = new PhutilRemarkupItalicRule();
96
$rules[] = new PhutilRemarkupDelRule();
97
$rules[] = new PhutilRemarkupUnderlineRule();
98
$rules[] = new PhutilRemarkupHighlightRule();
99
100
$blocks = array();
101
$blocks[] = new PhutilRemarkupQuotesBlockRule();
102
$blocks[] = new PhutilRemarkupReplyBlockRule();
103
$blocks[] = new PhutilRemarkupHeaderBlockRule();
104
$blocks[] = new PhutilRemarkupHorizontalRuleBlockRule();
105
$blocks[] = new PhutilRemarkupCodeBlockRule();
106
$blocks[] = new PhutilRemarkupLiteralBlockRule();
107
$blocks[] = new PhutilRemarkupNoteBlockRule();
108
$blocks[] = new PhutilRemarkupTableBlockRule();
109
$blocks[] = new PhutilRemarkupSimpleTableBlockRule();
110
$blocks[] = new PhutilRemarkupDefaultBlockRule();
111
$blocks[] = new PhutilRemarkupListBlockRule();
112
$blocks[] = new PhutilRemarkupInterpreterBlockRule();
113
114
foreach ($blocks as $block) {
115
if (!($block instanceof PhutilRemarkupCodeBlockRule)) {
116
$block->setMarkupRules($rules);
117
}
118
}
119
120
$engine->setBlockRules($blocks);
121
122
return $engine;
123
}
124
125
126
private function unescapeTrailingWhitespace($input) {
127
// Remove up to one "~" at the end of each line so trailing whitespace may
128
// be written in tests as " ~".
129
return preg_replace('/~$/m', '', $input);
130
}
131
132
}
133
134