Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
b719f6bb
Unverified
Commit
b719f6bb
authored
Sep 26, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding tests & logic to remove folders and its children
parent
90b92a9c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
145 additions
and
0 deletions
+145
-0
DeleteAllChildrenOfFolder.php
Modules/Media/Events/Handlers/DeleteAllChildrenOfFolder.php
+27
-0
DeleteFolderOnDisk.php
Modules/Media/Events/Handlers/DeleteFolderOnDisk.php
+45
-0
EloquentFolderRepository.php
.../Media/Repositories/Eloquent/EloquentFolderRepository.php
+12
-0
FolderRepository.php
Modules/Media/Repositories/FolderRepository.php
+7
-0
EloquentFolderRepositoryTest.php
Modules/Media/Tests/EloquentFolderRepositoryTest.php
+54
-0
No files found.
Modules/Media/Events/Handlers/DeleteAllChildrenOfFolder.php
0 → 100644
View file @
b719f6bb
<?php
namespace
Modules\Media\Events\Handlers
;
use
Modules\Media\Events\FolderIsDeleting
;
use
Modules\Media\Repositories\FolderRepository
;
class
DeleteAllChildrenOfFolder
{
/**
* @var FolderRepository
*/
private
$folder
;
public
function
__construct
(
FolderRepository
$folder
)
{
$this
->
folder
=
$folder
;
}
public
function
handle
(
FolderIsDeleting
$event
)
{
$children
=
$this
->
folder
->
allChildrenOf
(
$event
->
folder
);
foreach
(
$children
as
$child
)
{
$child
->
delete
();
}
}
}
Modules/Media/Events/Handlers/DeleteFolderOnDisk.php
0 → 100644
View file @
b719f6bb
<?php
namespace
Modules\Media\Events\Handlers
;
use
Illuminate\Contracts\Filesystem\Factory
;
use
Modules\Media\Events\FolderIsDeleting
;
class
DeleteFolderOnDisk
{
/**
* @var Factory
*/
private
$finder
;
public
function
__construct
(
Factory
$finder
)
{
$this
->
finder
=
$finder
;
}
public
function
handle
(
FolderIsDeleting
$event
)
{
$this
->
finder
->
disk
(
$this
->
getConfiguredFilesystem
())
->
deleteDirectory
(
$this
->
getDestinationPath
(
$event
->
folder
->
getOriginal
(
'path'
)));
}
/**
* @param string $path
* @return string
*/
private
function
getDestinationPath
(
$path
)
{
if
(
$this
->
getConfiguredFilesystem
()
===
'local'
)
{
return
basename
(
public_path
())
.
$path
;
}
return
$path
;
}
/**
* @return string
*/
private
function
getConfiguredFilesystem
()
{
return
config
(
'asgard.media.config.filesystem'
);
}
}
Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php
View file @
b719f6bb
...
...
@@ -2,6 +2,7 @@
namespace
Modules\Media\Repositories\Eloquent
;
use
Illuminate\Database\Eloquent\Collection
;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FolderIsCreating
;
...
...
@@ -71,6 +72,17 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return
$folder
->
delete
();
}
/**
* @param File $folder
* @return Collection
*/
public
function
allChildrenOf
(
File
$folder
)
{
$path
=
$folder
->
path
->
getRelativeUrl
();
return
$this
->
model
->
where
(
'path'
,
'like'
,
"
{
$path
}
%"
)
->
get
();
}
/**
* @param array $data
* @return string
...
...
Modules/Media/Repositories/FolderRepository.php
View file @
b719f6bb
...
...
@@ -2,6 +2,7 @@
namespace
Modules\Media\Repositories
;
use
Illuminate\Database\Eloquent\Collection
;
use
Modules\Core\Repositories\BaseRepository
;
use
Modules\Media\Entities\File
;
...
...
@@ -13,4 +14,10 @@ interface FolderRepository extends BaseRepository
* @return File|null
*/
public
function
findFolder
(
int
$folderId
);
/**
* @param File $folder
* @return Collection
*/
public
function
allChildrenOf
(
File
$folder
);
}
Modules/Media/Tests/EloquentFolderRepositoryTest.php
View file @
b719f6bb
...
...
@@ -262,6 +262,60 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
});
}
/** @test */
public
function
it_deletes_folder_from_disk
()
{
$folder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/my-folder'
)));
$this
->
folder
->
destroy
(
$folder
);
$this
->
assertFalse
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/my-folder'
)));
}
/** @test */
public
function
it_deletes_folder_in_subfolder_on_disk
()
{
$parentFolder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folder
=
$this
->
folder
->
create
([
'name'
=>
'Subfolder'
,
'parent_id'
=>
$parentFolder
->
id
]);
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/my-folder/subfolder'
)));
$this
->
folder
->
destroy
(
$folder
);
$this
->
assertFalse
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/my-folder/subfolder'
)));
}
/** @test */
public
function
it_can_remove_folder_with_files
()
{
$parentFolder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folderTwo
=
$this
->
folder
->
create
([
'name'
=>
'Child folder'
,
'parent_id'
=>
$parentFolder
->
id
]);
$file
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
);
$fileTwo
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-second-file.jpg'
);
$fileThree
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-third-file.jpg'
);
app
(
FileService
::
class
)
->
store
(
$file
,
$parentFolder
->
id
);
app
(
FileService
::
class
)
->
store
(
$fileTwo
,
$folderTwo
->
id
);
app
(
FileService
::
class
)
->
store
(
$fileThree
,
$folderTwo
->
id
);
$this
->
folder
->
destroy
(
$folderTwo
);
$this
->
assertFalse
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/my-folder/child-folder'
)));
}
/** @test */
public
function
it_removes_folder_and_files_from_database
()
{
$parentFolder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folderTwo
=
$this
->
folder
->
create
([
'name'
=>
'Child folder'
,
'parent_id'
=>
$parentFolder
->
id
]);
$file
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-file.jpg'
);
$fileTwo
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-second-file.jpg'
);
$fileThree
=
\Illuminate\Http\UploadedFile
::
fake
()
->
image
(
'my-third-file.jpg'
);
app
(
FileService
::
class
)
->
store
(
$file
,
$parentFolder
->
id
);
app
(
FileService
::
class
)
->
store
(
$fileTwo
,
$folderTwo
->
id
);
app
(
FileService
::
class
)
->
store
(
$fileThree
,
$folderTwo
->
id
);
$this
->
assertCount
(
5
,
File
::
all
());
$this
->
folder
->
destroy
(
$folderTwo
);
$this
->
assertCount
(
2
,
File
::
all
());
}
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
{
return
File
::
create
([
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment