Passed
Branch refactor-markdown-helper (ffdce7)
by Caen
06:11
created

MarkdownConverterServiceTest.php$0 ➔ makeService()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A MarkdownConverterServiceTest.php$0 ➔ __construct() 0 3 1
1
<?php
2
3
namespace Hyde\Framework\Testing\Feature;
4
5
use Hyde\Framework\Models\Pages\DocumentationPage;
6
use Hyde\Framework\Services\MarkdownConverterService;
7
use Hyde\Testing\TestCase;
8
use Illuminate\Support\Facades\Config;
9
10
/**
11
 * @covers \Hyde\Framework\Services\MarkdownConverterService
12
 */
13
class MarkdownConverterServiceTest extends TestCase
14
{
15
    public function test_service_can_parse_markdown_to_html()
16
    {
17
        $markdown = '# Hello World!';
18
19
        $html = (new MarkdownConverterService($markdown))->parse();
20
21
        $this->assertIsString($html);
22
        $this->assertEquals("<h1>Hello World!</h1>\n", $html);
23
    }
24
25
    public function test_service_can_parse_markdown_to_html_with_permalinks()
26
    {
27
        $markdown = '## Hello World!';
28
29
        $html = (new MarkdownConverterService($markdown))->withPermalinks()->parse();
30
31
        $this->assertIsString($html);
32
        $this->assertEquals(
33
            '<h2>Hello World!<a id="hello-world" href="#hello-world" class="heading-permalink" aria-hidden="true" '.
34
            'title="Permalink">#</a></h2>'."\n",
35
            $html
36
        );
37
    }
38
39
    public function test_torchlight_extension_is_not_enabled_by_default()
40
    {
41
        $markdown = '# Hello World!';
42
        $service = new MarkdownConverterService($markdown);
43
        $service->parse();
44
        $this->assertNotContains('Torchlight\Commonmark\V2\TorchlightExtension', $service->getExtensions());
45
    }
46
47
    public function test_torchlight_extension_is_enabled_automatically_when_has_torchlight_feature()
48
    {
49
        $markdown = '# Hello World!';
50
        $service = new MarkdownConverterService($markdown);
51
        $service->addFeature('torchlight')->parse();
52
        $this->assertContains('Torchlight\Commonmark\V2\TorchlightExtension', $service->getExtensions());
53
    }
54
55
    public function test_torchlight_integration_injects_attribution()
56
    {
57
        $markdown = '# Hello World! <!-- Syntax highlighted by torchlight.dev -->';
58
59
        // Enable the extension in config
60
61
        $service = new MarkdownConverterService($markdown);
62
63
        $html = $service->parse();
64
65
        $this->assertStringContainsString('Syntax highlighting by <a href="https://torchlight.dev/" '
66
                .'rel="noopener nofollow">Torchlight.dev</a>', $html);
67
    }
68
69
    public function test_bladedown_is_not_enabled_by_default()
70
    {
71
        $service = new MarkdownConverterService('[Blade]: {{ "Hello World!" }}');
72
        $this->assertEquals("<p>[Blade]: {{ &quot;Hello World!&quot; }}</p>\n", $service->parse());
73
    }
74
75
    public function test_bladedown_can_be_enabled()
76
    {
77
        config(['markdown.enable_blade' => true]);
78
        $service = new MarkdownConverterService('[Blade]: {{ "Hello World!" }}');
79
        $service->addFeature('bladedown')->parse();
80
        $this->assertEquals("Hello World!\n", $service->parse());
81
    }
82
83
    // test raw html tags are stripped by default
84
    public function test_raw_html_tags_are_stripped_by_default()
85
    {
86
        $markdown = '<p>foo</p><style>bar</style><script>hat</script>';
87
        $service = new MarkdownConverterService($markdown);
88
        $html = $service->parse();
89
        $this->assertEquals("<p>foo</p>&lt;style>bar&lt;/style>&lt;script>hat&lt;/script>\n", $html);
90
    }
91
92
    // test raw html tags are not stripped when explicitly enabled
93
    public function test_raw_html_tags_are_not_stripped_when_explicitly_enabled()
94
    {
95
        config(['markdown.allow_html' =>true]);
96
        $markdown = '<p>foo</p><style>bar</style><script>hat</script>';
97
        $service = new MarkdownConverterService($markdown);
98
        $html = $service->parse();
99
        $this->assertEquals("<p>foo</p><style>bar</style><script>hat</script>\n", $html);
100
    }
101
102
    public function test_has_features_array()
103
    {
104
        $service = $this->makeService();
105
106
        $this->assertIsArray($service->features);
107
    }
108
109
    public function test_the_features_array_is_empty_by_default()
110
    {
111
        $service = $this->makeService();
112
113
        $this->assertEmpty($service->features);
114
    }
115
116
    public function test_features_can_be_added_to_the_array()
117
    {
118
        $service = $this->makeService();
119
120
        $service->addFeature('test');
121
        $this->assertContains('test', $service->features);
122
    }
123
124
    public function test_features_can_be_removed_from_the_array()
125
    {
126
        $service = $this->makeService();
127
128
        $service->addFeature('test');
129
        $service->removeFeature('test');
130
        $this->assertNotContains('test', $service->features);
131
    }
132
133
    public function test_method_chaining_can_be_used_to_programmatically_add_features_to_the_array()
134
    {
135
        $service = $this->makeService();
136
137
        $service->addFeature('test')->addFeature('test2');
138
        $this->assertContains('test', $service->features);
139
        $this->assertContains('test2', $service->features);
140
    }
141
142
    public function test_method_chaining_can_be_used_to_programmatically_remove_features_from_the_array()
143
    {
144
        $service = $this->makeService();
145
146
        $service->addFeature('test')->addFeature('test2')->removeFeature('test');
147
        $this->assertNotContains('test', $service->features);
148
        $this->assertContains('test2', $service->features);
149
    }
150
151
    public function test_method_with_table_of_contents_method_chain_adds_the_table_of_contents_feature()
152
    {
153
        $service = $this->makeService();
154
155
        $service->withTableOfContents();
156
        $this->assertContains('table-of-contents', $service->features);
157
    }
158
159
    public function test_method_with_permalinks_method_chain_adds_the_permalinks_feature()
160
    {
161
        $service = $this->makeService();
162
163
        $service->withPermalinks();
164
        $this->assertContains('permalinks', $service->features);
165
    }
166
167
    public function test_has_feature_returns_true_if_the_feature_is_in_the_array()
168
    {
169
        $service = $this->makeService();
170
171
        $service->addFeature('test');
172
        $this->assertTrue($service->hasFeature('test'));
173
    }
174
175
    public function test_has_feature_returns_false_if_the_feature_is_not_in_the_array()
176
    {
177
        $service = $this->makeService();
178
179
        $this->assertFalse($service->hasFeature('test'));
180
    }
181
182
    public function test_method_can_enable_permalinks_returns_true_if_the_permalinks_feature_is_in_the_array()
183
    {
184
        $service = $this->makeService();
185
186
        $service->addFeature('permalinks');
187
        $this->assertTrue($service->canEnablePermalinks());
188
    }
189
190
    public function test_method_can_enable_permalinks_is_automatically_for_documentation_pages()
191
    {
192
        $service = $this->makeService();
193
194
        Config::set('docs.table_of_contents.enabled', true);
195
        $service->sourceModel = DocumentationPage::class;
196
197
        $this->assertTrue($service->canEnablePermalinks());
198
    }
199
200
    public function test_method_can_enable_permalinks_returns_false_if_the_permalinks_feature_is_not_in_the_array()
201
    {
202
        $service = $this->makeService();
203
204
        $this->assertFalse($service->canEnablePermalinks());
205
    }
206
207
    public function test_method_can_enable_torchlight_returns_true_if_the_torchlight_feature_is_in_the_array()
208
    {
209
        $service = $this->makeService();
210
211
        $service->addFeature('torchlight');
212
        $this->assertTrue($service->canEnableTorchlight());
213
    }
214
215
    public function test_method_can_enable_torchlight_returns_false_if_the_torchlight_feature_is_not_in_the_array()
216
    {
217
        $service = $this->makeService();
218
219
        $this->assertFalse($service->canEnableTorchlight());
220
    }
221
222
    protected function makeService()
223
    {
224
        return new class extends MarkdownConverterService
225
        {
226
            public array $features = [];
227
228
            public function __construct(string $markdown = '', ?string $sourceModel = null)
229
            {
230
                parent::__construct($markdown, $sourceModel);
231
            }
232
        };
233
    }
234
}
235