diff --git a/CHANGELOG.md b/CHANGELOG.md index b903eee..ad29cb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,4 +8,10 @@ All notable changes to this project will be documented in this file. - Initial release ## [1.0.1] - 2021-10-29 -- Bug fix. \ No newline at end of file +- Bug fix. + +## [1.1.0] - 2021-11-15 +- Bug fix. +- Readme Update +- Tests update. +- Sanitization runtime disable/enable feature added. \ No newline at end of file diff --git a/README.md b/README.md index 365b91f..2c1abd6 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ User::create([ It will throw an **\Illuminate\Database\QueryException** if the **data** column not present in the users table. ```bash -Illuminate\Database\QueryException with message 'SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `users` (`email`, `password`, `updated_at`, `created_at`) values (somemail@test.com, password, 2021-08-23 10:15:25, 2021-08-23 10:15:25))' +Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 table users has no column named data (SQL: insert into "users" ("email", "password", "data", "updated_at", "created_at") values (somemail@mail.com, password, data, 2021-11-14 20:11:04, 2021-11-14 20:11:04)) ``` The **Sanitize** package target to make it easier to handle such case as follow by including the **Sanitizable** trait in the models @@ -132,6 +132,13 @@ This will return back as such : The **sanitize** and **gibberish** methods can be used to check or manually sanitize and evaluate the in valid data that can be passed to create/update model records. +It is also possible to **disable/enable** the sanitization process at the runtime using the static methods **disableSanitization** and **enableSanitization** . For example, + +```php +User::disableSanitization(); // disable the sanitization process +User::enableSanitization(); // enable the sanitization process if previously disabled +``` + ## Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. diff --git a/src/Sanitizable.php b/src/Sanitizable.php index 61b1848..e9028ed 100644 --- a/src/Sanitizable.php +++ b/src/Sanitizable.php @@ -5,11 +5,42 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Schema; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\ModelNotFoundException; use Touhidurabir\ModelSanitize\Builder\SanitizableQueryBuilder; trait Sanitizable { + /** + * Shoud the sanitize be enabled + * + * @var bool + */ + protected static $sanitizationEnabled = true; + + + /** + * Disbale the model sanitation + * + * @return void + */ + public static function disableSanitization() { + + static::$sanitizationEnabled = false; + } + + + /** + * Enable the model sanitation + * + * @return void + */ + public static function enableSanitization() { + + static::$sanitizationEnabled = true; + } + + /** * Sanitize data list to model fillables * @@ -76,11 +107,16 @@ public static function gibberish(array $attributes = []) { * Create a new Eloquent query builder for the model. * * @param \Illuminate\Database\Query\Builder $query - * @return \Touhidurabir\ModelSanitize\Builder\SanitizableQueryBuilder|static + * @return \Touhidurabir\ModelSanitize\Builder\SanitizableQueryBuilder|\Illuminate\Database\Eloquent\Builder|static */ public function newEloquentBuilder($query) { - - return new SanitizableQueryBuilder($query); + + if ( static::$sanitizationEnabled ) { + + return new SanitizableQueryBuilder($query); + } + + return new Builder($query); } diff --git a/tests/App/Address.php b/tests/App/Address.php new file mode 100644 index 0000000..d025035 --- /dev/null +++ b/tests/App/Address.php @@ -0,0 +1,32 @@ +getSchemaBuilder()->create('addresses', function (Blueprint $table) { + $table->increments('id'); + $table->string('address_line_1'); + $table->string('address_line_2')->nullable(); + $table->string('nation')->nullable(); + $table->string('state')->nullable(); + $table->string('city')->nullable(); + $table->string('zip')->nullable(); + $table->text('extras')->nullable(); + $table->boolean('status')->default(true); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::connection()->getSchemaBuilder()->dropIfExists('addresses'); + } +} \ No newline at end of file diff --git a/tests/App/database/migrations/2014_10_12_000003_create_nations_table.php b/tests/App/database/migrations/2014_10_12_000003_create_nations_table.php new file mode 100644 index 0000000..d0a29ff --- /dev/null +++ b/tests/App/database/migrations/2014_10_12_000003_create_nations_table.php @@ -0,0 +1,35 @@ +getSchemaBuilder()->create('nations', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->string('code'); + $table->string('description')->nullable(); + $table->boolean('status')->default(true); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::connection()->getSchemaBuilder()->dropIfExists('nations'); + } +} \ No newline at end of file diff --git a/tests/App/database/migrations/2014_10_12_000004_create_states_table.php b/tests/App/database/migrations/2014_10_12_000004_create_states_table.php new file mode 100644 index 0000000..2fe2ae8 --- /dev/null +++ b/tests/App/database/migrations/2014_10_12_000004_create_states_table.php @@ -0,0 +1,36 @@ +getSchemaBuilder()->create('states', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->string('code'); + $table->string('description')->nullable(); + $table->integer('city_counts')->nullable(); + $table->boolean('status')->default(true); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::connection()->getSchemaBuilder()->dropIfExists('states'); + } +} \ No newline at end of file diff --git a/tests/LaravelIntegrationTest.php b/tests/LaravelIntegrationTest.php index d7a0453..1122dfd 100644 --- a/tests/LaravelIntegrationTest.php +++ b/tests/LaravelIntegrationTest.php @@ -2,11 +2,15 @@ namespace Touhidurabir\ModelSanitize\Tests; +use Exception; use Orchestra\Testbench\TestCase; use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Model; use Touhidurabir\ModelSanitize\Tests\App\User; +use Touhidurabir\ModelSanitize\Tests\App\State; +use Touhidurabir\ModelSanitize\Tests\App\Nation; use Touhidurabir\ModelSanitize\Tests\App\Profile; +use Touhidurabir\ModelSanitize\Tests\App\Address; use Touhidurabir\ModelSanitize\Facades\ModelSanitize; use Touhidurabir\ModelSanitize\ModelSanitizeServiceProvider; @@ -229,5 +233,83 @@ public function it_will_properly_sanitize_on_force_create() { $this->assertDatabaseHas('users', ['email' => 'newtestmail002@test.mail']); } + + + /** + * @test + */ + public function it_will_not_fill_guarded_attributes() { + + $address = Address::create([ + 'address_line_1' => '5435 marthas vanieyard', + 'nation' => 'US', + 'extras' => 'some extra data', + ]); + + $this->assertNull($address->extras); + } + + + /** + * @test + */ + public function it_will_only_fill_fillable_attributes() { + + $nation = Nation::create([ + 'name' => 'United States', + 'code' => 'US', + 'description' => 'some extra description', + ]); + + $this->assertNull($nation->description); + } + + + /** + * @test + */ + public function it_will_honour_both_guarded_and_fillable_if_defined() { + + $state = State::create([ + 'name' => 'New York', + 'code' => 'NY', + 'city_counts' => 12, + 'description' => 'some extra description', + ]); + + $this->assertDatabaseHas('states', [ + 'name' => 'New York', + 'code' => 'NY', + ]); + $this->assertNull($state->city_counts); + $this->assertNull($state->description); + } + + + /** + * @test + */ + public function the_sanitization_process_can_be_disabled_at_run_time() { + + $this->expectException(\Illuminate\Database\QueryException::class); + + User::disableSanitization(); + + $user = User::create(['email' => 'somemail@mail.com', 'password' => 'password', 'data' => 'data']); + } + + + /** + * @test + */ + public function the_disabled_sanitization_process_can_be_enabled_at_run_time() { + + User::disableSanitization(); + + User::enableSanitization(); + + $user = User::create(['email' => 'somemail@mail.com', 'password' => 'password', 'data' => 'data']); + $this->assertDatabaseHas('users', ['email' => 'somemail@mail.com', 'password' => 'password']); + } } \ No newline at end of file