From a3ef1be050425dbb016d72112aadb7ada1fa1fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ANDRE=20s=C3=A9bastien?= <sandre@afi-sa.fr> Date: Fri, 3 Dec 2021 13:15:58 +0100 Subject: [PATCH] hotline : #138189 : add a start limit for getNoticesByPreferences --- VERSIONS_HOTLINE/138189 | 1 + library/Class/Catalogue/Loader.php | 7 +- library/Class/CodifAuteur.php | 47 ++++- .../Templates/TemplatesAuthorTest.php | 169 ++++++++++++++++++ 4 files changed, 219 insertions(+), 5 deletions(-) create mode 100644 VERSIONS_HOTLINE/138189 diff --git a/VERSIONS_HOTLINE/138189 b/VERSIONS_HOTLINE/138189 new file mode 100644 index 00000000000..d351d6293d9 --- /dev/null +++ b/VERSIONS_HOTLINE/138189 @@ -0,0 +1 @@ + - ticket #138189 : Boîte auteur : en mode mûr avec affichage aléatoire, chercher plus d'auteurs pour avoir plus de diversité dans le résultat \ No newline at end of file diff --git a/library/Class/Catalogue/Loader.php b/library/Class/Catalogue/Loader.php index 9279c91613b..2c9fa5e516e 100644 --- a/library/Class/Catalogue/Loader.php +++ b/library/Class/Catalogue/Loader.php @@ -470,12 +470,15 @@ class Class_Catalogue_Loader extends Storm_Model_Loader { if ( $max_limited ) return ' LIMIT 5000'; + $start_limit = 0; + if (isset($preferences['start_limit'])) + $start_limit = (int) $preferences['start_limit']; if ( isset($preferences["aleatoire"]) && 1 == (int) $preferences["aleatoire"]) - return sprintf(' LIMIT 0,%d', (int) $preferences["nb_analyse"]); + return sprintf(' LIMIT %d,%d', $start_limit, (int) $preferences["nb_analyse"]); if (isset($preferences['nb_notices']) && $preferences["nb_notices"]) - return sprintf(' LIMIT 0,%d', (int) $preferences["nb_notices"]); + return sprintf(' LIMIT %d,%d', $start_limit, (int) $preferences["nb_notices"]); return ' LIMIT 5000'; //LL: j'ai rajouté une limite max car explosion mémoire sur des catalogues mal définis } diff --git a/library/Class/CodifAuteur.php b/library/Class/CodifAuteur.php index 819c81bd578..4816039f584 100644 --- a/library/Class/CodifAuteur.php +++ b/library/Class/CodifAuteur.php @@ -121,12 +121,12 @@ class CodifAuteurLoader extends Storm_Model_Loader { public function findAllByPreferences($preferences) { $preferences['nb_notices'] = static::RECORDS_LOAD_LIMIT; return Class_CodifAuteur::findAllFromRecordsAndPreferences(Class_Notice::getNoticesFromPreferences($preferences), - $preferences); + $preferences); } public function findAllFromRecordsAndPreferences($records, $preferences) { - if (!$authors_ids = Class_CodifAuteur::findAllIdsFromRecordsAndPreferences($records, $preferences)) + if (!$authors_ids = $this->_getFilteredAuthorsId($records, $preferences)) return []; $params = ['id_auteur' => $authors_ids]; @@ -154,7 +154,8 @@ class CodifAuteurLoader extends Storm_Model_Loader { if ($order_random) $params ['order'] = ''; - $authors = Class_CodifAuteur::findAllBy(array_filter($params)); + $authors = $this->_getAuthorsWithStartLimit($records, $params, + $preferences, $size); if ($order_random) { shuffle($authors); @@ -165,6 +166,46 @@ class CodifAuteurLoader extends Storm_Model_Loader { } + protected function _getAuthorsWithStartLimit($records, $params, + $preferences, $size) { + $authors = Class_CodifAuteur::findAllBy(array_filter($params)); + + if ($this->_doStartLimitOnlyOnce($records, $authors, $preferences, $size)) + return $authors; + + $preferences ['start_limit'] = $preferences['nb_notices']; + $preferences ['without_authors_ids'] = array_map( + function ($author) { + return $author->getId(); + }, + $authors); + + return array_merge($authors, + Class_CodifAuteur::findAllByPreferences($preferences)); + } + + + protected function _getFilteredAuthorsId($records, $preferences) { + if (!$authors_ids = Class_CodifAuteur::findAllIdsFromRecordsAndPreferences($records, $preferences)) + return []; + + if (isset($preferences['without_authors_ids'])) { + $authors_ids = array_filter($authors_ids, function ($id) use ($preferences) { + return !in_array($id, $preferences['without_authors_ids']); + }); + } + + return $authors_ids ? $authors_ids : []; + } + + + protected function _doStartLimitOnlyOnce($records, $authors, $preferences, $size) { + return isset($preferences['start_limit']) + || count($authors) >= $size + || count($records) < $preferences['nb_notices']; + } + + public function findAllIdsFromRecordsAndPreferences($records, $preferences) { if (!isset($preferences['authors_selection_mode']) || !($mode = $preferences['authors_selection_mode'])) diff --git a/tests/scenarios/Templates/TemplatesAuthorTest.php b/tests/scenarios/Templates/TemplatesAuthorTest.php index b995b851ff9..b323e9fcc04 100644 --- a/tests/scenarios/Templates/TemplatesAuthorTest.php +++ b/tests/scenarios/Templates/TemplatesAuthorTest.php @@ -286,3 +286,172 @@ class TemplatesAgendaWidgetWithDescriptionLengthTest extends AbstractControllerT $this->assertXPath('//div[contains(@class, "authors widget")]'); } } + + + + +/* hotline : #138189 */ +abstract class TemplatesAuthorWidgetRandomTestCase + extends AbstractControllerTestCase { + + protected + $_storm_default_to_volatile = true, + $_loader; + + public function setUp() { + parent::setUp(); + + $this->_buildTemplateProfil(['id' => 1, + 'libelle' => 'Authors Wall Random']) + ->setBoiteOfTypeInDivision(4, + Intonation_Library_Widget_Carousel_Author_Definition::CODE, + ['all_layout' => Intonation_Library_Widget_Carousel_Definition::CAROUSEL, + 'size' => 20, + 'order' => 'Random', + 'link_to_all' => 1, + 'layout' => 'carousel' + ]) + ->assertSave(); + + $this->_prepareFixture(); + $this->_loader = $this->onLoaderOfModel(Class_Notice::class); + + $this->dispatch('/opac/index/index/id_profil/1'); + } + + + protected function _prepareFixture() { + } +} + + + + +class TemplatesAuthorWidgetRandomWithoutStartLimitTest + extends TemplatesAuthorWidgetRandomTestCase { + + protected function _prepareFixture() { + for ($id_not = 1; $id_not < 200; $id_not++) + $this->fixture(Class_Notice::class, + ['id' => $id_not, + 'type_doc' => Class_TypeDoc::LIVRE, + 'facettes' => 'A' . (1000 + $id_not)]); + + for ($id_aut = 1050; $id_aut < 1100; $id_aut++) + $this->fixture(Class_CodifAuteur::class, + ['id' => $id_aut, + 'libelle' => 'Victor Hugo ' . $id_aut, + 'thumbnail_url' => 'http://wp/hugo.jpg']); + } + + + /** @test */ + public function withMoreThan20AuthorsShouldNotUseStartLimitParam() { + $prefs = $this->_loader + ->getFirstAttributeForLastCallOn('getNoticesFromPreferences'); + $this->assertFalse(in_array('start_limit', $prefs)); + } +} + + + + +class TemplatesAuthorWidgetRandomWithStartLimitTest + extends TemplatesAuthorWidgetRandomTestCase { + + protected function _prepareFixture() { + for ($id_not = 1; $id_not < 200; $id_not++) + $this->fixture(Class_Notice::class, + ['id' => $id_not, + 'type_doc' => Class_TypeDoc::LIVRE, + 'facettes' => 'A' . (1000 + $id_not)]); + + for ($id_aut = 1090; $id_aut < 1100; $id_aut++) + $this->fixture(Class_CodifAuteur::class, + ['id' => $id_aut, + 'libelle' => 'Victor Hugo ' . $id_aut, + 'thumbnail_url' => 'http://wp/hugo.jpg']); + } + + + /** @test */ + public function withLessThan20AuthorsShouldUseStartLimitParam() { + $prefs = $this->_loader + ->getFirstAttributeForLastCallOn('getNoticesFromPreferences'); + $this->assertTrue(100 == $prefs['start_limit']); + } +} + + + + +class TemplatesAuthorWidgetRandomWithStartLimitWithUniqueAuthorsTest + extends TemplatesAuthorWidgetRandomTestCase { + + protected function _prepareFixture() { + $notices = []; + for ($id_not = 1; $id_not < 90; $id_not++) + $notices [] = $this->fixture(Class_Notice::class, + ['id' => $id_not, + 'type_doc' => Class_TypeDoc::LIVRE, + 'facettes' => '']); + for ($id_not = 90; $id_not < 100; $id_not++) + $notices [] = $this->fixture(Class_Notice::class, + ['id' => $id_not, + 'type_doc' => Class_TypeDoc::LIVRE, + 'facettes' => 'A' . (1010 + $id_not)]); + for ($id_not = 100; $id_not < 175; $id_not++) + $notices [] = $this->fixture(Class_Notice::class, + ['id' => $id_not, + 'type_doc' => Class_TypeDoc::LIVRE, + 'facettes' => '']); + for ($id_not = 175; $id_not < 200; $id_not++) + $notices [] = $this->fixture(Class_Notice::class, + ['id' => $id_not, + 'type_doc' => Class_TypeDoc::LIVRE, + 'facettes' => 'A' . (925 + $id_not)]); + + $authors = []; + for ($id_aut = 1100; $id_aut < 1125; $id_aut++) + $authors [] = $this->fixture(Class_CodifAuteur::class, + ['id' => $id_aut, + 'libelle' => 'Victor Hugo ' . $id_aut, + 'thumbnail_url' => 'http://wp/hugo.jpg']); + + $this->onLoaderOfModel(Class_Notice::class) + ->whenCalled('getNoticesFromPreferences') + ->willDo(function ($preferences) use ($notices) { + if (isset($preferences['start_limit'])) + return array_slice($notices, 100, 200); + + return array_slice($notices, 0, 100); + }); + + $this->onLoaderOfModel(Class_CodifAuteur::class) + ->whenCalled('findAllBy') + ->willDo(function ($params) use ($authors) { + $nb_id_authors = isset($params['id_auteur']) + ? count($params['id_auteur']) + : 0; + if (10 === $nb_id_authors) + return array_slice($authors, 0, 10); + if (15 === $nb_id_authors) + return array_slice($authors, 10, 25); + + return $authors; + }); + } + + + /** @test */ + public function victorHugo1110ShouldNotBeDuplicated() { + $this->assertXPathCount('//div[@class="card-title"][text()="Victor Hugo 1110"]', + 1); + } + + + /** @test */ + public function pageShouldContains20Auhtors() { + $this->assertXPathCount('//div[@class="card-title"][contains(text(), "Victor Hugo")]', 20); + } +} -- GitLab