Aleksander Machniak
2014-02-05 b37954110d2184279a7f400d8750996a27b8f666
commit | author | age
b37954 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     }
20
21     /**
22      * Data for test_replace()
23      */
24     function data_replace()
25     {
26         return array(
27             array('http://domain.tld/path*path2', '<a href="http://domain.tld/path*path2">http://domain.tld/path*path2</a>'),
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'),
30             array('http://localhost/?foo=bar. Period', '<a href="http://localhost/?foo=bar">http://localhost/?foo=bar</a>. Period'),
31             array('www.domain.tld', '<a href="http://www.domain.tld">www.domain.tld</a>'),
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>'),
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'),
40             array('http://<test>', 'http://<test>'),
41             array('http://', 'http://'),
42             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>'),
43             array(' www.domain.tld ', ' <a href="http://www.domain.tld">www.domain.tld</a> '),
44             array(' www.domain.tld/#!download|856p1|2 ', ' <a href="http://www.domain.tld/#!download|856p1|2">www.domain.tld/#!download|856p1|2</a> '),
45         );
46     }
47
48     /**
49      * @dataProvider data_replace
50      */
51     function test_replace($input, $output)
52     {
53         $replacer = new rcube_string_replacer;
54         $result = $replacer->replace($input);
55         $result = $replacer->resolve($result);
56
57         $this->assertEquals($output, $result);
58     }
59
60     function test_linkrefs()
61     {
62         $input = "This is a sample message [1] to test the new linkref [ref0] replacement feature of [Roundcube].\n";
63         $input.= "\n";
64         $input.= "[1] http://en.wikipedia.org/wiki/Email\n";
65         $input.= "[ref0] www.link-ref.com\n";
66
67         $replacer = new rcube_string_replacer;
68         $result = $replacer->replace($input);
69         $result = $replacer->resolve($result);
70
71         $this->assertContains('[<a href="http://en.wikipedia.org/wiki/Email">1</a>] to', $result, "Numeric linkref replacements");
72         $this->assertContains('[<a href="http://www.link-ref.com">ref0</a>] repl', $result, "Alphanum linkref replacements");
73         $this->assertContains('of [Roundcube].', $result, "Don't touch strings wihtout an index entry");
74     }
75 }