Skip to content

Commit

Permalink
SoftDeletes support (#683)
Browse files Browse the repository at this point in the history
* SoftDeletes support

* Typo

* StyleCI fixes

* Added tests for softdeletes

* StyleCI fixes

* Fixed feedback
  • Loading branch information
royduin authored and freekmurze committed Jul 21, 2017
1 parent 5947365 commit 310ae7b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/HasMedia/HasMediaTrait.php
Expand Up @@ -36,6 +36,12 @@ public static function bootHasMediaTrait()
return;
}

if (in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, trait_uses_recursive($entity))) {
if (! $entity->forceDeleting) {
return;
}
}

$entity->media()->get()->each->delete();
});
}
Expand Down
34 changes: 33 additions & 1 deletion tests/Media/DeleteTest.php
Expand Up @@ -21,6 +21,38 @@ public function it_will_remove_the_files_when_deleting_a_media_object()
$this->assertFalse(File::isDirectory($this->getMediaDirectory($media->id)));
}

/**
* @test
*/
public function it_will_not_remove_the_files_when_delete_with_softdeletes_is_used()
{
$testModel = $this->testModelWithSoftDeletes->find($this->testModel->id);

$media = $testModel->addMedia($this->getTestJpg())->toMediaCollection('images');

$testModel = $testModel->fresh();

$testModel->delete();

$this->assertNotNull(Media::find($media->id));
}

/**
* @test
*/
public function it_will_remove_the_files_when_forcedelete_with_softdeletes_is_used()
{
$testModel = $this->testModelWithSoftDeletes->find($this->testModel->id);

$media = $testModel->addMedia($this->getTestJpg())->toMediaCollection('images');

$testModel = $testModel->fresh();

$testModel->forceDelete();

$this->assertNull(Media::find($media->id));
}

/**
* @test
*/
Expand All @@ -47,7 +79,7 @@ public function shouldDeletePreservingMedia()
/**
* @test
*/
public function it_will_remove_the_files_when_shouldDeletePreservingMedia_returns_true()
public function it_will_remove_the_files_when_shouldDeletePreservingMedia_returns_false()
{
$testModelClass = new class() extends TestModel {
public function shouldDeletePreservingMedia()
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase.php
Expand Up @@ -24,6 +24,9 @@ abstract class TestCase extends Orchestra
/** @var \Spatie\MediaLibrary\Test\TestModelWithMorphMap */
protected $testModelWithMorphMap;

/** @var \Spatie\MediaLibrary\Test\TestModelWithSoftDeletes */
protected $testModelWithSoftDeletes;

public function setUp()
{
parent::setUp();
Expand All @@ -37,6 +40,7 @@ public function setUp()
$this->testModelWithConversion = TestModelWithConversion::first();
$this->testModelWithoutMediaConversions = TestModelWithoutMediaConversions::first();
$this->testModelWithMorphMap = TestModelWithMorphMap::first();
$this->testModelWithSoftDeletes = TestModelWithSoftDeletes::first();
}

/**
Expand Down Expand Up @@ -94,6 +98,7 @@ protected function setUpDatabase($app)
$table->increments('id');
$table->string('name');
$table->integer('width')->nullable();
$table->softDeletes();
});

TestModel::create(['name' => 'test']);
Expand Down
21 changes: 21 additions & 0 deletions tests/TestModelWithSoftDeletes.php
@@ -0,0 +1,21 @@
<?php

namespace Spatie\MediaLibrary\Test;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;

class TestModelWithSoftDeletes extends Model implements HasMediaConversions
{
use HasMediaTrait, SoftDeletes;

protected $table = 'test_models';
protected $guarded = [];
public $timestamps = false;

public function registerMediaConversions()
{
}
}

0 comments on commit 310ae7b

Please sign in to comment.