Completed
Push — kraftbj-patch-2 ( f1a4bb...ba1272 )
by
unknown
25:04 queued 15:38
created

WP_Test_WPCom_GHF_Markdown_Parser   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A test_urls_preserve() 0 11 1
B get_text_urls() 0 56 1
1
<?php
2
/**
3
 * GitHub-Flavoured Markdown unit tests.
4
 *
5
 * @package Jetpack
6
 */
7
8
// Require the whole lib to process text.
9
require_once JETPACK__PLUGIN_DIR . '_inc/lib/markdown.php';
10
11
/**
12
 * Class for testing the WPCom_GHF_Markdown_Parser class.
13
 *
14
 * @covers WPCom_GHF_Markdown_Parser
15
 */
16
class WP_Test_WPCom_GHF_Markdown_Parser extends WP_UnitTestCase {
17
	/**
18
	 * Test that links are preserved when going through the Markdown parser.
19
	 *
20
	 * @covers WPCom_GHF_Markdown_Parser->transform
21
	 * @dataProvider get_text_urls
22
	 *
23
	 * @since 9.2.0
24
	 *
25
	 * @param string $text     The Markdown text we want to transform.
26
	 * @param string $expected Expected HTML content.
27
	 */
28
	public function test_urls_preserve( $text, $expected ) {
29
		/*
30
		 * Text always ends with a newline.
31
		 * Let's add it here (and not in the data provider)
32
		 * to make things clearer there.
33
		 */
34
		$expected .= "\n";
35
36
		$transformed_text = ( new WPCom_GHF_Markdown_Parser() )->transform( $text );
37
		$this->assertEquals( $expected, $transformed_text );
38
	}
39
40
	/**
41
	 * Get link examples to test how Markdown avoids transforming elements in links.
42
	 *
43
	 * @return array The test data.
44
	 */
45
	public function get_text_urls() {
46
		return array(
47
			'no_link_bold'                      => array(
48
				'Some **bold** text',
49
				'Some <strong>bold</strong> text',
50
			),
51
			'link_bold'                         => array(
52
				'**[A bold link](https://jetpack.com/)**',
53
				'<strong><a href="https://jetpack.com/">A bold link</a></strong>',
54
			),
55
			'link_undercore'                    => array(
56
				'[A link with underscore in URL](https://jetpack.com/_features_/)',
57
				'<a href="https://jetpack.com/_features_/">A link with underscore in URL</a>',
58
			),
59
			'link_alone'                        => array(
60
				'https://jetpack.com/',
61
				'https://jetpack.com/',
62
			),
63
			'link_underscore_alone'             => array(
64
				'https://jetpack.com/_features_/',
65
				'https://jetpack.com/_features_/',
66
			),
67
			'ftp_link_underscore'               => array(
68
				'ftp://[email protected]:123',
69
				'ftp://[email protected]:123',
70
			),
71
			// This is current behavior before this patch.
72
			'explicit URL'                      => array(
73
				'url: <https://jetpack.com/_features_/>',
74
				'url: <a href="https://jetpack.com/_features_/">https://jetpack.com/_features_/</a>',
75
			),
76
			// This too.
77
			'URL in link text'                  => array(
78
				'url: [https://jetpack.com/](https://jetpack.com/#xyz)',
79
				'url: <a href="https://jetpack.com/#xyz">https://jetpack.com/</a>',
80
			),
81
82
			'bolded URL'                        => array(
83
				'**https://jetpack.com/_features_/**',
84
				'<strong>https://jetpack.com/_features_/</strong>',
85
			),
86
			'emphasized URL'                    => array(
87
				'_https://jetpack.com/_features_/_',
88
				'<em>https://jetpack.com/_features_/</em>',
89
			),
90
			'URL with odd but legal characters' => array(
91
				'https://example.com/_form?type=*&q=a+(b+or+c)&arr[]=1&arr[]=2&value=_foo_bar',
92
				'https://example.com/_form?type=*&q=a+(b+or+c)&arr[]=1&arr[]=2&value=_foo_bar',
93
			),
94
			// Let's ensure we are not breaking regular HTML. This should not change.
95
			'image tag'                         => array(
96
				'<img src="whatever.png" alt="Text" width="1024" height="470" class="alignleft size-full wp-image-37190" />',
97
				'<img src="whatever.png" alt="Text" width="1024" height="470" class="alignleft size-full wp-image-37190" />',
98
			),
99
		);
100
	}
101
}
102