| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\TextExtracts\Test; |
| 4 |
|
| 5 |
use MediaWiki\Extension\TextExtracts\TextTruncator; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class TextTruncatorTest extends \PHPUnit\Framework\TestCase { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function testGetFirstSentences( $text, $sentences, $expected ) { |
| 21 |
$truncator = new TextTruncator( false ); |
| 22 |
$this->assertSame( $expected, $truncator->getFirstSentences( $text, $sentences ) ); |
| 23 |
} |
| 24 |
|
| 25 |
public static function provideGetFirstSentences() { |
| 26 |
$longLine = str_repeat( 'word ', 1000000 ); |
| 27 |
return [ |
| 28 |
[ |
| 29 |
'Foo is a bar. Such a smart boy. But completely useless.', |
| 30 |
2, |
| 31 |
'Foo is a bar. Such a smart boy.', |
| 32 |
], |
| 33 |
[ |
| 34 |
'Foo is a bar. Such a smart boy. But completely useless.', |
| 35 |
1, |
| 36 |
'Foo is a bar.', |
| 37 |
], |
| 38 |
[ |
| 39 |
'Foo is a bar. Such a smart boy.', |
| 40 |
2, |
| 41 |
'Foo is a bar. Such a smart boy.', |
| 42 |
], |
| 43 |
[ |
| 44 |
'Foo is a bar.', |
| 45 |
1, |
| 46 |
'Foo is a bar.', |
| 47 |
], |
| 48 |
[ |
| 49 |
'Foo is a bar.', |
| 50 |
2, |
| 51 |
'Foo is a bar.', |
| 52 |
], |
| 53 |
[ |
| 54 |
'', |
| 55 |
1, |
| 56 |
'', |
| 57 |
], |
| 58 |
'0 sentences mean empty result' => [ |
| 59 |
'Foo is a bar. Such a smart boy.', |
| 60 |
0, |
| 61 |
'', |
| 62 |
], |
| 63 |
"Don't explode on negative input" => [ |
| 64 |
'Foo is a bar. Such a smart boy.', |
| 65 |
-1, |
| 66 |
'', |
| 67 |
], |
| 68 |
'More sentences requested than is available' => [ |
| 69 |
'Foo is a bar. Such a smart boy.', |
| 70 |
3, |
| 71 |
'Foo is a bar. Such a smart boy.', |
| 72 |
], |
| 73 |
|
| 74 |
[ |
| 75 |
'Foo is a bar! Such a smart boy! But completely useless!', |
| 76 |
1, |
| 77 |
'Foo is a bar!', |
| 78 |
], |
| 79 |
|
| 80 |
[ |
| 81 |
"Acid phosphatase (EC 3.1.3.2) is a chemical you don't want to mess with. " . |
| 82 |
"Polyvinyl acetate, however, is another story.", |
| 83 |
1, |
| 84 |
"Acid phosphatase (EC 3.1.3.2) is a chemical you don't want to mess with.", |
| 85 |
], |
| 86 |
|
| 87 |
[ |
| 88 |
"foo\nbar\nbaz", |
| 89 |
2, |
| 90 |
'foo', |
| 91 |
], |
| 92 |
|
| 93 |
[ |
| 94 |
'Foo was born in 1977. He enjoys listening to Siouxsie and the Banshees.', |
| 95 |
1, |
| 96 |
'Foo was born in 1977.', |
| 97 |
], |
| 98 |
|
| 99 |
[ |
| 100 |
'P.J. Harvey is a singer. She is awesome!', |
| 101 |
1, |
| 102 |
'P.J. Harvey is a singer.', |
| 103 |
], |
| 104 |
|
| 105 |
[ |
| 106 |
html_entity_decode( 'Pigeons (lat. Columbidae) are birds. ' . |
| 107 |
'They primarily feed on seeds.' ), |
| 108 |
1, |
| 109 |
html_entity_decode( 'Pigeons (lat. Columbidae) are birds.' ), |
| 110 |
], |
| 111 |
|
| 112 |
[ |
| 113 |
$longLine, |
| 114 |
3, |
| 115 |
trim( $longLine ), |
| 116 |
], |
| 117 |
[ |
| 118 |
str_repeat( 'Sentence. ', 70000 ), |
| 119 |
65536, |
| 120 |
trim( str_repeat( 'Sentence. ', 65536 ) ), |
| 121 |
], |
| 122 |
|
| 123 |
'Preserve whitespace before end character' => [ |
| 124 |
'Aa . Bb', |
| 125 |
1, |
| 126 |
'Aa .', |
| 127 |
], |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function testGetFirstChars( $text, $chars, $expected ) { |
| 138 |
$truncator = new TextTruncator( false ); |
| 139 |
$this->assertSame( $expected, $truncator->getFirstChars( $text, $chars ) ); |
| 140 |
} |
| 141 |
|
| 142 |
public static function provideGetFirstChars() { |
| 143 |
$text = 'Lullzy lulz are lullzy!'; |
| 144 |
$html = 'foo<tag>bar</tag>'; |
| 145 |
$longText = str_repeat( 'тест ', 50000 ); |
| 146 |
$longTextExpected = trim( str_repeat( 'тест ', 13108 ) ); |
| 147 |
|
| 148 |
return [ |
| 149 |
[ $text, -8, '' ], |
| 150 |
[ $text, 0, '' ], |
| 151 |
[ $text, 100, $text ], |
| 152 |
[ $text, 1, 'Lullzy' ], |
| 153 |
[ $text, 6, 'Lullzy' ], |
| 154 |
|
| 155 |
[ $text, 8, 'Lullzy lulz' ], |
| 156 |
|
| 157 |
[ $html, 1, 'foo' ], |
| 158 |
|
| 159 |
[ $html, 4, 'foo<tag>' ], |
| 160 |
[ $html, 12, 'foo<tag>bar</tag>' ], |
| 161 |
[ $html, 13, 'foo<tag>bar</tag>' ], |
| 162 |
[ $html, 16, 'foo<tag>bar</tag>' ], |
| 163 |
[ $html, 17, 'foo<tag>bar</tag>' ], |
| 164 |
|
| 165 |
|
| 166 |
[ $longText, 65536, $longTextExpected ], |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
public function testTidyIntegration() { |
| 171 |
$truncator = new TextTruncator( true ); |
| 172 |
|
| 173 |
$text = '<b>Aa. Bb.</b>'; |
| 174 |
$this->assertSame( '<p><b>Aa.</b></p>', $truncator->getFirstSentences( $text, 1 ) ); |
| 175 |
$this->assertSame( '<p><b>Aa</b></p>', $truncator->getFirstChars( $text, 4 ) ); |
| 176 |
} |
| 177 |
|
| 178 |
} |