diff --git a/library/Class/Version/FilePersistence.php b/library/Class/Version/FilePersistence.php index d11656ad5c7d1167940919d2da2886f32a908970..279e2e71102859186b4f7271173ba2165632845a 100644 --- a/library/Class/Version/FilePersistence.php +++ b/library/Class/Version/FilePersistence.php @@ -148,6 +148,23 @@ class Class_Version_FilePersistence extends Class_Entity { } + public function clear($versions) { + if (!$versions->getName() || !$versions->getKey()) + return; + + $path = $this->_getFolderPath($versions); + + if (!$files = $this->_filesIn($path)) + return; + + $file_system = $this->getFileSystem(); + foreach($files as $file) + $file_system->unlink($path . '/' . $file); + + return $file_system->rmdir($path); + } + + public function encode($version) { return json_encode($version->getData()); } diff --git a/library/Class/Versions.php b/library/Class/Versions.php index a2d72328e57d27da92d77d05a70804e14f6f77c3..2ea5d53651beb8dc87e0decbe17d67d1afd43e3e 100644 --- a/library/Class/Versions.php +++ b/library/Class/Versions.php @@ -92,4 +92,9 @@ class Class_Versions { public function nextOf($version) { return static::getPersistence()->nextOf($this, $version->getId()); } + + + public function clear() { + return static::getPersistence()->clear($this); + } } diff --git a/library/ZendAfi/Controller/Plugin/Manager/Article.php b/library/ZendAfi/Controller/Plugin/Manager/Article.php index 79b83ee7fc81e454c7d6ebe8905ccce4650bd183..9bae8368733f482de759b33eaa5adb13964861be 100644 --- a/library/ZendAfi/Controller/Plugin/Manager/Article.php +++ b/library/ZendAfi/Controller/Plugin/Manager/Article.php @@ -95,6 +95,10 @@ class ZendAfi_Controller_Plugin_Manager_Article extends ZendAfi_Controller_Plugi } $article->delete(); + $this->withOtherPluginsDo(function($plugin) use($article) + { + $plugin->notifyAfterDelete($article); + }); $this->_redirect($this->_backDeleteUrl($article)); } diff --git a/library/ZendAfi/Controller/Plugin/Versionning/Article.php b/library/ZendAfi/Controller/Plugin/Versionning/Article.php index a7887f55573eb0a8d8388cfb1a80670a1de15f25..fa910acdce5505b90a234317dc6b09cdc9c4b9c8 100644 --- a/library/ZendAfi/Controller/Plugin/Versionning/Article.php +++ b/library/ZendAfi/Controller/Plugin/Versionning/Article.php @@ -152,6 +152,12 @@ class ZendAfi_Controller_Plugin_Versionning_Article } + public function notifyAfterDelete($model) { + $this->_versionsFor($model) + ->clear(); + } + + protected function _redirectToEdit($article) { $this->_redirect('admin/cms/edit/id/' . $article->getId()); } diff --git a/tests/scenarios/Versionning/VersionningTest.php b/tests/scenarios/Versionning/VersionningTest.php index 3bca4200bdced41e765bf6160dad4c0e5eedca0c..c0d495b5e3ec718bf64ed95bce22bd450917cd99 100644 --- a/tests/scenarios/Versionning/VersionningTest.php +++ b/tests/scenarios/Versionning/VersionningTest.php @@ -533,3 +533,71 @@ class VersionningArticelWithFullStackEditPostTest extends VersionningAdminTestCa $this->_file_deleted); } } + + + +class VersionningArticelDeleteTest extends VersionningAdminTestCase { + protected + $_files_deleted = [], + $_directory_deleted; + + public function setUp() { + parent::setUp(); + + $this->_file_system + ->whenCalled('file_exists')->answers(true) + ->whenCalled('is_readable')->answers(true) + + ->whenCalled('scandir') + ->answers(['.', '..', + '2017-03-19_102612_32.json', + '2016-12-03_074323_666.json', + '2016-11-07_000000_666.json' + ]) + + ->whenCalled('file_get_contents') + ->answers('') + + ->whenCalled('unlink') + ->willDo(function($path) { $this->_files_deleted[] = $path; }) + + ->whenCalled('rmdir') + ->willDo(function($path) + { + $this->_directory_deleted = $path; + return true; + }) + ; + + $this->dispatch('/admin/cms/force-delete/id/27', true); + Class_Article::clearCache(); + } + + + /** @test */ + public function articleShouldBeDeleted() { + $this->assertNull(Class_Article::find(27)); + } + + + + /** @test */ + public function allVersionShouldBeDeleted() { + $this->assertEquals(['2017-03-19_102612_32.json', + '2016-12-03_074323_666.json', + '2016-11-07_000000_666.json'], + array_map(function($item) + { + $parts = explode('/', $item); + return end($parts); + }, + $this->_files_deleted)); + } + + + /** @test */ + public function versionDirectoryShouldBeDeleted() { + $this->assertEquals('/temp/versions/article/27', + substr($this->_directory_deleted, -25)); + } +} \ No newline at end of file