Fix tests

This commit is contained in:
Nicolas Le Goff
2014-07-03 15:07:35 +02:00
parent 7818a5e8e3
commit 24b885eb7c
15 changed files with 198 additions and 27 deletions

View File

@@ -68,4 +68,5 @@ script:
#sqlite db generation should occur once Phraseanet is up to date :
- bin/developer phraseanet:regenerate-sqlite
- ./node_modules/.bin/grunt test
#ls -d tests/Alchemy/Tests/Phrasea/*/ | parallel --gnu 'bin/phpunit {};'
- bin/phpunit

View File

@@ -78,21 +78,21 @@ class V1 implements ControllerProviderInterface
->before([$this, 'ensureAdmin']);
$controllers->get('/monitor/task/{task}/', 'controller.api.v1:get_task')
->convert('task', [$app['converter.task'], 'convert'])
->convert('task', $app['converter.task-callback'])
->before([$this, 'ensureAdmin'])
->assert('task', '\d+');
$controllers->post('/monitor/task/{task}/', 'controller.api.v1:set_task_property')
->convert('task', [$app['converter.task'], 'convert'])
->convert('task', $app['converter.task-callback'])
->before([$this, 'ensureAdmin'])
->assert('task', '\d+');
$controllers->post('/monitor/task/{task}/start/', 'controller.api.v1:start_task')
->convert('task', [$app['converter.task'], 'convert'])
->convert('task', $app['converter.task-callback'])
->before([$this, 'ensureAdmin']);
$controllers->post('/monitor/task/{task}/stop/', 'controller.api.v1:stop_task')
->convert('task', [$app['converter.task'], 'convert'])
->convert('task', $app['converter.task-callback'])
->before([$this, 'ensureAdmin']);
$controllers->get('/monitor/phraseanet/', 'controller.api.v1:get_phraseanet_monitor')

View File

@@ -15,6 +15,7 @@ use Alchemy\Phrasea\Model\Converter\ApiApplicationConverter;
use Alchemy\Phrasea\Model\Converter\BasketConverter;
use Alchemy\Phrasea\Model\Converter\TaskConverter;
use Alchemy\Phrasea\Model\Converter\TokenConverter;
use Alchemy\Phrasea\Model\Entities\Task;
use Silex\Application;
use Silex\ServiceProviderInterface;
@@ -23,11 +24,15 @@ class ConvertersServiceProvider implements ServiceProviderInterface
public function register(Application $app)
{
$app['converter.task'] = $app->share(function ($app) {
return new TaskConverter($app['EM']);
return new TaskConverter($app['repo.tasks']);
});
$app['converter.task-callback'] = $app->protect(function($id) use ($app) {
return $app['converter.task']->convert($id);
});
$app['converter.basket'] = $app->share(function ($app) {
return new BasketConverter($app['EM']);
return new BasketConverter($app['repo.baskets']);
});
$app['converter.token'] = $app->share(function ($app) {

View File

@@ -12,16 +12,17 @@
namespace Alchemy\Phrasea\Model\Converter;
use Alchemy\Phrasea\Model\Entities\Basket;
use Alchemy\Phrasea\Model\Repositories\BasketRepository;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BasketConverter implements ConverterInterface
{
private $om;
private $repository;
public function __construct(ObjectManager $om)
public function __construct(BasketRepository $repository)
{
$this->om = $om;
$this->repository = $repository;
}
/**
@@ -31,7 +32,7 @@ class BasketConverter implements ConverterInterface
*/
public function convert($id)
{
if (null === $basket = $this->om->find('Phraseanet:Basket', (int) $id)) {
if (null === $basket = $this->repository->find((int) $id)) {
throw new NotFoundHttpException(sprintf('Basket %s not found.', $id));
}

View File

@@ -12,16 +12,17 @@
namespace Alchemy\Phrasea\Model\Converter;
use Alchemy\Phrasea\Model\Entities\Task;
use Alchemy\Phrasea\Model\Repositories\TaskRepository;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class TaskConverter implements ConverterInterface
{
private $om;
private $repository;
public function __construct(ObjectManager $om)
public function __construct(TaskRepository $repository)
{
$this->om = $om;
$this->repository = $repository;
}
/**
@@ -31,7 +32,7 @@ class TaskConverter implements ConverterInterface
*/
public function convert($id)
{
if (null === $task = $this->om->find('Phraseanet:Task', (int) $id)) {
if (null === $task = $this->repository->find((int) $id)) {
throw new NotFoundHttpException(sprintf('Task %s not found.', $id));
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Alchemy\Phrasea\Webhook;
use Alchemy\Phrasea\Model\Entities\WebhookEvent;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Webhook\Processor\FeedEntryProcessor;
class EventProcessorFactory
{
private $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function get(WebhookEvent $event)
{
switch ($event->getType()) {
case WebhookEvent::FEED_ENTRY_TYPE:
return new FeedEntryProcessor($event, $this->app);
break;
default:
throw new \RuntimeException(sprintf('No processor found for %s', $event->getType()));
}
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Alchemy\Phrasea\Webhook\Processor;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Model\Entities\WebhookEvent;
abstract class AbstractProcessor
{
protected $event;
protected $app;
public function __construct(WebhookEvent $event, Application $app)
{
$this->event = $event;
$this->app = $app;
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Alchemy\Phrasea\Webhook\Processor;
class FeedEntryProcessor extends AbstractProcessor implements ProcessorInterface
{
public function process()
{
$data = $this->event->getData();
if (!isset($data->{"entry_id"})) {
return null;
}
$entry = $this->app['EM']->getRepository('Phraseanet::Entry')->find($data->{"entry_id"});
if (null === $entry) {
return null;
}
$data = $this->event->getData();
$feed = $entry->getFeed();
$query = new \User_Query($this->app);
$query->include_phantoms(true)
->include_invite(false)
->include_templates(false)
->email_not_null(true);
if ($feed->getCollection($this->app)) {
$query->on_base_ids(array($feed->getCollection($this->app)->get_base_id()));
}
$start = 0;
$perLoop = 100;
$users = array();
do {
$results = $query->limit($start, $perLoop)->execute()->get_results();
foreach ($results as $user) {
$users[] = array(
'email' => $user->getEmail(),
'firstname' => $user->getFirstname() ?: null,
'lastname' => $user->getLastname() ?: null,
);
}
$start += $perLoop;
} while (count($results) > 0);
return array(
'event' => $this->event->getName(),
'users_were_notified' => isset($data->{'notify_email'}) ?: !!$data->{"notify_email"},
'feed' => array(
'id' => $feed->getId(),
'title' => $feed->getTitle(),
'description' => $feed->getSubtitle(),
),
'entry' => array(
'id' => $entry->getId(),
'author' => array(
'name' => $entry->getAuthorName(),
'email' => $entry->getAuthorEmail()
),
'title' => $entry->getTitle(),
'description' => $entry->getSubtitle(),
),
'users' => $users
);
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Alchemy\Phrasea\Webhook\Processor;
interface ProcessorInterface
{
public function process();
}

View File

@@ -1147,8 +1147,13 @@ class ACL implements cache_cacheableInterface
$usr_id = $this->user->getId();
foreach ($sbas_ids as $sbas_id) {
if (!$this->has_access_to_sbas($sbas_id))
if (!$this->has_access_to_sbas($sbas_id)) {
try {
$stmt_ins->execute([':sbas_id' => $sbas_id, ':usr_id' => $usr_id]);
} catch (DBALException $e) {
}
}
}
$stmt_ins->closeCursor();
$this->delete_data_from_cache(self::CACHE_RIGHTS_SBAS);
@@ -1589,7 +1594,7 @@ class ACL implements cache_cacheableInterface
*/
public function get_order_master_collections()
{
$sql = 'SELECT base_id FROM basusr WHERE order_master="1" AND usr_id= :usr_id ';
$sql = 'SELECT base_id FROM basusr WHERE order_master="1" AND usr_id= :usr_id';
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute([':usr_id' => $this->user->getId()]);
$rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);

View File

@@ -31,14 +31,15 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
parent::tearDown();
}
public function getApplicationPath()
{
return '/lib/Alchemy/Phrasea/Application/Api.php';
}
public function setUp()
{
parent::setUp();
self::$DI['app'] = self::$DI->share(function ($DI) {
return $this->loadApp('lib/Alchemy/Phrasea/Application/Api.php');
});
if (null === $this->adminAccessToken) {
$tokens = self::$DI['app']['repo.api-oauth-tokens']->findOauthTokens(self::$DI['oauth2-app-acc-user']);
if (count($tokens) === 0) {

View File

@@ -61,6 +61,32 @@ class PropertyTest extends \PhraseanetAuthenticatedWebTestCase
$story = \record_adapter::createStory(self::$DI['app'], self::$DI['collection']);
$story->appendChild($record2);
$acl = $this->getMockBuilder('ACL')
->disableOriginalConstructor()
->getMock();
$acl->expects($this->any())
->method('has_access_to_record')
->with($this->isInstanceOf('\record_adapter'))
->will($this->returnValue(true));
$acl->expects($this->any())
->method('has_right_on_base')
->with($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT), $this->equalTo('chgstatus'))
->will($this->returnValue(true));
$acl->expects($this->any())
->method('has_right_on_sbas')
->with($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT), $this->equalTo('chgstatus'))
->will($this->returnValue(true));
$aclProvider = $this->getMockBuilder('Alchemy\Phrasea\Authentication\ACLProvider')
->disableOriginalConstructor()
->getMock();
$aclProvider->expects($this->any())
->method('get')
->will($this->returnValue($acl));
self::$DI['app']['acl'] = $aclProvider;
self::$DI['client']->request('POST', '/prod/records/property/status/', [
'apply_to_children' => [$story->get_sbas_id() => true],
'status' => [

View File

@@ -10,7 +10,7 @@ class BasketConverterTest extends \PhraseanetTestCase
{
$basket = self::$DI['app']['EM']->find('Phraseanet:Basket', 1);
$converter = new BasketConverter(self::$DI['app']['EM']);
$converter = new BasketConverter(self::$DI['app']['EM']->getRepository('Phraseanet:Basket'));
$this->assertSame($basket, $converter->convert($basket->getId()));
}
@@ -20,7 +20,7 @@ class BasketConverterTest extends \PhraseanetTestCase
*/
public function testConvertFailure()
{
$converter = new BasketConverter(self::$DI['app']['EM']);
$converter = new BasketConverter(self::$DI['app']['EM']->getRepository('Phraseanet:Basket'));
$converter->convert('prout');
}
}

View File

@@ -10,7 +10,7 @@ class TaskConverterTest extends \PhraseanetTestCase
{
$task = self::$DI['app']['EM']->find('Phraseanet:Task', 1);
$converter = new TaskConverter(self::$DI['app']['EM']);
$converter = new TaskConverter(self::$DI['app']['EM']->getRepository('Phraseanet:Task'));
$this->assertSame($task, $converter->convert(1));
}
@@ -20,7 +20,7 @@ class TaskConverterTest extends \PhraseanetTestCase
*/
public function testConvertFailure()
{
$converter = new TaskConverter(self::$DI['app']['EM']);
$converter = new TaskConverter(self::$DI['app']['EM']->getRepository('Phraseanet:Task'));
$converter->convert('prout');
}
}

View File

@@ -46,7 +46,7 @@ abstract class PhraseanetTestCase extends WebTestCase
private static $fixtureIds = [];
private function initializeSqliteDB($path = '/tmp/db.sqlite')
protected function initializeSqliteDB($path = '/tmp/db.sqlite')
{
$path = $path . getmypid();
@@ -56,6 +56,11 @@ abstract class PhraseanetTestCase extends WebTestCase
copy(__DIR__ . '/../db-ref.sqlite', $path);
}
public function getApplicationPath()
{
return '/lib/Alchemy/Phrasea/Application/Root.php';
}
public function createApplication()
{
@@ -87,7 +92,7 @@ abstract class PhraseanetTestCase extends WebTestCase
\PHPUnit_Framework_Error_Notice::$enabled = true;
self::$DI['app'] = self::$DI->share(function ($DI) {
return $this->loadApp('/lib/Alchemy/Phrasea/Application/Root.php');
return $this->loadApp($this->getApplicationPath());
});
self::$DI['cli'] = self::$DI->share(function ($DI) {