Merge branch 'dorianneto-master'

This commit is contained in:
Arandi Lopez 2017-09-27 17:27:09 -05:00
commit 0f02589df8
5 changed files with 196 additions and 88 deletions

View File

@ -7,9 +7,15 @@ use Illuminate\Contracts\Validation\Validator;
class ProfaneValidator
{
/**
* [$badwords description]
* @var array
*/
protected $badwords = [];
/**
* [__construct description]
*/
function __construct()
{
// Get default locale string in laravel project
@ -49,6 +55,10 @@ class ProfaneValidator
);
}
/**
* [getBadwords description]
* @return [type] [description]
*/
public function getBadwords()
{
return $this->badwords;
@ -63,6 +73,11 @@ class ProfaneValidator
$this->badwords = $this->readDictionary($dictionary);
}
/**
* [readDictionary description]
* @param [type] $dictionary [description]
* @return [type] [description]
*/
protected function readDictionary($dictionary)
{
$badwords = [];
@ -95,6 +110,10 @@ class ProfaneValidator
return $badwords;
}
/**
* [getBaseDictPath description]
* @return [type] [description]
*/
protected function getBaseDictPath()
{
return property_exists($this, 'baseDictPath') ? $this->baseDictPath : __DIR__ . DIRECTORY_SEPARATOR .'dict/';

View File

@ -23,6 +23,11 @@ class Str
return false;
}
/**
* Remove accents or special characters from a string.
* @param string $string
* @return string
*/
public static function removeAccent($string)
{
$replace = [

View File

@ -0,0 +1,48 @@
<?php
namespace LaravelProfaneTests;
use LaravelProfane\ProfaneValidator;
class ProfaneValidatorBuilder
{
/**
* [$profaneValidator description]
* @var [type]
*/
protected $profaneValidator;
/**
* [__construct description]
* @param [type] $dictionary [description]
*/
public function __construct($dictionary = null)
{
$this->profaneValidator = new ProfaneValidator;
if ($dictionary) {
$this->profaneValidator->setDictionary($dictionary);
}
}
/**
* [validate description]
* @param array $parameters [description]
* @return [type] [description]
*/
public function validate(array $parameters)
{
list($attribute, $text, $dictionaries) = $parameters;
return $this->build()->validate($attribute, $text, $dictionaries);
}
/**
* [build description]
* @return LaravelProfane\ProfaneValidator
*/
public function build()
{
return $this->profaneValidator;
}
}

View File

@ -2,140 +2,156 @@
namespace LaravelProfaneTests;
use LaravelProfaneTests\TestCase;
use LaravelProfane\ProfaneValidator;
use LaravelProfaneTests\TestCase;
use LaravelProfaneTests\ProfaneValidatorBuilder;
class ProfaneValidatorTest extends TestCase
{
public function test_can_validate_a_word_with_numbers()
{
$attribute = 'username';
$word = 'culero23';
$parameters = ['es'];
$profane = new ProfaneValidator();
$this->assertFalse($profane->validate($attribute, $word, $parameters));
}
public function test_can_validate_a_text()
{
$attribute = 'description';
$text = 'fck you bitch. 幹!';
$parameters = ['es', 'en' , 'zh-tw'];
$profane = new ProfaneValidator();
$this->assertFalse($profane->validate($attribute, $text, $parameters));
}
public function test_can_evaluate_profanity_of_a_word()
{
$word = 'fuck';
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
public function test_can_evaluate_profanity_of_a_sentence()
{
$word = 'fuck you if you read this';
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
public function test_can_evaluate_profanity_of_a_html_string()
{
$word = '<b>fuck</b> you if you read this.';
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
public function test_can_evaluate_as_caseless_mode()
{
$word = '<b>FUCK</b> you BITCH if you read this.';
$profane = new ProfaneValidator();
$this->assertTrue($profane->isProfane($word));
}
public function test_match_exact_word()
{
$profane = new ProfaneValidator();
// it thinks class ~= ass
$this->assertTrue($profane->isProfane('class'));
// but this should be profane
$this->assertTrue($profane->isProfane('sucker96'));
}
public function test_can_set_dictionary_when_you_pass_a_locale()
{
$profane = new ProfaneValidator();
$profane->setDictionary('es');
$builder = new ProfaneValidatorBuilder('es');
$expected = include __DIR__.'/../src/dict/es.php';
$this->assertEquals($profane->getBadwords(), $expected);
$this->assertEquals($builder->build()->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_a_file()
{
$profane = new ProfaneValidator();
$profane->setDictionary(__DIR__.'/../src/dict/es.php');
$builder = new ProfaneValidatorBuilder(__DIR__.'/../src/dict/es.php');
$expected = include __DIR__.'/../src/dict/es.php';
$this->assertEquals($profane->getBadwords(), $expected);
$this->assertEquals($builder->build()->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_an_array_of_files()
{
$profane = new ProfaneValidator();
$profane->setDictionary([__DIR__.'/../src/dict/es.php', __DIR__.'/../src/dict/en.php']);
$builder = new ProfaneValidatorBuilder([
__DIR__.'/../src/dict/es.php',
__DIR__.'/../src/dict/en.php'
]);
$expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php');
$expected = array_merge(
include __DIR__.'/../src/dict/es.php',
include __DIR__.'/../src/dict/en.php'
);
$this->assertEquals($profane->getBadwords(), $expected);
$this->assertEquals($builder->build()->getBadwords(), $expected);
}
public function test_can_set_dictionary_when_you_pass_an_array_of_locales()
{
$profane = new ProfaneValidator();
$profane->setDictionary(['es', 'en', 'it', 'zh-tw']);
$expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/it.php', include __DIR__.'/../src/dict/zh-tw.php');
$this->assertEquals($profane->getBadwords(), $expected);
$builder = new ProfaneValidatorBuilder(['es', 'en', 'it', 'zh-tw']);
$expected = array_merge(
include __DIR__.'/../src/dict/es.php',
include __DIR__.'/../src/dict/en.php',
include __DIR__.'/../src/dict/it.php',
include __DIR__.'/../src/dict/zh-tw.php'
);
$this->assertEquals($builder->build()->getBadwords(), $expected);
}
public function test_can_validate_a_word_with_numbers()
{
$builder = new ProfaneValidatorBuilder();
$this->assertFalse($builder->validate(['username', 'culero23', ['es']]));
}
public function test_can_validate_a_text()
{
$builder = new ProfaneValidatorBuilder();
$this->assertFalse($builder->validate(['description', 'fck you bitch. 幹!', ['es', 'en' , 'zh-tw']]));
}
public function test_can_evaluate_profanity_of_a_word()
{
$builder = new ProfaneValidatorBuilder();
$word = 'fuck';
$this->assertTrue($builder->build()->isProfane($word));
}
public function test_can_evaluate_profanity_of_a_sentence()
{
$builder = new ProfaneValidatorBuilder();
$word = 'fuck you if you read this';
$this->assertTrue($builder->build()->isProfane($word));
}
public function test_can_evaluate_profanity_of_a_html_string()
{
$builder = new ProfaneValidatorBuilder();
$word = '<b>fuck</b> you if you read this.';
$this->assertTrue($builder->build()->isProfane($word));
}
public function test_can_evaluate_as_caseless_mode()
{
$builder = new ProfaneValidatorBuilder();
$word = '<b>FUCK</b> you BITCH if you read this.';
$this->assertTrue($builder->build()->isProfane($word));
}
public function test_match_exact_word()
{
$builder = new ProfaneValidatorBuilder();
// it thinks class ~= ass
$this->assertTrue($builder->build()->isProfane('class'));
// but this should be profane
$this->assertTrue($builder->build()->isProfane('sucker96'));
}
public function test_can_validate_a_bad_word_with_accent()
{
$profane = new ProfaneValidator();
$profane->setDictionary('sk');
$builder = new ProfaneValidatorBuilder('sk');
$word = "piča";
$this->assertTrue( $profane->isProfane($word) );
$this->assertTrue($builder->build()->isProfane($word));
}
public function test_enie_in_spanish_is_evaluated()
{
$profane = new ProfaneValidator();
$profane->setDictionary('es');
$word = "coño";
$builder = new ProfaneValidatorBuilder('es');
// in spanish coño =! cono
$this->assertTrue( $profane->isProfane($word) );
$word = "coño";
$this->assertTrue($builder->build()->isProfane($word));
}
public function test_can_validate_a_word_in_greek()
{
$this->mockConfigs();
$profane = new ProfaneValidator();
$profane->setDictionary('gr');
$builder = new ProfaneValidatorBuilder('gr');
$word = "μαλάκας";
$this->assertTrue($profane->isProfane($word));
$this->assertTrue($builder->build()->isProfane($word));
}
public function test_can_validate_a_text_in_greek()
{
$this->mockConfigs();
$attribute = 'description';
$text = 'εισαι πουτανα';
$parameters = ['en' , 'gr'];
$profane = new ProfaneValidator();
$builder = new ProfaneValidatorBuilder();
$this->assertFalse($profane->validate($attribute, $text, $parameters));
$this->assertFalse($builder->validate(['description', 'εισαι πουτανα', ['en' , 'gr']]));
}
}

View File

@ -7,6 +7,26 @@ use LaravelProfane\Str;
class StrTest extends TestCase
{
public function test_string_contains_a_piece_insensitive_match_from_text()
{
$this->assertTrue(Str::containsCaseless('Fuck! This class is so bad!', 'ass'));
}
public function test_text_contains_insensitive_match_from_array()
{
$this->assertTrue(Str::containsCaseless('Fuck! This class is so bad!', ['fuk', 'fuck']));
}
public function test_text_contains_insensitive_match_from_string()
{
$this->assertTrue(Str::containsCaseless('Fuck! This class is so bad!', 'fUcK'));
}
public function test_text_contains_the_same_insensitive_match_from_string()
{
$this->assertTrue(Str::containsCaseless('Fuck! This class is so bad!', 'Fuck'));
}
public function test_remove_accents_in_spanish_text()
{
$this->assertEquals('cojon', Str::removeAccent('cojón'));