Browse Source

Add autoLink improvements.

mscherer 1 year ago
parent
commit
e3dc4b48be

+ 1 - 1
src/View/Helper/TextHelper.php

@@ -206,7 +206,7 @@ class TextHelper extends CakeTextHelper {
 	 * - ellipsis (defaults to UTF8 version)
 	 * @return string html/$plain
 	 */
-	protected function prepareLinkName(string $link, array $options = []): string {
+	public function prepareLinkName(string $link, array $options = []): string {
 		// strip protocol if desired (default)
 		if (!isset($options['stripProtocol']) || $options['stripProtocol'] !== false) {
 			$link = $this->stripProtocol($link);

+ 0 - 2
tests/TestCase/Model/Table/TableTest.php

@@ -406,8 +406,6 @@ class TableTest extends TestCase {
 		$res = $this->Users->validateUrl($data, ['deep' => false, 'autoComplete' => false]);
 		$this->assertTrue((env('REMOTE_ADDR') !== '127.0.0.1') ? !$res : $res);
 
-		//$this->skipIf(strpos($_SERVER['HTTP_HOST'], '.') === false, 'No online HTTP_HOST');
-
 		$data = '/some/link';
 		$res = $this->Users->validateUrl($data, ['deep' => false, 'sameDomain' => true]);
 		$this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);

+ 18 - 13
tests/TestCase/View/Helper/TextHelperTest.php

@@ -33,8 +33,6 @@ class TextHelperTest extends TestCase {
 	}
 
 	/**
-	 * TextExtHelperTest::testAutoLinkEmails()
-	 *
 	 * @return void
 	 */
 	public function testAutoLinkEmails() {
@@ -60,8 +58,6 @@ class TextHelperTest extends TestCase {
 	}
 
 	/**
-	 * TextExtHelperTest::testAutoLinkEmailsWithHtmlOrDangerousStrings()
-	 *
 	 * @return void
 	 */
 	public function testAutoLinkEmailsWithHtmlOrDangerousStrings() {
@@ -88,8 +84,6 @@ class TextHelperTest extends TestCase {
 	}
 
 	/**
-	 * TextExtHelperTest::testAutoLinkUrls()
-	 *
 	 * @return void
 	 */
 	public function testAutoLinkUrls() {
@@ -115,8 +109,6 @@ class TextHelperTest extends TestCase {
 	}
 
 	/**
-	 * TextExtHelperTest::testAutoLinkUrlsWithEscapeFalse()
-	 *
 	 * @return void
 	 */
 	public function testAutoLinkUrlsWithEscapeFalse() {
@@ -140,15 +132,30 @@ class TextHelperTest extends TestCase {
 	}
 
 	/**
-	 * TextExtHelperTest::testAutoLinkUrlsWithHtmlOrDangerousStrings()
-	 *
 	 * @return void
 	 */
 	public function testAutoLinkUrlsWithHtmlOrDangerousStrings() {
 		$text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and more';
 		$expected = 'Text &lt;i&gt;with a url&lt;/i&gt; <a href="http://www.cot.ag?id=2&amp;sub=3">www.cot.ag?id=2&amp;sub=3</a> and more';
 		$result = $this->Text->autoLinkUrls($text);
-		//pr(h($text));
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testAutoLinkUrlsWithCallback() {
+		$text = 'Text www.cot.ag?id=2&sub=3 and more';
+		$expected = 'Text <a href="http://www.cot.ag?id=2&amp;sub=3" target="_blank" rel="nofollow">www.cot.ag?id=2&amp;sub=3</a> and more';
+		$that = $this->Text;
+		$result = $this->Text->autoLinkUrls($text, ['callable' => function (string $link, string $url, array $options) use ($that) {
+			$linkOptions = $options;
+
+			$linkOptions['target'] = '_blank';
+			$linkOptions['rel'] = 'nofollow';
+
+			return $that->Html->link($that->prepareLinkName($link, $options), $url, $linkOptions);
+		}]);
 		$this->assertEquals($expected, $result);
 	}
 
@@ -188,8 +195,6 @@ class TextHelperTest extends TestCase {
 	}
 
 	/**
-	 * TextExtHelperTest::testAutoLinkUrlsWithCakeTests()
-	 *
 	 * @return void
 	 */
 	public function testAutoLinkUrlsWithCakeTests() {