diff --git a/application/modules/opac/controllers/PanierController.php b/application/modules/opac/controllers/PanierController.php index b5e5e366dcd432de5e1ee147c75d8d1634089c57..d32ce2de1e9e11f7f34387b53c7ad02ddc87deb9 100644 --- a/application/modules/opac/controllers/PanierController.php +++ b/application/modules/opac/controllers/PanierController.php @@ -97,34 +97,37 @@ class PanierController extends ZendAfi_Controller_Action { public function addAction() { - $this->_prepareAdd($this->view->url(['controller' => 'panier', - 'action' => 'add']), - function() { - return $this->_redirect($this->view->absoluteUrl(['action' => 'index', - 'render' => null, - 'id_panier' => null])); - }); + $this->_addFormAndSave(['controller' => 'panier', + 'action' => 'add'], + + ['controller' => 'panier', + 'action' => 'index', + 'render' => null, + 'id_panier' => null]); } public function addAjaxAction() { - $this->_prepareAdd($this->view->url(['controller' => 'panier', - 'action' => 'add-ajax']), - function() { - return $this->_redirect($this->view->absoluteUrl(['action' => 'add-record-ajax', - 'render' => null, - 'id_panier' => null])); - }); + $after_save_url = ['controller' => 'panier', + 'action' => $this->_getParam('selection') ? 'add-selection' : 'add-record-ajax', + 'render' => null, + 'selection' => null, + 'id_panier' => null]; + + $this->_addFormAndSave(['controller' => 'panier', + 'action' => 'add-ajax'], + + $after_save_url); } - protected function _prepareAdd($action, $callback) { + protected function _addFormAndSave($action, $after_save_url) { $this->view->titre = $this->view->_('Créer un panier'); $this->view->form = ZendAfi_Form_Panier::newWith($this->_request->getParams()); - $this->view->form->setAction($action); + $this->view->form->setAction($this->view->url($action)); if($this->saveModelWithForm(Class_PanierNotice::newForUser($this->_user), $this->view->form)) { - call_user_func($callback); + $this->_redirect($this->view->url($after_save_url)); } } @@ -158,9 +161,14 @@ class PanierController extends ZendAfi_Controller_Action { public function switchAjaxAction() { return $this->_switchAndRedirect('switch-ajax', - $this->view->absoluteUrl(['action' => 'add-record-ajax', - 'render' => null, - 'id_panier' => null])); + + ['controller' => 'panier', + 'action' => ($this->_getParam('selection') + ? 'add-selection' + : 'add-record-ajax'), + 'render' => null, + 'selection' => null, + 'id_panier' => null]); } @@ -184,7 +192,7 @@ class PanierController extends ZendAfi_Controller_Action { } return $url - ? $this->_redirect($url) + ? $this->_redirect($this->view->url($url)) : $this->_redirectClose($this->_getReferer()); } @@ -200,6 +208,30 @@ class PanierController extends ZendAfi_Controller_Action { } + public function addSelectionAction() { + $this->view->titre = $this->_('Mettre la sélection dans un panier'); + + if(!$this->_user) + return $this->_forward('popup-login', 'auth', 'opac', ['redirect' => $this->view->url()]); + + $this->view->panier = $panier = $this->_user->getPanierCourant(); + + if (!$this->_request->isPost()) + return; + + $records = Class_Notice::findAllBy(['id_notice' => (new Class_RecordSelection)->values()]); + array_map(function($record) use($panier) { $panier->addNotice($record); }, + $records); + + $panier->save(); + $panier->index(); + + $this->_helper->notify($this->_('Sélection ajoutée dans le panier "%s"', $panier->getLibelle())); + + $this->_redirectClose($this->_getReferer()); + } + + public function addRecordAjaxAction() { $this->view->titre = $popup_title = $this->_('Ajouter un document dans un panier'); diff --git a/application/modules/opac/views/scripts/panier/add-record-ajax.phtml b/application/modules/opac/views/scripts/panier/add-record-ajax.phtml index a5174c12336d80d4c1e279c4eea5ffb4e9639db8..28a5e0ef3b7d89ca783d90d104ddd9e93cb44cb0 100644 --- a/application/modules/opac/views/scripts/panier/add-record-ajax.phtml +++ b/application/modules/opac/views/scripts/panier/add-record-ajax.phtml @@ -20,7 +20,7 @@ echo $this->tag('div', $this->_('Ajouter %s au panier %s ?', $this->tag('b', $this->notice->getTitrePrincipal()), $this->tag('b', $this->panier->getLibelle())), - ['class' => 'center']) + ['class' => 'center']) . $actions); diff --git a/application/modules/opac/views/scripts/panier/add-selection.phtml b/application/modules/opac/views/scripts/panier/add-selection.phtml new file mode 100644 index 0000000000000000000000000000000000000000..16548345ae66b638107bc8a03a5a13242dbf0178 --- /dev/null +++ b/application/modules/opac/views/scripts/panier/add-selection.phtml @@ -0,0 +1,32 @@ +<?php +$actions = $this->tag('div', $this->tagAnchor($this->url(['controller' => 'panier', + 'action' => 'add-ajax', + 'selection' => '1']), + $this->_('Nouveau panier'), + ['class' => 'bouton', + 'data-popup' => 'true']) . + + $this->tagAnchor($this->url(['controller' => 'panier', + 'action' => 'switch-ajax', + 'id_panier' => $this->panier->getId(), + 'selection' => '1']), + $this->_('Changer de panier'), + ['class' => 'bouton', + 'data-popup' => 'true']), + ['class' => 'boutons']); + +echo $this->tag('div', + $this->tag('p', + $this->_('Ajouter ma sélection au panier "%s" ?', + $this->panier->getLibelle()), + ['class' => 'center']) + . $actions); + +echo $this->renderForm(ZendAfi_Form::newWith([]) + ->setAction($this->url(['controller'=>'panier', + 'action'=>'add-selection', + 'id_panier'=>$this->panier->getId()], + null, + true))); + +?> diff --git a/library/ZendAfi/View/Helper/TagSelectRecord.php b/library/ZendAfi/View/Helper/TagSelectRecord.php index e0d774cf8e4c487bb69c1822aab74f98f84ec4be..43f8078665643809c996f444aadff5882274ee3a 100644 --- a/library/ZendAfi/View/Helper/TagSelectRecord.php +++ b/library/ZendAfi/View/Helper/TagSelectRecord.php @@ -48,7 +48,8 @@ class ZendAfi_View_Helper_TagSelectRecord extends ZendAfi_View_Helper_BaseHelper . $this->_tag('span', (new Class_RecordSelection())->count()) . $this->_tag('ul', - implode([$this->_tag('li', $this->_selectPageLink($criteria)), + implode([$this->_tag('li', $this->_addSelectionToCartLink()), + $this->_tag('li', $this->_selectPageLink($criteria)), $this->_tag('li', $this->_selectAllLink($criteria)), $this->_tag('li', $this->_selectViewLink()), $this->_tag('li', $this->_clearSelectionLink()), @@ -101,6 +102,20 @@ class ZendAfi_View_Helper_TagSelectRecord extends ZendAfi_View_Helper_BaseHelper } + protected function _addSelectionToCartLink() { + $url_add = Class_Url::relative(['controller' => 'panier', + 'action' => 'add-selection']); + + + return $this->_tag('a', + $this->_('Mettre dans un panier'), + ['href' => $url_add, + 'data-popup' => 'true', + 'title' => $this->_('Ajouter toutes les notices de la sélection à un panier') + ]); + } + + protected function _selectAllLink($criteria) { Class_ScriptLoader::getInstance() ->addInlineScript('function updateRecordSelectionCountAndCheckAll(data) { ' diff --git a/tests/application/modules/opac/controllers/PanierControllerTest.php b/tests/application/modules/opac/controllers/PanierControllerTest.php index 18791e37ccd42f9670743f8224f32f669653cc0f..314db89683cf720fe40f8e3a081233d41b8ed5d2 100644 --- a/tests/application/modules/opac/controllers/PanierControllerTest.php +++ b/tests/application/modules/opac/controllers/PanierControllerTest.php @@ -118,10 +118,11 @@ abstract class PanierControllerTestCase extends AbstractControllerTestCase { 'panier_notice_catalogues' => [$panier_domaine_histoire]])) ->setCatalogue($this->fixture('Class_Catalogue', ['id' => 97, - 'libelle' => 'histoire'])) + 'libelle' => 'histoire', + 'indexer' => 1, + 'auteur' => '1'])) ->save(); - $panier_domaine_bd = $this->fixture('Class_PanierNoticeCatalogue', ['id' => 72]); $panier_domaine_bd @@ -252,15 +253,20 @@ class PanierControllerAsSimpleUserActionTest extends PanierControllerSimpleLogge $this->dispatch('/opac/panier', true); } + + /** @test */ public function panierDomaineBDShouldBeVisibleForSimpleUser() { $this->assertXPathCount('//td[contains(text(), "Mes BD")]', 1,$this->_response->getBody()); } + /** @test */ public function forInviteShouldContainsOnePanier() { $this->assertXPathCount('//span[contains(text(), "Vous avez 1 panier")]', 1); } + + /** @test */ public function panierDomaineSelectionJeunesseShouldNotBeVisibleForSimpleUser() { $this->assertNotXPathCount('//td[contains(text(), "selection jeunesse")]', 1,$this->_response->getBody()); @@ -274,9 +280,7 @@ class PanierControllerIndexAsRedacteurActionTest extends PanierControllerTestCas parent::setUp(); $this->manon->changeRoleTo(ZendAfi_Acl_AdminControllerRoles::MODO_PORTAIL); - $this->dispatch('/opac/panier', true); - } @@ -286,6 +290,7 @@ class PanierControllerIndexAsRedacteurActionTest extends PanierControllerTestCas 'Vous avez 2 paniers'); } + /** @test */ public function panierDomaineShouldContainLinkToCreerPanier(){ $this->assertNotXPath('//a[contains(@href,"panier/creer-panier")]',$this->response->getBody()); @@ -320,6 +325,7 @@ class PanierControllerIndexWithPanierIdFifteenTest extends PanierControllerTestC $this->assertAction('index'); } + /** @test */ public function panierMesBDShouldBeVisible() { $this->assertQueryContentContains('td', 'Mes BD'); @@ -593,6 +599,25 @@ class PanierControllerAjoutNoticeBlackSadToPanierDomaineHistoireTest extends Pan +class PanierControllerAjoutNoticeBlackSadToPanierDomaineHistoirePostTest extends PanierControllerTestCase { + public function setUp() { + parent::setUp(); + $this->manon + ->changeRoleTo(ZendAfi_Acl_AdminControllerRoles::MODO_PORTAIL) + ->setPanierCourant(Class_PanierNotice::find(38)); + $this->postDispatch('/panier/add-record-ajax/id_notice/12', []); + Class_PanierNotice::clearCache(); + } + + /** @test */ + public function cardDomainShouldBeIndexedInRecord() { + $this->assertContains('Q97', Class_Notice::find(12)->getFacettes()); + } +} + + + + class PanierControllerModifierTitrePanierMesRomansToMesLivresTest extends PanierControllerTestCase { public function setUp() { parent::setUp(); @@ -1076,7 +1101,7 @@ class PanierControllerAjouterNoticeDansBoitePanierTest extends AbstractControlle } /** @test */ - public function contextShouldExpectation() { + public function postDispatchIdNotice4ShouldaddLeMontespanToMyCart() { $panier = $this->fixture('Class_PanierNotice', ['id' => 1, 'titre' => 'my cart', diff --git a/tests/scenarios/SearchSelection/SearchSelectionTest.php b/tests/scenarios/SearchSelection/SearchSelectionTest.php index 8c4f9a3a5552de066d89b5f0f3d53f7a0a2e0b2a..74069182bedefc613185ddf269778b3756e280c1 100644 --- a/tests/scenarios/SearchSelection/SearchSelectionTest.php +++ b/tests/scenarios/SearchSelection/SearchSelectionTest.php @@ -40,36 +40,32 @@ abstract class SearchSelectionTestCase extends AbstractControllerTestCase { 'liste_format' => Class_Systeme_ModulesAppli::LISTE_FORMAT_VIGNETTES, 'liste_codes' => "TAN9"]]]); - $this - ->onLoaderOfModel('Class_Notice') - ->whenCalled('findAllBy') - ->answers([ - Class_Notice::newInstanceWithId(8, - ['clef_alpha'=>'HARRY_POTTER_ROWLING_1', - 'titre_principal' => 'Harry Ppotter', - 'clef_oeuvre' =>'HARRY_POTTER', - 'url_vignette'=>'no', - 'url_image'=>'no', - 'date_creation'=> '2013-12-30']), - - Class_Notice::newInstanceWithId(9, - ['clef_alpha'=>'MILLENIUM_LARSSON_1', - 'titre_principal' => 'Millenium', - 'clef_oeuvre' =>'MILLENIUM_LARSSON', - 'url_vignette'=>'no', - 'url_image'=>'no', - 'date_creation'=> '2013-12-30']), - - Class_Notice::newInstanceWithId(10, - ['clef_alpha'=>'INTERSTELLAR_NOLAN_3', - 'titre_principal' => 'Interstellar', - 'clef_oeuvre' =>'INTERSTALLAR_NOLAN', - 'url_vignette'=>'no', - 'url_image'=>'no', - 'date_creation'=> '2015-11-28']) - ]) - ; - + $this->fixture('Class_Notice', + ['id' => 8, + 'clef_alpha'=>'HARRY_POTTER_ROWLING_1', + 'titre_principal' => 'Harry Ppotter', + 'clef_oeuvre' =>'HARRY_POTTER', + 'url_vignette'=>'no', + 'url_image'=>'no', + 'date_creation'=> '2013-12-30']); + + $this->fixture('Class_Notice', + ['id' => 9, + 'clef_alpha'=>'MILLENIUM_LARSSON_1', + 'titre_principal' => 'Millenium', + 'clef_oeuvre' =>'MILLENIUM_LARSSON', + 'url_vignette'=>'no', + 'url_image'=>'no', + 'date_creation'=> '2013-12-30']); + + $this->fixture('Class_Notice', + ['id' => 10, + 'clef_alpha'=>'INTERSTELLAR_NOLAN_3', + 'titre_principal' => 'Interstellar', + 'clef_oeuvre' =>'INTERSTALLAR_NOLAN', + 'url_vignette'=>'no', + 'url_image'=>'no', + 'date_creation'=> '2015-11-28']); } } @@ -153,6 +149,18 @@ class SearchSelectionWithSessionTest extends SearchSelectionTestCase { } + + /** @test */ + public function selectionMenuShouldContainsActionToAddToBasket() { + $url = Class_url::relative(['module' => 'opac', + 'controller' => 'panier', + 'action' => 'add-selection']); + + $this->assertXPathContentContains('//div[@class="record-selection"]/ul/li/a[@href="' . $url . '"][@data-popup="true"]', + 'Mettre dans un panier'); + } + + /** @test */ public function selectionMenuShouldContainsActionToSelectPage() { $url = Class_url::relative(['module' => 'opac', @@ -309,7 +317,7 @@ class SearchSelectionSelectViewWithSelectionTest extends SearchSelectionTestCase /** @test */ public function pageShouldContainsRecordMillenium() { - $this->assertXPathContentContains('//div[@class="vignette_titre"]//a', 'Millenium'); + $this->assertXPathContentContains('//div[@class="vignette_titre"]//a', 'Interstellar'); } @@ -358,3 +366,151 @@ class SearchSelectionAdminVarsTest extends Admin_AbstractControllerTestCase { $this->assertXpathContentContains('//td', 'ENABLE_SEARCH_MULTIPLE_RECORD_SELECTION'); } } + + + +class SearchSelectionAddToCartWithoutUserTest extends SearchSelectionTestCase { + /** @test */ + public function withoutUserSessionShouldDisplayLoginPage() { + ZendAfi_Auth::getInstance()->clearIdentity(); + $this->dispatch('/panier/add-selection', true); + $this->assertController('auth'); + } +} + + + +abstract class SearchSelectionAddToCartWithUserTestCase extends SearchSelectionTestCase { + public function setUp() { + parent::setUp(); + + $histoire = $this->fixture('Class_Catalogue', + ['id' => 97, + 'libelle' => 'histoire', + 'indexer' => 1, + 'auteur' => '1']); + + $this->cart = $this->fixture('Class_PanierNotice', + ['id' => 15, + 'id_panier' => 2, + 'libelle' => 'my cart', + 'date_maj' => '25/05/2010', + 'notices' => '', + 'user' => $this->user, + 'catalogues' => [$histoire]]); + + $this->user = $this->fixture('Class_Users', + ['id' => 23, + 'pseudo' => 'zorn', + 'nom' => 'john', + 'login' => 'jzorn', + 'password' => '123', + 'panier_courant' => $this->cart]); + + ZendAfi_Auth::getInstance()->logUser($this->user); + } +} + + + + +class SearchSelectionAddToCartTest extends SearchSelectionAddToCartWithUserTestCase { + public function setUp() { + parent::setUp(); + $this->dispatch('/panier/add-selection', true); + } + + + /** @test */ + public function titleShouldBeAddSelectionToCart() { + $this->assertXPathContentContains('//title', 'Mettre la sélection dans un panier'); + } + + + /** @test */ + public function pageShouldContainsLinkAddToMyCart() { + $this->assertXPathContentContains('//p', 'Ajouter ma sélection au panier "my cart"'); + } + + + /** @test */ + public function pageShouldContainsLinkAddToCreateCart() { + $this->assertXPathContentContains('//a[contains(@href, "panier/add-ajax/selection/1")]', + 'Nouveau panier'); + } + + + /** @test */ + public function pageShouldContainsLinkToSwitchCart() { + $this->assertXPathContentContains('//a[contains(@href, "panier/switch-ajax/id_panier/15/selection/1")]', + 'Changer de panier'); + } + + + /** @test */ + public function pageShouldContainsFormToAddSelection() { + $this->assertXPath('//form[contains(@action, "/panier/add-selection/id_panier/15")]'); + } +} + + + + +class SearchSelectionPostAddSelectionToCartTest extends SearchSelectionAddToCartWithUserTestCase { + public function setUp() { + parent::setUp(); + Zend_Registry::get('session')->search_record_selection = [8, 10]; + $this->postDispatch('/panier/add-selection/id_panier/15', + []); + + Class_PanierNotice::clearCache(); + } + + + /** @test */ + public function cartShouldContainsRecordsPotterAndInterstellar() { + $this->assertEquals(['HARRY_POTTER_ROWLING_1', 'INTERSTELLAR_NOLAN_3'], + Class_PanierNotice::find(15)->getClesNotices()); + } + + + /** @test */ + public function cartDomainShouldBeIndexedInRecord() { + $this->assertContains('Q97', Class_Notice::find(8)->getFacettes()); + } +} + + + + +class SearchSelectionPostNewCartSelectionTest extends SearchSelectionAddToCartWithUserTestCase { + public function setUp() { + parent::setUp(); + + $this->postDispatch('/panier/add-ajax/selection/1', + ['libelle' => 'my new selection']); + } + + + /** @test */ + public function responseShouldBeARedirectToAddSelection() { + $this->assertRedirectTo('/panier/add-selection'); + } +} + + + +class SearchSelectionPostSwitchCartTest extends SearchSelectionAddToCartWithUserTestCase { + public function setUp() { + parent::setUp(); + + $this->postDispatch('/panier/switch-ajax/id_panier/2/selection/1', + ['id_panier' => 15]); + } + + + /** @test */ + public function responseShouldBeARedirectToAddSelection() { + $this->assertRedirectTo('/panier/add-selection'); + } +} \ No newline at end of file