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

dev #74784 : dedicated json action + do not redirect when not found

parent 5ddcc37d
Branches
Tags
2 merge requests!2897Dev#74784 lille localisation d ouvrages a partir de bilbiomobi,!2885Dev#74784 lille localisation d ouvrages a partir de bilbiomobi
Pipeline #5314 passed with stage
in 29 minutes and 21 seconds
......@@ -60,6 +60,13 @@ class RechercheController extends ZendAfi_Controller_Action {
}
public function preDispatch() {
if ('json' === $this->_getParam('format')
&& 'viewnotice' === $this->_request->getActionName())
$this->_forward('json-record');
}
public function guideeAction() {
$this->view->statut = 'guidee';
$this->simpleAction();
......@@ -314,13 +321,8 @@ class RechercheController extends ZendAfi_Controller_Action {
$id_notice = array_pop($notices)->getId();
}
if (!$notice = Class_Notice::find($id_notice)) {
$this->_redirect('opac/recherche/simple');
return;
}
if ('json' === $this->_getParam('format', ''))
return $this->_renderJsonRecord($notice);
if (!$notice = Class_Notice::find($id_notice))
return $this->_redirect('opac/recherche/simple');
Class_ScriptLoader::getInstance()->addRecordMeta($notice);
......@@ -406,10 +408,30 @@ class RechercheController extends ZendAfi_Controller_Action {
}
protected function _renderJsonRecord($record) {
return $this->_helper->json($record
->acceptVisitor(new Class_Notice_JsonVisitor())
->data());
public function jsonRecordAction() {
$this->_helper->getHelper('viewRenderer')->setNoRender();
($record = $this->_findRecordByKeyOrId((string)$this->_getParam('clef'),
(int)$this->_getParam('id')))
? $this->_helper->json($record
->acceptVisitor(new Class_Notice_JsonVisitor())
->data())
: $this->_response->setHttpResponseCode(404);
}
protected function _findRecordByKeyOrId($key, $id) {
if (!$key || (!$records = Class_Notice::getAllNoticesByClefAlpha($key)))
return Class_Notice::find($id);
if (!$id && (count($records) > 1))
return;
foreach($records as $record)
if ($id == $record->getId())
return $record;
return array_pop($records);
}
......
......@@ -486,7 +486,7 @@ class RechercheControllerJsonWithWrongFromTest extends RechercheControllerJsonTe
class RechercheControllerJsonViewNoticeTeste extends AbstractControllerTestCase {
abstract class RechercheControllerJsonViewNoticeTestCase extends AbstractControllerTestCase {
protected
$_storm_default_to_volatile = true,
$_json;
......@@ -529,6 +529,51 @@ class RechercheControllerJsonViewNoticeTeste extends AbstractControllerTestCase
'source' => Class_Url::absolute($album->getPermalink(), null, true),
'target' => $record->getAbsoluteUrl(),
]);
}
}
class RechercheControllerJsonViewNoticeNotFoundTest extends RechercheControllerJsonViewNoticeTestCase {
/** @test */
public function withKeyAndIdShouldBeNotFound() {
$this->dispatch('/recherche/viewnotice/clef/HOWTOTRAINAPIRATETEAM--HARLOCK-1-GALLIMARDJEUNESSE-2032-1/id/9999/format/json', true);
$this->assertResponseCode('404');
}
/** @test */
public function withKeyOnlyAndManyRecordsShouldBeNotFound() {
$this->onLoaderOfModel('Class_Notice')
->whenCalled('getAllNoticesByClefAlpha')
->answers([new Class_Notice(), new Class_Notice(), new Class_Notice()]);
$this->dispatch('/recherche/viewnotice/clef/HOWTOTRAINAPIRATETEAM--HARLOCK-1-GALLIMARDJEUNESSE-2032-1/format/json', true);
$this->assertResponseCode('404');
}
/** @test */
public function withKeyOnlyAndNoRecordShouldBeNotFound() {
$this->dispatch('/recherche/viewnotice/clef/HOWTOTRAINAPIRATETEAM--HARLOCK-1-GALLIMARDJEUNESSE-2032-1/format/json', true);
$this->assertResponseCode('404');
}
/** @test */
public function withIdOnlyShouldBeNotFound() {
$this->dispatch('/recherche/viewnotice/id/9999/format/json', true);
$this->assertResponseCode('404');
}
}
class RechercheControllerJsonViewNoticeFoundTest extends RechercheControllerJsonViewNoticeTestCase {
public function setUp() {
parent::setUp();
$this->dispatch('/recherche/viewnotice/id/234/format/json', true);
$this->_json = json_decode($this->_response->getBody());
......@@ -577,4 +622,4 @@ class RechercheControllerJsonViewNoticeTeste extends AbstractControllerTestCase
public function recordFirstRelationTypeShouldHaveOneLink($relations) {
$this->assertEquals(99, $relations[0]->entries[0]->id);
}
}
}
\ No newline at end of file
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