112 lines
5.2 KiB
PHP
112 lines
5.2 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Tests\Unit;
|
||
|
|
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use StreamGoals\Core\ProfanityFilter;
|
||
|
|
use StreamGoals\Core\Settings;
|
||
|
|
use StreamGoals\PayPal\OrderValidator;
|
||
|
|
use StreamGoals\Core\FeeCalculator;
|
||
|
|
|
||
|
|
final class ProfanityFilterTest extends TestCase {
|
||
|
|
|
||
|
|
private function f( array $o = array() ): ProfanityFilter {
|
||
|
|
return new ProfanityFilter( array_merge( Settings::defaults(), $o ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_builtin_masks_with_equal_length_case_insensitive(): void {
|
||
|
|
$this->assertSame( 'what the **** is this', $this->f()->filter( 'what the shit is this' ) );
|
||
|
|
$this->assertSame( 'what the **** is this', $this->f()->filter( 'what the SHiT is this' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_whole_word_only_scunthorpe(): void {
|
||
|
|
$f = $this->f( array( 'profanity_words' => "ass\ncock" ) );
|
||
|
|
foreach ( array( 'Scunthorpe', 'class', 'assist', 'Cockburn', 'bassist', 'passage', 'shitake' ) as $w ) {
|
||
|
|
$this->assertSame( $w, $f->filter( $w ), $w );
|
||
|
|
}
|
||
|
|
$this->assertSame( 'a *** b', $f->filter( 'a ass b' ) );
|
||
|
|
$this->assertSame( '***, ***!', $f->filter( 'ass, ass!' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_leetspeak(): void {
|
||
|
|
$f = $this->f();
|
||
|
|
$this->assertSame( '****', $f->filter( 'sh1t' ) );
|
||
|
|
$this->assertSame( '****', $f->filter( '$h!t' ) );
|
||
|
|
$f2 = $this->f( array( 'profanity_words' => 'bad' ) );
|
||
|
|
$this->assertSame( '***', $f2->filter( 'b@d' ) );
|
||
|
|
$this->assertSame( '***', $f2->filter( 'B4D' ) );
|
||
|
|
$f3 = $this->f( array( 'profanity_words' => 'fool' ) );
|
||
|
|
$this->assertSame( '****', $f3->filter( 'f00l' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_pure_numbers_and_trailing_punctuation_untouched(): void {
|
||
|
|
$f = $this->f( array( 'profanity_words' => "ass\nsose" ) );
|
||
|
|
$this->assertSame( '455 and 5053', $f->filter( '455 and 5053' ) ); // "ass"/"sose" via leet, but no letters
|
||
|
|
$this->assertSame( 'wow! great!!', $f->filter( 'wow! great!!' ) );
|
||
|
|
$this->assertSame( 'hello!! ***!!', $f->filter( 'hello!! ass!!' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_unicode(): void {
|
||
|
|
$f = $this->f( array( 'profanity_words' => "scheiße\nмат\nÄRGER" ) );
|
||
|
|
$this->assertSame( 'oh ******* no', $f->filter( 'oh Scheiße no' ) );
|
||
|
|
$this->assertSame( 'это ***', $f->filter( 'это МАТ' ) );
|
||
|
|
$this->assertSame( '*****!', $f->filter( 'ärger!' ) );
|
||
|
|
$this->assertSame( 'матрос', $f->filter( 'матрос' ) );
|
||
|
|
$this->assertSame( '😀 *** 😀', $f->filter( '😀 мат 😀' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_streamer_words_parsing(): void {
|
||
|
|
$raw = "# comment\n\n foo \r\nBAR\n#baz\ntwo words\n" . str_repeat( 'x', 41 ) . "\nfoo\n";
|
||
|
|
$this->assertSame( array( 'foo', 'BAR' ), ProfanityFilter::parseWords( $raw ) );
|
||
|
|
$f = $this->f( array( 'profanity_use_builtin' => false, 'profanity_words' => $raw ) );
|
||
|
|
$this->assertSame( '*** *** baz words', $f->filter( 'foo bar baz words' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_word_count_cap(): void {
|
||
|
|
$raw = implode( "\n", array_map( static fn( $i ) => 'w' . $i, range( 1, 2000 ) ) );
|
||
|
|
$this->assertLessThanOrEqual( ProfanityFilter::MAX_WORDS, count( ProfanityFilter::parseWords( $raw ) ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_empty_list_and_builtin_off(): void {
|
||
|
|
$f = $this->f( array( 'profanity_use_builtin' => false, 'profanity_words' => '' ) );
|
||
|
|
$this->assertFalse( $f->isEnabled() );
|
||
|
|
$this->assertSame( 'shit happens', $f->filter( 'shit happens' ) );
|
||
|
|
$f = $this->f( array( 'profanity_use_builtin' => false, 'profanity_words' => 'zzz' ) );
|
||
|
|
$this->assertSame( 'shit ***', $f->filter( 'shit zzz' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_master_switch_off(): void {
|
||
|
|
$f = $this->f( array( 'profanity_filter' => false, 'profanity_words' => 'foo' ) );
|
||
|
|
$this->assertSame( 'shit foo', $f->filter( 'shit foo' ) );
|
||
|
|
$f = $this->f( array( 'profanity_filter' => '0' ) );
|
||
|
|
$this->assertSame( 'shit', $f->filter( 'shit' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_never_throws_on_odd_input(): void {
|
||
|
|
$f = $this->f();
|
||
|
|
$this->assertSame( '', $f->filter( '' ) );
|
||
|
|
$bad = "ok \xC3\x28 shit \xFF";
|
||
|
|
$out = $f->filter( $bad );
|
||
|
|
$this->assertIsString( $out );
|
||
|
|
$this->assertStringContainsString( '****', $out );
|
||
|
|
$this->assertIsString( $f->filter( str_repeat( 'a', 100000 ) ) );
|
||
|
|
$this->assertSame( '\\ $ @ ! ? ^ (', $f->filter( '\\ $ @ ! ? ^ (' ) );
|
||
|
|
$weird = new ProfanityFilter( array( 'profanity_words' => array( 'x' ), 'profanity_filter' => null ) );
|
||
|
|
$this->assertIsString( $weird->filter( 'x' ) );
|
||
|
|
$this->assertIsString( ( new ProfanityFilter( array() ) )->filter( 'shit' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_validator_filters_name_and_message_before_storing(): void {
|
||
|
|
$goal = array( 'id' => 3, 'currency' => 'USD', 'status' => 'active', 'starts_at' => null, 'ends_at' => null );
|
||
|
|
$s = array_merge( Settings::defaults(), array( 'profanity_words' => 'jerk' ) );
|
||
|
|
$v = OrderValidator::validate( array( 'amount' => '10.00', 'display_name' => 'Big Jerk', 'message' => 'you shit, class act' ), $goal, $s, new FeeCalculator( 349, 49 ), 1800000000 );
|
||
|
|
$this->assertSame( 'Big ****', $v['donor_name'] );
|
||
|
|
$this->assertSame( 'you ****, class act', $v['message'] );
|
||
|
|
|
||
|
|
$s['profanity_filter'] = false;
|
||
|
|
$v = OrderValidator::validate( array( 'amount' => '10.00', 'display_name' => 'Big Jerk', 'message' => 'shit' ), $goal, $s, new FeeCalculator( 349, 49 ), 1800000000 );
|
||
|
|
$this->assertSame( 'Big Jerk', $v['donor_name'] );
|
||
|
|
$this->assertSame( 'shit', $v['message'] );
|
||
|
|
}
|
||
|
|
}
|