mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-24 02:13:15 +00:00
Add ApiLog Manipulator tests
This commit is contained in:
@@ -12,9 +12,12 @@ use Gedmo\Mapping\Annotation as Gedmo;
|
|||||||
class ApiLog
|
class ApiLog
|
||||||
{
|
{
|
||||||
const DATABOXES_RESOURCE = 'databoxes';
|
const DATABOXES_RESOURCE = 'databoxes';
|
||||||
const RECORDS_RESOURCE = 'record';
|
const RECORDS_RESOURCE = 'records';
|
||||||
const BASKETS_RESOURCE = 'baskets';
|
const BASKETS_RESOURCE = 'baskets';
|
||||||
const FEEDS_RESOURCE = 'feeds';
|
const FEEDS_RESOURCE = 'feeds';
|
||||||
|
const QUARANTINE_RESOURCE = 'quarantine';
|
||||||
|
const STORIES_RESOURCE = 'stories';
|
||||||
|
const MONITOR_RESOURCE = 'monitor';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="integer")
|
* @ORM\Column(type="integer")
|
||||||
|
|||||||
@@ -77,54 +77,139 @@ class ApiLogManipulator implements ManipulatorInterface
|
|||||||
*/
|
*/
|
||||||
private function setDetails(ApiLog $log, Request $request, Response $response)
|
private function setDetails(ApiLog $log, Request $request, Response $response)
|
||||||
{
|
{
|
||||||
$resource = $general = $aspect = $action = null;
|
|
||||||
$chunks = explode('/', trim($request->getPathInfo(), '/'));
|
$chunks = explode('/', trim($request->getPathInfo(), '/'));
|
||||||
|
|
||||||
if (false === $response->isOk() || sizeof($chunks) === 0) {
|
if (false === $response->isOk() || sizeof($chunks) === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$resource = $chunks[0];
|
|
||||||
|
|
||||||
if (count($chunks) == 2 && (int) $chunks[1] == 0) {
|
switch ($chunks[0]) {
|
||||||
$general = $chunks[1];
|
case ApiLog::DATABOXES_RESOURCE :
|
||||||
} else {
|
$this->hydrateDataboxes($log, $chunks);
|
||||||
switch ($resource) {
|
break;
|
||||||
case ApiLog::DATABOXES_RESOURCE :
|
case ApiLog::RECORDS_RESOURCE :
|
||||||
if ((int) $chunks[1] > 0 && count($chunks) == 3) {
|
$this->hydrateRecords($log, $chunks);
|
||||||
$aspect = $chunks[2];
|
break;
|
||||||
}
|
case ApiLog::BASKETS_RESOURCE :
|
||||||
break;
|
$this->hydrateBaskets($log, $chunks);
|
||||||
case ApiLog::RECORDS_RESOURCE :
|
break;
|
||||||
if ((int) $chunks[1] > 0 && count($chunks) == 4) {
|
case ApiLog::FEEDS_RESOURCE :
|
||||||
if (!isset($chunks[3])) {
|
$this->hydrateFeeds($log, $chunks);
|
||||||
$aspect = "record";
|
break;
|
||||||
} elseif (preg_match("/^set/", $chunks[3])) {
|
case ApiLog::QUARANTINE_RESOURCE :
|
||||||
$action = $chunks[3];
|
$this->hydrateQuarantine($log, $chunks);
|
||||||
} else {
|
break;
|
||||||
$aspect = $chunks[3];
|
case ApiLog::STORIES_RESOURCE :
|
||||||
}
|
$this->hydrateStories($log, $chunks);
|
||||||
}
|
break;
|
||||||
break;
|
case ApiLog::MONITOR_RESOURCE :
|
||||||
case ApiLog::BASKETS_RESOURCE :
|
$this->hydrateMonitor($log, $chunks);
|
||||||
if ((int) $chunks[1] > 0 && count($chunks) == 3) {
|
break;
|
||||||
if (preg_match("/^set/", $chunks[2]) || preg_match("/^delete/", $chunks[2])) {
|
}
|
||||||
$action = $chunks[2];
|
}
|
||||||
} else {
|
|
||||||
$aspect = $chunks[2];
|
private function hydrateDataboxes(ApiLog $log, $chunks)
|
||||||
}
|
{
|
||||||
}
|
$log->setResource($chunks[0]);
|
||||||
break;
|
$log->setGeneral($chunks[0]);
|
||||||
case ApiLog::FEEDS_RESOURCE :
|
if (count($chunks) === 2) {
|
||||||
if ((int) $chunks[1] > 0 && count($chunks) == 3) {
|
$log->setAction($chunks[1]);
|
||||||
$aspect = $chunks[2];
|
}
|
||||||
}
|
if ((int) $chunks[1] > 0 && count($chunks) === 3) {
|
||||||
break;
|
$log->setAspect($chunks[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function hydrateRecords(ApiLog $log, $chunks)
|
||||||
|
{
|
||||||
|
$log->setResource($chunks[0]);
|
||||||
|
$log->setGeneral($chunks[0]);
|
||||||
|
if (count($chunks) === 2) {
|
||||||
|
$log->setAction($chunks[1]);
|
||||||
|
}
|
||||||
|
if (count($chunks) === 3 && (int) $chunks[1] > 0 && (int) $chunks[2] > 0) {
|
||||||
|
$log->setAction('get');
|
||||||
|
}
|
||||||
|
if ((int) $chunks[1] > 0 && (int) $chunks[2] > 0 && count($chunks) == 4) {
|
||||||
|
if (preg_match("/^set/", $chunks[3])) {
|
||||||
|
$log->setAction($chunks[3]);
|
||||||
|
} else {
|
||||||
|
$log->setAspect($chunks[3]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$log->setResource($resource);
|
private function hydrateBaskets(ApiLog $log, $chunks)
|
||||||
$log->setGeneral($general);
|
{
|
||||||
$log->setAspect($aspect);
|
$log->setResource($chunks[0]);
|
||||||
$log->setAction($action);
|
$log->setGeneral($chunks[0]);
|
||||||
|
if (count($chunks) === 2) {
|
||||||
|
$log->setAction($chunks[1]);
|
||||||
|
}
|
||||||
|
if ((int) $chunks[1] > 0 && count($chunks) == 3) {
|
||||||
|
if (preg_match("/^set/", $chunks[2]) || preg_match("/^delete/", $chunks[2])) {
|
||||||
|
$log->setAction($chunks[2]);
|
||||||
|
} else {
|
||||||
|
$log->setAspect($chunks[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function hydrateFeeds(ApiLog $log, $chunks)
|
||||||
|
{
|
||||||
|
$log->setResource($chunks[0]);
|
||||||
|
$log->setGeneral($chunks[0]);
|
||||||
|
if (count($chunks) === 2) {
|
||||||
|
if (preg_match("/^content$/", $chunks[1])) {
|
||||||
|
$log->setAspect($chunks[1]);
|
||||||
|
} else {
|
||||||
|
$log->setAction($chunks[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count($chunks) === 3) {
|
||||||
|
if ((int) $chunks[1] > 0) {
|
||||||
|
$log->setAspect($chunks[2]);
|
||||||
|
}
|
||||||
|
if (preg_match("/^entry$/", $chunks[1]) && (int) $chunks[2] > 0) {
|
||||||
|
$log->setAspect($chunks[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function hydrateQuarantine(ApiLog $log, $chunks)
|
||||||
|
{
|
||||||
|
$log->setResource($chunks[0]);
|
||||||
|
$log->setGeneral($chunks[0]);
|
||||||
|
if (count($chunks) === 2) {
|
||||||
|
$log->setAction($chunks[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function hydrateStories(ApiLog $log, $chunks)
|
||||||
|
{
|
||||||
|
$log->setGeneral($chunks[0]);
|
||||||
|
$log->setResource($chunks[0]);
|
||||||
|
if ((int) $chunks[1] > 0 && (int) $chunks[2] > 0 && count($chunks) == 4) {
|
||||||
|
$log->setAspect($chunks[3]);
|
||||||
|
}
|
||||||
|
if (count($chunks) === 3 && (int) $chunks[1] > 0 && (int) $chunks[2] > 0) {
|
||||||
|
$log->setAction('get');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function hydrateMonitor(ApiLog $log, $chunks)
|
||||||
|
{
|
||||||
|
$log->setGeneral($chunks[0]);
|
||||||
|
if (count($chunks) === 2) {
|
||||||
|
$log->setAspect($chunks[1]);
|
||||||
|
}
|
||||||
|
if (count($chunks) === 3 && (int) $chunks[2] > 0) {
|
||||||
|
$log->setAspect($chunks[1]);
|
||||||
|
$log->setAction('get');
|
||||||
|
}
|
||||||
|
if (count($chunks) === 4) {
|
||||||
|
$log->setAspect($chunks[1]);
|
||||||
|
$log->setAction($chunks[3]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Alchemy\Tests\Phrasea\Model\Manipulator;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\Model\Manipulator\ApiLogManipulator;
|
||||||
|
use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class ApiLogManipulatorTest extends \PhraseanetTestCase
|
||||||
|
{
|
||||||
|
public function testCreate()
|
||||||
|
{
|
||||||
|
$manipulator = new ApiAccountManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-accounts']);
|
||||||
|
$account = $manipulator->create(self::$DI['oauth2-app-user'], self::$DI['user']);
|
||||||
|
$nbLogs = count(self::$DI['app']['repo.api-logs']->findAll());
|
||||||
|
$manipulator = new ApiLogManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-logs']);
|
||||||
|
$manipulator->create($account, Request::create('/databoxes/list/', 'POST'), new Response());
|
||||||
|
$this->assertGreaterThan($nbLogs, count(self::$DI['app']['repo.api-accounts']->findAll()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider apiRouteProvider
|
||||||
|
*/
|
||||||
|
public function testsLogHydration($path, $expected)
|
||||||
|
{
|
||||||
|
$emMock = $this->getMock('\Doctrine\ORM\EntityManager', array('persist', 'flush'), array(), '', false);
|
||||||
|
$account = $this->getMock('\Alchemy\Phrasea\Model\Entities\ApiAccount');
|
||||||
|
$manipulator = new ApiLogManipulator($emMock, self::$DI['app']['repo.api-logs']);
|
||||||
|
$log = $manipulator->create($account, Request::create($path, 'POST'), new Response());
|
||||||
|
$this->assertEquals($expected['resource'], $log->getResource());
|
||||||
|
$this->assertEquals($expected['general'], $log->getGeneral());
|
||||||
|
$this->assertEquals($expected['aspect'], $log->getAspect());
|
||||||
|
$this->assertEquals($expected['action'], $log->getAction());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDelete()
|
||||||
|
{
|
||||||
|
$manipulator = new ApiAccountManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-accounts']);
|
||||||
|
$account = $manipulator->create(self::$DI['oauth2-app-user'], self::$DI['user']);
|
||||||
|
$manipulator = new ApiLogManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-logs']);
|
||||||
|
$log = $manipulator->create($account, Request::create('/databoxes/list/', 'POST'), new Response());
|
||||||
|
$countBefore = count(self::$DI['app']['repo.api-logs']->findAll());
|
||||||
|
$manipulator->delete($log);
|
||||||
|
$this->assertGreaterThan(count(self::$DI['app']['repo.api-logs']->findAll()), $countBefore);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUpdate()
|
||||||
|
{
|
||||||
|
$manipulator = new ApiAccountManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-accounts']);
|
||||||
|
$account = $manipulator->create(self::$DI['oauth2-app-user'], self::$DI['user']);
|
||||||
|
$manipulator = new ApiLogManipulator(self::$DI['app']['EM'], self::$DI['app']['repo.api-logs']);
|
||||||
|
$log = $manipulator->create($account, Request::create('/databoxes/list/', 'POST'), new Response());
|
||||||
|
$log->setAspect('a-new-aspect');
|
||||||
|
$manipulator->update($log);
|
||||||
|
$log = self::$DI['app']['repo.api-logs']->find($log->getId());
|
||||||
|
$this->assertEquals('a-new-aspect', $log->getAspect());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function apiRouteProvider()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['/databoxes/list/', ['resource' => 'databoxes', 'aspect' => null, 'action' => 'list', 'general' => 'databoxes']],
|
||||||
|
['/databoxes/1/collections/', ['resource' => 'databoxes', 'aspect' => 'collections', 'action' => null, 'general' => 'databoxes']],
|
||||||
|
['/databoxes/1/status/', ['resource' => 'databoxes', 'aspect' => 'status', 'action' => null, 'general' => 'databoxes']],
|
||||||
|
['/databoxes/1/metadatas/', ['resource' => 'databoxes', 'aspect' => 'metadatas', 'action' => null, 'general' => 'databoxes']],
|
||||||
|
['/databoxes/1/termsOfUse/', ['resource' => 'databoxes', 'aspect' => 'termsOfUse', 'action' => null, 'general' => 'databoxes']],
|
||||||
|
['/quarantine/list/', ['resource' => 'quarantine', 'aspect' => null, 'action' => 'list', 'general' => 'quarantine']],
|
||||||
|
['/records/add/', ['resource' => 'records', 'aspect' => null, 'action' => 'add', 'general' => 'records']],
|
||||||
|
['/records/search/', ['resource' => 'records', 'aspect' => null, 'action' => 'search', 'general' => 'records']],
|
||||||
|
['/records/1/1/caption/', ['resource' => 'records', 'aspect' => 'caption', 'action' => null, 'general' => 'records']],
|
||||||
|
['/records/1/1/metadatas/', ['resource' => 'records', 'aspect' => 'metadatas', 'action' => null, 'general' => 'records']],
|
||||||
|
['/records/1/1/status/', ['resource' => 'records', 'aspect' => 'status', 'action' => null, 'general' => 'records']],
|
||||||
|
['/records/1/1/embed/', ['resource' => 'records', 'aspect' => 'embed', 'action' => null, 'general' => 'records']],
|
||||||
|
['/records/1/1/related/', ['resource' => 'records', 'aspect' => 'related', 'action' => null, 'general' => 'records']],
|
||||||
|
['/records/1/1/', ['resource' => 'records', 'aspect' => null, 'action' => 'get', 'general' => 'records']],
|
||||||
|
['/records/1/1/setstatus/', ['resource' => 'records', 'aspect' => null, 'action' => 'setstatus', 'general' => 'records']],
|
||||||
|
['/records/1/1/setmetadatas/', ['resource' => 'records', 'aspect' => null, 'action' => 'setmetadatas', 'general' => 'records']],
|
||||||
|
['/records/1/1/setcollection/', ['resource' => 'records', 'aspect' => null, 'action' => 'setcollection', 'general' => 'records']],
|
||||||
|
['/stories/1/1/embed/', ['resource' => 'stories', 'aspect' => 'embed', 'action' => null, 'general' => 'stories']],
|
||||||
|
['/stories/1/1/', ['resource' => 'stories', 'aspect' => null, 'action' => 'get', 'general' => 'stories']],
|
||||||
|
['/baskets/add/', ['resource' => 'baskets', 'aspect' => null, 'action' => 'add', 'general' => 'baskets']],
|
||||||
|
['/baskets/1/content/', ['resource' => 'baskets', 'aspect' => 'content', 'action' => '', 'general' => 'baskets']],
|
||||||
|
['/baskets/1/delete/', ['resource' => 'baskets', 'aspect' => null, 'action' => 'delete', 'general' => 'baskets']],
|
||||||
|
['/baskets/1/setdescription/', ['resource' => 'baskets', 'aspect' => null, 'action' => 'setdescription', 'general' => 'baskets']],
|
||||||
|
['/baskets/1/setname/', ['resource' => 'baskets', 'aspect' => null, 'action' => 'setname', 'general' => 'baskets']],
|
||||||
|
['/feeds/list/', ['resource' => 'feeds', 'aspect' => null, 'action' => 'list', 'general' => 'feeds']],
|
||||||
|
['/feeds/1/content/', ['resource' => 'feeds', 'aspect' => 'content', 'action' => null, 'general' => 'feeds']],
|
||||||
|
['/feeds/content/', ['resource' => 'feeds', 'aspect' => 'content', 'action' => null, 'general' => 'feeds']],
|
||||||
|
['/feeds/entry/1/', ['resource' => 'feeds', 'aspect' => 'entry', 'action' => null, 'general' => 'feeds']],
|
||||||
|
['/monitor/phraseanet/', ['resource' => null, 'aspect' => 'phraseanet', 'action' => null, 'general' => 'monitor']],
|
||||||
|
['/monitor/scheduler/', ['resource' => null, 'aspect' => 'scheduler', 'action' => null, 'general' => 'monitor']],
|
||||||
|
['/monitor/task/1/', ['resource' => null, 'aspect' => 'task', 'action' => 'get', 'general' => 'monitor']],
|
||||||
|
['/monitor/task/1/stop/', ['resource' => null, 'aspect' => 'task', 'action' => 'stop', 'general' => 'monitor']],
|
||||||
|
['/monitor/task/1/start/', ['resource' => null, 'aspect' => 'task', 'action' => 'start', 'general' => 'monitor']],
|
||||||
|
['/monitor/tasks/', ['resource' => null, 'aspect' => 'tasks', 'action' => null, 'general' => 'monitor']],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user