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); $record = $this->appbox->get_databox($databox_id)->get_record($record_id);
$ret = array(); $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()); $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 * @todo ajouter une option pour avoir les values serialisées
* dans un cas multi * dans un cas multi
*/ */
return array( return array(
'meta_id' => $value->getId(), 'meta_id' => $value->getId(),
'meta_structure_id' => $field->get_meta_struct_id(), 'meta_structure_id' => $field->get_meta_struct_id(),

View File

@@ -354,9 +354,10 @@ class record_adapter implements record_Interface, cache_cacheableInterface
* *
* @return Array * @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,22 +611,35 @@ class record_adapter implements record_Interface, cache_cacheableInterface
*/ */
public function getSubdfefByDeviceAndMime($devices = null, $mimes = null) 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'])) {
if ($devices && ! array_intersect($databoxSubdef->getDevices(), $searchDevices)) { $mime_ok = ! $mimes || in_array($availableSubdefs['document']->get_mime(), (array) $mime);
continue; $devices_ok = ! $devices || array_intersect($availableSubdefs['document']->getDevices(), (array) $devices);
if ($mime_ok && $devices_ok) {
$subdefs['document'] = $availableSubdefs['document'];
} }
array_push($subdefNames, $databoxSubdef->get_name());
} }
$subdefs = array(); $searchDevices = array_merge((array) $devices, (array) databox_subdef::DEVICE_ALL);
foreach ($this->get_subdefs() as $subdef) { foreach ($this->databox->get_subdef_structure() as $databoxSubdefs) {
foreach ($databoxSubdefs as $databoxSubdef) {
if ($devices && ! array_intersect($databoxSubdef->getDevices(), $searchDevices)) {
continue;
}
array_push($subdefNames, $databoxSubdef->get_name());
}
}
foreach ($availableSubdefs as $subdef) {
if ( ! in_array($subdef->get_name(), $subdefNames)) { if ( ! in_array($subdef->get_name(), $subdefNames)) {
continue; continue;
@@ -639,7 +653,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface
continue; continue;
} }
$subdefs[] = $subdef; $subdefs[$subdef->get_name()] = $subdef;
} }
return $subdefs; return $subdefs;
@@ -1650,7 +1664,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface
$this->generate_subdef($subdef, $pathdest); $this->generate_subdef($subdef, $pathdest);
if (file_exists($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); 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 $account_id;
protected static $application; protected static $application;
protected static $databoxe_ids = array(); protected static $databoxe_ids = array();
protected static $need_records = 1;
protected static $need_subdefs = true;
public function setUp() public function setUp()
{ {
@@ -485,34 +487,26 @@ class ApiJsonApplication extends PhraseanetWebTestCaseAbstract
public function testRecordsEmbedRoute() public function testRecordsEmbedRoute()
{ {
foreach (static::$databoxe_ids as $databox_id) { $keys = array_keys(self::$record_1->get_subdefs());
$databox = databox::get_instance($databox_id);
$collection = array_shift($databox->get_collections());
$system_file = new system_file(__DIR__ . '/../../../testfiles/cestlafete.jpg');
$record = record_adapter::create($collection, $system_file); $record_id = self::$record_1->get_record_id();
$keys = array_keys($record->get_subdefs()); $route = '/records/' . self::$record_1->get_sbas_id() . '/' . $record_id . '/embed/?oauth_token=' . self::$token;
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
$record_id = $record->get_record_id(); $this->client->request('GET', $route);
$content = json_decode($this->client->getResponse()->getContent());
$route = '/records/' . $databox_id . '/' . $record_id . '/embed/?oauth_token=' . self::$token; $this->evaluateResponse200($this->client->getResponse());
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE')); $this->evaluateMetaJson200($content);
$crawler = $this->client->request('GET', $route); foreach ($content->response as $embed) {
$content = json_decode($this->client->getResponse()->getContent()); foreach ($keys as $key) {
$this->assertObjectHasAttribute($key, $embed);
$this->evaluateResponse200($this->client->getResponse()); $this->checkEmbed($key, $embed->$key, self::$record_1);
$this->evaluateMetaJson200($content);
foreach ($content->response as $embed) {
foreach ($keys as $key) {
$this->assertObjectHasAttribute($key, $embed);
$this->checkEmbed($key, $embed->$key, $record);
}
} }
$record->delete();
} }
$route = '/records/24892534/51654651553/embed/?oauth_token=' . self::$token; $route = '/records/24892534/51654651553/embed/?oauth_token=' . self::$token;
$this->evaluateNotFoundRoute($route, array('GET')); $this->evaluateNotFoundRoute($route, array('GET'));
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE')); $this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
@@ -521,6 +515,31 @@ class ApiJsonApplication extends PhraseanetWebTestCaseAbstract
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE')); $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) protected function checkEmbed($subdef_name, $embed, record_adapter $record)
{ {
$this->assertObjectHasAttribute("permalink", $embed); $this->assertObjectHasAttribute("permalink", $embed);

View File

@@ -12,6 +12,8 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
protected static $account_id; protected static $account_id;
protected static $application; protected static $application;
protected static $databoxe_ids = array(); protected static $databoxe_ids = array();
protected static $need_records = 1;
protected static $need_subdefs = true;
/** /**
* *
@@ -111,7 +113,7 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
if (403 != $content["meta"]["http_code"]) { if (403 != $content["meta"]["http_code"]) {
$fail = new \Exception('Result does not match expected 403, returns ' . $content["meta"]["http_code"]); $fail = new \Exception('Result does not match expected 403, returns ' . $content["meta"]["http_code"]);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
$fail = $e; $fail = $e;
} }
@@ -492,34 +494,24 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
public function testRecordsEmbedRoute() public function testRecordsEmbedRoute()
{ {
foreach (static::$databoxe_ids as $databox_id) { $keys = array_keys(self::$record_1->get_subdefs());
$databox = databox::get_instance($databox_id);
$collection = array_shift($databox->get_collections());
$system_file = new system_file(__DIR__ . '/../../../testfiles/cestlafete.jpg');
$record = record_adapter::create($collection, $system_file); $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'));
$keys = array_keys($record->get_subdefs()); $this->client->request('GET', $route, array(), array(), array("HTTP_ACCEPT" => "application/yaml"));
$content = self::$yaml->parse($this->client->getResponse()->getContent());
$record_id = $record->get_record_id(); $this->evaluateResponse200($this->client->getResponse());
$this->evaluateMetaYaml200($content);
$route = '/records/' . $databox_id . '/' . $record_id . '/embed/?oauth_token=' . self::$token; foreach ($content["response"] as $embed) {
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE')); foreach ($keys as $key) {
$this->assertArrayHasKey($key, $embed);
$crawler = $this->client->request('GET', $route, array(), array(), array("HTTP_ACCEPT" => "application/yaml")); $this->checkEmbed($key, $embed[$key], self::$record_1);
$content = self::$yaml->parse($this->client->getResponse()->getContent());
$this->evaluateResponse200($this->client->getResponse());
$this->evaluateMetaYaml200($content);
foreach ($content["response"] as $embed) {
foreach ($keys as $key) {
$this->assertArrayHasKey($key, $embed);
$this->checkEmbed($key, $embed[$key], $record);
}
} }
$record->delete();
} }
$route = '/records/24892534/51654651553/embed/?oauth_token=' . self::$token; $route = '/records/24892534/51654651553/embed/?oauth_token=' . self::$token;
$this->evaluateNotFoundRoute($route, array('GET')); $this->evaluateNotFoundRoute($route, array('GET'));
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE')); $this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE'));
@@ -528,6 +520,31 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
$this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE')); $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") protected function assertPermalinkHeaders($url, media_subdef $subdef, $type_url = "page_url")
{ {
$headers = http_query::getHttpHeaders($url); $headers = http_query::getHttpHeaders($url);
@@ -608,7 +625,7 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
{ {
$code = http_query::getHttpCodeFromUrl(self::$core->getRegistry()->get('GV_ServerName')); $code = http_query::getHttpCodeFromUrl(self::$core->getRegistry()->get('GV_ServerName'));
if($code == 0) { if ($code == 0) {
$this->markTestSkipped('Install does not seem to rely on a webserver'); $this->markTestSkipped('Install does not seem to rely on a webserver');
} }
@@ -718,7 +735,7 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
foreach ($field->get_values() as $value) { foreach ($field->get_values() as $value) {
if ($field->is_readonly() === false && $field->is_multi() === false) { if ($field->is_readonly() === false && $field->is_multi() === false) {
$saved_value = $toupdate[$field->get_meta_struct_id()]['value']; $saved_value = $toupdate[$field->get_meta_struct_id()]['value'];
$this->assertEquals($value->getValue(), $saved_value, $this->client->getResponse()->getContent()." contains values"); $this->assertEquals($value->getValue(), $saved_value, $this->client->getResponse()->getContent() . " contains values");
} }
} }
} }
@@ -728,7 +745,7 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract
if ( ! in_array($metadata["meta_id"], array_keys($toupdate))) if ( ! in_array($metadata["meta_id"], array_keys($toupdate)))
continue; continue;
$saved_value = $toupdate[$metadata["meta_structure_id"]]['value']; $saved_value = $toupdate[$metadata["meta_structure_id"]]['value'];
$this->assertEquals($saved_value, $metadata["value"], "Asserting that " . $this->client->getResponse()->getContent()." contains values"); $this->assertEquals($saved_value, $metadata["value"], "Asserting that " . $this->client->getResponse()->getContent() . " contains values");
} }
$record->delete(); $record->delete();
} }