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

hotline #129127 : better site title detection + network error capture

parent 002914e5
Branches
Tags
1 merge request!3910hotline #129127 : better site title detection + network error capture
Pipeline #12751 passed with stage
in 54 minutes and 18 seconds
- ticket #129127 : Sitothèque : Correction d'une erreur possible lors de l'import, amélioration des informations en cas d'erreur
\ No newline at end of file
<?php
echo $this->renderForm($this->import_form);
?>
......@@ -566,25 +566,36 @@ class ZendAfi_Controller_Plugin_Manager_Album extends ZendAfi_Controller_Plugin_
$import_form = $this->_view
->newForm(['id' => 'import', 'class' => 'form'])
->setMethod('post')
->addElement('url', 'url', ['label' => $this->_view->_('URL du site web'),
'required' => true,
'allowEmpty' => false])
->addDisplayGroup(['url'], 'website', ['legend' => $this->_view->_('Site web')])
->addElement('submit', 'submit', ['label' => $this->_view->_('Importer')]);
->addElement('url', 'url',
['label' => $this->_('URL du site web'),
'required' => true,
'allowEmpty' => false])
->addUniqDisplayGroup('website', ['legend' => $this->_('Site web')])
->addElement('submit', 'submit',
['label' => $this->_('Importer')]);
if ($this->_request->isPost() && $import_form->isValid($this->_request->getPost())) {
$this->_view->import_form = $import_form;
$this->_view->titre = $this->_('Importer un site');
if (!$this->_request->isPost())
return;
if (!$import_form->isValid($this->_request->getPost()))
return;
try {
$album = $this->createAlbumFromUrl($this->_request->getPost('url'));
if ($album && $album->save()) {
$this->_redirect('/admin/album/edit_album/id/'.$album->getId());
return;
}
} catch(Exception $e) {
$import_form->url->addError($this->_('Impossible d\'ajouter ce site : %s',
$e->getMessage()));
return;
}
$this->_view->import_form = $import_form;
$album->save();
$this->_redirect('/admin/album/edit_album/id/'.$album->getId());
}
public function resourceDeletePosterAction() {
$this->_helper->getHelper('viewRenderer')->setNoRender(true);
......@@ -601,14 +612,15 @@ class ZendAfi_Controller_Plugin_Manager_Album extends ZendAfi_Controller_Plugin_
protected function createAlbumFromUrl($url) {
$html = Class_WebService_SimpleWebClient::getInstance()->open_url($url);
$dom = new Zend_Dom_Query($html);
$title_node = $dom->queryXpath('//head/title')->current();
$category = Class_AlbumCategorie::getOrCreateRootCategory('Sites web');
$album = Class_Album::newInstance(['type_doc_id' => Class_TypeDoc::WEBSITE,
'categorie' => $category]);
$title_node = $dom->queryXpath('//head/title')->current();
$album->setTitre($title_node ? trim($title_node->textContent) : $url);
$album->setTitre($title_node && trim($title_node->textContent)
? trim($title_node->textContent)
: $url);
if ($description_node = $dom->queryXpath('//head/meta[@name="description"]')->current())
$album->setDescription($description_node->getAttribute('content'));
......@@ -619,8 +631,7 @@ class ZendAfi_Controller_Plugin_Manager_Album extends ZendAfi_Controller_Plugin_
$album->addRessource($resource);
$album->save();
$thumbnailer = (new Class_WebService_WebSiteThumbnail());
$thumbnailer = new Class_WebService_WebSiteThumbnail();
$poster_name = $thumbnailer->fileNameFromUrl($url);
$poster_path = $resource->getPosterPath();
......
......@@ -20,9 +20,8 @@
*/
abstract class AlbumControllerAddWebsiteFromUrlTestCase extends AbstractControllerTestCase {
protected
$_storm_default_to_volatile = true;
abstract class AlbumControllerAddWebsiteFromUrlTestCase extends Admin_AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
public function setUp() {
parent::setUp();
......@@ -34,23 +33,25 @@ protected
class AlbumControllerAddWebsiteFromUrlTest extends AlbumControllerAddWebsiteFromUrlTestCase {
public function setUp() {
parent::setUp();
$this->dispatch('/admin/album/add-website', true);
$this->dispatch('/admin/album/add-website');
}
/** @test */
public function formShouldBePreset() {
$this->assertXPath('//form');
public function formShouldContainsInputUrl() {
$this->assertXPath('//form//input[@name="url"]');
}
}
abstract class AlbumControllerAddWebsiteFromUrlPostTestCase extends AlbumControllerAddWebsiteFromUrlTestCase {
abstract class AlbumControllerAddWebsiteFromUrlPostTestCase
extends AlbumControllerAddWebsiteFromUrlTestCase {
protected
$_album,
$_resource;
......@@ -61,11 +62,10 @@ abstract class AlbumControllerAddWebsiteFromUrlPostTestCase extends AlbumControl
->whenCalled('open_url')
->answers($this->_getContent()));
$this->postDispatch('/admin/album/add-website',
['url' => $this->_getUrl()]);
$this->postDispatch('/admin/album/add-website', ['url' => $this->_getUrl()]);
$this->_album = Class_Album::find(1);
$this->_resource = $this->_album->getRessources()[0];
if ($this->_album = Class_Album::find(1))
$this->_resource = $this->_album->getRessources()[0];
}
......@@ -166,6 +166,7 @@ class AlbumControllerAddWebsiteImportEnssibFromUrlTest
class AlbumControllerAddWebsiteImportBnfFromUrlTest
extends AlbumControllerAddWebsiteFromUrlPostTestCase {
......@@ -183,4 +184,62 @@ class AlbumControllerAddWebsiteImportBnfFromUrlTest
public function titleShouldBeBnfDotFr() {
$this->assertEquals('http://www.bnf.fr', $this->_album->getTitre());
}
}
\ No newline at end of file
}
/** @see http://forge.afi-sa.fr/issues/129127 */
class AlbumControllerAddWebsiteImportAssociationBilanCarboneFromUrlTest
extends AlbumControllerAddWebsiteFromUrlPostTestCase {
protected function _getContent() {
return file_get_contents(__DIR__ . '/associationbilancarbone.fr.html');
}
protected function _getUrl() {
return 'https://www.associationbilancarbone.fr';
}
/** @test */
public function titleShouldBeUrl() {
$this->assertEquals($this->_getUrl(), $this->_album->getTitre());
}
}
/** @see http://forge.afi-sa.fr/issues/129127 */
class AlbumControllerAddWebsiteFromUrlWithConnexionErrorTest
extends AlbumControllerAddWebsiteFromUrlTestCase {
public function setUp() {
parent::setUp();
$webclient = $this->mock()
->whenCalled('open_url')
->willDo(function()
{
throw new RuntimeException('Oups !');
});
Class_WebService_SimpleWebClient::setInstance($webclient);
$this->postDispatch('/admin/album/add-website',
['url' => 'http://will-not-answer.net']);
}
/** @test */
public function shouldNotRedirect() {
$this->assertNotRedirect();
}
/** @test */
public function pageShouldContainsErrorOups() {
$this->assertXPathContentContains('//ul[@class="errors"]', 'Oups !');
}
}
This diff is collapsed.
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