Aleksander Machniak
2016-04-12 e8ab3d96bd4340fb262e5d83a2e5988f47bba3b0
commit | author | age
5f8ada 1 <?php
AM 2
3 /**
4  * Test class to test rcube_string_replacer class
5  *
6  * @package Tests
7  */
8 class Framework_StringReplacer extends PHPUnit_Framework_TestCase
9 {
10
11     /**
12      * Class constructor
13      */
14     function test_class()
15     {
16         $sr = new rcube_string_replacer;
17
18         $this->assertInstanceOf('rcube_string_replacer', $sr, "Class constructor");
19     }
22c67d 20
AM 21     /**
22      * Data for test_replace()
23      */
24     function data_replace()
25     {
26         return array(
1e3254 27             array('http://domain.tld/path*path2', '<a href="http://domain.tld/path*path2">http://domain.tld/path*path2</a>'),
AM 28             array("Click this link:\nhttps://mail.xn--brderli-o2a.ch/rc/ EOF", "Click this link:\n<a href=\"https://mail.xn--brderli-o2a.ch/rc/\">https://mail.xn--brderli-o2a.ch/rc/</a> EOF"),
29             array('Start http://localhost/?foo End', 'Start <a href="http://localhost/?foo">http://localhost/?foo</a> End'),
cff07b 30             array('http://localhost/?foo=bar. Period', '<a href="http://localhost/?foo=bar">http://localhost/?foo=bar</a>. Period'),
1e3254 31             array('www.domain.tld', '<a href="http://www.domain.tld">www.domain.tld</a>'),
AM 32             array('WWW.DOMAIN.TLD', '<a href="http://WWW.DOMAIN.TLD">WWW.DOMAIN.TLD</a>'),
33             array('[http://link.com]', '[<a href="http://link.com">http://link.com</a>]'),
34             array('http://link.com?a[]=1', '<a href="http://link.com?a[]=1">http://link.com?a[]=1</a>'),
35             array('http://link.com?a[]', '<a href="http://link.com?a[]">http://link.com?a[]</a>'),
36             array('(http://link.com)', '(<a href="http://link.com">http://link.com</a>)'),
37             array('http://link.com?a(b)c', '<a href="http://link.com?a(b)c">http://link.com?a(b)c</a>'),
38             array('http://link.com?(link)', '<a href="http://link.com?(link)">http://link.com?(link)</a>'),
cff07b 39             array('https://github.com/a/b/compare/3a0f82...1f4b2a after', '<a href="https://github.com/a/b/compare/3a0f82...1f4b2a">https://github.com/a/b/compare/3a0f82...1f4b2a</a> after'),
16915e 40             array('http://<test>', 'http://<test>'),
AM 41             array('http://', 'http://'),
e8ab3d 42             array('test@www.test', '<a href="mailto:test@www.test">test@www.test</a>'),
70229c 43             array('1@1.com www.domain.tld', '<a href="mailto:1@1.com">1@1.com</a> <a href="http://www.domain.tld">www.domain.tld</a>'),
AM 44             array(' www.domain.tld ', ' <a href="http://www.domain.tld">www.domain.tld</a> '),
0bac7b 45             array(' www.domain.tld/#!download|856p1|2 ', ' <a href="http://www.domain.tld/#!download|856p1|2">www.domain.tld/#!download|856p1|2</a> '),
001d33 46             // #1489898: allow some unicode characters
AM 47             array('https://www.google.com/maps/place/New+York,+État+de+New+York/@40.7056308,-73.9780035,11z/data=!3m1!4b1!4m2!3m1!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62',
48                 '<a href="https://www.google.com/maps/place/New+York,+État+de+New+York/@40.7056308,-73.9780035,11z/data=!3m1!4b1!4m2!3m1!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62">https://www.google.com/maps/place/New+York,+État+de+New+York/@40.7056308,-73.9780035,11z/data=!3m1!4b1!4m2!3m1!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62</a>'
49             ),
22c67d 50         );
AM 51     }
52
53     /**
54      * @dataProvider data_replace
55      */
56     function test_replace($input, $output)
57     {
58         $replacer = new rcube_string_replacer;
59         $result = $replacer->replace($input);
60         $result = $replacer->resolve($result);
61
62         $this->assertEquals($output, $result);
63     }
e480ca 64
TB 65     function test_linkrefs()
66     {
67         $input = "This is a sample message [1] to test the new linkref [ref0] replacement feature of [Roundcube].\n";
68         $input.= "\n";
69         $input.= "[1] http://en.wikipedia.org/wiki/Email\n";
70         $input.= "[ref0] www.link-ref.com\n";
71
72         $replacer = new rcube_string_replacer;
73         $result = $replacer->replace($input);
74         $result = $replacer->resolve($result);
75
76         $this->assertContains('[<a href="http://en.wikipedia.org/wiki/Email">1</a>] to', $result, "Numeric linkref replacements");
77         $this->assertContains('[<a href="http://www.link-ref.com">ref0</a>] repl', $result, "Alphanum linkref replacements");
78         $this->assertContains('of [Roundcube].', $result, "Don't touch strings wihtout an index entry");
79     }
5f8ada 80 }