diff --git a/VERSIONS_HOTLINE/143141 b/VERSIONS_HOTLINE/143141 new file mode 100644 index 0000000000000000000000000000000000000000..183de0d3e9fb4caa72d1e20296e73291522d0b95 --- /dev/null +++ b/VERSIONS_HOTLINE/143141 @@ -0,0 +1 @@ + - ticket #143141 : Admin : Album netoyer les PNB pour supprimer les facettes associés à des codifications supprimés \ No newline at end of file diff --git a/library/Class/Migration/AlbumClearCodif.php b/library/Class/Migration/AlbumClearCodif.php new file mode 100644 index 0000000000000000000000000000000000000000..fc39244914f8db2599b8fca6c6c0f7fb0e075141 --- /dev/null +++ b/library/Class/Migration/AlbumClearCodif.php @@ -0,0 +1,177 @@ +<?php +/** + * Copyright (c) 2012-2021, 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_Migration_AlbumClearCodif { + + protected + $_cat_id, + $_list_codif_status; + + public function __construct($cat_id) { + $this->_cat_id = $cat_id; + $this->_list_codif_status = new Storm_Collection(); + } + + + public function run() { + try { + if (!$this->_cat_id + || !$category = Class_AlbumCategorie::find($this->_cat_id)) + return; + + $albums = Class_Album::findAllBy(['cat_id' => $category->getSousCategoriesIds()]); + array_map(function ($album) { + $this->_withAlbumDo($album); + }, $albums); + + (new Storm_Cache())->clean(); + } catch (Exception $e) { + return; + } + } + + + protected function _withAlbumDo($album) { + $must_save = $this->_checkGenre($album); + $must_save |= $this->_checkSection($album); + $must_save |= $this->_checkAnnexe($album); + $must_save |= $this->_checkMatiere($album); + + if ($must_save) { + $album->save(); + $album->index(); + } + } + + + protected function _checkGenre($album) { + return $this->_checkCodif($album, 'genre', Class_CodifGenre::class); + } + + + protected function _checkSection($album) { + return $this->_checkCodif($album, 'sections', Class_CodifSection::class); + } + + + protected function _checkAnnexe($album) { + return $this->_checkCodif($album, 'annexes', Class_CodifAnnexe::class); + } + + + protected function _checkMatiere($album) { + return $this->_checkCodif($album, 'matiere', Class_CodifMatiere::class); + } + + + protected function _checkCodif($album, $attrib, $class_name) { + $is_updated = false; + $list_codif = []; + foreach (explode(';', $album->$attrib) as $codif_id) + $is_updated |= $this->_isUpdatedListCodif($class_name, $codif_id, $list_codif); + + if ($is_updated) + $album->$attrib = implode(';', $list_codif); + + return $is_updated; + } + + + protected function _isUpdatedListCodif($class_name, $codif_id, &$list_codif) { + $valid = $this->_existForClassName($class_name, $codif_id); + if ($valid) + $list_codif [] = $codif_id; + + return !$valid; + } + + + protected function _existForClassName($class_name, $codif_id) { + $codif_for_type = $this->_list_codif_status + ->detect(function ($codif_status) use ($class_name, $codif_id) { + return $class_name == $codif_status->getClassName() + && $codif_id == $codif_status->getCodifId(); + }); + + if (!$codif_for_type) + $codif_for_type = $this->_addNewCodif($class_name, $codif_id); + + return $codif_for_type->isExist(); + } + + + protected function _addNewCodif($type, $codif_id) { + $new_codif = (new CodifStatus($type, $codif_id))->init(); + if ($new_codif->isValid()) + $this->_list_codif_status->add($new_codif); + + return $new_codif; + } +} + + + + +class CodifStatus { + + protected + $_exist = false, + $_class_name, + $_codif_id; + + public function __construct($class_name, $codif_id) { + $this->_class_name = $class_name; + $this->_codif_id = $codif_id; + } + + + public function isValid() { + return $this->_class_name && $this->_codif_id; + } + + + public function init() { + if (!$this->isValid()) + return $this; + + $codif = $this->_class_name::find($this->_codif_id); + if ($codif) + $this->_exist = true; + + return $this; + } + + + public function getClassName() { + return $this->_class_name; + } + + + public function getCodifId() { + return $this->_codif_id; + } + + + public function isExist() { + return $this->_exist; + } +} diff --git a/scripts/album_clear_codif.php b/scripts/album_clear_codif.php new file mode 100644 index 0000000000000000000000000000000000000000..ba1d9f1afefbb0605c8e7ab699d8f0c42225ad41 --- /dev/null +++ b/scripts/album_clear_codif.php @@ -0,0 +1,4 @@ +<?php +require __DIR__ . '/../console.php'; + +(new Class_Migration_AlbumClearCodif($argv[1]))->run(); diff --git a/tests/library/Class/Migration/AlbumClearCodifTest.php b/tests/library/Class/Migration/AlbumClearCodifTest.php new file mode 100644 index 0000000000000000000000000000000000000000..96abc2590685e2ab1ff307ae4d91973f45681b7f --- /dev/null +++ b/tests/library/Class/Migration/AlbumClearCodifTest.php @@ -0,0 +1,135 @@ +<?php +/** + * Copyright (c) 2012-2021, 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 + */ + + +/* hotline: #143141 */ +class AlbumClearCodifTest extends ModelTestCase { + + protected $_storm_default_to_volatile = true; + + public function setUp() { + parent::setUp(); + + $this->fixture(Class_CodifGenre::class, + ['id' => 1, + 'libelle' => 'Adulte' + ]); + $this->fixture(Class_CodifAnnexe::class, + ['id' => 4, + 'libelle' => 'Biviers' + ]); + $this->fixture(Class_CodifAnnexe::class, + ['id' => 7, + 'libelle' => 'Froges' + ]); + $this->fixture(Class_CodifMatiere::class, + ['id' => 8, + 'libelle' => 'Documentaire Guides' + ]); + $this->fixture(Class_CodifMatiere::class, + ['id' => 9, + 'libelle' => 'Documentaire Histoire' + ]); + $this->fixture(Class_CodifMatiere::class, + ['id' => 12, + 'libelle' => 'Documentaire Humour' + ]); + + $this->fixture(Class_AlbumCategorie::class, + ['id' => 100, + 'parent_id' => 0 + ]); + $this->fixture(Class_AlbumCategorie::class, + ['id' => 101, + 'parent_id' => 0 + ]); + + $this->fixture(Class_Album::class, + ['id' => 200, + 'cat_id' => 100, + 'genre' => '1;5', + 'sections' => '2', + 'annexes' => '4;6;7', + 'matiere' => '8;9;10;11;12', + 'titre' => 'Amuse-bouche' + ]); + $this->fixture(Class_Album::class, + ['id' => 201, + 'cat_id' => 101, + 'genre' => '1;5', + 'sections' => '2', + 'annexes' => '4;6;7', + 'matiere' => '8;9;10;11;12', + 'titre' => 'Animal' + ]); + + (new Class_Migration_AlbumClearCodif(100))->run(); + } + + + /** @test */ + public function album200ShouldHaveLostGenreId5() { + $this->assertEquals('1', Class_Album::find(200)->getGenre()); + } + + + /** @test */ + public function album201ShouldNotHaveLostGenreIds() { + $this->assertEquals('1;5', Class_Album::find(201)->getGenre()); + } + + + /** @test */ + public function album200ShouldHaveLostSectionId2() { + $this->assertEquals('', Class_Album::find(200)->getSections()); + } + + + /** @test */ + public function album201ShouldNotHaveLostSectionIds() { + $this->assertEquals('2', Class_Album::find(201)->getSections()); + } + + + /** @test */ + public function album200ShouldHaveLostAnnexeId6() { + $this->assertEquals('4;7', Class_Album::find(200)->getAnnexes()); + } + + + /** @test */ + public function album201ShouldNotHaveLostAnnexeIds() { + $this->assertEquals('4;6;7', Class_Album::find(201)->getAnnexes()); + } + + + /** @test */ + public function album200ShouldHaveLostMatiereId10And11() { + $this->assertEquals('8;9;12', Class_Album::find(200)->getMatiere()); + } + + + /** @test */ + public function album201ShouldNotHaveLostMatiereIds() { + $this->assertEquals('8;9;10;11;12', Class_Album::find(201)->getMatiere()); + } + +}