Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/class-convertkit-api-traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -2682,6 +2682,11 @@ public function convert_relative_to_absolute_urls(\DOMNodeList $elements, string
continue;
}

// Skip if the attribute's value is an anchor.
if (strpos($element->getAttribute($attribute), '#') !== false) {
continue;
}

// Remove element if it's rocket-loader.min.js. Including it prevents landing page redirects from working.
if (strpos($element->getAttribute($attribute), 'rocket-loader.min.js') !== false) {
if ($element->parentNode instanceof \DOMNode) {
Expand Down
155 changes: 155 additions & 0 deletions tests/Integration/KitMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Tests;

use lucatume\WPBrowser\TestCase\WPTestCase;

/**
* Test methods that don't interact with the Kit API,
* such as convert_relative_to_absolute_urls().
*
* @since 2.6.2
*/
class KitMethodsTest extends WPTestCase
{
/**
* Kit API Class
*
* @var object
*/
protected $api;

/**
* Performs actions before each test.
*
* @since 2.6.2
*/
public function setUp(): void
{
parent::setUp();

// Include class from /src to test.
require_once 'src/class-convertkit-api-traits.php';
require_once 'src/class-convertkit-api-v4.php';

// Initialize the classes we want to test.
$this->api = new \ConvertKit_API_V4(
client_id: $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'],
redirect_uri: $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'],
access_token: $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'],
refresh_token: $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN']
);
}

/**
* Test the convert_relative_to_absolute_urls() method.
*
* @since 2.6.2
*
* @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>
<a href="#anchor">Anchor</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
);

// Assert string contains expected HTML elements that should not be modified.
$this->assertStringContainsString(
'<a href="#anchor">Anchor</a>',
$output
);
}

/**
* Test that the get_body_html() method returns the expected HTML.
*
* @since 2.6.2
*/
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));
}
}