diff --git a/lib/classes/API/V1/adapter.class.php b/lib/classes/API/V1/adapter.class.php index a69f1e7e81..ea3a77d673 100644 --- a/lib/classes/API/V1/adapter.class.php +++ b/lib/classes/API/V1/adapter.class.php @@ -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(), diff --git a/lib/classes/record/adapter.class.php b/lib/classes/record/adapter.class.php index 55a33c33b1..2166725bf9 100644 --- a/lib/classes/record/adapter.class.php +++ b/lib/classes/record/adapter.class.php @@ -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,22 +611,35 @@ 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'])) { - if ($devices && ! array_intersect($databoxSubdef->getDevices(), $searchDevices)) { - continue; + $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']; } - - 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)) { 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); } diff --git a/tests/Alchemy/Phrasea/Application/ApiJsonTest.php b/tests/Alchemy/Phrasea/Application/ApiJsonTest.php index c67abe4232..31802893cb 100644 --- a/tests/Alchemy/Phrasea/Application/ApiJsonTest.php +++ b/tests/Alchemy/Phrasea/Application/ApiJsonTest.php @@ -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,34 +487,26 @@ 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()); + $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->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE')); + $this->evaluateResponse200($this->client->getResponse()); + $this->evaluateMetaJson200($content); - $crawler = $this->client->request('GET', $route); - $content = json_decode($this->client->getResponse()->getContent()); - - $this->evaluateResponse200($this->client->getResponse()); - $this->evaluateMetaJson200($content); - - foreach ($content->response as $embed) { - foreach ($keys as $key) { - $this->assertObjectHasAttribute($key, $embed); - $this->checkEmbed($key, $embed->$key, $record); - } + foreach ($content->response as $embed) { + foreach ($keys as $key) { + $this->assertObjectHasAttribute($key, $embed); + $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); diff --git a/tests/Alchemy/Phrasea/Application/ApiYamlTest.php b/tests/Alchemy/Phrasea/Application/ApiYamlTest.php index a875f8816d..b140f6627c 100644 --- a/tests/Alchemy/Phrasea/Application/ApiYamlTest.php +++ b/tests/Alchemy/Phrasea/Application/ApiYamlTest.php @@ -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; /** * @@ -111,7 +113,7 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract if (403 != $content["meta"]["http_code"]) { $fail = new \Exception('Result does not match expected 403, returns ' . $content["meta"]["http_code"]); } - + } catch (\Exception $e) { $fail = $e; } @@ -492,34 +494,24 @@ 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); + $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; - $this->evaluateMethodNotAllowedRoute($route, array('POST', 'PUT', 'DELETE')); - - $crawler = $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()); - $this->evaluateMetaYaml200($content); - - foreach ($content["response"] as $embed) { - foreach ($keys as $key) { - $this->assertArrayHasKey($key, $embed); - $this->checkEmbed($key, $embed[$key], $record); - } + foreach ($content["response"] as $embed) { + foreach ($keys as $key) { + $this->assertArrayHasKey($key, $embed); + $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); @@ -608,7 +625,7 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract { $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'); } @@ -718,7 +735,7 @@ class ApiYamlApplication extends PhraseanetWebTestCaseAbstract foreach ($field->get_values() as $value) { if ($field->is_readonly() === false && $field->is_multi() === false) { $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))) continue; $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(); }