diff --git a/config/config.sample.yml b/config/config.sample.yml index 589e80ebf0..7004ec6fec 100644 --- a/config/config.sample.yml +++ b/config/config.sample.yml @@ -21,6 +21,7 @@ dev: #Assign your phraseanet application connection #Connections are defined in connexions.yml configuration file database: main_connexion + api-timers: true #Assign your template engine service & ORM service #Services are defined in service.yml configuration file diff --git a/lib/Alchemy/Phrasea/Application/Api.php b/lib/Alchemy/Phrasea/Application/Api.php index b3a18e6c34..3c9b8e1288 100644 --- a/lib/Alchemy/Phrasea/Application/Api.php +++ b/lib/Alchemy/Phrasea/Application/Api.php @@ -12,6 +12,11 @@ namespace Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application as PhraseaApplication; +use Alchemy\Phrasea\Core\PhraseaEvents; +use Alchemy\Phrasea\Core\Event\ApiLoadEndEvent; +use Alchemy\Phrasea\Core\Event\ApiLoadStartEvent; +use Alchemy\Phrasea\Core\Event\ApiOAuth2StartEvent; +use Alchemy\Phrasea\Core\Event\ApiOAuth2EndEvent; use Silex\Application as SilexApplication; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -22,6 +27,10 @@ use Symfony\Component\HttpFoundation\Request; return call_user_func(function($environment = 'prod') { $app = new PhraseaApplication($environment); + + $app->register(new \API_V1_Timer()); + $app['dispatcher']->dispatch(PhraseaEvents::API_LOAD_START, new ApiLoadStartEvent()); + $app->disableCookies(); /** @@ -48,6 +57,7 @@ return call_user_func(function($environment = 'prod') { * @ throws \API_V1_exception_forbidden */ $app->before(function($request) use ($app) { + $app['dispatcher']->dispatch(PhraseaEvents::API_OAUTH2_START, new ApiOAuth2StartEvent()); $oauth2_adapter = new \API_OAuth2_Adapter($app); $oauth2_adapter->verifyAccessToken(); @@ -62,6 +72,7 @@ return call_user_func(function($environment = 'prod') { } if ($app->isAuthenticated()) { + $app['dispatcher']->dispatch(PhraseaEvents::API_OAUTH2_END, new ApiOAuth2EndEvent()); return; } @@ -70,6 +81,7 @@ return call_user_func(function($environment = 'prod') { $app->openAccount($auth, $oauth2_adapter->get_ses_id()); $oauth2_adapter->remember_this_ses_id($app['session']->get('session_id')); + $app['dispatcher']->dispatch(PhraseaEvents::API_OAUTH2_END, new ApiOAuth2EndEvent()); return; }, 256); @@ -752,5 +764,7 @@ return call_user_func(function($environment = 'prod') { return $response; }); + $app['dispatcher']->dispatch(PhraseaEvents::API_LOAD_END, new ApiLoadEndEvent()); + return $app; }, isset($environment) ? $environment : null); diff --git a/lib/Alchemy/Phrasea/Application/ApiVersion.php b/lib/Alchemy/Phrasea/Application/ApiVersion.php index 2561d67555..cb5528b4d0 100644 --- a/lib/Alchemy/Phrasea/Application/ApiVersion.php +++ b/lib/Alchemy/Phrasea/Application/ApiVersion.php @@ -11,8 +11,11 @@ namespace Alchemy\Phrasea\Application; -use Silex\Application as SilexApplication; use Alchemy\Phrasea\Application as PhraseaApplication; +use Alchemy\Phrasea\Core\PhraseaEvents; +use Alchemy\Phrasea\Core\Event\ApiLoadEndEvent; +use Alchemy\Phrasea\Core\Event\ApiLoadStartEvent; +use Silex\Application as SilexApplication; use Symfony\Component\HttpFoundation\Request; return call_user_func(function($environment = 'prod') { @@ -20,6 +23,9 @@ return call_user_func(function($environment = 'prod') { $app = new PhraseaApplication($environment); $app->disableCookies(); + $app->register(new \API_V1_Timer()); + $app['dispatcher']->dispatch(PhraseaEvents::API_LOAD_START, new ApiLoadStartEvent()); + $app->get('/', function(Request $request, SilexApplication $app) { $apiAdapter = new \API_V1_adapter($app); @@ -45,5 +51,7 @@ return call_user_func(function($environment = 'prod') { ))->get_response(); }); + $app['dispatcher']->dispatch(PhraseaEvents::API_LOAD_END, new ApiLoadEndEvent()); + return $app; }, isset($environment) ? $environment : null); diff --git a/lib/Alchemy/Phrasea/Controller/Permalink.php b/lib/Alchemy/Phrasea/Controller/Permalink.php index 23e84a8021..a4bfad0b1d 100644 --- a/lib/Alchemy/Phrasea/Controller/Permalink.php +++ b/lib/Alchemy/Phrasea/Controller/Permalink.php @@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Controller; use Alchemy\Phrasea\Application as PhraseaApplication; use Silex\Application; +use Symfony\Component\HttpFoundation\Request; /** * @@ -28,8 +29,7 @@ class Permalink extends AbstractDelivery $that = $this; - $controllers->get('/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/view/', function($label, $sbas_id, $record_id, $key, $subdef, PhraseaApplication $app) { - + $deliverPermaview = function($sbas_id, $record_id, $key, $subdef, PhraseaApplication $app) { $databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $record = \media_Permalink_Adapter::challenge_token($app, $databox, $key, $record_id, $subdef); @@ -47,9 +47,9 @@ class Permalink extends AbstractDelivery ); return $app['twig']->render('overview.html.twig', $params); - })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); + }; - $controllers->get('/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/', function(Application $app, $label, $sbas_id, $record_id, $key, $subdef) use ($that) { + $deliverPermalink = function(Application $app, $sbas_id, $record_id, $key, $subdef) use ($that) { $databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $record = \media_Permalink_Adapter::challenge_token($app, $databox, $key, $record_id, $subdef); @@ -93,6 +93,26 @@ class Permalink extends AbstractDelivery } return $that->deliverContent($app['request'], $record, $subdef, $watermark, $stamp, $app); + }; + + $controllers->get('/v1/{sbas_id}/{record_id}/{subdef}/', function (PhraseaApplication $app, Request $request, $sbas_id, $record_id, $subdef) use ($deliverPermaview) { + $key = $request->query->get('token'); + + return $deliverPermaview($sbas_id, $record_id, $key, $subdef, $app); + })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); + + $controllers->get('/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/view/', function(PhraseaApplication $app, $label, $sbas_id, $record_id, $key, $subdef) use ($deliverPermaview) { + return $deliverPermaview($sbas_id, $record_id, $key, $subdef, $app); + })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); + + $controllers->get('/v1/{sbas_id}/{record_id}/{subdef}/{label}', function (PhraseaApplication $app, Request $request, $sbas_id, $record_id, $subdef, $label) use ($deliverPermalink) { + $key = $request->query->get('token'); + + return $deliverPermalink($app, $sbas_id, $record_id, $key, $subdef); + })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); + + $controllers->get('/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/', function(PhraseaApplication $app, $label, $sbas_id, $record_id, $key, $subdef) use ($deliverPermalink) { + return $deliverPermalink($app, $sbas_id, $record_id, $key, $subdef); })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); return $controllers; diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Query.php b/lib/Alchemy/Phrasea/Controller/Prod/Query.php index c6513e642d..3457a9a726 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Query.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Query.php @@ -224,7 +224,6 @@ class Query implements ControllerProviderInterface $json['results'] = $app['twig']->render($template, array( 'results' => $result, - 'GV_social_tools' => $app['phraseanet.registry']->get('GV_social_tools'), 'highlight' => $result->getQuery(), 'searchEngine' => $app['phraseanet.SE'], 'suggestions' => $prop diff --git a/lib/Alchemy/Phrasea/Core/Configuration.php b/lib/Alchemy/Phrasea/Core/Configuration.php index 246dfbb155..406c8b6d49 100644 --- a/lib/Alchemy/Phrasea/Core/Configuration.php +++ b/lib/Alchemy/Phrasea/Core/Configuration.php @@ -204,7 +204,10 @@ class Configuration public function setConfigurations($configurations) { - return $this->specifications->setConfigurations($configurations); + $this->specifications->setConfigurations($configurations); + $this->configuration = new ParameterBag($configurations[$this->environment]); + + return $this->getConfigurations(); } public function setServices($services) diff --git a/lib/Alchemy/Phrasea/Core/Event/ApiLoadEndEvent.php b/lib/Alchemy/Phrasea/Core/Event/ApiLoadEndEvent.php new file mode 100644 index 0000000000..304bbf5e9b --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Event/ApiLoadEndEvent.php @@ -0,0 +1,23 @@ +starting = microtime(true); + } + + public function register(Application $app) + { + $app['api.timers'] = new ArrayCollection(); + $app['api.timers.start'] = $this->starting; + + $callback = function (Event $event) use ($app) { + $name = $event->getName(); + $n = 1; + while(isset($app['api.timers']->{$name})) { + $n++; + $name = $event->getName() . '#' . $n; + } + $app['api.timers']->add(array( + 'name' => $name, + 'memory' => memory_get_usage(), + 'time' => microtime(true) - $app['api.timers.start'], + )); + }; + + $app['dispatcher']->addListener(KernelEvents::CONTROLLER, $callback, -999999); + $app['dispatcher']->addListener(KernelEvents::REQUEST, $callback, 999999); + $app['dispatcher']->addListener(KernelEvents::REQUEST, $callback, -999999); + $app['dispatcher']->addListener(KernelEvents::RESPONSE, $callback, -999999); + $app['dispatcher']->addListener(KernelEvents::EXCEPTION, $callback, 999999); + $app['dispatcher']->addListener(PhraseaEvents::API_OAUTH2_START, $callback); + $app['dispatcher']->addListener(PhraseaEvents::API_OAUTH2_END, $callback); + $app['dispatcher']->addListener(PhraseaEvents::API_LOAD_END, $callback); + $app['dispatcher']->addListener(PhraseaEvents::API_RESULT, $callback); + } + + public function boot(Application $app) + { + } +} \ No newline at end of file diff --git a/lib/classes/API/V1/adapter.php b/lib/classes/API/V1/adapter.php index 376a1cd490..23c0da35d5 100644 --- a/lib/classes/API/V1/adapter.php +++ b/lib/classes/API/V1/adapter.php @@ -64,7 +64,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_error_message(Request $request, $error, $message) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_error_message($error, $message); return $result; @@ -78,7 +78,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_error_code(Request $request, $code) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_error_code($code); return $result; @@ -102,7 +102,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_scheduler(Application $app) { - $result = new \API_V1_result($app['request'], $this); + $result = new \API_V1_result($app, $app['request'], $this); $taskManager = $app['task-manager']; $ret = $taskManager->getSchedulerState(); @@ -129,7 +129,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_task_list(Application $app) { - $result = new \API_V1_result($app['request'], $this); + $result = new \API_V1_result($app, $app['request'], $this); $taskManager = $app['task-manager']; $tasks = $taskManager->getTasks(); @@ -168,7 +168,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_task(Application $app, $taskId) { - $result = new \API_V1_result($app['request'], $this); + $result = new \API_V1_result($app, $app['request'], $this); $taskManager = $app['task-manager']; @@ -190,7 +190,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function start_task(Application $app, $taskId) { - $result = new \API_V1_result($app['request'], $this); + $result = new \API_V1_result($app, $app['request'], $this); $taskManager = $app['task-manager']; @@ -213,7 +213,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function stop_task(Application $app, $taskId) { - $result = new API_V1_result($app['request'], $this); + $result = new API_V1_result($app, $app['request'], $this); $taskManager = $app['task-manager']; @@ -238,7 +238,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function set_task_property(Application $app, $taskId) { - $result = new API_V1_result($app['request'], $this); + $result = new API_V1_result($app, $app['request'], $this); $title = $app['request']->get('title'); $autostart = $app['request']->get('autostart'); @@ -499,7 +499,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_phraseanet_monitor(Application $app) { - $result = new API_V1_result($app['request'], $this); + $result = new API_V1_result($app, $app['request'], $this); $ret = array_merge( $this->get_config_info($app), $this->get_cache_info($app), $this->get_gv_info($app) @@ -519,7 +519,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_databoxes(Request $request) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_datas(array("databoxes" => $this->list_databoxes())); @@ -536,7 +536,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_databox_collections(Request $request, $databox_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_datas( array( @@ -559,7 +559,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_databox_status(Request $request, $databox_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_datas( array( @@ -583,7 +583,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_databox_metadatas(Request $request, $databox_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_datas( array( @@ -608,7 +608,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_databox_terms(Request $request, $databox_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_datas( array( @@ -622,7 +622,7 @@ class API_V1_adapter extends API_V1_Abstract public function caption_records(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); $fields = $record->get_caption()->get_fields(); @@ -728,7 +728,7 @@ class API_V1_adapter extends API_V1_Abstract $ret['url'] = '/quarantine/item/' . $output->getId() . '/'; } - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_datas($ret); @@ -758,7 +758,7 @@ class API_V1_adapter extends API_V1_Abstract $ret[] = $this->list_lazaret_file($lazaretFile); } - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_datas(array( 'offset_start' => $offset_start, @@ -784,7 +784,7 @@ class API_V1_adapter extends API_V1_Abstract $ret = array('quarantine_item' => $this->list_lazaret_file($lazaretFile)); - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $result->set_datas($ret); @@ -834,7 +834,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function search(Request $request) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); list($ret, $search_result) = $this->prepare_search_request($request); @@ -867,7 +867,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function search_records(Request $request) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); list($ret, $search_result) = $this->prepare_search_request($request); @@ -935,7 +935,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_record_related(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $that = $this; $baskets = array_map(function ($basket) use ($that) { @@ -971,7 +971,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_record_metadatas(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); @@ -995,7 +995,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_record_status(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $record = $this->app['phraseanet.appbox'] ->get_databox($databox_id) @@ -1026,7 +1026,7 @@ class API_V1_adapter extends API_V1_Abstract public function get_record_embed(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); @@ -1056,7 +1056,7 @@ class API_V1_adapter extends API_V1_Abstract public function get_story_embed(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $record = $this->app['phraseanet.appbox'] ->get_databox($databox_id) @@ -1078,7 +1078,7 @@ class API_V1_adapter extends API_V1_Abstract public function set_record_metadatas(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); try { @@ -1105,7 +1105,7 @@ class API_V1_adapter extends API_V1_Abstract public function set_record_status(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); $record = $databox->get_record($record_id); $status_bits = $databox->get_statusbits(); @@ -1159,7 +1159,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function set_record_collection(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); $record = $databox->get_record($record_id); @@ -1185,7 +1185,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_record(Request $request, $databox_id, $record_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); try { $record = $databox->get_record($record_id); @@ -1209,7 +1209,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_story(Request $request, $databox_id, $story_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); try { $story = $databox->get_record($story_id); @@ -1231,7 +1231,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function search_baskets(Request $request) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $usr_id = $session = $this->app['phraseanet.user']->get_id(); @@ -1269,7 +1269,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function create_basket(Request $request) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $name = $request->get('name'); @@ -1318,7 +1318,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_basket(Request $request, $basket_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $repository = $this->app['EM']->getRepository('\Entities\Basket'); @@ -1415,7 +1415,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function set_basket_title(Request $request, $basket_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $name = $request->get('name'); @@ -1443,7 +1443,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function set_basket_description(Request $request, $basket_id) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $desc = $request->get('description'); @@ -1471,7 +1471,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function search_publications(Request $request, User_Adapter $user) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $coll = Feed_Collection::load_all($this->app, $user); @@ -1504,7 +1504,7 @@ class API_V1_adapter extends API_V1_Abstract */ public function get_publication(Request $request, $publication_id, User_Adapter $user) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $feed = Feed_Adapter::load_with_user($this->app, $user, $publication_id); @@ -1527,7 +1527,7 @@ class API_V1_adapter extends API_V1_Abstract public function get_publications(Request $request, User_Adapter $user) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $feed = Feed_Aggregate::load_with_user($this->app, $user); @@ -1550,7 +1550,7 @@ class API_V1_adapter extends API_V1_Abstract public function get_feed_entry(Request $request, $entry_id, User_Adapter $user) { - $result = new API_V1_result($request, $this); + $result = new API_V1_result($this->app, $request, $this); $entry = Feed_Entry_Adapter::load_from_id($this->app, $entry_id); @@ -1729,8 +1729,9 @@ class API_V1_adapter extends API_V1_Abstract 'is_activated' => $permalink->get_is_activated(), 'label' => $permalink->get_label(), 'updated_on' => $permalink->get_last_modified()->format(DATE_ATOM), - 'page_url' => $permalink->get_page($registry), - 'url' => $permalink->get_url($registry) + 'page_url' => $permalink->get_page(), + 'download_url' => $permalink->get_url() . '&download', + 'url' => $permalink->get_url() ); } diff --git a/lib/classes/API/V1/result.php b/lib/classes/API/V1/result.php index b01dd4a7fc..3f26f6ed04 100644 --- a/lib/classes/API/V1/result.php +++ b/lib/classes/API/V1/result.php @@ -9,19 +9,16 @@ * file that was distributed with this source code. */ -use Symfony\Component\HttpFoundation\Response; +use Alchemy\Phrasea\Core\Event\ApiResultEvent; +use Alchemy\Phrasea\Core\PhraseaEvents; +use Silex\Application; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; -/** - * - * - * - * @package APIv1 - * @license http://opensource.org/licenses/gpl-3.0 GPLv3 - * @link www.phraseanet.com - */ class API_V1_result { + protected $app; + /** * * @var string @@ -103,10 +100,11 @@ class API_V1_result * * @return API_V1_result */ - public function __construct(Request $request, API_V1_adapter $api) + public function __construct(Application $app, Request $request, API_V1_adapter $api) { $date = new DateTime(); + $this->app = $app; $this->request = $request; $this->api_version = $api->get_version(); $this->response_time = $date->format(DATE_ATOM); @@ -197,6 +195,14 @@ class API_V1_result , 'response' => $this->response ); + $this->app['dispatcher']->dispatch(PhraseaEvents::API_RESULT, new ApiResultEvent()); + + $conf = $this->app['phraseanet.configuration']->getPhraseanet(); + + if ($conf->has('api-timers') && true === $conf->get('api-timers')) { + $ret['timers'] = $this->app['api.timers']->toArray(); + } + $return_value = false; switch ($this->response_type) { diff --git a/lib/classes/media/Permalink/Adapter.php b/lib/classes/media/Permalink/Adapter.php index 2ebb590729..2df501922c 100644 --- a/lib/classes/media/Permalink/Adapter.php +++ b/lib/classes/media/Permalink/Adapter.php @@ -10,6 +10,7 @@ */ use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\Exception\RuntimeException; /** * @@ -145,30 +146,29 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea */ public function get_url() { - return sprintf('%spermalink/v1/%s/%d/%d/%s/%s/' - , $this->app['phraseanet.registry']->get('GV_ServerName') - , $this->get_label() - , $this->media_subdef->get_sbas_id() - , $this->media_subdef->get_record_id() - , $this->get_token() - , $this->media_subdef->get_name() + return sprintf('%spermalink/v1/%d/%d/%s/%s.%s?token=%s', + $this->app['phraseanet.registry']->get('GV_ServerName'), + $this->media_subdef->get_sbas_id(), + $this->media_subdef->get_record_id(), + $this->media_subdef->get_name(), + $this->get_label(), + pathinfo($this->media_subdef->get_record()->get_original_name(), PATHINFO_EXTENSION), + $this->get_token() ); } /** * - * @param registryInterface $registry * @return string */ - public function get_page(registryInterface $registry) + public function get_page() { - return sprintf('%spermalink/v1/%s/%d/%d/%s/%s/view/' - , $registry->get('GV_ServerName') - , $this->get_label() - , $this->media_subdef->get_sbas_id() - , $this->media_subdef->get_record_id() - , $this->get_token() - , $this->media_subdef->get_name() + return sprintf('%spermalink/v1/%d/%d/%s/?token=%s', + $this->app['phraseanet.registry']->get('GV_ServerName'), + $this->media_subdef->get_sbas_id(), + $this->media_subdef->get_record_id(), + $this->media_subdef->get_name(), + $this->get_token() ); } @@ -225,7 +225,7 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea */ public function set_label($label) { - $label = trim($label); + $label = trim($label) ? trim($label) : 'untitled'; while (strpos($label, ' ') !== false) $label = str_replace(' ', ' ', $label); @@ -334,13 +334,21 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea , ':activated' => '1' ); + $error = null; $stmt = $databox->get_connection()->prepare($sql); - $stmt->execute($params); + try { + $stmt->execute($params); + } catch (\PDOException $e) { + $error = $e; + } $stmt->closeCursor(); - unset($stmt); + + if ($error) { + throw new RuntimeException('Permalink already exists', $e->getCode(), $e); + } $permalink = self::getPermalink($app, $databox, $media_subdef); - $permalink->set_label(strip_tags($media_subdef->get_record()->get_title())); + $permalink->set_label(strip_tags($media_subdef->get_record()->get_title(false, null, true))); return $permalink; } diff --git a/lib/classes/media/Permalink/Interface.php b/lib/classes/media/Permalink/Interface.php index 2d55e07118..ef81c2fcbd 100644 --- a/lib/classes/media/Permalink/Interface.php +++ b/lib/classes/media/Permalink/Interface.php @@ -22,7 +22,7 @@ interface media_Permalink_Interface public function get_url(); - public function get_page(registryInterface $registry); + public function get_page(); public function get_id(); diff --git a/lib/classes/patch/379.class.php b/lib/classes/patch/379.class.php new file mode 100644 index 0000000000..97ce1de6bf --- /dev/null +++ b/lib/classes/patch/379.class.php @@ -0,0 +1,64 @@ +release; + } + + public function require_all_upgrades() + { + return false; + } + + /** + * + * @return Array + */ + public function concern() + { + return $this->concern; + } + + /** + * @param base $databox + */ + public function apply(base $appbox, Application $app) + { + $sql = 'UPDATE permalinks SET label = "untitled" WHERE label = ""'; + $stmt = $appbox->get_connection()->prepare($sql); + $stmt->execute(); + $stmt->closeCursor(); + + return true; + } +} + diff --git a/templates/web/common/drop_down_options.html.twig b/templates/web/common/drop_down_options.html.twig index f8c08e099e..dcc32752f7 100644 --- a/templates/web/common/drop_down_options.html.twig +++ b/templates/web/common/drop_down_options.html.twig @@ -30,13 +30,15 @@ {% trans 'action : print' %} - {% if record.is_grouping() is empty %} -
-
- {% trans 'reponses:: partager' %} + {% if registry.get('GV_social_tools') == 'all' or (registry.get('GV_social_tools') == 'publishers' and user.ACL().has_right_on_sbas(record.get_sbas_id(), 'bas_chupub')) %} + {% if record.is_grouping() is empty %} +
+
+ {% trans 'reponses:: partager' %} +
-
+ {% endif %} {% endif %}
diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php b/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php index 584679ddc0..2d78e93ef4 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php @@ -2135,11 +2135,18 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract $this->assertEquals($subdef->get_permalink()->get_page(self::$DI['app']['phraseanet.registry']), $permalink['page_url']); $this->checkUrlCode200($permalink['page_url']); $this->assertPermalinkHeaders($permalink['page_url'], $subdef); + $this->assertArrayHasKey("url", $permalink); $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['url']); $this->assertEquals($subdef->get_permalink()->get_url(), $permalink['url']); $this->checkUrlCode200($permalink['url']); $this->assertPermalinkHeaders($permalink['url'], $subdef, "url"); + + $this->assertArrayHasKey("download_url", $permalink); + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['download_url']); + $this->assertEquals($subdef->get_permalink()->get_url() . '&download', $permalink['download_url']); + $this->checkUrlCode200($permalink['download_url']); + $this->assertPermalinkHeaders($permalink['download_url'], $subdef, "download_url"); } } @@ -2157,6 +2164,10 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract $this->assertTrue(strpos($headers['content_type'], $subdef->get_mime()) === 0, 'Verify that header ' . $headers['content_type'] . ' contains subdef mime type ' . $subdef->get_mime()); $this->assertEquals($subdef->get_size(), $headers["download_content_length"]); break; + case "download_url" : + $this->assertTrue(strpos($headers['content_type'], $subdef->get_mime()) === 0, 'Verify that header ' . $headers['content_type'] . ' contains subdef mime type ' . $subdef->get_mime()); + $this->assertEquals($subdef->get_size(), $headers["download_content_length"]); + break; } } diff --git a/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php b/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php index c827e9e2c8..8eb6b0c935 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php @@ -22,7 +22,7 @@ class ApplicationOverviewTest extends \PhraseanetWebTestCaseAuthenticatedAbstrac function testDatafilesNonExistentSubdef() { self::$DI['client']->request('GET', '/datafiles/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/asubdefthatdoesnotexists/'); - + $this->assertNotFoundResponse(self::$DI['client']->getResponse()); } @@ -72,6 +72,7 @@ class ApplicationOverviewTest extends \PhraseanetWebTestCaseAuthenticatedAbstrac function testPermalinkAuthenticated() { $this->assertTrue(self::$DI['app']->isAuthenticated()); + $this->get_a_permalinkBCcompatibility(); $this->get_a_permalink(); } @@ -79,6 +80,7 @@ class ApplicationOverviewTest extends \PhraseanetWebTestCaseAuthenticatedAbstrac { self::$DI['app']->closeAccount(); $this->assertFalse(self::$DI['app']->isAuthenticated()); + $this->get_a_permalinkBCcompatibility(); $this->get_a_permalink(); } @@ -114,11 +116,11 @@ class ApplicationOverviewTest extends \PhraseanetWebTestCaseAuthenticatedAbstrac $this->assertEquals(200, $response->getStatusCode()); } - protected function get_a_permalink() + protected function get_a_permalinkBCcompatibility() { $token = self::$DI['record_1']->get_preview()->get_permalink()->get_token(); $url = '/permalink/v1/whateverIwannt/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/' . $token . '/preview/'; - + $crawler = self::$DI['client']->request('GET', $url); $response = self::$DI['client']->getResponse(); @@ -129,4 +131,23 @@ class ApplicationOverviewTest extends \PhraseanetWebTestCaseAuthenticatedAbstrac $response = self::$DI['client']->getResponse(); $this->assertEquals(200, $response->getStatusCode()); } + + protected function get_a_permalink() + { + $token = self::$DI['record_1']->get_preview()->get_permalink()->get_token(); + $url = '/permalink/v1/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/preview/whateverIwannt.jpg?token=' . $token . ''; + + $crawler = self::$DI['client']->request('GET', $url); + $response = self::$DI['client']->getResponse(); + + $this->assertEquals(200, $response->getStatusCode()); + + $url = '/permalink/v1/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/preview/?token=' . $token . ''; + + $crawler = self::$DI['client']->request('GET', $url); + $response = self::$DI['client']->getResponse(); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals('text/html; charset=UTF-8', $response->headers->get('Content-Type')); + } } diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php index 8e631b6d74..73c5298279 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php @@ -559,7 +559,7 @@ class RssFeedTest extends \PhraseanetWebTestCaseAbstract $is_thumbnail = false; $record = $entry_item->get_record(); - if (substr($current_attributes["url"], 0 - strlen("/preview/")) == "/preview/") { + if (false !== strpos($current_attributes["url"], 'preview')) { $ressource = $record->get_subdef('preview'); } else { $ressource = $record->get_thumbnail(); diff --git a/tests/classes/api/v1/api_v1_TimerTest.php b/tests/classes/api/v1/api_v1_TimerTest.php new file mode 100644 index 0000000000..1baa8aced9 --- /dev/null +++ b/tests/classes/api/v1/api_v1_TimerTest.php @@ -0,0 +1,48 @@ +getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $dispatcher->expects($this->exactly(9)) + ->method('addListener'); + $app['dispatcher'] = $dispatcher; + $app->register(new API_V1_Timer()); + + $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $app['api.timers']); + $this->assertGreaterThan($start, $app['api.timers.start']); + } + + public function testTriggerEvent() + { + $app = new Application(); + $app->register(new API_V1_Timer()); + + $app['dispatcher']->dispatch(PhraseaEvents::API_RESULT, new Event()); + + $timers = $app['api.timers']->toArray(); + + $this->assertCount(1, $timers); + + $timer = array_pop($timers); + + $this->assertArrayHasKey('name', $timer); + $this->assertArrayHasKey('memory', $timer); + $this->assertArrayHasKey('time', $timer); + + $this->assertEquals(PhraseaEvents::API_RESULT, $timer['name']); + $this->assertGreaterThan(0, $timer['time']); + $this->assertGreaterThan(400000, $timer['memory']); + } +} diff --git a/tests/classes/api/v1/api_v1_adapterTest.php b/tests/classes/api/v1/api_v1_adapterTest.php index 6219f59d29..5b58359bb8 100644 --- a/tests/classes/api/v1/api_v1_adapterTest.php +++ b/tests/classes/api/v1/api_v1_adapterTest.php @@ -13,6 +13,7 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract public function setUp() { parent::setUp(); + self::$DI['app']->register(new \API_V1_Timer()); $this->object = new API_V1_adapter(self::$DI['app']); } diff --git a/tests/classes/api/v1/api_v1_resultTest.php b/tests/classes/api/v1/api_v1_resultTest.php index 4ce9372790..a98945eeb8 100644 --- a/tests/classes/api/v1/api_v1_resultTest.php +++ b/tests/classes/api/v1/api_v1_resultTest.php @@ -11,14 +11,31 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract */ protected $api; + protected $app; + public function setUp() { parent::setUp(); + $this->app = require __DIR__ . '/../../../lib/Alchemy/Phrasea/Application/Api.php'; + + $conf = $this->app['Core']->getConfiguration(); + $confs = $conf->getConfigurations(); + $confs[$conf->getEnvironnement()]['phraseanet']['api-timers'] = true; + + $this->app['Core']->getConfiguration()->setConfigurations($confs); + $this->api = $this->getMock("API_V1_adapter", array("get_version"), array(), "", false); $this->api->expects($this->any())->method("get_version")->will($this->returnValue("my_super_version1.0")); } - public function testFormat() + protected function assertIsTimer($timer) + { + $this->assertObjectHasAttribute('name', $timer); + $this->assertObjectHasAttribute('memory', $timer); + $this->assertObjectHasAttribute('time', $timer); + } + + public function testFormatJson() { $server = array( "HTTP_ACCEPT" => "application/json" @@ -29,7 +46,7 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract ); $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $return = $api_result->format(); $this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $return); $response = json_decode($return); @@ -43,6 +60,13 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract $this->checkResponseFieldMeta($response, "api_version", "my_super_version1.0", PHPUnit_Framework_Constraint_IsType::TYPE_STRING); $this->checkResponseFieldMeta($response, "request", "GET my/base/path/my/request/uri", PHPUnit_Framework_Constraint_IsType::TYPE_STRING); + $this->assertObjectHasAttribute("timers", $response); + $this->assertCount(2, $response->timers); + + foreach ($response->timers as $timer) { + $this->assertIsTimer($timer); + } + $date = new \DateTime(); $now = $date->format('U'); @@ -66,6 +90,16 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract $this->assertObjectHasAttribute("error_details", $response->meta); $this->assertNull($response->meta->error_details); + $this->assertObjectHasAttribute("timers", $response); + $this->assertCount(2, $response->timers); + + foreach ($response->timers as $timer) { + $this->assertIsTimer($timer); + } + } + + public function testFormatYaml() + { $server = array( "HTTP_ACCEPT" => "application/yaml" , 'REQUEST_METHOD' => 'GET' @@ -75,7 +109,7 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract ); $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $return = $api_result->format(); $sfYaml = new Symfony\Component\Yaml\Parser(); $response = $sfYaml->parse($return); @@ -111,7 +145,12 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract $this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $response["meta"]["charset"]); $this->assertEquals("UTF-8", $response["meta"]["charset"]); + $this->assertArrayHasKey("timers", $response); + $this->assertCount(2, $response['timers']); + } + public function testFormatJsonP() + { $server = array( "HTTP_ACCEPT" => "application/yaml" , 'REQUEST_METHOD' => 'GET' @@ -121,14 +160,22 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract ); $request = new Request(array("callback" => "my_callback_function"), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $return = $api_result->format(); $this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $return); $this->assertRegexp("/my_callback_function\(\{.+\}\)/", $return); + $response = json_decode(substr($return, 21, strlen($return) - 22)); + + $this->assertObjectHasAttribute("timers", $response); + $this->assertCount(2, $response->timers); + + foreach ($response->timers as $timer) { + $this->assertIsTimer($timer); + } } /** - * @depends testFormat + * @depends testFormatJson */ public function testSet_datas() { @@ -141,7 +188,7 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract ); $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $api_result->set_datas(array("pirouette" => "cacahuete", "black" => true, "bob" => array("bob"))); $response = json_decode($api_result->format()); $this->checkResponseFieldResponse($response, "pirouette", "cacahuete", PHPUnit_Framework_Constraint_IsType::TYPE_STRING); @@ -161,7 +208,7 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); $data = array("pirouette" => "cacahuete", "black" => true, "bob" => array("bob")); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $api_result->set_datas($data); $this->assertEquals($data, $api_result->get_datas()); @@ -179,7 +226,7 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); $data = array(); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $api_result->set_datas($data); $this->assertEquals($data, $api_result->get_datas()); @@ -203,60 +250,60 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract { $server = array("HTTP_ACCEPT" => "application/json"); $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $this->assertEquals("application/json", $api_result->get_content_type()); $server = array("HTTP_ACCEPT" => "application/yaml"); $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $this->assertEquals('application/yaml', $api_result->get_content_type()); $server = array("HTTP_ACCEPT" => "text/yaml"); $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $this->assertEquals('application/yaml', $api_result->get_content_type()); $server = array("HTTP_ACCEPT" => ""); $request = new Request(array("callback" => "hello"), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $this->assertEquals('text/javascript', $api_result->get_content_type()); $server = array("HTTP_ACCEPT" => "unknow"); $request = new Request(array("callback" => ""), array(), array(), array(), array(), $server); - $api_result = new API_V1_result($request, $this->api); + $api_result = new API_V1_result($this->app, $request, $this->api); $this->assertEquals("application/json", $api_result->get_content_type()); } /** - * @depends testFormat + * @depends testFormatJson */ public function testSet_error_message() { - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, 'detaillage'); $this->assertErrorMessage($api_result, 400, API_V1_result::ERROR_BAD_REQUEST, API_V1_exception_badrequest::get_details(), 'detaillage'); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_message(API_V1_result::ERROR_UNAUTHORIZED, 'detaillage'); $this->assertErrorMessage($api_result, 401, API_V1_result::ERROR_UNAUTHORIZED, API_V1_exception_unauthorized::get_details(), 'detaillage'); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_message(API_V1_result::ERROR_FORBIDDEN, 'detaillage'); $this->assertErrorMessage($api_result, 403, API_V1_result::ERROR_FORBIDDEN, API_V1_exception_forbidden::get_details(), 'detaillage'); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_message(API_V1_result::ERROR_NOTFOUND, 'detaillage'); $this->assertErrorMessage($api_result, 404, API_V1_result::ERROR_NOTFOUND, API_V1_exception_notfound::get_details(), 'detaillage'); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_message(API_V1_result::ERROR_METHODNOTALLOWED, 'detaillage'); $this->assertErrorMessage($api_result, 405, API_V1_result::ERROR_METHODNOTALLOWED, API_V1_exception_methodnotallowed::get_details(), 'detaillage'); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_message(API_V1_result::ERROR_INTERNALSERVERERROR, 'detaillage'); $this->assertErrorMessage($api_result, 500, API_V1_result::ERROR_INTERNALSERVERERROR, API_V1_exception_internalservererror::get_details(), 'detaillage'); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_message(OAUTH2_ERROR_INVALID_REQUEST, 'detaillage'); $this->assertErrorMessage($api_result, 200, OAUTH2_ERROR_INVALID_REQUEST, NULL, 'detaillage'); } @@ -289,73 +336,73 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract } /** - * @depends testFormat + * @depends testFormatJson */ public function testSet_error_code() { - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(400); $this->assertErrorMessage($api_result, 400, API_V1_result::ERROR_BAD_REQUEST, API_V1_exception_badrequest::get_details(), null); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(401); $this->assertErrorMessage($api_result, 401, API_V1_result::ERROR_UNAUTHORIZED, API_V1_exception_unauthorized::get_details(), null); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(403); $this->assertErrorMessage($api_result, 403, API_V1_result::ERROR_FORBIDDEN, API_V1_exception_forbidden::get_details(), null); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(404); $this->assertErrorMessage($api_result, 404, API_V1_result::ERROR_NOTFOUND, API_V1_exception_notfound::get_details(), null); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(405); $this->assertErrorMessage($api_result, 405, API_V1_result::ERROR_METHODNOTALLOWED, API_V1_exception_methodnotallowed::get_details(), null); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(500); $this->assertErrorMessage($api_result, 500, API_V1_result::ERROR_INTERNALSERVERERROR, API_V1_exception_internalservererror::get_details(), null); } public function testGet_http_code() { - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(400); $this->assertEquals(400, $api_result->get_http_code()); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(401); $this->assertEquals(401, $api_result->get_http_code()); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(403); $this->assertEquals(403, $api_result->get_http_code()); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(404); $this->assertEquals(404, $api_result->get_http_code()); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(405); $this->assertEquals(405, $api_result->get_http_code()); - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_error_code(500); $this->assertEquals(500, $api_result->get_http_code()); - $api_result = new API_V1_result(new Request(array("callback" => "my_callback")), $this->api); + $api_result = new API_V1_result($this->app, new Request(array("callback" => "my_callback")), $this->api); $api_result->set_error_code(400); $this->assertEquals(200, $api_result->get_http_code()); - $api_result = new API_V1_result(new Request(array("callback" => "my_callback")), $this->api); + $api_result = new API_V1_result($this->app, new Request(array("callback" => "my_callback")), $this->api); $api_result->set_error_code(500); $this->assertEquals(500, $api_result->get_http_code()); } public function testSet_http_code() { - $api_result = new API_V1_result(new Request(), $this->api); + $api_result = new API_V1_result($this->app, new Request(), $this->api); $api_result->set_http_code(500); $this->assertEquals(500, $api_result->get_http_code()); @@ -366,7 +413,7 @@ class API_V1_resultTest extends PhraseanetPHPUnitAuthenticatedAbstract $api_result->set_http_code(403); $this->assertEquals(403, $api_result->get_http_code()); - $api_result = new API_V1_result(new Request(array("callback" => "my_callback")), $this->api); + $api_result = new API_V1_result($this->app, new Request(array("callback" => "my_callback")), $this->api); $api_result->set_http_code(500); $this->assertEquals(500, $api_result->get_http_code()); diff --git a/tests/classes/media/Permalink/media_Permalink_AdapterTest.php b/tests/classes/media/Permalink/media_Permalink_AdapterTest.php index 7bea316ea8..9acd4e1adf 100644 --- a/tests/classes/media/Permalink/media_Permalink_AdapterTest.php +++ b/tests/classes/media/Permalink/media_Permalink_AdapterTest.php @@ -7,13 +7,19 @@ class media_Permalink_AdapterTest extends PhraseanetPHPUnitAbstract */ static $object; - public static function setUpBeforeClass() + public function setUp() { - parent::setUpBeforeClass(); + parent::setUp(); $databox = self::$DI['record_1']->get_databox(); static::$object = media_Permalink_Adapter::getPermalink(self::$DI['app'], $databox, self::$DI['record_1']->get_subdef('document')); } + public function testGet_label() + { + $this->assertInternalType('string', static::$object->get_label()); + $this->assertEquals('test001', static::$object->get_label()); + } + public function testGetPermalink() { $this->assertTrue((static::$object instanceof media_Permalink_Adapter)); @@ -34,105 +40,65 @@ class media_Permalink_AdapterTest extends PhraseanetPHPUnitAbstract static::$object->set_label('coucou les chicos'); $this->assertEquals('coucou-les-chicos', static::$object->get_label()); static::$object->set_label(''); - $this->assertEquals('', static::$object->get_label()); + $this->assertEquals('untitled', static::$object->get_label()); static::$object->set_label('JE ANp ra&é"\/,;:!§/.?%µ*ù$]@^\[{#~234567890°+\'(-è_çà'); $this->assertEquals('JE-ANp-raeu234567890-e_ca', static::$object->get_label()); } public function testGet_url() { - $url = self::$DI['app']['phraseanet.registry']->get('GV_ServerName') . 'permalink/v1/' . static::$object->get_label() . '/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/' . - static::$object->get_token() . '/document/'; + $url = self::$DI['app']['phraseanet.registry']->get('GV_ServerName') . 'permalink/v1/' + . self::$DI['record_1']->get_sbas_id() . '/' + . self::$DI['record_1']->get_record_id() + . '/document/' . static::$object->get_label() + . '.' . pathinfo(self::$DI['record_1']->get_original_name(), PATHINFO_EXTENSION) + . '?token=' . static::$object->get_token(); $this->assertEquals($url, static::$object->get_url(self::$DI['app']['phraseanet.registry'])); } - /** - * @todo Implement testGet_page(). - */ public function testGet_page() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $url = self::$DI['app']['phraseanet.registry']->get('GV_ServerName') . 'permalink/v1/' + . self::$DI['record_1']->get_sbas_id() . '/' + . self::$DI['record_1']->get_record_id() + . '/document/' + . '?token=' . static::$object->get_token(); + + $this->assertEquals($url, static::$object->get_page()); } - /** - * @todo Implement testGet_id(). - */ public function testGet_id() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertInternalType('integer', static::$object->get_id()); } - /** - * @todo Implement testGet_token(). - */ public function testGet_token() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertInternalType('string', static::$object->get_token()); } - /** - * @todo Implement testGet_is_activated(). - */ public function testGet_is_activated() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - $this->assertTrue(is_bool(static::$object->get_is_activated)); + $this->assertInternalType('boolean', static::$object->get_is_activated()); } - /** - * @todo Implement testGet_created_on(). - */ public function testGet_created_on() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertInstanceOf('DateTime', static::$object->get_created_on()); } - /** - * @todo Implement testGet_last_modified(). - */ public function testGet_last_modified() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertInstanceOf('DateTime', static::$object->get_last_modified()); } /** - * @todo Implement testGet_label(). + * @expectedException Alchemy\Phrasea\Exception\RuntimeException */ - public function testGet_label() + public function testCreateAPermalinkAlreadyCreated() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @todo Implement testCreate(). - */ - public function testCreate() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $databox = self::$DI['record_1']->get_databox(); + media_Permalink_Adapter::create(self::$DI['app'], $databox, self::$DI['record_1']->get_subdef('document')); } }