From a80a93c94ca20daae7fad9b2f8c35524ecbbecb4 Mon Sep 17 00:00:00 2001 From: Arandi Lopez Date: Mon, 18 Jul 2016 22:05:30 -0500 Subject: [PATCH] README and tests --- REAME.md | 98 ++++++++++++ composer.lock | 273 ++++++++++++++++++++++++++++++++- src/ProfaneValidator.php | 20 +-- src/dict/es.php | 4 +- tests/ProfaneValidatorTest.php | 171 +++++---------------- 5 files changed, 416 insertions(+), 150 deletions(-) create mode 100644 REAME.md diff --git a/REAME.md b/REAME.md new file mode 100644 index 0000000..b317c6c --- /dev/null +++ b/REAME.md @@ -0,0 +1,98 @@ +# Laravel Profanity Valitador + +I made this package to perform a validation for swear words using Laravel validation service. + +## Instalation + +Install via composer +```shell +composer require arandilopez/laravel-profane +``` + +## Configuration +Add the `ProfaneServiceProvider` class in your `config/app.php` file. + +```php + [ + // ... + LaravelProfane\ProfaneServiceProvider::class, + ]; + + // ... +]; +``` + +## How to use + +This package register a custom validator. You can use in your controller's `validate` function. + +```php +validate($request, [ + 'username' => 'required|profane' + ]); + + // ... + } +} +``` + +By default the validator will load the `en` locale. If you want to use others dictionaries you can pass them as parameters in the validator. + +```php +validate($request, [ + 'username' => 'required|profane:es,en' + ]); + + // ... + } +} +``` + +You can also send as parameter a path of a file which is a dictionary. + +```php +validate($request, [ + 'username' => 'required|profane:es,en,'.storage('mydicts/fr.php') + ]); + + // ... + } +} +``` + +## Getting Help +If you're stuck getting something to work, or need to report a bug, please [post an issue in the Github Issues for this project](https://github.com/arandilopez/laravel-profane/issues). + +## Contributing +If you're interesting in contributing code to this project, clone it by running: + +```shell +git clone git@github.com:arandilopez/laravel-profane.git +``` + +Pull requests are welcome, but please make sure you provide unit tests to cover your changes. + +## License +This project is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT). diff --git a/composer.lock b/composer.lock index 499f2a0..0b36f46 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "40c093543d0ac25fb2e1118cc63e9d73", - "content-hash": "e00c4ea54cdffc9d6913a39c0778bf30", + "hash": "0f0aa31b7c00edacb1560194542658ad", + "content-hash": "c6bec2b57059869a9aad33d459ba4f22", "packages": [ { "name": "doctrine/inflector", @@ -74,6 +74,49 @@ ], "time": "2015-11-06 14:35:42" }, + { + "name": "illuminate/container", + "version": "v5.2.37", + "source": { + "type": "git", + "url": "https://github.com/illuminate/container.git", + "reference": "7ec395833738b9059f829348ddc9a59d0118ac88" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/container/zipball/7ec395833738b9059f829348ddc9a59d0118ac88", + "reference": "7ec395833738b9059f829348ddc9a59d0118ac88", + "shasum": "" + }, + "require": { + "illuminate/contracts": "5.2.*", + "php": ">=5.5.9" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.2-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Container\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com" + } + ], + "description": "The Illuminate Container package.", + "homepage": "http://laravel.com", + "time": "2016-05-29 02:18:23" + }, { "name": "illuminate/contracts", "version": "v5.2.37", @@ -172,6 +215,56 @@ "homepage": "http://laravel.com", "time": "2016-05-30 02:40:53" }, + { + "name": "illuminate/validation", + "version": "v5.2.37", + "source": { + "type": "git", + "url": "https://github.com/illuminate/validation.git", + "reference": "70b089ce508229b446c85e0143e48dc0eed90c15" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/validation/zipball/70b089ce508229b446c85e0143e48dc0eed90c15", + "reference": "70b089ce508229b446c85e0143e48dc0eed90c15", + "shasum": "" + }, + "require": { + "illuminate/container": "5.2.*", + "illuminate/contracts": "5.2.*", + "illuminate/support": "5.2.*", + "php": ">=5.5.9", + "symfony/http-foundation": "2.8.*|3.0.*", + "symfony/translation": "2.8.*|3.0.*" + }, + "suggest": { + "illuminate/database": "Required to use the database presence verifier (5.2.*)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.2-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Validation\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com" + } + ], + "description": "The Illuminate Validation package.", + "homepage": "http://laravel.com", + "time": "2016-06-03 13:17:37" + }, { "name": "paragonie/random_compat", "version": "v1.4.1", @@ -219,6 +312,182 @@ "random" ], "time": "2016-03-18 20:34:03" + }, + { + "name": "symfony/http-foundation", + "version": "v3.0.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-foundation.git", + "reference": "1341139f906d295baa4f4abd55293d07e25a065a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/1341139f906d295baa4f4abd55293d07e25a065a", + "reference": "1341139f906d295baa4f4abd55293d07e25a065a", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "symfony/polyfill-mbstring": "~1.1" + }, + "require-dev": { + "symfony/expression-language": "~2.8|~3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpFoundation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony HttpFoundation Component", + "homepage": "https://symfony.com", + "time": "2016-06-29 07:02:21" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.2.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "dff51f72b0706335131b00a7f49606168c582594" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/dff51f72b0706335131b00a7f49606168c582594", + "reference": "dff51f72b0706335131b00a7f49606168c582594", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2016-05-18 14:26:46" + }, + { + "name": "symfony/translation", + "version": "v3.0.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/translation.git", + "reference": "6bf844e1ee3c820c012386c10427a5c67bbefec8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/translation/zipball/6bf844e1ee3c820c012386c10427a5c67bbefec8", + "reference": "6bf844e1ee3c820c012386c10427a5c67bbefec8", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "symfony/config": "<2.8" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~2.8|~3.0", + "symfony/intl": "~2.8|~3.0", + "symfony/yaml": "~2.8|~3.0" + }, + "suggest": { + "psr/log": "To use logging capability in translator", + "symfony/config": "", + "symfony/yaml": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Translation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Translation Component", + "homepage": "https://symfony.com", + "time": "2016-06-29 05:40:00" } ], "packages-dev": [ diff --git a/src/ProfaneValidator.php b/src/ProfaneValidator.php index 283ac05..31e6430 100644 --- a/src/ProfaneValidator.php +++ b/src/ProfaneValidator.php @@ -27,10 +27,10 @@ class ProfaneValidator * @param \Illuminate\Contracts\Validation\Validator $validator [description] * @return bool */ - public function validate($attribute, $value, $parameters, $validator) + public function validate($attribute, $value, $parameters) { if ($parameters) { - $this->loadDictionary($parameters); + $this->setDictionary($parameters); } return !$this->isProfane($value); @@ -51,15 +51,6 @@ class ProfaneValidator return $this->badwords; } - /** - * Merge the current dictionary with the passed - * @param array|string $dictionary - */ - public function loadDictionary($dictionary) - { - $this->badwords = array_merge($this->badwords, $this->readDictionary($dictionary)); - } - /** * Set the dictionary to use * @param array|string $dictionary @@ -72,7 +63,7 @@ class ProfaneValidator protected function readDictionary($dictionary) { $badwords = []; - $baseDictPath = __DIR__ . DIRECTORY_SEPARATOR .'dict/'; + $baseDictPath = $this->getBaseDictPath(); if (is_array($dictionary)) { foreach ($dictionary as $file) { if (file_exists($baseDictPath.$file.'.php')) { @@ -98,4 +89,9 @@ class ProfaneValidator return $badwords; } + + protected function getBaseDictPath() + { + return property_exists($this, 'baseDictPath') ? $this->baseDictPath : __DIR__ . DIRECTORY_SEPARATOR .'dict/'; + } } diff --git a/src/dict/es.php b/src/dict/es.php index 14c999a..b27db98 100644 --- a/src/dict/es.php +++ b/src/dict/es.php @@ -22,8 +22,10 @@ return [ 'cojón', 'cojones', 'comepollas', - 'cono', + // 'cono', 'coño', + 'culero', + 'culera', 'culo', 'follar', 'follen', diff --git a/tests/ProfaneValidatorTest.php b/tests/ProfaneValidatorTest.php index befd5e3..f274203 100644 --- a/tests/ProfaneValidatorTest.php +++ b/tests/ProfaneValidatorTest.php @@ -12,18 +12,34 @@ class ProfaneValidatorTest extends PHPUnit_Framework_TestCase m::close(); } + public function test_can_validate_a_word() + { + $this->mockConfigs(); + $attribute = 'username'; + $word = 'culero23'; + $parameters = ['es']; + + $profane = new ProfaneValidator(); + + $this->assertFalse($profane->validate($attribute, $word, $parameters)); + } + + public function test_can_validate_a_text() + { + $this->mockConfigs(); + $attribute = 'description'; + $text = 'fck you bitch'; + $parameters = ['es', 'en']; + + $profane = new ProfaneValidator(); + + $this->assertFalse($profane->validate($attribute, $text, $parameters)); + } + public function test_can_evaluate_profanity_of_a_word() { $word = 'fuck'; - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); + $this->mockConfigs(); $profane = new ProfaneValidator(); @@ -33,15 +49,7 @@ class ProfaneValidatorTest extends PHPUnit_Framework_TestCase public function test_can_evaluate_profanity_of_a_sentence() { $word = 'fuck you if you read this'; - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); + $this->mockConfigs(); $profane = new ProfaneValidator(); @@ -51,15 +59,7 @@ class ProfaneValidatorTest extends PHPUnit_Framework_TestCase public function test_can_evaluate_profanity_of_a_html_string() { $word = 'fuck you if you read this.'; - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); + $this->mockConfigs(); $profane = new ProfaneValidator(); @@ -68,15 +68,7 @@ class ProfaneValidatorTest extends PHPUnit_Framework_TestCase public function test_can_set_dictionary_when_you_pass_a_locale() { - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); + $this->mockConfigs(); $profane = new ProfaneValidator(); $profane->setDictionary('es'); @@ -88,15 +80,7 @@ class ProfaneValidatorTest extends PHPUnit_Framework_TestCase public function test_can_set_dictionary_when_you_pass_a_file() { - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); + $this->mockConfigs(); $profane = new ProfaneValidator(); $profane->setDictionary(__DIR__.'/../src/dict/es.php'); @@ -108,45 +92,29 @@ class ProfaneValidatorTest extends PHPUnit_Framework_TestCase public function test_can_set_dictionary_when_you_pass_an_array_of_files() { - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); + $this->mockConfigs(); $profane = new ProfaneValidator(); - $profane->setDictionary([__DIR__.'/../src/dict/es.php']); + $profane->setDictionary([__DIR__.'/../src/dict/es.php', __DIR__.'/../src/dict/en.php']); - $expected = include __DIR__.'/../src/dict/es.php'; + $expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php'); $this->assertEquals($profane->getBadwords(), $expected); } public function test_can_set_dictionary_when_you_pass_an_array_of_locales() { - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); + $this->mockConfigs(); $profane = new ProfaneValidator(); - $profane->setDictionary(['es']); + $profane->setDictionary(['es', 'en']); - $expected = include __DIR__.'/../src/dict/es.php'; + $expected = array_merge(include __DIR__.'/../src/dict/es.php', include __DIR__.'/../src/dict/en.php'); $this->assertEquals($profane->getBadwords(), $expected); } - public function test_can_load_dictionary_when_you_pass_a_locale() + private function mockConfigs() { Config::shouldReceive('get') ->once() @@ -157,72 +125,5 @@ class ProfaneValidatorTest extends PHPUnit_Framework_TestCase ->once() ->with('app.locale') ->andReturn(true); - - $profane = new ProfaneValidator(); - $profane->loadDictionary('es'); - - $expected = array_merge(include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/es.php'); - - $this->assertEquals($profane->getBadwords(), $expected); - } - - public function test_can_load_dictionary_when_you_pass_a_file() - { - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); - - $profane = new ProfaneValidator(); - $profane->loadDictionary(__DIR__.'/../src/dict/es.php'); - - $expected = array_merge(include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/es.php'); - - $this->assertEquals($profane->getBadwords(), $expected); - } - - public function test_can_load_dictionary_when_you_pass_an_array_of_files() - { - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); - - $profane = new ProfaneValidator(); - $profane->loadDictionary([__DIR__.'/../src/dict/es.php']); - - $expected = array_merge(include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/es.php'); - - $this->assertEquals($profane->getBadwords(), $expected); - } - - public function test_can_load_dictionary_when_you_pass_an_array_of_locales() - { - Config::shouldReceive('get') - ->once() - ->with('app.locale') - ->andReturn('en'); - - Config::shouldReceive('has') - ->once() - ->with('app.locale') - ->andReturn(true); - - $profane = new ProfaneValidator(); - $profane->loadDictionary(['es']); - - $expected = array_merge(include __DIR__.'/../src/dict/en.php', include __DIR__.'/../src/dict/es.php'); - - $this->assertEquals($profane->getBadwords(), $expected); } }