-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathConvertKitMethodsTest.php
More file actions
132 lines (121 loc) · 4.04 KB
/
ConvertKitMethodsTest.php
File metadata and controls
132 lines (121 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
use PHPUnit\Framework\TestCase;
use Dotenv\Dotenv;
use ConvertKit_API\ConvertKit_API;
/**
* Test methods in ConvertKit_API_Traits that don't interact with the API,
* such as convert_relative_to_absolute_urls().
*/
class ConvertKitMethodsTest extends TestCase
{
/**
* Kit API Class
*
* @var object
*/
protected $api;
/**
* Initialize the API class before each test.
*
* @since 2.4.1
*
* @return void
*/
protected function setUp(): void
{
$this->api = new ConvertKit_API();
}
/**
* Test the convert_relative_to_absolute_urls() method.
*
* @since 2.4.1
*
* @return void
*/
public function testConvertRelativeToAbsoluteUrls()
{
// Setup HTML in DOMDocument.
$html = new \DOMDocument();
$html->loadHTML('<html>
<head>
<script type="text/javascript" src="rocket-loader.min.js"></script>
<link rel="stylesheet" href="//fonts.googleapis.com">
</head>
<body>
<a href="/test">Test</a>
<img src="/test.jpg" />
<script type="text/javascript" src="/test.js"></script>
<form action="/test">Test</form>
</body>
</html>');
// Define URL to prepend to relative URLs.
$url_scheme_host_only = 'https://example.com';
// Convert relative URLs to absolute URLs for elements we want to test.
$this->api->convert_relative_to_absolute_urls(
$html->getElementsByTagName('a'),
'href',
$url_scheme_host_only
);
$this->api->convert_relative_to_absolute_urls(
$html->getElementsByTagName('link'),
'href',
$url_scheme_host_only
);
$this->api->convert_relative_to_absolute_urls(
$html->getElementsByTagName('img'),
'src',
$url_scheme_host_only
);
$this->api->convert_relative_to_absolute_urls(
$html->getElementsByTagName('script'),
'src',
$url_scheme_host_only
);
$this->api->convert_relative_to_absolute_urls(
$html->getElementsByTagName('form'),
'action',
$url_scheme_host_only
);
// Fetch HTML string.
$output = $html->saveHTML();
// Assert string contains expected HTML elements that should not be modified.
$this->assertStringContainsString('<link rel="stylesheet" href="//fonts.googleapis.com">', $output);
// Assert string does not contain HTML elements that should be removed.
$this->assertStringNotContainsString(
'<script type="text/javascript" src="rocket-loader.min.js"></script>',
$output
);
// Assert string contains expected HTML elements that should be modified.
$this->assertStringContainsString(
'<a href="' . $url_scheme_host_only . '/test">Test</a>',
$output
);
$this->assertStringContainsString(
'<img src="' . $url_scheme_host_only . '/test.jpg">',
$output
);
$this->assertStringContainsString(
'<script type="text/javascript" src="' . $url_scheme_host_only . '/test.js"></script>',
$output
);
$this->assertStringContainsString(
'<form action="' . $url_scheme_host_only . '/test">Test</form>',
$output
);
}
/**
* Test that the get_body_html() method returns the expected HTML.
*
* @since 2.4.1
*/
public function testGetBodyHtml()
{
$content = '<h1>Vantar þinn ungling sjálfstraust í stærðfræði?</h1><p>This is a test</p>';
$html = new \DOMDocument();
$html->loadHTML('
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>' . $content . '</body>
</html>');
$this->assertEquals($content, $this->api->get_body_html($html));
}
}