| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- App::uses('TextExtHelper', 'Tools.View/Helper');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- class TextExtHelperTest extends MyCakeTestCase {
- public $Text;
- public function setUp() {
- parent::setUp();
- $this->Text = new TextExtHelper(new View(null));
- }
- public function testObject() {
- $this->assertInstanceOf('TextExtHelper', $this->Text);
- }
- public function testAutoLinkEmails() {
- $text = 'Text with a url euro@euro.de and more';
- $expected = 'Text with a url <a href="mailto:euro@euro.de">euro@euro.de</a> and more';
- $result = $this->Text->autoLinkEmails($text, array());
- $this->assertEquals($result, $expected);
- $text = 'Text with a url euro@euro.de and more';
- $expected = 'Text with a url <script language=javascript><!--
- document.write(\'<a\'+ \' hre\'+ \'f="ma\'+ \'ilto:\'+ \'eu\'+ \'ro@\'+ \'euro\'+ \'.d\'+ \'e"\'+ \' t\'+ \'itle\'+ \'="\'+ \'Für \'+ \'den\'+ \' G\'+ \'ebra\'+ \'uch\'+ \' eines\'+ \' exte\'+ \'rn\'+ \'en E-\'+ \'Mail-P\'+ \'rogra\'+ \'mms"\'+ \' cl\'+ \'ass="e\'+ \'mail"\'+ \'>\');
- //--></script>
- euro<span>@</span>euro.de
- <script language=javascript><!--
- document.write(\'</a>\');
- //--></script> and more\'';
- $result = $this->Text->autoLinkEmails($text, array('obfuscate'=>true));
- //pr($text);
- //echo $result;
- //pr(h($result));
- $this->assertNotEquals($result, $text);
- }
- public function testAutoLinkEmailsWithHtmlOrDangerousStrings() {
- $text = 'Text <i>with a email</i> euro@euro.de and more';
- $expected = 'Text <i>with a email</i> <a href="mailto:euro@euro.de">euro@euro.de</a> and more';
- $result = $this->Text->autoLinkEmails($text);
- //pr(h($text));
- $this->assertEquals($result, $expected);
- }
- public function testStripProtocol() {
- $urls = array(
- 'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
- 'www.cakephp.org' => 'www.cakephp.org'
- );
- foreach ($urls as $url => $expected) {
- $is = $this->Text->stripProtocol($url);
- $this->assertEquals($is, $expected);
- }
- }
- public function testAutoLinkUrls() {
- $texts = array(
- 'text http://www.cakephp.org/bla/bla some more text' => '',
- 'This is a test text with URL http://www.cakephp.org\tand some more text' => 'This is a test text with URL http://www.cakephp.org\tand some more text'
- );
- foreach ($texts as $text => $expected) {
- //$is = $this->Text->stripProtocol($url);
- //$this->assertEquals($is, $expected);
- }
- $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekr and more';
- $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekr">www.cot.ag/c...</a> and more';
- $result = $this->Text->autoLinkUrls($text, array('maxLength'=>12));
- $this->assertEquals($result, $expected);
- $text = 'Text with a url http://www.cot.ag/cuIb2Q/eru and more';
- $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eru">www.cot.ag/cuIb2Q/eru</a> and more';
- $result = $this->Text->autoLinkUrls($text, array('stripProtocol'=>true));
- $this->assertEquals($result, $expected);
- $text = 'Text with a url http://www.cot.ag/cuIb2Q/eruierieriu-erjekr and more';
- $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekr">http://www.cot.ag/cuIb2Q/eruierieriu-erjekr</a> and more';
- $result = $this->Text->autoLinkUrls($text, array('stripProtocol'=>false, 'maxLength'=>0));
- $this->assertEquals($result, $expected);
- $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer-werwerwe-werwerwer-werwerdfrffsd-werwer and more';
- $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer-werwerwe-werwerwer-werwerdfrffsd-werwer">www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-w...</a> and more';
- $result = $this->Text->autoLinkUrls($text);
- $this->assertEquals($result, $expected);
- }
- public function testAutoLinkUrlsWithEscapeFalse() {
- $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer and more';
- $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer">www.cot.ag/cuIb2Q/er...</a> and more';
- $result = $this->Text->autoLinkUrls($text, array('maxLength'=>20), array('escape'=>false));
- $this->assertEquals($result, $expected);
- # not yet working
- /*
- $text = 'Text with a url www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer and more';
- $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q/eruierieriu-erjekrwerweuwrweir-werwer">www.cot.ag/cuIb2Q/er…</a> and more';
- $result = $this->Text->autoLinkUrls($text, array('maxLength'=>20), array('escape'=>false, 'html'=>true));
- $this->assertEquals($result, $expected);
- */
- $text = '<h3>google<h3> a http://maps.google.de/maps?f=d&source=s_d&saddr=m%C3%BCnchen&daddr=Berlin&hl=de&geocode=FXaL3gIdGrOwACnZX4yj-XWeRzF9mLF9SrgMAQ%3BFY1xIQMdSKTMACkBWQM_N06oRzFwO15bRiAhBA&mra=ls&sll=52.532932,13.41156&sspn=0.77021,2.348328&g=berlin&ie=UTF8&t=h&z=6 link';
- $expected = '<h3>google<h3> a <a href="http://maps.google.de/maps?f=d&source=s_d&saddr=m%C3%BCnchen&daddr=Berlin&hl=de&geocode=FXaL3gIdGrOwACnZX4yj-XWeRzF9mLF9SrgMAQ%3BFY1xIQMdSKTMACkBWQM_N06oRzFwO15bRiAhBA&mra=ls&sll=52.532932,13.41156&sspn=0.77021,2.348328&g=berlin&ie=UTF8&t=h&z=6">maps.google.de/maps?f=d&source...</a> link';
- $result = $this->Text->autoLinkUrls($text, array('maxLength'=>30));
- $this->assertEquals($result, $expected);
- }
- public function testAutoLinkUrlsWithHtmlOrDangerousStrings() {
- $text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and more';
- $expected = 'Text <i>with a url</i> <a href="http://www.cot.ag?id=2&sub=3">www.cot.ag?id=2&sub=3</a> and more';
- $result = $this->Text->autoLinkUrls($text);
- //pr(h($text));
- $this->assertEquals($result, $expected);
- }
- /**
- * combined (emails + urls)
- * 2011-04-03 ms
- */
- public function testAutoLink() {
- $text = 'Text <i>with a url</i> www.cot.ag?id=2&sub=3 and some email@domain.com more';
- $expected = 'Text <i>with a url</i> <a href="http://www.cot.ag?id=2&sub=3">www.cot.ag?id=2&sub=3</a> and some <a href="mailto:email@domain.com">email@domain.com</a> more';
- $result = $this->Text->autoLink($text);
- //pr(h($text));
- $this->assertEquals($result, $expected);
- }
- /* from cake */
- /**
- * test invalid email addresses.
- *
- * @return void
- */
- public function testAutoLinkEmailInvalid() {
- $result = $this->Text->autoLinkEmails('this is a myaddress@gmx-de test');
- $expected = 'this is a myaddress@gmx-de test';
- $this->assertEquals($expected, $result);
- $result = $this->Text->autoLink('this is a myaddress@gmx-de test');
- $expected = 'this is a myaddress@gmx-de test';
- $this->assertEquals($expected, $result);
- }
- public function testAutoLinkUrlsWithCakeTests() {
- $text = 'This is a test text';
- $expected = 'This is a test text';
- $result = $this->Text->autoLinkUrls($text);
- $this->assertEquals($result, $expected);
- $text = 'This is a test that includes (www.cakephp.org)';
- $expected = 'This is a test that includes (<a href="http://www.cakephp.org">www.cakephp.org</a>)';
- $result = $this->Text->autoLinkUrls($text);
- $this->assertEquals($result, $expected);
- $text = 'Text with a partial www.cakephp.org URL';
- $expected = 'Text with a partial <a href="http://www.cakephp.org"\s*>www.cakephp.org</a> URL';
- $result = $this->Text->autoLinkUrls($text);
- $this->assertRegExp('#^' . $expected . '$#', $result);
- $text = 'Text with a partial www.cakephp.org URL';
- $expected = 'Text with a partial <a href="http://www.cakephp.org" \s*class="link">www.cakephp.org</a> URL';
- $result = $this->Text->autoLinkUrls($text, array(), array('class' => 'link'));
- $this->assertRegExp('#^' . $expected . '$#', $result);
- $text = 'Text with a partial WWW.cakephp.org URL';
- $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> URL';
- $result = $this->Text->autoLinkUrls($text);
- $this->assertRegExp('#^' . $expected . '$#', $result);
- $text = 'Text with a partial WWW.cakephp.org © URL';
- $expected = 'Text with a partial <a href="http://WWW.cakephp.org"\s*>WWW.cakephp.org</a> © URL';
- $result = $this->Text->autoLinkUrls($text, array('escape' => false), array('escape' => false));
- $this->assertRegExp('#^' . $expected . '$#', $result);
- $text = 'Text with a url www.cot.ag/cuIb2Q and more';
- $expected = 'Text with a url <a href="http://www.cot.ag/cuIb2Q">www.cot.ag/cuIb2Q</a> and more';
- $result = $this->Text->autoLinkUrls($text);
- $this->assertEquals($result, $expected);
- }
- /**
- * test minimizeUrl
- *
- * @access public
- * @return void
- * 2009-03-11 ms
- */
- public function testMinimizeUrl() {
- $url = 'http://www.test.de';
- $this->assertEquals($url, $this->Text->minimizeUrl($url,20));
- $url = 'http://www.test.de';
- $this->assertEquals($url, $this->Text->minimizeUrl($url,18));
- $url = 'http://www.test.de';
- $this->assertEquals('www.test.de', $this->Text->minimizeUrl($url,17));
- $url = 'http://www.testpage.de';
- $this->assertEquals('ww…ge.de', $this->Text->minimizeUrl($url,10));
- $url = 'http://www.testpage.de';
- $this->assertEquals('ww...ge.de', $this->Text->minimizeUrl($url,10, array('placeholder'=>'...')));
- # without full http://
- $url = 'www.testpage.de';
- $this->assertEquals($url, $this->Text->minimizeUrl($url,15));
- $url = 'www.testpage.de';
- $this->assertEquals('www.te…ge.de', $this->Text->minimizeUrl($url,14));
- }
- /**
- * test shortenText
- *
- * @access public
- * @return void
- * 2009-03-11 ms
- */
- public function testShortenText() {
- }
- }
|