From e61adc906687d25c50c05ce031177776bf3c5df1 Mon Sep 17 00:00:00 2001 From: Dorian Neto Date: Mon, 25 Sep 2017 19:10:55 -0300 Subject: [PATCH 1/4] Tests to the "containsCaseless" method from class Str --- src/Str.php | 5 +++++ tests/StrTest.php | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) 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/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')); From c51d2fc6720daae471090ed8a7ab73b241730d34 Mon Sep 17 00:00:00 2001 From: Dorian Neto Date: Tue, 26 Sep 2017 18:36:06 -0300 Subject: [PATCH 2/4] Comments added in validator file methods --- src/ProfaneValidator.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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/'; From 4e96be7f3c64392035d23ab6d50940adf4046a5f Mon Sep 17 00:00:00 2001 From: Dorian Neto Date: Tue, 26 Sep 2017 18:37:22 -0300 Subject: [PATCH 3/4] Builder class created to help the tests --- tests/ProfaneValidatorBuilder.php | 48 ++++++++ tests/ProfaneValidatorTest.php | 190 ++++++++++++++++-------------- 2 files changed, 151 insertions(+), 87 deletions(-) create mode 100644 tests/ProfaneValidatorBuilder.php diff --git a/tests/ProfaneValidatorBuilder.php b/tests/ProfaneValidatorBuilder.php new file mode 100644 index 0000000..374cd68 --- /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, $parameters) = $parameters; + + return $this->build()->validate($attribute, $text, $parameters); + } + + /** + * [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']])); } } From 360398f834a6de139cdf57b704592a0fb65fbd89 Mon Sep 17 00:00:00 2001 From: Dorian Neto Date: Wed, 27 Sep 2017 18:55:52 -0300 Subject: [PATCH 4/4] Fix tests on PHP 5.6 --- tests/ProfaneValidatorBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ProfaneValidatorBuilder.php b/tests/ProfaneValidatorBuilder.php index 374cd68..299a5d3 100644 --- a/tests/ProfaneValidatorBuilder.php +++ b/tests/ProfaneValidatorBuilder.php @@ -32,9 +32,9 @@ class ProfaneValidatorBuilder */ public function validate(array $parameters) { - list($attribute, $text, $parameters) = $parameters; + list($attribute, $text, $dictionaries) = $parameters; - return $this->build()->validate($attribute, $text, $parameters); + return $this->build()->validate($attribute, $text, $dictionaries); } /**