Skip to content

Commit

Permalink
Null column names should disable userstamps
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmcdonald-uk committed Mar 9, 2020
1 parent f7076cb commit 7c00acc
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Listeners/Creating.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Creating
*/
public function handle($model)
{
if (! $model->isUserstamping()) {
if (! $model->isUserstamping() || is_null($model->getCreatedByColumn())) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/Deleting.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Deleting
*/
public function handle($model)
{
if (! $model->isUserstamping()) {
if (! $model->isUserstamping() || is_null($model->getDeletedByColumn())) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/Restoring.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Restoring
*/
public function handle($model)
{
if (! $model->isUserstamping()) {
if (! $model->isUserstamping() || is_null($model->getDeletedByColumn())) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/Updating.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Updating
*/
public function handle($model)
{
if (! $model->isUserstamping() || is_null(Auth::id())) {
if (! $model->isUserstamping() || is_null($model->getUpdatedByColumn()) || is_null(Auth::id())) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Userstamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function destroyer()
*/
public function getCreatedByColumn()
{
return defined('static::CREATED_BY') && ! is_null(static::CREATED_BY) ? static::CREATED_BY : 'created_by';
return defined('static::CREATED_BY') ? static::CREATED_BY : 'created_by';
}

/**
Expand All @@ -96,7 +96,7 @@ public function getCreatedByColumn()
*/
public function getUpdatedByColumn()
{
return defined('static::UPDATED_BY') && ! is_null(static::UPDATED_BY) ? static::UPDATED_BY : 'updated_by';
return defined('static::UPDATED_BY') ? static::UPDATED_BY : 'updated_by';
}

/**
Expand All @@ -106,7 +106,7 @@ public function getUpdatedByColumn()
*/
public function getDeletedByColumn()
{
return defined('static::DELETED_BY') && ! is_null(static::DELETED_BY) ? static::DELETED_BY : 'deleted_by';
return defined('static::DELETED_BY') ? static::DELETED_BY : 'deleted_by';
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/UserstampsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,26 +230,26 @@ public function testCustomColumnNamesAreSupported()
$this->assertNull($foo->alt_deleted_by);
}

public function testNullColumnNamesAreSupported()
public function testNullColumnNamesDisableUserstamps()
{
$this->app['auth']->loginUsingId(1);

$foo = $this->createFooWithNullColumnNames();

$this->assertEquals(1, $foo->created_by);
$this->assertEquals(1, $foo->updated_by);
$this->assertNull($foo->created_by);
$this->assertNull($foo->updated_by);

$this->app['auth']->loginUsingId(2);

$foo->update([
'bar' => 'bar',
]);

$this->assertEquals(2, $foo->updated_by);
$this->assertNull($foo->updated_by);

$foo->delete();

$this->assertEquals(2, $foo->deleted_by);
$this->assertNull($foo->deleted_by);

$foo->restore();
$this->assertNull($foo->deleted_by);
Expand Down

0 comments on commit 7c00acc

Please sign in to comment.