Skip to content
Snippets Groups Projects
Commit 0e6c1957 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

dev #59717 : deleting profile delete its widget versions

parent e4c83310
Branches
Tags
3 merge requests!2334Master,!2191Dev#59717 versionning des configurations de boites,!2185Dev#59717 versionning des configurations de boites
Pipeline #1390 failed with stage
in 5 minutes and 27 seconds
......@@ -27,7 +27,14 @@ class Admin_ProfilController extends ZendAfi_Controller_Action {
private $id_bib; // Id bib sélectionnée (pour les combos de sélection)
public function getPlugins() {
return ['ZendAfi_Controller_Plugin_Versionning_Profile'];
}
public function preDispatch() {
parent::preDispatch();
$session = $_SESSION['admin'];
// Zone et bib du filtre (initialisé dans le plugin DefineUrls)
$this->id_zone = $session['filtre_localisation']['id_zone'];
......@@ -523,9 +530,17 @@ class Admin_ProfilController extends ZendAfi_Controller_Action {
public function deleteAction() {
$profil = Class_Profil::getLoader()->find($this->id_profil);
if (!$profil = Class_Profil::find($this->id_profil))
return $this->_redirect('admin/profil');
$profil->delete();
$this->_plugins
->eachDo(function($plugin) use($profil)
{
$plugin->notifyAfterDelete($profil);
});
// si on travaille sur un profil on reste dans le contexte
if ($profil->hasParentProfil()
and strpos($_SERVER['HTTP_REFERER'], 'id_profil'))
......
......@@ -149,22 +149,53 @@ class Class_Version_FilePersistence extends Class_Entity {
public function clear($versions) {
$this->_clearMain($versions);
$this->_clearDependents($versions);
}
protected function _clearMain($versions) {
if (!$versions->getName() || !$versions->getKey())
return;
$path = $this->_getFolderPath($versions);
return $this->_clearPath($path);
}
protected function _clearPath($path) {
if (!$files = $this->_filesIn($path))
return;
$file_system = $this->getFileSystem();
foreach($files as $file)
foreach ($files as $file)
$file_system->unlink($path . '/' . $file);
return $file_system->rmdir($path);
}
protected function _clearDependents($versions) {
$versions->withHasManyDo([$this, 'clearHasManyLink']);
return true;
}
public function clearHasManyLink($link) {
if (!$link->getName() || !$link->getPattern())
return;
$path = PATH_TEMP
. implode('/', [static::BASE_PATH,
$this->_sanitize($link->getName())]);
foreach($this->_filesIn($path) as $file)
if (preg_match('|' . $link->getPattern() . '|u', $file))
$this->_clearPath($path . '/' . $file);
}
public function count($versions) {
if (!$versions->getName() || !$versions->getKey())
return 0;
......
......@@ -21,7 +21,10 @@
class Class_Versions {
protected $_name, $_key;
protected
$_name,
$_key,
$_has_many = [];
protected static $_persistence;
......@@ -39,18 +42,21 @@ class Class_Versions {
public static function forArticle($article) {
return new static('article', $article->getId());
return new Class_Versions_Article($article->getId());
}
public static function forWidget($widget) {
return new static('widget',
$widget->getProfileId() . '_' . $widget->getId());
return new Class_Versions_Widget($widget->getProfileId() . '_' . $widget->getId());
}
public function __construct($name, $key) {
$this->_name = $name;
public static function forProfile($profile) {
return new Class_Versions_Profile($profile->getId());
}
public function __construct($key) {
$this->_key = $key;
}
......@@ -113,4 +119,12 @@ class Class_Versions {
public function clear() {
return static::getPersistence()->clear($this);
}
public function withHasManyDo($callable) {
foreach($this->_has_many as $link)
call_user_func($callable, $link);
return $this;
}
}
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Versions_Article extends Class_Versions {
protected $_name = 'article';
}
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Versions_HasManyLink {
protected $_name, $_pattern;
public function __construct($name, $pattern) {
$this->_name = $name;
$this->_pattern = $pattern;
}
public function getName() {
return $this->_name;
}
public function getPattern() {
return $this->_pattern;
}
}
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Versions_Profile extends Class_Versions {
protected $_name = 'profile';
public function __construct($key) {
parent::__construct($key);
$this->_has_many[] = new Class_Versions_HasManyLink('widget',
'^' . $key . '_([0-9])+');
}
}
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Versions_Widget extends Class_Versions {
protected $_name = 'widget';
}
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_Controller_Plugin_Versionning_Profile
extends ZendAfi_Controller_Plugin_Abstract {
public function notifyAfterDelete($model) {
$this->_versionsFor($model)
->clear();
}
protected function _versionsFor($model) {
return Class_Versions::forProfile($model);
}
}
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
abstract class VersionningProfileTestCase extends Admin_AbstractControllerTestCase {
protected
$_storm_default_to_volatile = true,
$_file_system,
$_type_module;
public function setUp() {
parent::setUp();
$this->_file_system = $this->mock();
Class_Version_FilePersistence::setFileSystem($this->_file_system);
Class_Version_FilePersistence::setTimeSource(new TimeSourceForTest('2017-04-25 11:24:08'));
Class_Folder_Manager::setFileSystem($this->_file_system);
$profil = $this->fixture('Class_Profil', ['id' => 3]);
}
public function tearDown() {
Class_Version_FilePersistence::setFileSystem(null);
Class_Version_FilePersistence::setTimeSource(null);
Class_Folder_Manager::setFileSystem(null);
parent::tearDown();
}
}
class VersionningProfileDeleteTest extends VersionningProfileTestCase {
protected
$_files_deleted = [],
$_directories_deleted = [];
public function setUp() {
parent::setUp();
$this->fixture('Class_Profil', ['id' => 3]);
$this->_file_system
->whenCalled('file_exists')->answers(true)
->whenCalled('is_readable')->answers(true)
->whenCalled('scandir')
->willDo(function($path)
{
if ('profile/3' == substr($path, -9))
return ['.', '..',
'2017-05-02_163129_32.json'];
if ('widget' == substr($path, -6))
return ['.', '..',
'2_7',
'3_8'];
if ('widget/3_8' == substr($path, -10))
return ['.', '..',
'2017-03-19_102612_32.json',
'2016-12-03_074323_666.json',
'2016-11-07_000000_666.json'
];
return ['.', '..'];
})
->whenCalled('file_get_contents')
->answers('')
->whenCalled('unlink')
->willDo(function($path) { $this->_files_deleted[] = $path; })
->whenCalled('rmdir')
->willDo(function($path)
{
$this->_directories_deleted[] = $path;
return true;
})
;
$this->dispatch('/admin/profil/delete/id_profil/3');
Class_Profil::clearCache();
}
protected function assertArrayItemEndingWith($items, $end) {
foreach($items as $item)
if ($end == substr($item, -strlen($end)))
return;
$this->fail(sprintf('failed to assert array contains item ENDING with "%s" %s',
$end,
json_encode($items, JSON_PRETTY_PRINT)));
}
/** @test */
public function profileShouldBeDeleted() {
$this->assertNull(Class_Profil::find(3));
}
/** @test */
public function allVersionsShouldBeDeleted() {
$this->assertEquals(['2017-05-02_163129_32.json',
'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 profileVersionDirectoryShouldBeDeleted() {
$this->assertArrayItemEndingWith($this->_directories_deleted,
'/temp/versions/profile/3');
}
/** @test */
public function widgetsVersionDirectoryShouldBeDeleted() {
$this->assertArrayItemEndingWith($this->_directories_deleted,
'/temp/versions/widget/3_8');
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment