|
|
@@ -535,7 +535,7 @@ TEXT;
|
|
|
$text1 = 'The quick brown fox jumps over the lazy dog';
|
|
|
$text2 = 'Heizölrückstoßabdämpfung';
|
|
|
$text3 = '<b>© 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
|
|
|
- $text4 = '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great, or?';
|
|
|
+ $text4 = '<IMG src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great, or?';
|
|
|
$text5 = '0<b>1<i>2<span class="myclass">3</span>4<u>5</u>6</i>7</b>8<b>9</b>0';
|
|
|
$text6 = '<p><strong>Extra dates have been announced for this year\'s tour.</strong></p><p>Tickets for the new shows in</p>';
|
|
|
$text7 = 'El moño está en el lugar correcto. Eso fue lo que dijo la niña, ¿habrá dicho la verdad?';
|
|
|
@@ -543,32 +543,34 @@ TEXT;
|
|
|
$text9 = 'НОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыь';
|
|
|
$text10 = 'http://example.com/something/foo:bar';
|
|
|
|
|
|
- $this->assertSame($this->Text->truncate('Hello', 3), '...');
|
|
|
- $this->assertSame($this->Text->truncate('Hello', 3, ['exact' => false]), 'Hel...');
|
|
|
- $this->assertSame($this->Text->truncate($text1, 15), 'The quick br...');
|
|
|
- $this->assertSame($this->Text->truncate($text1, 15, ['exact' => false]), 'The quick...');
|
|
|
- $this->assertSame($this->Text->truncate($text1, 100), 'The quick brown fox jumps over the lazy dog');
|
|
|
- $this->assertSame($this->Text->truncate($text2, 10), 'Heiz&ou...');
|
|
|
- $this->assertSame($this->Text->truncate($text2, 10, ['exact' => false]), 'Heizö...');
|
|
|
- $this->assertSame($this->Text->truncate($text3, 20), '<b>© 2005-20...');
|
|
|
- $this->assertSame($this->Text->truncate($text4, 15), '<img src="my...');
|
|
|
- $this->assertSame($this->Text->truncate($text5, 6, ['ellipsis' => '']), '0<b>1<');
|
|
|
- $this->assertSame($this->Text->truncate($text1, 15, ['html' => true]), "The quick brow\xe2\x80\xa6");
|
|
|
- $this->assertSame($this->Text->truncate($text1, 15, ['exact' => false, 'html' => true]), "The quick\xe2\x80\xa6");
|
|
|
- $this->assertSame($this->Text->truncate($text2, 10, ['html' => true]), "Heizölrüc\xe2\x80\xa6");
|
|
|
- $this->assertSame($this->Text->truncate($text2, 10, ['exact' => false, 'html' => true]), "Heizölrüc\xe2\x80\xa6");
|
|
|
- $this->assertSame($this->Text->truncate($text3, 20, ['html' => true]), "<b>© 2005-2007, Cake S\xe2\x80\xa6</b>");
|
|
|
- $this->assertSame($this->Text->truncate($text4, 15, ['html' => true]), "<img src=\"mypic.jpg\"> This image ta\xe2\x80\xa6");
|
|
|
- $this->assertSame($this->Text->truncate($text4, 45, ['html' => true]), "<img src=\"mypic.jpg\"> This image tag is not XHTML conform!<br><hr/><b>But the\xe2\x80\xa6</b>");
|
|
|
- $this->assertSame($this->Text->truncate($text4, 90, ['html' => true]), '<img src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great,' . "\xe2\x80\xa6");
|
|
|
- $this->assertSame($this->Text->truncate($text5, 6, ['ellipsis' => '', 'html' => true]), '0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>');
|
|
|
- $this->assertSame($this->Text->truncate($text5, 20, ['ellipsis' => '', 'html' => true]), $text5);
|
|
|
- $this->assertSame($this->Text->truncate($text6, 57, ['exact' => false, 'html' => true]), "<p><strong>Extra dates have been announced for this year's\xe2\x80\xa6</strong></p>");
|
|
|
- $this->assertSame($this->Text->truncate($text7, 255), $text7);
|
|
|
- $this->assertSame($this->Text->truncate($text7, 15), 'El moño está...');
|
|
|
- $this->assertSame($this->Text->truncate($text8, 15), 'Vive la R' . chr(195) . chr(169) . 'pu...');
|
|
|
- $this->assertSame($this->Text->truncate($text9, 10), 'НОПРСТУ...');
|
|
|
- $this->assertSame($this->Text->truncate($text10, 30), 'http://example.com/somethin...');
|
|
|
+ $this->assertSame('...', $this->Text->truncate('Hello', 3));
|
|
|
+ $this->assertSame('Hel...', $this->Text->truncate('Hello', 3, ['exact' => false]));
|
|
|
+ $this->assertSame('The quick br...', $this->Text->truncate($text1, 15));
|
|
|
+ $this->assertSame('The quick...', $this->Text->truncate($text1, 15, ['exact' => false]));
|
|
|
+ $this->assertSame('The quick brown fox jumps over the lazy dog', $this->Text->truncate($text1, 100));
|
|
|
+ $this->assertSame('Heiz&ou...', $this->Text->truncate($text2, 10));
|
|
|
+ $this->assertSame('Heizö...', $this->Text->truncate($text2, 10, ['exact' => false]));
|
|
|
+ $this->assertSame('<b>© 2005-20...', $this->Text->truncate($text3, 20));
|
|
|
+ $this->assertSame('<IMG src="my...', $this->Text->truncate($text4, 15));
|
|
|
+ $this->assertSame('0<b>1<', $this->Text->truncate($text5, 6, ['ellipsis' => '']));
|
|
|
+ $this->assertSame("The quick brow\xe2\x80\xa6", $this->Text->truncate($text1, 15, ['html' => true]));
|
|
|
+ $this->assertSame("The quick\xe2\x80\xa6", $this->Text->truncate($text1, 15, ['exact' => false, 'html' => true]));
|
|
|
+ $this->assertSame("Heizölrüc\xe2\x80\xa6", $this->Text->truncate($text2, 10, ['html' => true]));
|
|
|
+ $this->assertSame("Heizölrück\xe2\x80\xa6", $this->Text->truncate($text2, 10, ['exact' => false, 'html' => true]));
|
|
|
+ $this->assertSame("<b>© 2005-2007, Cake S\xe2\x80\xa6</b>", $this->Text->truncate($text3, 20, ['html' => true]));
|
|
|
+ $this->assertSame("<IMG src=\"mypic.jpg\"> This image ta\xe2\x80\xa6", $this->Text->truncate($text4, 15, ['html' => true]));
|
|
|
+ $this->assertSame("<IMG src=\"mypic.jpg\"> This image tag is not XHTML conform!<br><hr/><b>But the\xe2\x80\xa6</b>", $this->Text->truncate($text4, 45, ['html' => true]));
|
|
|
+ $this->assertSame('<IMG src="mypic.jpg"> This image tag is not XHTML conform!<br><hr/><b>But the following image tag should be conform <img src="mypic.jpg" alt="Me, myself and I" /></b><br />Great,' . "\xe2\x80\xa6", $this->Text->truncate($text4, 90, ['html' => true]));
|
|
|
+ $this->assertSame('0<b>1<i>2<span class="myclass">3</span>4<u>5</u></i></b>', $this->Text->truncate($text5, 6, ['ellipsis' => '', 'html' => true]));
|
|
|
+ $this->assertSame($text5, $this->Text->truncate($text5, 20, ['ellipsis' => '', 'html' => true]));
|
|
|
+ $this->assertSame("<p><strong>Extra dates have been announced for this year's\xe2\x80\xa6</strong></p>", $this->Text->truncate($text6, 48, ['exact' => false, 'html' => true]));
|
|
|
+ $this->assertSame($text7, $this->Text->truncate($text7, 255));
|
|
|
+ $this->assertSame('El moño está...', $this->Text->truncate($text7, 15));
|
|
|
+ $this->assertSame('Vive la R' . chr(195) . chr(169) . 'pu...', $this->Text->truncate($text8, 15));
|
|
|
+ $this->assertSame('НОПРСТУ...', $this->Text->truncate($text9, 10));
|
|
|
+ $this->assertSame('http://example.com/somethin...', $this->Text->truncate($text10, 30));
|
|
|
+ $this->assertSame('1 <b>2...</b>', $this->Text->truncate('1 <b>2 345</b>', 6, ['exact' => false, 'html' => true, 'ellipsis' => '...']));
|
|
|
+ $this->assertSame('&', $this->Text->truncate('&', 1, ['html' => true]));
|
|
|
|
|
|
$text = '<p><span style="font-size: medium;"><a>Iamatestwithnospacesandhtml</a></span></p>';
|
|
|
$result = $this->Text->truncate($text, 10, [
|
|
|
@@ -576,7 +578,7 @@ TEXT;
|
|
|
'exact' => false,
|
|
|
'html' => true
|
|
|
]);
|
|
|
- $expected = '<p><span style="font-size: medium;"><a>Iamates...</a></span></p>';
|
|
|
+ $expected = '<p><span style="font-size: medium;"><a>Iamatestwi...</a></span></p>';
|
|
|
$this->assertEquals($expected, $result);
|
|
|
}
|
|
|
|
|
|
@@ -595,7 +597,7 @@ TEXT;
|
|
|
]);
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
- $expected = '<a href="http://example.org">hell..</a>';
|
|
|
+ $expected = '<a href="http://example.org">hello..</a>';
|
|
|
$result = Text::truncate($text, 6, [
|
|
|
'ellipsis' => '..',
|
|
|
'exact' => false,
|
|
|
@@ -629,6 +631,39 @@ TEXT;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Test truncate() method with trimWidth
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testTruncateTrimWidth()
|
|
|
+ {
|
|
|
+ $text = 'The quick brown fox jumps over the lazy dog';
|
|
|
+ $this->assertEquals('The quick brown...', Text::truncate($text, 18, ['ellipsis' => '...', 'trimWidth' => false]));
|
|
|
+ $this->assertEquals('The quick brown...', Text::truncate($text, 18, ['ellipsis' => '...', 'trimWidth' => true]));
|
|
|
+
|
|
|
+ $text = 'はしこい茶色の狐はのろまな犬を飛び越える';
|
|
|
+ $this->assertEquals('はしこい茶色の狐はのろまな犬を...', Text::truncate($text, 18, ['ellipsis' => '...', 'trimWidth' => false]));
|
|
|
+ $this->assertEquals('はしこい茶色の...', Text::truncate($text, 18, ['ellipsis' => '...', 'trimWidth' => true]));
|
|
|
+
|
|
|
+ $text = 'はしこい茶色の狐 - The quick brown fox';
|
|
|
+ $this->assertEquals('はしこい茶色の狐 - The quick bro...', Text::truncate($text, 27, ['ellipsis' => '...', 'trimWidth' => false]));
|
|
|
+ $this->assertEquals('はしこい茶色の狐 - The q...', Text::truncate($text, 27, ['ellipsis' => '...', 'trimWidth' => true]));
|
|
|
+ $this->assertEquals('はしこい茶色の狐 - The...', Text::truncate($text, 27, ['ellipsis' => '...', 'trimWidth' => true, 'exact' => false]));
|
|
|
+
|
|
|
+ $text = '<p>はしこい<font color="brown">茶色</font>の狐はのろまな犬を飛び越える</p>';
|
|
|
+ $this->assertEquals('<p>はしこい<font color="brown">茶色</font>の狐はのろまな犬を...</p>', Text::truncate($text, 18, ['ellipsis' => '...', 'trimWidth' => false, 'html' => true]));
|
|
|
+ $this->assertEquals('<p>はしこい<font color="brown">茶色</font>の...</p>', Text::truncate($text, 18, ['ellipsis' => '...', 'trimWidth' => true, 'html' => true]));
|
|
|
+
|
|
|
+ $text = <<<HTML
|
|
|
+<IMG src="mypic.jpg">このimageタグはXHTMLに準拠していない!<br>
|
|
|
+<hr/><b>でも次のimageタグは準拠しているはず <img src="mypic.jpg" alt="私の、私自身そして私" /></b><br />
|
|
|
+素晴らしい、でしょ?
|
|
|
+HTML;
|
|
|
+ $this->assertEquals("<IMG src=\"mypic.jpg\">このimageタグはXHTMLに準拠していない!<br>\n<hr/><b>でも次の…</b>", Text::truncate($text, 30, ['html' => true]));
|
|
|
+ $this->assertEquals("<IMG src=\"mypic.jpg\">このimageタグはXHTMLに準拠し…", Text::truncate($text, 30, ['html' => true, 'trimWidth' => true]));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* testTail method
|
|
|
*
|
|
|
* @return void
|
|
|
@@ -1748,4 +1783,102 @@ TEXT;
|
|
|
$result = Text::slug($string, $options);
|
|
|
$this->assertEquals($expected, $result);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Text truncateByWidth method
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testTruncateByWidth()
|
|
|
+ {
|
|
|
+ $this->assertSame('<p>あ...', Text::truncateByWidth('<p>あいうえお</p>', 8));
|
|
|
+ $this->assertSame('<p>あい...</p>', Text::truncateByWidth('<p>あいうえお</p>', 8, ['html' => true, 'ellipsis' => '...']));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test _strlen method
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testStrlen()
|
|
|
+ {
|
|
|
+ $method = new \ReflectionMethod('Cake\Utility\Text', '_strlen');
|
|
|
+ $method->setAccessible(true);
|
|
|
+ $strlen = function () use ($method) {
|
|
|
+ return $method->invokeArgs(null, func_get_args());
|
|
|
+ };
|
|
|
+
|
|
|
+ $text = 'データベースアクセス & ORM';
|
|
|
+ $this->assertEquals(20, $strlen($text, []));
|
|
|
+ $this->assertEquals(16, $strlen($text, ['html' => true]));
|
|
|
+ $this->assertEquals(30, $strlen($text, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals(26, $strlen($text, ['html' => true, 'trimWidth' => true]));
|
|
|
+
|
|
|
+ $text = '&undefined;';
|
|
|
+ $this->assertEquals(11, $strlen($text, []));
|
|
|
+ $this->assertEquals(11, $strlen($text, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals(11, $strlen($text, ['html' => true]));
|
|
|
+ $this->assertEquals(11, $strlen($text, ['html' => true, 'trimWidth' => true]));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test _substr method
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSubstr()
|
|
|
+ {
|
|
|
+ $method = new \ReflectionMethod('Cake\Utility\Text', '_substr');
|
|
|
+ $method->setAccessible(true);
|
|
|
+ $substr = function () use ($method) {
|
|
|
+ return $method->invokeArgs(null, func_get_args());
|
|
|
+ };
|
|
|
+
|
|
|
+ $text = 'データベースアクセス & ORM';
|
|
|
+ $this->assertEquals('アクセス', $substr($text, 6, 4, []));
|
|
|
+ $this->assertEquals('アクセス', $substr($text, 6, 8, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals('アクセス', $substr($text, 6, 4, ['html' => true]));
|
|
|
+ $this->assertEquals(' & ', $substr($text, 10, 7, []));
|
|
|
+ $this->assertEquals(' & ', $substr($text, 10, 7, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals(' & ', $substr($text, 10, 3, ['html' => true]));
|
|
|
+ $this->assertEquals(' & ', $substr($text, -10, 7, []));
|
|
|
+ $this->assertEquals(' & ', $substr($text, -10, 7, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals(' & ', $substr($text, -6, 3, ['html' => true]));
|
|
|
+ $this->assertEquals(' & ', $substr($text, -10, -3, []));
|
|
|
+ $this->assertEquals(' & ', $substr($text, -10, -3, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals(' & ', $substr($text, -6, -3, ['html' => true]));
|
|
|
+ $this->assertEquals('ORM', $substr($text, -3, 1000, []));
|
|
|
+ $this->assertEquals('ORM', $substr($text, -3, 1000, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals('ORM', $substr($text, -3, 1000, ['html' => true]));
|
|
|
+ $this->assertEquals('ORM', $substr($text, -3, null, []));
|
|
|
+ $this->assertEquals('ORM', $substr($text, -3, null, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals('ORM', $substr($text, -3, null, ['html' => true]));
|
|
|
+ $this->assertEquals('データ', $substr($text, -1000, 3, []));
|
|
|
+ $this->assertEquals('データ', $substr($text, -1000, 6, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals('データ', $substr($text, -1000, 3, ['html' => true]));
|
|
|
+ $this->assertEquals('', $substr($text, 0, 0, []));
|
|
|
+ $this->assertEquals('', $substr($text, 0, 0, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals('', $substr($text, 0, 0, ['html' => true]));
|
|
|
+ $this->assertEquals('', $substr($text, 1000, 1, []));
|
|
|
+ $this->assertEquals('', $substr($text, 1000, 1, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals('', $substr($text, 1000, 1, ['html' => true]));
|
|
|
+ $this->assertEquals('', $substr($text, 0, -1000, []));
|
|
|
+ $this->assertEquals('', $substr($text, 0, -1000, ['trimWidth' => true]));
|
|
|
+ $this->assertEquals('', $substr($text, 0, -1000, ['html' => true]));
|
|
|
+
|
|
|
+ // ABCDE
|
|
|
+ $text = 'ABCDE';
|
|
|
+ $this->assertEquals('BCD', $substr($text, 1, 3, ['html' => true]));
|
|
|
+ $this->assertEquals('BCD', $substr($text, 1, 3, ['html' => true, 'trimWidth' => true]));
|
|
|
+ $this->assertEquals('BCD', $substr($text, -4, -1, ['html' => true]));
|
|
|
+ $this->assertEquals('BCD', $substr($text, -4, -1, ['html' => true, 'trimWidth' => true]));
|
|
|
+
|
|
|
+ // あいうえお
|
|
|
+ $text = 'あいうえお';
|
|
|
+ $this->assertEquals('いうえ', $substr($text, 1, 3, ['html' => true]));
|
|
|
+ $this->assertEquals('いうえ', $substr($text, 1, 6, ['html' => true, 'trimWidth' => true]));
|
|
|
+ $this->assertEquals('いうえ', $substr($text, -4, -1, ['html' => true]));
|
|
|
+ $this->assertEquals('いうえ', $substr($text, -4, -1, ['html' => true, 'trimWidth' => true]));
|
|
|
+ $this->assertEquals('いうえ', $substr($text, -4, -2, ['html' => true, 'trimWidth' => true]));
|
|
|
+ }
|
|
|
}
|