Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
8.45% |
6 / 71 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_text_processing_make_clickable_test | |
8.45% |
6 / 71 |
|
50.00% |
2 / 4 |
428.90 | |
0.00% |
0 / 1 |
| make_clickable_data | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
272 | |||
| test_make_clickable | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| make_clickable_mixed_serverurl_data | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
| test_make_clickable_mixed_serverurl | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | class phpbb_text_processing_make_clickable_test extends phpbb_test_case |
| 15 | { |
| 16 | public static function make_clickable_data() |
| 17 | { |
| 18 | // value => whether it should work |
| 19 | $prefix_texts = array( |
| 20 | '' => true, |
| 21 | "np \n" => true, |
| 22 | 'bp text ' => true, |
| 23 | 'cp text>' => true, |
| 24 | 'ep text.' => array('w' => false), // doesn't work for www. type urls, but for everything else |
| 25 | ); |
| 26 | $suffix_texts = array( |
| 27 | '' => true, |
| 28 | "\n ns" => true, |
| 29 | ' bs text.' => true, |
| 30 | '>cs text' => true, |
| 31 | '"ds text' => true, |
| 32 | '. es text.' => true, |
| 33 | ', fs text.' => true, |
| 34 | ); |
| 35 | |
| 36 | $urls = array( |
| 37 | 'http://example.com' => array('tag' => 'm', 'url' => false, 'text' => false), // false means same as key |
| 38 | 'http://example.com/' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 39 | 'http://example.com/path?query=abc' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 40 | 'http://example.com/1' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 41 | 'http://example.com/some/very/long/path/with/over/55/characters?and=a&long=query&too=1' => array('tag' => 'm', 'url' => false, 'text' => 'http://example.com/some/very/long/path/ ... uery&too=1'), |
| 42 | 'http://localhost' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 43 | 'http://localhost/#abc' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 44 | |
| 45 | 'www.example.com/path/' => array('tag' => 'w', 'url' => 'http://www.example.com/path/', 'text' => false), |
| 46 | 'randomwww.example.com/path/' => false, |
| 47 | |
| 48 | 'http://thisdomain.org' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 49 | 'http://thisdomain.org/' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 50 | 'http://thisdomain.org/1' => array('tag' => 'l', 'url' => false, 'text' => '1'), |
| 51 | 'http://thisdomain.org/path/some?query=abc#test' => array('tag' => 'l', 'url' => false, 'text' => 'path/some?query=abc#test'), |
| 52 | |
| 53 | 'javascript:www.example.com/' => false, |
| 54 | ); |
| 55 | |
| 56 | $test_data = array(); |
| 57 | |
| 58 | // run the test for each combination |
| 59 | foreach ($prefix_texts as $prefix => $prefix_success) |
| 60 | { |
| 61 | foreach ($suffix_texts as $suffix => $suffix_success) |
| 62 | { |
| 63 | foreach ($urls as $url => $url_type) |
| 64 | { |
| 65 | $input = $prefix . $url . $suffix; |
| 66 | // no valid url => no change |
| 67 | $output = $input; |
| 68 | |
| 69 | if ( |
| 70 | ($prefix_success && $suffix_success && is_array($url_type)) && |
| 71 | // handle except syntax for prefix/suffix |
| 72 | (!is_array($prefix_success) || !isset($prefix_success[$url_type['tag']]) || $prefix_success[$url_type['tag']] == true) && |
| 73 | (!is_array($suffix_success) || !isset($suffix_success[$url_type['tag']]) || $suffix_success[$url_type['tag']] == true) |
| 74 | ) |
| 75 | { |
| 76 | // false means it's the same as the url, less typing |
| 77 | $url_type['url'] = ($url_type['url']) ? $url_type['url'] : $url; |
| 78 | $url_type['text'] = ($url_type['text']) ? $url_type['text'] : $url; |
| 79 | |
| 80 | $class = ($url_type['tag'] === 'l') ? 'postlink-local' : 'postlink'; |
| 81 | |
| 82 | // replace the url with the desired output format |
| 83 | $output = $prefix . '<!-- ' . $url_type['tag'] . ' --><a class="' . $class . '" href="' . $url_type['url'] . '">' . $url_type['text'] . '</a><!-- ' . $url_type['tag'] . ' -->' . $suffix; |
| 84 | } |
| 85 | $test_data[] = array($input, $output); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return $test_data; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @dataProvider make_clickable_data |
| 95 | */ |
| 96 | public function test_make_clickable($input, $expected) |
| 97 | { |
| 98 | $result = make_clickable($input, 'http://thisdomain.org'); |
| 99 | |
| 100 | $label = 'Making text clickable: ' . $input; |
| 101 | $this->assertEquals($expected, $result, $label); |
| 102 | } |
| 103 | |
| 104 | public static function make_clickable_mixed_serverurl_data() |
| 105 | { |
| 106 | $urls = array( |
| 107 | 'http://thisdomain.org' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 108 | 'http://thisdomain.org/' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 109 | 'http://thisdomain.org/1' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 110 | 'http://thisdomain.org/path/some?query=abc#test' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 111 | |
| 112 | 'https://www.phpbb.com' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 113 | 'https://www.phpbb.com/' => array('tag' => 'm', 'url' => false, 'text' => false), |
| 114 | 'https://www.phpbb.com/1' => array('tag' => 'l', 'url' => false, 'text' => '1'), |
| 115 | 'https://www.phpbb.com/path/some?query=abc#test' => array('tag' => 'l', 'url' => false, 'text' => 'path/some?query=abc#test'), |
| 116 | ); |
| 117 | |
| 118 | $test_data = array(); |
| 119 | |
| 120 | // run the test for each combination |
| 121 | foreach ($urls as $url => $url_type) |
| 122 | { |
| 123 | // false means it's the same as the url, less typing |
| 124 | $url_type['url'] = ($url_type['url']) ? $url_type['url'] : $url; |
| 125 | $url_type['text'] = ($url_type['text']) ? $url_type['text'] : $url; |
| 126 | |
| 127 | $class = ($url_type['tag'] === 'l') ? 'postlink-local' : 'postlink'; |
| 128 | |
| 129 | // replace the url with the desired output format |
| 130 | $output = '<!-- ' . $url_type['tag'] . ' --><a class="' . $class . '" href="' . $url_type['url'] . '">' . $url_type['text'] . '</a><!-- ' . $url_type['tag'] . ' -->'; |
| 131 | |
| 132 | $test_data[] = array($url, $output); |
| 133 | } |
| 134 | |
| 135 | return $test_data; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @dataProvider make_clickable_mixed_serverurl_data |
| 140 | */ |
| 141 | public function test_make_clickable_mixed_serverurl($input, $expected) |
| 142 | { |
| 143 | $result = make_clickable($input, 'https://www.phpbb.com'); |
| 144 | |
| 145 | $label = 'Making text clickable: ' . $input; |
| 146 | $this->assertEquals($expected, $result, $label); |
| 147 | } |
| 148 | |
| 149 | } |
| 150 |