diff --git a/src/ProfaneValidator.php b/src/ProfaneValidator.php index 769884f..e7437c2 100644 --- a/src/ProfaneValidator.php +++ b/src/ProfaneValidator.php @@ -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/'; diff --git a/src/Str.php b/src/Str.php index 228903a..ac805fe 100644 --- a/src/Str.php +++ b/src/Str.php @@ -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 = [ diff --git a/tests/ProfaneValidatorBuilder.php b/tests/ProfaneValidatorBuilder.php new file mode 100644 index 0000000..299a5d3 --- /dev/null +++ b/tests/ProfaneValidatorBuilder.php @@ -0,0 +1,48 @@ +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; + } +} diff --git a/tests/ProfaneValidatorTest.php b/tests/ProfaneValidatorTest.php index 9654201..e0eea94 100644 --- a/tests/ProfaneValidatorTest.php +++ b/tests/ProfaneValidatorTest.php @@ -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 = 'fuck you if you read this.'; - $profane = new ProfaneValidator(); - $this->assertTrue($profane->isProfane($word)); - } - - public function test_can_evaluate_as_caseless_mode() - { - $word = 'FUCK 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 = 'fuck you if you read this.'; + + $this->assertTrue($builder->build()->isProfane($word)); + } + + public function test_can_evaluate_as_caseless_mode() + { + $builder = new ProfaneValidatorBuilder(); + + $word = 'FUCK 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']])); } } diff --git a/tests/StrTest.php b/tests/StrTest.php index f80a64a..e64d919 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -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'));