Add option to the API embed route

This commit is contained in:
Romain Neutron
2012-04-30 16:34:51 +02:00
parent abc905223c
commit 871a0eeeb8
4 changed files with 115 additions and 62 deletions

View File

@@ -451,7 +451,11 @@ class API_V1_adapter extends API_V1_Abstract
$record = $this->appbox->get_databox($databox_id)->get_record($record_id);
$ret = array();
foreach ($record->get_embedable_medias() as $name => $media) {
$devices = $request->get('devices', array());
$mimes = $request->get('mimes', array());
foreach ($record->get_embedable_medias($devices, $mimes) as $name => $media) {
$ret[$name] = $this->list_embedable_media($media, $this->appbox->get_registry());
}
@@ -1117,7 +1121,6 @@ class API_V1_adapter extends API_V1_Abstract
* @todo ajouter une option pour avoir les values serialisées
* dans un cas multi
*/
return array(
'meta_id' => $value->getId(),
'meta_structure_id' => $field->get_meta_struct_id(),

View File

@@ -354,9 +354,10 @@ class record_adapter implements record_Interface, cache_cacheableInterface
*
* @return Array
*/
public function get_embedable_medias()
public function get_embedable_medias($devices = null, $mimes = null)
{
return $this->get_subdefs();
return $this->getSubdfefByDeviceAndMime($devices, $mimes);
}
/**
@@ -610,11 +611,25 @@ class record_adapter implements record_Interface, cache_cacheableInterface
*/
public function getSubdfefByDeviceAndMime($devices = null, $mimes = null)
{
$subdefNames = array();
$subdefNames = $subdefs = array();
$searchDevices = array_merge((array) $devices, databox_subdef::DEVICE_ALL);
$availableSubdefs = $this->get_subdefs();
foreach ($this->databox->get_subdef_structure() as $databoxSubdef) {
if (isset($availableSubdefs['document'])) {
$mime_ok = ! $mimes || in_array($availableSubdefs['document']->get_mime(), (array) $mime);
$devices_ok = ! $devices || array_intersect($availableSubdefs['document']->getDevices(), (array) $devices);
if ($mime_ok && $devices_ok) {
$subdefs['document'] = $availableSubdefs['document'];
}
}
$searchDevices = array_merge((array) $devices, (array) databox_subdef::DEVICE_ALL);
foreach ($this->databox->get_subdef_structure() as $databoxSubdefs) {
foreach ($databoxSubdefs as $databoxSubdef) {
if ($devices && ! array_intersect($databoxSubdef->getDevices(), $searchDevices)) {
continue;
@@ -622,10 +637,9 @@ class record_adapter implements record_Interface, cache_cacheableInterface
array_push($subdefNames, $databoxSubdef->get_name());
}
}
$subdefs = array();
foreach ($this->get_subdefs() as $subdef) {
foreach ($availableSubdefs as $subdef) {
if ( ! in_array($subdef->get_name(), $subdefNames)) {
continue;
@@ -639,7 +653,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface
continue;
}
$subdefs[] = $subdef;
$subdefs[$subdef->get_name()] = $subdef;
}
return $subdefs;
@@ -1650,7 +1664,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface
$this->generate_subdef($subdef, $pathdest);
if (file_exists($pathdest)) {
$baseurl = $subdef->get_baseurl() ? $subdef->get_baseurl() . substr(dirname($pathdest), strlen($subdef->get_path()) + 1) : '';
$baseurl = $subdef->get_baseurl() ? $subdef->get_baseurl() . substr(dirname($pathdest), strlen($subdef->get_path())) : '';
media_subdef::create($this, $subdef->get_name(), new system_file($pathdest), $baseurl);
}

View File

@@ -12,6 +12,8 @@ class ApiJsonApplication extends PhraseanetWebTestCaseAbstract
protected static $account_id;
protected static $application;
protected static $databoxe_ids = array();
protected static $need_records = 1;
protected static $need_subdefs = true;
public function setUp()
{
@@ -485,21 +487,14 @@ class ApiJsonApplication extends PhraseanetWebTestCaseAbstract
public function testRecordsEmbedRoute()
{
foreach (static::$databoxe_ids as $databox_id) {
$databox = databox::get_instance($databox_id);
$collection = array_shift($databox->get_collections());
$system_file = new system_file(__DIR__ . '/../../../testfiles/cestlafete.jpg');
$keys = array_keys(self::$record_1->get_subdefs());
$record = record_adapter::create($collection, $system_file);
$record_id = self::$record_1->get_record_id();
$keys = array_keys($record->get_subdefs());
$record_id = $record->get_record_id();
$route = '/records/' . $databox_id . '/' . $record_id . '/embed/?oauth_token=' . self::$token;
$route = '/records/' . self::$record_1->get_sbas_id() . '/' . $record_id . '/embed/?oauth_token=' . self::$token;
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
$crawler = $this->client->request('GET', $route);
$this->client->request('GET', $route);
$content = json_decode($this->client->getResponse()->getContent());
$this->evaluateResponse200($this->client->getResponse());
@@ -508,11 +503,10 @@ class ApiJsonApplication extends PhraseanetWebTestCaseAbstract
foreach ($content->response as $embed) {
foreach ($keys as $key) {
$this->assertObjectHasAttribute($key, $embed);
$this->checkEmbed($key, $embed->$key, $record);
$this->checkEmbed($key, $embed->$key, self::$record_1);
}
}
$record->delete();
}
$route = '/records/24892534/51654651553/embed/?oauth_token=' . self::$token;
$this->evaluateNotFoundRoute($route, array('GET'));
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
@@ -521,6 +515,31 @@ class ApiJsonApplication extends PhraseanetWebTestCaseAbstract
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
}
public function testRecordsEmbedRouteMime()
{
$route = '/records/' . self::$record_1->get_sbas_id() . '/' . self::$record_1->get_record_id() . '/embed/?oauth_token=' . self::$token;
$this->client->request('GET', $route, array('mimes' => array('image/jpg', 'image/jpeg')));
$content = json_decode($this->client->getResponse()->getContent());
foreach ($content->response as $embed) {
foreach (array('thumbnail', 'preview') as $key) {
$this->assertObjectHasAttribute($key, $embed);
$this->checkEmbed($key, $embed->$key, self::$record_1);
}
}
}
public function testRecordsEmbedRouteDevices()
{
$route = '/records/' . self::$record_1->get_sbas_id() . '/' . self::$record_1->get_record_id() . '/embed/?oauth_token=' . self::$token;
$this->client->request('GET', $route, array('devices' => array('nodevice')));
$content = json_decode($this->client->getResponse()->getContent());
$this->assertEquals(0, count($content->response->embed));
}
protected function checkEmbed($subdef_name, $embed, record_adapter $record)
{
$this->assertObjectHasAttribute("permalink", $embed);

View File

@@ -12,6 +12,8 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
protected static $account_id;
protected static $application;
protected static $databoxe_ids = array();
protected static $need_records = 1;
protected static $need_subdefs = true;
/**
*
@@ -492,21 +494,12 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
public function testRecordsEmbedRoute()
{
foreach (static::$databoxe_ids as $databox_id) {
$databox = databox::get_instance($databox_id);
$collection = array_shift($databox->get_collections());
$system_file = new system_file(__DIR__ . '/../../../testfiles/cestlafete.jpg');
$keys = array_keys(self::$record_1->get_subdefs());
$record = record_adapter::create($collection, $system_file);
$keys = array_keys($record->get_subdefs());
$record_id = $record->get_record_id();
$route = '/records/' . $databox_id . '/' . $record_id . '/embed/?oauth_token=' . self::$token;
$route = '/records/' . self::$record_1->get_sbas_id() . '/' . self::$record_1->get_record_id() . '/embed/?oauth_token=' . self::$token;
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
$crawler = $this->client->request('GET', $route, array(), array(), array("HTTP_ACCEPT" => "application/yaml"));
$this->client->request('GET', $route, array(), array(), array("HTTP_ACCEPT" => "application/yaml"));
$content = self::$yaml->parse($this->client->getResponse()->getContent());
$this->evaluateResponse200($this->client->getResponse());
@@ -515,11 +508,10 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
foreach ($content["response"] as $embed) {
foreach ($keys as $key) {
$this->assertArrayHasKey($key, $embed);
$this->checkEmbed($key, $embed[$key], $record);
$this->checkEmbed($key, $embed[$key], self::$record_1);
}
}
$record->delete();
}
$route = '/records/24892534/51654651553/embed/?oauth_token=' . self::$token;
$this->evaluateNotFoundRoute($route, array('GET'));
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
@@ -528,6 +520,31 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
}
public function testRecordsEmbedRouteMime()
{
$route = '/records/' . self::$record_1->get_sbas_id() . '/' . self::$record_1->get_record_id() . '/embed/?oauth_token=' . self::$token;
$this->client->request('GET', $route, array('mimes' => array('image/jpg', 'image/jpeg')), array(), array("HTTP_ACCEPT" => "application/yaml"));
$content = self::$yaml->parse($this->client->getResponse()->getContent());
foreach ($content["response"] as $embed) {
foreach (array('thumbnail', 'preview') as $key) {
$this->assertArrayHasKey($key, $embed);
$this->checkEmbed($key, $embed[$key], self::$record_1);
}
}
}
public function testRecordsEmbedRouteDevices()
{
$route = '/records/' . self::$record_1->get_sbas_id() . '/' . self::$record_1->get_record_id() . '/embed/?oauth_token=' . self::$token;
$this->client->request('GET', $route, array('devices' => array('nodevice')), array(), array("HTTP_ACCEPT" => "application/yaml"));
$content = self::$yaml->parse($this->client->getResponse()->getContent());
$this->assertEquals(0, count($content["response"]['embed']));
}
protected function assertPermalinkHeaders($url, media_subdef $subdef, $type_url = "page_url")
{
$headers = http_query::getHttpHeaders($url);