diff --git a/lib/Alchemy/Phrasea/Command/Developer/RegenerateSqliteDb.php b/lib/Alchemy/Phrasea/Command/Developer/RegenerateSqliteDb.php index 245bae6340..9ae416b979 100644 --- a/lib/Alchemy/Phrasea/Command/Developer/RegenerateSqliteDb.php +++ b/lib/Alchemy/Phrasea/Command/Developer/RegenerateSqliteDb.php @@ -80,6 +80,7 @@ class RegenerateSqliteDb extends Command $DI = new \Pimple(); $this->generateUsers($this->container['EM'], $DI); + $this->insertOauthApps($DI); $this->generateCollection($DI); $this->generateRecord($DI); $this->insertOneStoryInWz($this->container['EM'], $DI); @@ -96,6 +97,9 @@ class RegenerateSqliteDb extends Command $fixtures['user']['test_phpunit_alt1'] = $DI['user_alt1']->get_id(); $fixtures['user']['test_phpunit_alt2'] = $DI['user_alt2']->get_id(); + $fixtures['oauth']['user'] = $DI['app-user']->get_id(); + $fixtures['oauth']['user_notAdmin'] = $DI['app-user_notAdmin']->get_id(); + $fixtures['databox']['records'] = $DI['databox']->get_sbas_id(); $fixtures['collection']['coll'] = $DI['coll']->get_base_id(); $fixtures['collection']['coll_no_access'] = $DI['coll_no_access']->get_base_id(); @@ -103,6 +107,7 @@ class RegenerateSqliteDb extends Command $fixtures['record']['record_story_1'] = $DI['record_story_1']->get_record_id(); $fixtures['record']['record_story_2'] = $DI['record_story_2']->get_record_id(); + $fixtures['record']['record_story_3'] = $DI['record_story_3']->get_record_id(); $fixtures['record']['record_1'] = $DI['record_1']->get_record_id(); $fixtures['record']['record_2'] = $DI['record_2']->get_record_id(); @@ -143,6 +148,19 @@ class RegenerateSqliteDb extends Command return 0; } + private function insertOauthApps(\Pimple $DI) + { + $DI['app-user'] = \API_OAuth2_Application::create($this->container, $DI['user'], 'test application for user'); + $DI['app-user']->set_redirect_uri('http://callback.com/callback/'); + $DI['app-user']->set_website('http://website.com/'); + $DI['app-user']->set_type(\API_OAuth2_Application::WEB_TYPE); + + $DI['app-user_notAdmin'] = \API_OAuth2_Application::create($this->container, $DI['user_notAdmin'], 'test application for user not admin'); + $DI['app-user_notAdmin']->set_redirect_uri('http://callback.com/callback/'); + $DI['app-user_notAdmin']->set_website('http://website.com/'); + $DI['app-user_notAdmin']->set_type(\API_OAuth2_Application::WEB_TYPE); + } + private function insertAuthFailures(EntityManager $em, \Pimple $DI) { $ip = '192.168.16.178'; @@ -276,10 +294,12 @@ class RegenerateSqliteDb extends Command $media = $this->container['mediavorus']->guess($this->container['root.path'] . '/tests/files/cestlafete.jpg'); - foreach (range(1, 2) as $i) { + foreach (range(1, 3) as $i) { $story = \record_adapter::createStory($this->container, $DI['coll']); - $story->substitute_subdef('preview', $media, $this->container); - $story->substitute_subdef('thumbnail', $media, $this->container); + if ($i < 3) { + $story->substitute_subdef('preview', $media, $this->container); + $story->substitute_subdef('thumbnail', $media, $this->container); + } $DI['record_story_' . $i] = $story; } } diff --git a/lib/classes/API/OAuth2/Token.php b/lib/classes/API/OAuth2/Token.php index 56a8e54028..a3a932775c 100644 --- a/lib/classes/API/OAuth2/Token.php +++ b/lib/classes/API/OAuth2/Token.php @@ -142,7 +142,7 @@ class API_OAuth2_Token $stmt->execute($params); $stmt->closeCursor(); - $this->session_id = (int) $session_id; + $this->session_id = $session_id !== null ? (int) $session_id : $session_id; return $this; } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9f37b79670..2706ce9449 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,5 +28,11 @@ vendor + + diff --git a/tests/Alchemy/Tests/Phrasea/ACL/BasketACLTest.php b/tests/Alchemy/Tests/Phrasea/ACL/BasketACLTest.php index b10183ceeb..e3d8d4ac74 100644 --- a/tests/Alchemy/Tests/Phrasea/ACL/BasketACLTest.php +++ b/tests/Alchemy/Tests/Phrasea/ACL/BasketACLTest.php @@ -9,53 +9,42 @@ class BasketACLTest extends \PhraseanetTestCase public function testOwnerIsOwner() { $basket = self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Basket', 4); - - $acl = new BasketACL(); - $this->assertTrue($acl->isOwner($basket, self::$DI['user'])); + $this->assertTrue((new BasketACL())->isOwner($basket, self::$DI['user'])); } public function testParticipantIsNotAnOwner() { $basket = self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Basket', 4); - - $acl = new BasketACL(); - $this->assertFalse($acl->isOwner($basket, self::$DI['user_alt1'])); + $this->assertFalse((new BasketACL())->isOwner($basket, self::$DI['user_alt1'])); } public function testUserIsNotTheOwner() { $basket = self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Basket', 1); - $acl = new BasketACL(); - $this->assertFalse($acl->isOwner($basket, self::$DI['user_alt1'])); + $this->assertFalse((new BasketACL())->isOwner($basket, self::$DI['user_alt1'])); } public function testOwnerHasAccessInValidationEnv() { $basket = self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Basket', 4); - - $acl = new BasketACL(); - $this->assertTrue($acl->hasAccess($basket, self::$DI['user'])); + $this->assertTrue((new BasketACL())->hasAccess($basket, self::$DI['user'])); } public function testOwnerHasAccess() { $basket = self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Basket', 1); - $acl = new BasketACL(); - $this->assertTrue($acl->hasAccess($basket, self::$DI['user'])); + $this->assertTrue((new BasketACL())->hasAccess($basket, self::$DI['user'])); } public function testParticipantHasAccess() { $basket = self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Basket', 4); - - $acl = new BasketACL(); - $this->assertTrue($acl->hasAccess($basket, self::$DI['user_alt1'])); + $this->assertTrue((new BasketACL())->hasAccess($basket, self::$DI['user_alt1'])); } public function testUserHasNotAccess() { $basket = self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Basket', 1); - $acl = new BasketACL(); - $this->assertFalse($acl->hasAccess($basket, self::$DI['user_alt1'])); + $this->assertFalse((new BasketACL())->hasAccess($basket, self::$DI['user_alt1'])); } } diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiJSONPTest.php b/tests/Alchemy/Tests/Phrasea/Application/ApiJSONPTest.php index 5fa685377f..2e04a74608 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiJSONPTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiJSONPTest.php @@ -30,14 +30,14 @@ class ApiJSONPApplication extends ApiTestCase $this->assertEquals(200, $response->getStatusCode(), 'Test status code 405 ' . $response->getContent()); } - public function getParameters(array $parameters = []) + protected function getParameters(array $parameters = []) { $parameters['callback'] = 'jsFunction'; return $parameters; } - public function unserialize($data) + protected function unserialize($data) { if (strpos($data, 'jsFunction(') !== 0) { $this->fail('Invalid JSONP response'); @@ -50,7 +50,7 @@ class ApiJSONPApplication extends ApiTestCase return json_decode(substr($data, 11, -1), true); } - public function getAcceptMimeType() + protected function getAcceptMimeType() { return 'application/json'; } diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiJsonTest.php b/tests/Alchemy/Tests/Phrasea/Application/ApiJsonTest.php index 9a37e37564..5d6058a38e 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiJsonTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiJsonTest.php @@ -4,18 +4,17 @@ namespace Alchemy\Tests\Phrasea\Application; class ApiJsonApplication extends ApiTestCase { - - public function getParameters(array $parameters = []) + protected function getParameters(array $parameters = []) { return $parameters; } - public function unserialize($data) + protected function unserialize($data) { return json_decode($data, true); } - public function getAcceptMimeType() + protected function getAcceptMimeType() { return 'application/json'; } diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiRootTest.php b/tests/Alchemy/Tests/Phrasea/Application/ApiRootTest.php index 4454a8449c..9f2ba08da1 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiRootTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiRootTest.php @@ -9,7 +9,7 @@ class ApiRootTest extends \PhraseanetWebTestCase { /** * - * @var Symfony\Component\HttpKernel\Client + * @var \Symfony\Component\HttpKernel\Client */ protected $client; @@ -17,15 +17,9 @@ class ApiRootTest extends \PhraseanetWebTestCase { parent::setUp(); - self::$DI['app'] = self::$DI->share(function () { - $environment = 'test'; - $app = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Api.php'; - - $app['debug'] = true; - - return $app; + self::$DI['app'] = self::$DI->share(function ($DI) { + return $this->loadApp('/lib/Alchemy/Phrasea/Application/Api.php'); }); - } public function testRoot() diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiTestCase.php b/tests/Alchemy/Tests/Phrasea/Application/ApiTestCase.php index b2d90abba9..8570fd57af 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiTestCase.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiTestCase.php @@ -7,6 +7,7 @@ use Alchemy\Phrasea\Border\File; use Alchemy\Phrasea\Core\PhraseaEvents; use Alchemy\Phrasea\Authentication\Context; use Alchemy\Phrasea\Model\Entities\Task; +use Guzzle\Common\Exception\GuzzleException; use Symfony\Component\HttpKernel\Client; use Symfony\Component\HttpFoundation\Response; @@ -37,12 +38,11 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase * @var \API_OAuth2_Application */ private static $adminApplication; - private static $databoxe_ids = []; private static $apiInitialized = false; - abstract public function getParameters(array $parameters = []); - abstract public function unserialize($data); - abstract public function getAcceptMimeType(); + abstract protected function getParameters(array $parameters = []); + abstract protected function unserialize($data); + abstract protected function getAcceptMimeType(); public function tearDown() { @@ -59,33 +59,19 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase }); if (!self::$apiInitialized) { - self::$oauthApplication = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user_notAdmin'], 'test API v1'); - self::$account = \API_OAuth2_Account::load_with_user(self::$DI['app'], self::$oauthApplication, self::$DI['user_notAdmin']); + self::$account = \API_OAuth2_Account::load_with_user(self::$DI['app'], self::$DI['oauth2-app-user_notAdmin'], self::$DI['user_notAdmin']); self::$token = self::$account->get_token()->get_value(); - self::$adminToken = null; - - self::$adminApplication = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test2 API v1'); - self::$adminAccount = \API_OAuth2_Account::load_with_user(self::$DI['app'], self::$adminApplication, self::$DI['user']); + self::$adminAccount = \API_OAuth2_Account::load_with_user(self::$DI['app'], self::$DI['oauth2-app-user'], self::$DI['user']); self::$adminToken = self::$adminAccount->get_token()->get_value(); + self::$apiInitialized = true; } } public static function tearDownAfterClass() { - //delete database entry - self::$account->delete(); - self::$oauthApplication->delete(); - - if (self::$adminToken) { - self::$adminAccount->delete(); - self::$adminApplication->delete(); - } - self::$apiInitialized = false; - self::$databoxe_ids = []; - self::$token = self::$account = self::$oauthApplication = self::$adminToken = self::$adminAccount = self::$adminApplication = null; @@ -168,7 +154,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase $this->assertArrayHasKey('de', $databox['labels']); $this->assertArrayHasKey('nl', $databox['labels']); $this->assertArrayHasKey('version', $databox); - self::$databoxe_ids[] = $databox['databox_id']; + break; } } @@ -596,34 +582,34 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase public function testDataboxCollectionRoute() { $this->setToken(self::$token); - foreach (self::$databoxe_ids as $databox_id) { - $route = '/api/v1/databoxes/' . $databox_id . '/collections/'; - $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); + $databox_id = self::$DI['record_1']->get_sbas_id(); + $route = '/api/v1/databoxes/' . $databox_id . '/collections/'; + $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); - $crawler = self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); - $this->evaluateResponse200(self::$DI['client']->getResponse()); - $this->evaluateMeta200($content); + self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $this->evaluateResponse200(self::$DI['client']->getResponse()); + $this->evaluateMeta200($content); - $this->assertArrayHasKey('collections', $content['response']); - foreach ($content['response']['collections'] as $collection) { - $this->assertTrue(is_array($collection), 'Une collection est un objet'); - $this->assertArrayHasKey('base_id', $collection); - $this->assertArrayHasKey('collection_id', $collection); - $this->assertArrayHasKey('name', $collection); - $this->assertArrayHasKey('labels', $collection); - $this->assertArrayHasKey('fr', $collection['labels']); - $this->assertArrayHasKey('en', $collection['labels']); - $this->assertArrayHasKey('de', $collection['labels']); - $this->assertArrayHasKey('nl', $collection['labels']); - $this->assertArrayHasKey('record_amount', $collection); - $this->assertTrue(is_int($collection['base_id'])); - $this->assertGreaterThan(0, $collection['base_id']); - $this->assertTrue(is_int($collection['collection_id'])); - $this->assertGreaterThan(0, $collection['collection_id']); - $this->assertTrue(is_string($collection['name'])); - $this->assertTrue(is_int($collection['record_amount'])); - } + $this->assertArrayHasKey('collections', $content['response']); + foreach ($content['response']['collections'] as $collection) { + $this->assertTrue(is_array($collection), 'Une collection est un objet'); + $this->assertArrayHasKey('base_id', $collection); + $this->assertArrayHasKey('collection_id', $collection); + $this->assertArrayHasKey('name', $collection); + $this->assertArrayHasKey('labels', $collection); + $this->assertArrayHasKey('fr', $collection['labels']); + $this->assertArrayHasKey('en', $collection['labels']); + $this->assertArrayHasKey('de', $collection['labels']); + $this->assertArrayHasKey('nl', $collection['labels']); + $this->assertArrayHasKey('record_amount', $collection); + $this->assertTrue(is_int($collection['base_id'])); + $this->assertGreaterThan(0, $collection['base_id']); + $this->assertTrue(is_int($collection['collection_id'])); + $this->assertGreaterThan(0, $collection['collection_id']); + $this->assertTrue(is_string($collection['name'])); + $this->assertTrue(is_int($collection['record_amount'])); + break; } $route = '/api/v1/databoxes/24892534/collections/'; $this->evaluateNotFoundRoute($route, ['GET']); @@ -640,43 +626,43 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase public function testDataboxStatusRoute() { $this->setToken(self::$token); - foreach (self::$databoxe_ids as $databox_id) { - $databox = self::$DI['app']['phraseanet.appbox']->get_databox($databox_id); - $ref_status = $databox->get_statusbits(); - $route = '/api/v1/databoxes/' . $databox_id . '/status/'; - $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); + $databox_id = self::$DI['record_1']->get_sbas_id(); + $databox = self::$DI['app']['phraseanet.appbox']->get_databox($databox_id); + $ref_status = $databox->get_statusbits(); + $route = '/api/v1/databoxes/' . $databox_id . '/status/'; + $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); - $crawler = self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); - $this->evaluateResponse200(self::$DI['client']->getResponse()); - $this->evaluateMeta200($content); + self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $this->evaluateResponse200(self::$DI['client']->getResponse()); + $this->evaluateMeta200($content); - $this->assertArrayHasKey('status', $content['response']); - foreach ($content['response']['status'] as $status) { - $this->assertTrue(is_array($status), 'Un bloc status est un objet'); - $this->assertArrayHasKey('bit', $status); - $this->assertTrue(is_int($status['bit'])); - $this->assertGreaterThan(3, $status['bit']); - $this->assertLessThan(65, $status['bit']); - $this->assertArrayHasKey('label_on', $status); - $this->assertArrayHasKey('label_off', $status); - $this->assertArrayHasKey('labels', $status); - $this->assertArrayHasKey('fr', $status['labels']); - $this->assertArrayHasKey('en', $status['labels']); - $this->assertArrayHasKey('de', $status['labels']); - $this->assertArrayHasKey('nl', $status['labels']); - $this->assertArrayHasKey('img_on', $status); - $this->assertArrayHasKey('img_off', $status); - $this->assertArrayHasKey('searchable', $status); - $this->assertArrayHasKey('printable', $status); - $this->assertTrue(is_bool($status['searchable'])); - $this->assertTrue($status['searchable'] === (bool) $ref_status[$status['bit']]['searchable']); - $this->assertTrue(is_bool($status['printable'])); - $this->assertTrue($status['printable'] === (bool) $ref_status[$status['bit']]['printable']); - $this->assertTrue($status['label_on'] === $ref_status[$status['bit']]['labelon']); - $this->assertTrue($status['img_off'] === $ref_status[$status['bit']]['img_off']); - $this->assertTrue($status['img_on'] === $ref_status[$status['bit']]['img_on']); - } + $this->assertArrayHasKey('status', $content['response']); + foreach ($content['response']['status'] as $status) { + $this->assertTrue(is_array($status), 'Un bloc status est un objet'); + $this->assertArrayHasKey('bit', $status); + $this->assertTrue(is_int($status['bit'])); + $this->assertGreaterThan(3, $status['bit']); + $this->assertLessThan(65, $status['bit']); + $this->assertArrayHasKey('label_on', $status); + $this->assertArrayHasKey('label_off', $status); + $this->assertArrayHasKey('labels', $status); + $this->assertArrayHasKey('fr', $status['labels']); + $this->assertArrayHasKey('en', $status['labels']); + $this->assertArrayHasKey('de', $status['labels']); + $this->assertArrayHasKey('nl', $status['labels']); + $this->assertArrayHasKey('img_on', $status); + $this->assertArrayHasKey('img_off', $status); + $this->assertArrayHasKey('searchable', $status); + $this->assertArrayHasKey('printable', $status); + $this->assertTrue(is_bool($status['searchable'])); + $this->assertTrue($status['searchable'] === (bool) $ref_status[$status['bit']]['searchable']); + $this->assertTrue(is_bool($status['printable'])); + $this->assertTrue($status['printable'] === (bool) $ref_status[$status['bit']]['printable']); + $this->assertTrue($status['label_on'] === $ref_status[$status['bit']]['labelon']); + $this->assertTrue($status['img_off'] === $ref_status[$status['bit']]['img_off']); + $this->assertTrue($status['img_on'] === $ref_status[$status['bit']]['img_on']); + break; } $route = '/api/v1/databoxes/24892534/status/'; $this->evaluateNotFoundRoute($route, ['GET']); @@ -694,77 +680,77 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase public function testDataboxMetadatasRoute() { $this->setToken(self::$token); - foreach (self::$databoxe_ids as $databox_id) { - $databox = self::$DI['app']['phraseanet.appbox']->get_databox($databox_id); - $ref_structure = $databox->get_meta_structure(); + $databox_id = self::$DI['record_1']->get_sbas_id(); + $databox = self::$DI['app']['phraseanet.appbox']->get_databox($databox_id); + $ref_structure = $databox->get_meta_structure(); - try { - $ref_structure->get_element('idbarbouze'); - $this->fail('An expected exception has not been raised.'); - } catch (\Exception_Databox_FieldNotFound $e) { + try { + $ref_structure->get_element('idbarbouze'); + $this->fail('An expected exception has not been raised.'); + } catch (\Exception_Databox_FieldNotFound $e) { + } + + $route = '/api/v1/databoxes/' . $databox_id . '/metadatas/'; + $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); + + self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $this->evaluateResponse200(self::$DI['client']->getResponse()); + $this->evaluateMeta200($content); + + $this->assertArrayHasKey('document_metadatas', $content['response']); + foreach ($content['response']['document_metadatas'] as $metadatas) { + $this->assertTrue(is_array($metadatas), 'Un bloc metadata est un objet'); + $this->assertArrayHasKey('id', $metadatas); + $this->assertArrayHasKey('namespace', $metadatas); + $this->assertArrayHasKey('source', $metadatas); + $this->assertArrayHasKey('tagname', $metadatas); + $this->assertArrayHasKey('name', $metadatas); + $this->assertArrayHasKey('separator', $metadatas); + $this->assertArrayHasKey('thesaurus_branch', $metadatas); + $this->assertArrayHasKey('type', $metadatas); + $this->assertArrayHasKey('labels', $metadatas); + $this->assertArrayHasKey('indexable', $metadatas); + $this->assertArrayHasKey('multivalue', $metadatas); + $this->assertArrayHasKey('readonly', $metadatas); + $this->assertArrayHasKey('required', $metadatas); + + $this->assertTrue(is_int($metadatas['id'])); + $this->assertTrue(is_string($metadatas['namespace'])); + $this->assertTrue(is_string($metadatas['name'])); + $this->assertTrue(is_array($metadatas['labels'])); + $this->assertTrue(is_null($metadatas['source']) || is_string($metadatas['source'])); + $this->assertTrue(is_string($metadatas['tagname'])); + $this->assertTrue((strlen($metadatas['name']) > 0)); + $this->assertTrue(is_string($metadatas['separator'])); + + $this->assertEquals(['fr', 'en', 'de', 'nl'], array_keys($metadatas['labels'])); + + if ($metadatas['multivalue']) { + $this->assertTrue((strlen($metadatas['separator']) > 0)); } - $route = '/api/v1/databoxes/' . $databox_id . '/metadatas/'; - $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); + $this->assertTrue(is_string($metadatas['thesaurus_branch'])); + $this->assertTrue(in_array($metadatas['type'], [\databox_field::TYPE_DATE, \databox_field::TYPE_STRING, \databox_field::TYPE_NUMBER, \databox_field::TYPE_TEXT])); + $this->assertTrue(is_bool($metadatas['indexable'])); + $this->assertTrue(is_bool($metadatas['multivalue'])); + $this->assertTrue(is_bool($metadatas['readonly'])); + $this->assertTrue(is_bool($metadatas['required'])); - $crawler = self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); - $this->evaluateResponse200(self::$DI['client']->getResponse()); - $this->evaluateMeta200($content); - - $this->assertArrayHasKey('document_metadatas', $content['response']); - foreach ($content['response']['document_metadatas'] as $metadatas) { - $this->assertTrue(is_array($metadatas), 'Un bloc metadata est un objet'); - $this->assertArrayHasKey('id', $metadatas); - $this->assertArrayHasKey('namespace', $metadatas); - $this->assertArrayHasKey('source', $metadatas); - $this->assertArrayHasKey('tagname', $metadatas); - $this->assertArrayHasKey('name', $metadatas); - $this->assertArrayHasKey('separator', $metadatas); - $this->assertArrayHasKey('thesaurus_branch', $metadatas); - $this->assertArrayHasKey('type', $metadatas); - $this->assertArrayHasKey('labels', $metadatas); - $this->assertArrayHasKey('indexable', $metadatas); - $this->assertArrayHasKey('multivalue', $metadatas); - $this->assertArrayHasKey('readonly', $metadatas); - $this->assertArrayHasKey('required', $metadatas); - - $this->assertTrue(is_int($metadatas['id'])); - $this->assertTrue(is_string($metadatas['namespace'])); - $this->assertTrue(is_string($metadatas['name'])); - $this->assertTrue(is_array($metadatas['labels'])); - $this->assertTrue(is_null($metadatas['source']) || is_string($metadatas['source'])); - $this->assertTrue(is_string($metadatas['tagname'])); - $this->assertTrue((strlen($metadatas['name']) > 0)); - $this->assertTrue(is_string($metadatas['separator'])); - - $this->assertEquals(['fr', 'en', 'de', 'nl'], array_keys($metadatas['labels'])); - - if ($metadatas['multivalue']) { - $this->assertTrue((strlen($metadatas['separator']) > 0)); - } - - $this->assertTrue(is_string($metadatas['thesaurus_branch'])); - $this->assertTrue(in_array($metadatas['type'], [\databox_field::TYPE_DATE, \databox_field::TYPE_STRING, \databox_field::TYPE_NUMBER, \databox_field::TYPE_TEXT])); - $this->assertTrue(is_bool($metadatas['indexable'])); - $this->assertTrue(is_bool($metadatas['multivalue'])); - $this->assertTrue(is_bool($metadatas['readonly'])); - $this->assertTrue(is_bool($metadatas['required'])); - - $element = $ref_structure->get_element($metadatas['id']); - $this->assertTrue($element->is_indexable() === $metadatas['indexable']); - $this->assertTrue($element->is_required() === $metadatas['required']); - $this->assertTrue($element->is_readonly() === $metadatas['readonly']); - $this->assertTrue($element->is_multi() === $metadatas['multivalue']); - $this->assertTrue($element->get_type() === $metadatas['type']); - $this->assertTrue($element->get_tbranch() === $metadatas['thesaurus_branch']); - $this->assertTrue($element->get_separator() === $metadatas['separator']); - $this->assertTrue($element->get_name() === $metadatas['name']); - $this->assertTrue($element->get_tag()->getName() === $metadatas['tagname']); - $this->assertTrue($element->get_tag()->getTagname() === $metadatas['source']); - $this->assertTrue($element->get_tag()->getGroupName() === $metadatas['namespace']); - } + $element = $ref_structure->get_element($metadatas['id']); + $this->assertTrue($element->is_indexable() === $metadatas['indexable']); + $this->assertTrue($element->is_required() === $metadatas['required']); + $this->assertTrue($element->is_readonly() === $metadatas['readonly']); + $this->assertTrue($element->is_multi() === $metadatas['multivalue']); + $this->assertTrue($element->get_type() === $metadatas['type']); + $this->assertTrue($element->get_tbranch() === $metadatas['thesaurus_branch']); + $this->assertTrue($element->get_separator() === $metadatas['separator']); + $this->assertTrue($element->get_name() === $metadatas['name']); + $this->assertTrue($element->get_tag()->getName() === $metadatas['tagname']); + $this->assertTrue($element->get_tag()->getTagname() === $metadatas['source']); + $this->assertTrue($element->get_tag()->getGroupName() === $metadatas['namespace']); + break; } $route = '/api/v1/databoxes/24892534/metadatas/'; $this->evaluateNotFoundRoute($route, ['GET']); @@ -782,22 +768,22 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase public function testDataboxTermsOfUseRoute() { $this->setToken(self::$token); - foreach (self::$databoxe_ids as $databox_id) { - $route = '/api/v1/databoxes/' . $databox_id . '/termsOfUse/'; - $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); + $databox_id = self::$DI['record_1']->get_sbas_id(); + $route = '/api/v1/databoxes/' . $databox_id . '/termsOfUse/'; + $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); - self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); - $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); - $this->evaluateResponse200(self::$DI['client']->getResponse()); - $this->evaluateMeta200($content); + self::$DI['client']->request('GET', $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); + $this->evaluateResponse200(self::$DI['client']->getResponse()); + $this->evaluateMeta200($content); - $this->assertArrayHasKey('termsOfUse', $content['response']); - foreach ($content['response']['termsOfUse'] as $terms) { - $this->assertTrue(is_array($terms), 'Une bloc cgu est un objet'); - $this->assertArrayHasKey('locale', $terms); - $this->assertTrue(in_array($terms['locale'], array_keys(Application::getAvailableLanguages()))); - $this->assertArrayHasKey('terms', $terms); - } + $this->assertArrayHasKey('termsOfUse', $content['response']); + foreach ($content['response']['termsOfUse'] as $terms) { + $this->assertTrue(is_array($terms), 'Une bloc cgu est un objet'); + $this->assertArrayHasKey('locale', $terms); + $this->assertTrue(in_array($terms['locale'], array_keys(Application::getAvailableLanguages()))); + $this->assertArrayHasKey('terms', $terms); + break; } $route = '/api/v1/databoxes/24892534/termsOfUse/'; $this->evaluateNotFoundRoute($route, ['GET']); @@ -815,7 +801,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase public function testSearchRoute() { $this->setToken(self::$token); - $crawler = self::$DI['client']->request('POST', '/api/v1/search/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('POST', '/api/v1/search/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->evaluateResponse200(self::$DI['client']->getResponse()); @@ -852,7 +838,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase self::$DI['record_story_1']; - $crawler = self::$DI['client']->request('POST', '/api/v1/search/', $this->getParameters(['search_type' => 1]), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('POST', '/api/v1/search/', $this->getParameters(['search_type' => 1]), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->evaluateResponse200(self::$DI['client']->getResponse()); @@ -885,7 +871,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase public function testRecordsSearchRoute() { $this->setToken(self::$token); - $crawler = self::$DI['client']->request('POST', '/api/v1/records/search/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('POST', '/api/v1/records/search/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->evaluateResponse200(self::$DI['client']->getResponse()); @@ -897,6 +883,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase foreach ($response['results'] as $record) { $this->evaluateGoodRecord($record); + break; } } @@ -915,7 +902,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase ->disableOriginalConstructor() ->getMock() )); - $crawler = self::$DI['client']->request($method, '/api/v1/records/search/', $this->getParameters(['query' => 'koala']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request($method, '/api/v1/records/search/', $this->getParameters(['query' => 'koala']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); } public function provideAvailableSearchMethods() @@ -1013,8 +1000,6 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase { $this->setToken(self::$token); - $keys = array_keys(self::$DI['record_1']->get_subdefs()); - $route = '/api/v1/records/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/embed/'; $this->evaluateMethodNotAllowedRoute($route, ['POST', 'PUT', 'DELETE']); @@ -1078,14 +1063,12 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase $route = '/api/v1/records/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/embed/'; - self::$DI['client']->request('GET', $route, $this->getParameters(['mimes' => ['image/jpg', 'image/jpeg']]), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('GET', $route, $this->getParameters(['mimes' => ['image/png']]), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->assertArrayHasKey('embed', $content['response']); - foreach ($content['response']['embed'] as $embed) { - $this->checkEmbed($embed, self::$DI['record_1']); - } + $this->assertEquals(0, count($content['response']['embed'])); } /** @@ -1213,7 +1196,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase } $this->evaluateMethodNotAllowedRoute($route, ['GET', 'PUT', 'DELETE']); - $crawler = self::$DI['client']->request('POST', $route, $this->getParameters(['status' => $tochange]), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('POST', $route, $this->getParameters(['status' => $tochange]), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); /** @@ -1235,7 +1218,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase $tochange[$n] = $value == '0' ? '1' : '0'; } - $crawler = self::$DI['client']->request('POST', $route, $this->getParameters(['status' => $tochange]), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('POST', $route, $this->getParameters(['status' => $tochange]), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); /** @@ -1394,7 +1377,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase $this->evaluateMethodNotAllowedRoute($route, ['GET', 'PUT', 'DELETE']); - $crawler = self::$DI['client']->request('POST', $route, $this->getParameters(['name' => 'un Joli Nom']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('POST', $route, $this->getParameters(['name' => 'un Joli Nom']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->evaluateResponse200(self::$DI['client']->getResponse()); @@ -1406,7 +1389,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase $this->assertEquals($content['response']['basket']['name'], 'un Joli Nom'); - $crawler = self::$DI['client']->request('POST', $route, $this->getParameters(['name' => 'un Joli Nom']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('POST', $route, $this->getParameters(['name' => 'un Joli Nom']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->evaluateResponse200(self::$DI['client']->getResponse()); @@ -1420,7 +1403,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase $this->assertEquals($content['response']['basket']['name'], 'un Joli Nom'); - $crawler = self::$DI['client']->request('POST', $route, $this->getParameters(['name' => 'aéaa']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request('POST', $route, $this->getParameters(['name' => 'aéaa']), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->evaluateResponse200(self::$DI['client']->getResponse()); @@ -1660,8 +1643,6 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase */ public function testFeedList() { - $title = 'Yellow title'; - $created_feed = self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Feed', 1); $this->setToken(self::$token); @@ -2063,7 +2044,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase protected function evaluateNotFoundRoute($route, $methods) { foreach ($methods as $method) { - $crawler = self::$DI['client']->request($method, $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request($method, $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->evaluateResponseNotFound(self::$DI['client']->getResponse()); @@ -2076,109 +2057,142 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase if ($embed['filesize'] === 0) { var_dump($embed); } + $subdef = $record->get_subdef($embed['name']); $this->assertArrayHasKey("name", $embed); $this->assertArrayHasKey("permalink", $embed); - $this->checkPermalink($embed['permalink'], $record->get_subdef($embed['name'])); + $this->checkPermalink($embed['permalink'], $subdef); $this->assertArrayHasKey("height", $embed); - $this->assertEquals($embed['height'], $record->get_subdef($embed['name'])->get_height()); + $this->assertEquals($embed['height'], $subdef->get_height()); $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $embed['height']); $this->assertArrayHasKey("width", $embed); - $this->assertEquals($embed['width'], $record->get_subdef($embed['name'])->get_width()); + $this->assertEquals($embed['width'], $subdef->get_width()); $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $embed['width']); $this->assertArrayHasKey("filesize", $embed); - $this->assertEquals($embed['filesize'], $record->get_subdef($embed['name'])->get_size()); + $this->assertEquals($embed['filesize'], $subdef->get_size()); $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $embed['filesize']); $this->assertArrayHasKey("player_type", $embed); - $this->assertEquals($embed['player_type'], $record->get_subdef($embed['name'])->get_type()); + $this->assertEquals($embed['player_type'], $subdef->get_type()); $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $embed['player_type']); $this->assertArrayHasKey("mime_type", $embed); - $this->assertEquals($embed['mime_type'], $record->get_subdef($embed['name'])->get_mime()); + $this->assertEquals($embed['mime_type'], $subdef->get_mime()); $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $embed['mime_type']); $this->assertArrayHasKey("devices", $embed); - $this->assertEquals($embed['devices'], $record->get_subdef($embed['name'])->getDevices()); + $this->assertEquals($embed['devices'], $subdef->getDevices()); $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $embed['devices']); } protected function checkPermalink($permalink, \media_subdef $subdef) { - if ($subdef->is_physically_present()) { - $this->assertNotNull($subdef->get_permalink()); - $this->assertInternalType('array', $permalink); - $this->assertArrayHasKey("created_on", $permalink); - $now = new \Datetime($permalink['created_on']); - $interval = $now->diff($subdef->get_permalink()->get_created_on()); - $this->assertTrue(abs($interval->format('U')) < 2); - $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['created_on']); - $this->assertDateAtom($permalink['created_on']); - $this->assertArrayHasKey("id", $permalink); - $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $permalink['id']); - $this->assertEquals($subdef->get_permalink()->get_id(), $permalink['id']); - $this->assertArrayHasKey("is_activated", $permalink); - $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_BOOL, $permalink['is_activated']); - $this->assertEquals($subdef->get_permalink()->get_is_activated(), $permalink['is_activated']); - $this->assertArrayHasKey("label", $permalink); - $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['label']); - $this->assertArrayHasKey("updated_on", $permalink); - $this->assertEquals($subdef->get_permalink()->get_last_modified()->format(DATE_ATOM), $permalink['updated_on']); - $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['updated_on']); - $this->assertDateAtom($permalink['updated_on']); - $this->assertArrayHasKey("page_url", $permalink); - $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['page_url']); - $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"); + if (!$subdef->is_physically_present()) { + return; } + $start = microtime(true); + $this->assertNotNull($subdef->get_permalink()); + $this->assertInternalType('array', $permalink); + $this->assertArrayHasKey("created_on", $permalink); + $now = new \Datetime($permalink['created_on']); + $interval = $now->diff($subdef->get_permalink()->get_created_on()); + $this->assertTrue(abs($interval->format('U')) < 2); + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['created_on']); + $this->assertDateAtom($permalink['created_on']); + $this->assertArrayHasKey("id", $permalink); + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $permalink['id']); + $this->assertEquals($subdef->get_permalink()->get_id(), $permalink['id']); + $this->assertArrayHasKey("is_activated", $permalink); + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_BOOL, $permalink['is_activated']); + $this->assertEquals($subdef->get_permalink()->get_is_activated(), $permalink['is_activated']); + $this->assertArrayHasKey("label", $permalink); + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['label']); + $this->assertArrayHasKey("updated_on", $permalink); + $this->assertEquals($subdef->get_permalink()->get_last_modified()->format(DATE_ATOM), $permalink['updated_on']); + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['updated_on']); + $this->assertDateAtom($permalink['updated_on']); + $this->assertArrayHasKey("page_url", $permalink); + $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING, $permalink['page_url']); + $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"); + } + + private function executeRequest($url) + { + static $request = []; + + if (isset($request[$url])) { + return $request[$url]; + } + + static $webserver; + + if (null === $webserver) { + try { + $code = self::$DI['local-guzzle']->head('/api/')->send()->getStatusCode(); + } catch (GuzzleException $e) { + $code = null; + } + $webserver = ($code < 200 || $code >= 400) ? false : rtrim(self::$DI['app']['phraseanet.registry']->get('GV_ServerName'), '/'); + } + if (false === $webserver) { + $this->markTestSkipped('Install does not seem to rely on a webserver'); + } + if (0 === strpos($url, $webserver)) { + $url = substr($url, strlen($webserver)); + } + + return $request[$url] = self::$DI['local-guzzle']->head($url)->send(); } protected function assertPermalinkHeaders($url, \media_subdef $subdef, $type_url = "page_url") { - $headers = \http_query::getHttpHeaders($url); - $this->assertEquals(200, $headers["http_code"]); + $response = $this->executeRequest($url); + + $this->assertEquals(200, $response->getStatusCode()); switch ($type_url) { case "page_url" : - $this->assertTrue(strpos($headers['content_type'], "text/html") === 0); - $this->assertNotEquals($subdef->get_size(), $headers["download_content_length"]); + $this->assertTrue(strpos((string) $response->getHeader('content-type'), "text/html") === 0); + if ($response->hasHeader('content-length')) { + $this->assertNotEquals($subdef->get_size(), (string) $response->getHeader('content-length')); + } break; case "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"]); + $this->assertTrue(strpos((string) $response->getHeader('content-type'), $subdef->get_mime()) === 0, 'Verify that header ' . (string) $response->getHeader('content-type') . ' contains subdef mime type ' . $subdef->get_mime()); + if ($response->hasHeader('content-length')) { + $this->assertEquals($subdef->get_size(), (string) $response->getHeader('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"]); + $this->assertTrue(strpos((string) $response->getHeader('content-type'), $subdef->get_mime()) === 0, 'Verify that header ' . (string) $response->getHeader('content-type') . ' contains subdef mime type ' . $subdef->get_mime()); + if ($response->hasHeader('content-length')) { + $this->assertEquals($subdef->get_size(), (string) $response->getHeader('content-length')); + } break; } } protected function checkUrlCode200($url) { - $code = \http_query::getHttpCodeFromUrl(self::$DI['app']['phraseanet.registry']->get('GV_ServerName')); - - if ($code == 0) { - $this->markTestSkipped('Install does not seem to rely on a webserver'); - } - - $code = \http_query::getHttpCodeFromUrl($url); + $response = $this->executeRequest($url); + $code = $response->getStatusCode(); $this->assertEquals(200, $code, sprintf('verification de url %s', $url)); } protected function evaluateMethodNotAllowedRoute($route, $methods) { foreach ($methods as $method) { - $crawler = self::$DI['client']->request($method, $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); + self::$DI['client']->request($method, $route, $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]); $content = $this->unserialize(self::$DI['client']->getResponse()->getContent()); $this->assertTrue(self::$DI['client']->getResponse()->headers->has('Allow')); $this->evaluateResponseMethodNotAllowed(self::$DI['client']->getResponse()); diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiYamlTest.php b/tests/Alchemy/Tests/Phrasea/Application/ApiYamlTest.php index c4a84df19c..97e0e866d8 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiYamlTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiYamlTest.php @@ -2,26 +2,21 @@ namespace Alchemy\Tests\Phrasea\Application; +use Symfony\Component\Yaml\Yaml; + class ApiYamlApplication extends ApiTestCase { - - public function getParameters(array $parameters = []) + protected function getParameters(array $parameters = []) { return $parameters; } - public function unserialize($data) + protected function unserialize($data) { - try { - $ret = \Symfony\Component\Yaml\Yaml::parse($data); - } catch (\Exception $e) { - $this->fail("Unable to parse data : \n" . $data . "\nexception : " . $e->getMessage() . "\n"); - } - - return $ret; + return Yaml::parse($data); } - public function getAcceptMimeType() + protected function getAcceptMimeType() { return 'text/yaml'; } diff --git a/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php b/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php index 3f049b3c16..f3134afb20 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php @@ -4,7 +4,6 @@ namespace Alchemy\Tests\Phrasea\Application; class LightboxTest extends \PhraseanetAuthenticatedWebTestCase { - protected $client; protected $feed; protected $entry; @@ -20,11 +19,6 @@ class LightboxTest extends \PhraseanetAuthenticatedWebTestCase ->getMock(); } - public function tearDown() - { - parent::tearDown(); - } - public function testRouteSlash() { $this->authenticate(self::$DI['app']); diff --git a/tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php b/tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php index f50caef88e..14bd637c13 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php +++ b/tests/Alchemy/Tests/Phrasea/Application/OAuth2Test.php @@ -15,47 +15,35 @@ class oauthv2_application_test extends \PhraseanetAuthenticatedWebTestCase * * @var API_OAuth2_Application */ - public static $appli; public static $account_id; public static $account; public $oauth; protected $client; protected $queryParameters; - public function bootTestCase() - { - self::$appli = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test'); - self::$appli->set_description('une description') - ->set_redirect_uri('http://callback.com/callback/') - ->set_website('http://website.com/') - ->set_type(\API_OAuth2_Application::WEB_TYPE); - } - - public static function tearDownAfterClass() - { - if (self::$appli !== false) { - self::deleteInsertedRow(self::$DI['app']['phraseanet.appbox'], self::$appli); - } - self::$appli = null; - parent::tearDownAfterClass(); - } - public function setUp() { parent::setUp(); - $environment = 'test'; - self::$DI['app'] = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Api.php'; + self::$DI['app'] = self::$DI->share(function ($DI) { + return $this->loadApp('/lib/Alchemy/Phrasea/Application/Api.php'); + }); $this->queryParameters = [ "response_type" => "code", - "client_id" => self::$appli->get_client_id(), - "redirect_uri" => self::$appli->get_redirect_uri(), + "client_id" => self::$DI['oauth2-app-user']->get_client_id(), + "redirect_uri" => self::$DI['oauth2-app-user']->get_redirect_uri(), "scope" => "", "state" => "valueTest" ]; } + public static function tearDownAfterClass() + { + self::$account_id = self::$account = null; + parent::tearDownAfterClass(); + } + public static function deleteInsertedRow(\appbox $appbox, \API_OAuth2_Application $app) { $conn = $appbox->get_connection(); @@ -121,7 +109,7 @@ class oauthv2_application_test extends \PhraseanetAuthenticatedWebTestCase public static function getAccount() { $sql = "SELECT api_account_id FROM api_accounts WHERE application_id = :app_id AND usr_id = :usr_id"; - $t = [":app_id" => self::$appli->get_id(), ":usr_id" => self::$DI['user']->get_id()]; + $t = [":app_id" => self::$DI['oauth2-app-user']->get_id(), ":usr_id" => self::$DI['user']->get_id()]; $conn = self::$DI['app']['phraseanet.appbox']->get_connection(); $stmt = $conn->prepare($sql); $stmt->execute($t); @@ -146,7 +134,7 @@ class oauthv2_application_test extends \PhraseanetAuthenticatedWebTestCase //session off $apps = \API_OAuth2_Application::load_authorized_app_by_user(self::$DI['app'], self::$DI['user']); foreach ($apps as $app) { - if ($app->get_client_id() == self::$appli->get_client_id()) { + if ($app->get_client_id() == self::$DI['oauth2-app-user']->get_client_id()) { $authorize = true; self::$DI['client']->followRedirects(); @@ -159,10 +147,10 @@ class oauthv2_application_test extends \PhraseanetAuthenticatedWebTestCase $acc = self::getAccount(); $acc->set_revoked(true); // revoked to show form - $crawler = self::$DI['client']->request('GET', '/api/oauthv2/authorize', $this->queryParameters); + self::$DI['client']->request('GET', '/api/oauthv2/authorize', $this->queryParameters); $this->assertTrue(self::$DI['client']->getResponse()->isSuccessful()); - $this->assertRegExp("/" . self::$appli->get_client_id() . "/", self::$DI['client']->getResponse()->getContent()); - $this->assertRegExp("/" . str_replace("/", '\/', self::$appli->get_redirect_uri()) . "/", self::$DI['client']->getResponse()->getContent()); + $this->assertRegExp("/" . self::$DI['oauth2-app-user']->get_client_id() . "/", self::$DI['client']->getResponse()->getContent()); + $this->assertRegExp("/" . str_replace("/", '\/', self::$DI['oauth2-app-user']->get_redirect_uri()) . "/", self::$DI['client']->getResponse()->getContent()); $this->assertRegExp("/" . $this->queryParameters["response_type"] . "/", self::$DI['client']->getResponse()->getContent()); $this->assertRegExp("/" . $this->queryParameters["scope"] . "/", self::$DI['client']->getResponse()->getContent()); $this->assertRegExp("/" . $this->queryParameters["state"] . "/", self::$DI['client']->getResponse()->getContent()); diff --git a/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php b/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php index b0e5c229d7..e553756c1c 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php @@ -27,13 +27,9 @@ class OverviewTest extends \PhraseanetAuthenticatedWebTestCase public function testEtag() { - $tmp = tempnam(sys_get_temp_dir(), 'testEtag'); - copy(__DIR__ . '/../../../../files/cestlafete.jpg', $tmp); - $record = self::$DI['record_1']; - $record->generate_subdefs($record->get_databox(), self::$DI['app']); - $crawler = self::$DI['client']->request('GET', '/datafiles/' . $record->get_sbas_id() . '/' . $record->get_record_id() . '/preview/'); + self::$DI['client']->request('GET', '/datafiles/' . $record->get_sbas_id() . '/' . $record->get_record_id() . '/preview/'); $response = self::$DI['client']->getResponse(); /* @var $response \Symfony\Component\HttpFoundation\Response */ @@ -44,8 +40,6 @@ class OverviewTest extends \PhraseanetAuthenticatedWebTestCase $this->assertEquals(0, $response->getTtl()); $this->assertGreaterThanOrEqual(0, $response->getAge()); $this->assertNull($response->getExpires()); - - unlink($tmp); } public function testDatafilesRouteNotAuthenticated() diff --git a/tests/Alchemy/Tests/Phrasea/Border/Attribute/StoryTest.php b/tests/Alchemy/Tests/Phrasea/Border/Attribute/StoryTest.php index b223a44713..ebc73db64e 100644 --- a/tests/Alchemy/Tests/Phrasea/Border/Attribute/StoryTest.php +++ b/tests/Alchemy/Tests/Phrasea/Border/Attribute/StoryTest.php @@ -7,40 +7,13 @@ use Alchemy\Phrasea\Border\Attribute\AttributeInterface; class StoryTest extends \PhraseanetTestCase { - /** - * @var Story - */ - protected $object; - protected $story; - - /** - * @covers Alchemy\Phrasea\Border\Attribute\Attribute - * @covers Alchemy\Phrasea\Border\Attribute\Story::__construct - */ - public function setUp() - { - parent::setUp(); - $this->story = \record_adapter::createStory(self::$DI['app'], self::$DI['collection']);; - $this->object = new Story($this->story); - } - - /** - * @covers Alchemy\Phrasea\Border\Attribute\Story::__destruct - */ - public function tearDown() - { - $this->story->delete(); - $this->object = null; - parent::tearDown(); - } - /** * @covers Alchemy\Phrasea\Border\Attribute\Story::getName - * @todo Implement testGetName(). */ public function testGetName() { - $this->assertEquals(AttributeInterface::NAME_STORY, $this->object->getName()); + $story = new Story(self::$DI['record_story_1']); + $this->assertEquals(AttributeInterface::NAME_STORY, $story->getName()); } /** @@ -48,7 +21,9 @@ class StoryTest extends \PhraseanetTestCase */ public function testGetValue() { - $this->assertSame($this->story, $this->object->getValue()); + $record = self::$DI['record_story_1']; + $story = new Story($record); + $this->assertSame($record, $story->getValue()); } /** @@ -56,7 +31,8 @@ class StoryTest extends \PhraseanetTestCase */ public function testAsString() { - $this->assertInternalType('string', $this->object->asString()); + $story = new Story(self::$DI['record_story_1']); + $this->assertInternalType('string', $story->asString()); } /** @@ -64,9 +40,9 @@ class StoryTest extends \PhraseanetTestCase */ public function testLoadFromString() { - $loaded = Story::loadFromString(self::$DI['app'], $this->object->asString()); - - $this->assertEquals($this->object, $loaded); + $story = new Story(self::$DI['record_story_1']); + $loaded = Story::loadFromString(self::$DI['app'], $story->asString()); + $this->assertEquals($story, $loaded); } /** diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Admin/PublicationTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Admin/PublicationTest.php index acdcb8c03b..0aa60f5fb8 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Admin/PublicationTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Admin/PublicationTest.php @@ -8,6 +8,12 @@ class Module_Admin_Route_PublicationTest extends \PhraseanetAuthenticatedWebTest public static $api = null; protected $client; + public static function tearDownAfterClass() + { + self::$account = self::$api = null; + parent::tearDownAfterClass(); + } + public function testList() { $crawler = self::$DI['client']->request('GET', '/admin/publications/list/'); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php index 9f0a666dad..53c110c329 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/BridgeTest.php @@ -37,6 +37,12 @@ class BridgeApplication extends \PhraseanetAuthenticatedWebTestCase parent::tearDown(); } + public static function tearDownAfterClass() + { + self::$api = self::$account = null; + parent::tearDownAfterClass(); + } + /** * @todo create a new basket dont take an existing one */ diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php index edd75bd9e7..7d8860f5e2 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/LazaretTest.php @@ -13,7 +13,6 @@ class LazaretTest extends \PhraseanetAuthenticatedWebTestCase * @return Client A Client instance */ protected $client; - private static $need_records = false; public function tearDown() { diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php index 00393c9fd9..bac84702a7 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php @@ -4,16 +4,12 @@ namespace Alchemy\Tests\Phrasea\Controller\Prod; class ControllerRootTest extends \PhraseanetAuthenticatedWebTestCase { - protected $client; - /** * Default route test */ public function testRouteSlash() { - $this->authenticate(self::$DI['app']); - - $crawler = self::$DI['client']->request('GET', '/prod/'); + self::$DI['client']->request('GET', '/prod/'); $response = self::$DI['client']->getResponse(); /* @var $response \Symfony\Component\HttpFoundation\Response */ diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/UploadTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/UploadTest.php index 26d5b8f9c9..bbee5e71d6 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/UploadTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/UploadTest.php @@ -16,7 +16,6 @@ class UploadTest extends \PhraseanetAuthenticatedWebTestCase */ protected $client; protected $tmpFile; - private static $need_records = false; public function setUp() { diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php index e4446bd1d3..ef206af1ae 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php @@ -7,26 +7,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class AccountTest extends \PhraseanetAuthenticatedWebTestCase { - private static $authorizedApp; - - public function bootTestCase() - { - try { - self::$authorizedApp = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test API v1'); - } catch (\Exception $e) { - - } - } - - public static function tearDownAfterClass() - { - if (self::$authorizedApp) { - self::$authorizedApp->delete(); - } - - parent::tearDownAfterClass(); - } - /** * @covers \Alchemy\Phrasea\Controller\Root\Account::displayAccount * @covers \Alchemy\Phrasea\Controller\Root\Account::call @@ -351,11 +331,7 @@ class AccountTest extends \PhraseanetAuthenticatedWebTestCase */ public function testAUthorizedAppGrantAccessSuccessfull($revoke, $expected) { - if (null === self::$authorizedApp) { - $this->markTestSkipped('Application could not be created'); - } - - self::$DI['client']->request('GET', '/account/security/application/' . self::$authorizedApp->get_id() . '/grant/', [ + self::$DI['client']->request('GET', '/account/security/application/' . self::$DI['oauth2-app-user']->get_id() . '/grant/', [ 'revoke' => $revoke ], [], [ 'HTTP_ACCEPT' => 'application/json', @@ -372,7 +348,7 @@ class AccountTest extends \PhraseanetAuthenticatedWebTestCase $account = \API_OAuth2_Account::load_with_user( self::$DI['app'] - , self::$authorizedApp + , self::$DI['oauth2-app-user'] , self::$DI['user'] ); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/DevelopersTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/DevelopersTest.php index a340027d32..8d1d86f390 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/DevelopersTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/DevelopersTest.php @@ -89,10 +89,9 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase */ public function testGetApp() { - $oauthApp = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); + $oauthApp = self::$DI['oauth2-app-user']; self::$DI['client']->request('GET', '/developers/application/' . $oauthApp->get_id() . '/'); $this->assertTrue(self::$DI['client']->getResponse()->isOk()); - $oauthApp->delete(); } /** @@ -123,9 +122,7 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase public function testDeleteApp() { $oauthApp = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); - $this->XMLHTTPRequest('DELETE', '/developers/application/' . $oauthApp->get_id() . '/'); - $this->assertTrue(self::$DI['client']->getResponse()->isOk()); try { @@ -165,10 +162,8 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase */ public function testRenewAppCallbackError2() { - $oauthApp = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); - + $oauthApp = self::$DI['oauth2-app-user']; $this->XMLHTTPRequest('POST', '/developers/application/'.$oauthApp->get_id().'/callback/'); - $this->assertTrue(self::$DI['client']->getResponse()->isOk()); $content = json_decode(self::$DI['client']->getResponse()->getContent()); $this->assertFalse($content->success); @@ -179,7 +174,7 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase */ public function testRenewAppCallback() { - $oauthApp = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); + $oauthApp = self::$DI['oauth2-app-user']; $this->XMLHTTPRequest('POST', '/developers/application/' . $oauthApp->get_id() . '/callback/', [ 'callback' => 'my.callback.com' @@ -222,7 +217,7 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase */ public function testRenewAccessToken() { - $oauthApp = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); + $oauthApp = self::$DI['oauth2-app-user']; $this->XMLHTTPRequest('POST', '/developers/application/' . $oauthApp->get_id() . '/access_token/'); @@ -261,7 +256,7 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase */ public function testAuthorizeGrantpasswordToken() { - $oauthApp = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); + $oauthApp = self::$DI['oauth2-app-user']; $this->XMLHTTPRequest('POST', '/developers/application/' . $oauthApp->get_id() . '/authorize_grant_password/', [ 'grant' => '1' diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php index e0bdb4f313..e8da3a49bb 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php @@ -53,6 +53,12 @@ class LoginTest extends \PhraseanetAuthenticatedWebTestCase parent::tearDown(); } + public static function tearDownAfterClass() + { + self::$demands = self::$collections = self::$login = self::$email = null; + parent::tearDownAfterClass(); + } + public function testRegisterWithNoTou() { $this->logout(self::$DI['app']); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php index b4d28d6ed4..655ea096f0 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/RSSFeedTest.php @@ -12,19 +12,6 @@ use Symfony\Component\HttpFoundation\Response; class RssFeedTest extends \PhraseanetWebTestCase { - private static $initialized = false; - - public function setUp() - { - parent::setUp(); - - if (!self::$initialized) { - - @unlink('/tmp/db.sqlite'); - copy(__DIR__ . '/../../../../../db-ref.sqlite', '/tmp/db.sqlite'); - } - } - public function testPublicFeedAggregated() { self::$DI['app']['EM']->find('Alchemy\Phrasea\Model\Entities\Feed', 2); diff --git a/tests/Alchemy/Tests/Phrasea/Core/Configuration/DisplaySettingServiceTest.php b/tests/Alchemy/Tests/Phrasea/Core/Configuration/DisplaySettingServiceTest.php index dc5b9f9722..22ff8ddc51 100644 --- a/tests/Alchemy/Tests/Phrasea/Core/Configuration/DisplaySettingServiceTest.php +++ b/tests/Alchemy/Tests/Phrasea/Core/Configuration/DisplaySettingServiceTest.php @@ -35,6 +35,7 @@ class DisplaySettingServiceTest extends \PhraseanetTestCase self::$DI['app']['conf']->set('registry', self::$appSettings); } + self::$userSettings = self::$appSettings = null; parent::tearDownAfterClass(); } diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php index c35bfb795f..7191f9da08 100644 --- a/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineAbstractTest.php @@ -51,6 +51,12 @@ abstract class SearchEngineAbstractTest extends \PhraseanetAuthenticatedTestCase self::$searchEngine->setOptions($options); } + public static function tearDownAfterClass() + { + self::$searchEngine = self::$searchEngineClass = self::$initialized = null; + parent::tearDownAfterClass(); + } + /** * @return SearchEngineOptions */ diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/SphinxSearchEngineTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/SphinxSearchEngineTest.php index a9f7f630d4..ad2b4aeaa1 100644 --- a/tests/Alchemy/Tests/Phrasea/SearchEngine/SphinxSearchEngineTest.php +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/SphinxSearchEngineTest.php @@ -105,6 +105,7 @@ class SphinxSearchEngineTest extends SearchEngineAbstractTest unlink(self::$config); } + self::$skipped = self::$config = self::$searchd = null; parent::tearDownAfterClass(); } diff --git a/tests/classes/ACLTest.php b/tests/classes/ACLTest.php index ef8f53a60d..5ac179e192 100644 --- a/tests/classes/ACLTest.php +++ b/tests/classes/ACLTest.php @@ -28,6 +28,7 @@ class ACLTest extends \PhraseanetAuthenticatedTestCase } self::resetUsersRights(self::$DI['app'], self::$DI['user']); + self::$object = null; parent::tearDownAfterClass(); } diff --git a/tests/classes/Bridge/Api/Bridge_Api_AbstractTest.php b/tests/classes/Bridge/Api/Bridge_Api_AbstractTest.php index 7a3ffef527..2290371dc6 100644 --- a/tests/classes/Bridge/Api/Bridge_Api_AbstractTest.php +++ b/tests/classes/Bridge/Api/Bridge_Api_AbstractTest.php @@ -44,6 +44,7 @@ class Bridge_Api_AbstractTest extends \PhraseanetWebTestCase if (self::$account instanceof Bridge_Account) { self::$account->delete(); } + self::$api = self::$account = null; parent::tearDownAfterClass(); } diff --git a/tests/classes/Bridge/Bridge_AccountTest.php b/tests/classes/Bridge/Bridge_AccountTest.php index f20c74e0a6..c5659e78d3 100644 --- a/tests/classes/Bridge/Bridge_AccountTest.php +++ b/tests/classes/Bridge/Bridge_AccountTest.php @@ -53,6 +53,9 @@ class Bridge_AccountTest extends \PhraseanetAuthenticatedTestCase if (self::$api) { self::$api->delete(); } + + self::$object = self::$api = self::$dist_id = self::$named = self::$id = null; + parent::tearDownAfterClass(); } diff --git a/tests/classes/PhraseanetPHPUnitListener.php b/tests/classes/PhraseanetPHPUnitListener.php index 03d203cb71..351e3ad496 100644 --- a/tests/classes/PhraseanetPHPUnitListener.php +++ b/tests/classes/PhraseanetPHPUnitListener.php @@ -2,28 +2,27 @@ class PhraseanetPHPUnitListener implements PHPUnit_Framework_TestListener { + private static $enableDurationCapture = false; private static $skipped = []; + private static $duration = []; + private static $csv = []; + private static $durationByTest = []; public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) { - return; } public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) { - return; } public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) { - return; } public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) { static::$skipped[] = get_class($test) . ':' . $test->getName() . ' - ' . $e->getMessage(); - - return; } public static function getSkipped() @@ -31,30 +30,89 @@ class PhraseanetPHPUnitListener implements PHPUnit_Framework_TestListener return static::$skipped; } + public static function getDuration() + { + return static::$duration; + } + + public static function getCsv() + { + return static::$csv; + } + + public static function getDurationByTest() + { + return static::$durationByTest; + } + public static function resetSkipped() { static::$skipped = []; + } - return; + public static function resetDuration() + { + static::$duration = []; + static::$durationByTest = []; + static::$csv = []; } public function startTest(PHPUnit_Framework_Test $test) { - return; + if (!static::$enableDurationCapture) { + return; + } + if (!isset(static::$durationByTest[get_class($test)]['executions'])) { + static::$durationByTest[get_class($test)]['executions'] = 0; + } + + static::$durationByTest[get_class($test)]['executions']++; + static::$duration[self::generateName($test)] = microtime(true); + static::$csv[self::generateName($test)] = [ + 'duration' => microtime(true), + 'test' => get_class($test), + 'name' => $test->getName(), + ]; } public function endTest(PHPUnit_Framework_Test $test, $time) { - return; + if (!static::$enableDurationCapture) { + return; + } + $name = self::generateName($test); + static::$duration[$name] = microtime(true) - static::$duration[$name]; + static::$csv[self::generateName($test)]['duration'] = microtime(true) - static::$csv[self::generateName($test)]['duration']; } public function startTestSuite(PHPUnit_Framework_TestSuite $suite) { - return; + if (!static::$enableDurationCapture) { + return; + } + + if (!class_exists($suite->getName())) { + return; + } + + static::$durationByTest[$suite->getName()]['time'] = microtime(true); } public function endTestSuite(PHPUnit_Framework_TestSuite $suite) { - return; + if (!static::$enableDurationCapture) { + return; + } + + if (!class_exists($suite->getName())) { + return; + } + + static::$durationByTest[$suite->getName()]['time'] = microtime(true) - static::$durationByTest[$suite->getName()]['time']; + } + + private static function generateName(PHPUnit_Framework_Test $test) + { + return get_class($test) . '::' . $test->getName(); } } diff --git a/tests/classes/PhraseanetTestCase.php b/tests/classes/PhraseanetTestCase.php index 8591ec015b..d35be403dd 100644 --- a/tests/classes/PhraseanetTestCase.php +++ b/tests/classes/PhraseanetTestCase.php @@ -16,6 +16,7 @@ use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface; use Symfony\Component\Routing\RequestContext; use Alchemy\Tests\Tools\TranslatorMockTrait; +use Guzzle\Http\Client as Guzzle; abstract class PhraseanetTestCase extends WebTestCase { @@ -86,6 +87,10 @@ abstract class PhraseanetTestCase extends WebTestCase return $this->loadCLI(); }); + self::$DI['local-guzzle'] = self::$DI->share(function ($DI) { + return new Guzzle(self::$DI['app']['phraseanet.registry']->get('GV_ServerName')); + }); + self::$DI['client'] = self::$DI->share(function ($DI) { return new Client($DI['app'], []); }); @@ -109,6 +114,14 @@ abstract class PhraseanetTestCase extends WebTestCase return User_Adapter::getInstance(self::$fixtureIds['user']['test_phpunit_alt2'], $DI['app']); }); + self::$DI['oauth2-app-user'] = self::$DI->share(function ($DI) { + return new \API_OAuth2_Application($DI['app'], self::$fixtureIds['oauth']['user']); + }); + + self::$DI['oauth2-app-user_notAdmin'] = self::$DI->share(function ($DI) { + return new \API_OAuth2_Application($DI['app'], self::$fixtureIds['oauth']['user_notAdmin']); + }); + self::$DI['logger'] = self::$DI->share(function () { $logger = new Logger('tests'); $logger->pushHandler(new NullHandler()); @@ -152,7 +165,7 @@ abstract class PhraseanetTestCase extends WebTestCase }); } - foreach (range(1, 2) as $i) { + foreach (range(1, 3) as $i) { self::$DI['record_story_' . $i] = self::$DI->share(function ($DI) use ($i) { return new \record_adapter($DI['app'], self::$fixtureIds['databox']['records'], self::$fixtureIds['record']['record_story_'.$i]); }); @@ -207,6 +220,7 @@ abstract class PhraseanetTestCase extends WebTestCase public static function tearDownAfterClass() { self::$testCaseBooted = false; + parent::tearDownAfterClass(); } protected function bootTestCase() @@ -448,6 +462,26 @@ abstract class PhraseanetTestCase extends WebTestCase self::$recordsInitialized = []; } + $duration = PhraseanetPHPUnitListener::getDurationByTest(); + $tests = array(); + + foreach ($duration as $name => $data) { + $tests[$name . '(total : '.$data['time'].' and '.$data['executions'].' executions)'] = $data['time'] / $data['executions']; + } + + asort($tests); + + $csvData = PhraseanetPHPUnitListener::getCsv(); + if (count($csvData) > 0) { + foreach ($csvData as $data) { + file_put_contents(__DIR__ . '/../../report.csv', "\"".implode('","', array_map(function ($value) { + return str_replace('"', '""', $value); + }, $data))."\"\n", FILE_APPEND); + } + } + + PhraseanetPHPUnitListener::resetDuration(); + return; } diff --git a/tests/classes/api/oauthv2/API_OAuth2_AuthCodeTest.php b/tests/classes/api/oauthv2/API_OAuth2_AuthCodeTest.php index 6130e5a7b8..2a400db8e8 100644 --- a/tests/classes/api/oauthv2/API_OAuth2_AuthCodeTest.php +++ b/tests/classes/api/oauthv2/API_OAuth2_AuthCodeTest.php @@ -14,21 +14,12 @@ class API_OAuth2_AuthCodeTest extends \PhraseanetTestCase public function setUp() { parent::setUp(); - $this->application = API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); - $this->account = API_OAuth2_Account::load_with_user(self::$DI['app'], $this->application, self::$DI['user']); - + $this->account = API_OAuth2_Account::load_with_user(self::$DI['app'], self::$DI['oauth2-app-user'], self::$DI['user']); $expires = time() + 100; $this->code = random::generatePassword(8); - $this->object = API_OAuth2_AuthCode::create(self::$DI['app'], $this->account, $this->code, $expires); } - public function tearDown() - { - $this->application->delete(); - parent::tearDown(); - } - public function testGet_code() { $this->assertEquals($this->code, $this->object->get_code()); diff --git a/tests/classes/api/oauthv2/API_OAuth2_RefreshTokenTest.php b/tests/classes/api/oauthv2/API_OAuth2_RefreshTokenTest.php index 02622c1ca0..3c88e301bd 100644 --- a/tests/classes/api/oauthv2/API_OAuth2_RefreshTokenTest.php +++ b/tests/classes/api/oauthv2/API_OAuth2_RefreshTokenTest.php @@ -14,8 +14,7 @@ class API_OAuth2_RefreshTokenTest extends \PhraseanetTestCase public function setUp() { parent::setUp(); - $this->application = API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); - $this->account = API_OAuth2_Account::load_with_user(self::$DI['app'], $this->application, self::$DI['user']); + $this->account = API_OAuth2_Account::load_with_user(self::$DI['app'], self::$DI['oauth2-app-user'], self::$DI['user']); $expires = time() + 100; $this->token = random::generatePassword(8); @@ -24,12 +23,6 @@ class API_OAuth2_RefreshTokenTest extends \PhraseanetTestCase $this->object = API_OAuth2_RefreshToken::create(self::$DI['app'], $this->account, $expires, $this->token, $this->scope); } - public function tearDown() - { - $this->application->delete(); - parent::tearDown(); - } - public function testGet_value() { $this->assertEquals($this->token, $this->object->get_value()); diff --git a/tests/classes/api/oauthv2/api_oauthv2_AccountTest.php b/tests/classes/api/oauthv2/api_oauthv2_AccountTest.php index f1595e4c43..c6396a3d04 100644 --- a/tests/classes/api/oauthv2/api_oauthv2_AccountTest.php +++ b/tests/classes/api/oauthv2/api_oauthv2_AccountTest.php @@ -2,7 +2,6 @@ class API_OAuth2_AccountTest extends \PhraseanetTestCase { - /** * @var API_OAuth2_Account */ @@ -11,65 +10,36 @@ class API_OAuth2_AccountTest extends \PhraseanetTestCase public function setUp() { parent::setUp(); - $this->application = API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); - $this->object = API_OAuth2_Account::load_with_user(self::$DI['app'], $this->application, self::$DI['user']); + $this->object = API_OAuth2_Account::load_with_user(self::$DI['app'], self::$DI['oauth2-app-user'], self::$DI['user']); } - public function tearDown() - { - $this->application->delete(); - parent::tearDown(); - } - - public function testGet_id() + public function testGettersAndSetters() { $this->assertTrue(is_int($this->object->get_id())); - } - public function testGet_user() - { $this->assertInstanceOf('User_Adapter', $this->object->get_user()); $this->assertEquals(self::$DI['user']->get_id(), $this->object->get_user()->get_id()); - } - public function testGet_api_version() - { $this->assertEquals('1.0', $this->object->get_api_version()); - } - public function testIs_revoked() - { $this->assertTrue(is_bool($this->object->is_revoked())); - $this->assertFalse($this->object->is_revoked()); - } - public function testSet_revoked() - { $this->object->set_revoked(true); $this->assertTrue($this->object->is_revoked()); $this->object->set_revoked(false); $this->assertFalse($this->object->is_revoked()); - } - public function testGet_created_on() - { $this->assertInstanceOf('DateTime', $this->object->get_created_on()); - } - public function testGet_token() - { $this->assertInstanceOf('API_OAuth2_Token', $this->object->get_token()); - } - public function testGet_application() - { $this->assertInstanceOf('API_OAuth2_Application', $this->object->get_application()); - $this->assertEquals($this->application, $this->object->get_application()); + $this->assertEquals(self::$DI['oauth2-app-user'], $this->object->get_application()); } public function testLoad_with_user() { - $loaded = API_OAuth2_Account::load_with_user(self::$DI['app'], $this->application, self::$DI['user']); + $loaded = API_OAuth2_Account::load_with_user(self::$DI['app'], self::$DI['oauth2-app-user'], self::$DI['user']); $this->assertInstanceOf('API_OAuth2_Account', $loaded); $this->assertEquals($this->object, $loaded); } diff --git a/tests/classes/api/oauthv2/api_oauthv2_ApplicationTest.php b/tests/classes/api/oauthv2/api_oauthv2_ApplicationTest.php index 3763370f74..94395916ea 100644 --- a/tests/classes/api/oauthv2/api_oauthv2_ApplicationTest.php +++ b/tests/classes/api/oauthv2/api_oauthv2_ApplicationTest.php @@ -2,29 +2,12 @@ class API_OAuth2_ApplicationTest extends \PhraseanetTestCase { - /** - * @var API_OAuth2_Application - */ - protected $object; - - public function setUp() - { - parent::setUp(); - $this->object = API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); - } - - public function tearDown() - { - $this->object->delete(); - parent::tearDown(); - } - public function testLoad_from_client_id() { - $client_id = $this->object->get_client_id(); + $client_id = self::$DI['oauth2-app-user']->get_client_id(); $loaded = API_OAuth2_Application::load_from_client_id(self::$DI['app'], $client_id); $this->assertInstanceOf('API_OAuth2_Application', $loaded); - $this->assertEquals($this->object, $loaded); + $this->assertEquals(self::$DI['oauth2-app-user'], $loaded); } public function testLoad_dev_app_by_user() @@ -34,7 +17,7 @@ class API_OAuth2_ApplicationTest extends \PhraseanetTestCase $this->assertTrue(count($apps) > 0); $found = false; foreach ($apps as $app) { - if ($app->get_id() === $this->object->get_id()) + if ($app->get_id() === self::$DI['oauth2-app-user']->get_id()) $found = true; $this->assertInstanceOf('API_OAuth2_Application', $app); } @@ -51,7 +34,7 @@ class API_OAuth2_ApplicationTest extends \PhraseanetTestCase $found = false; foreach ($apps as $app) { - if ($app->get_id() === $this->object->get_id()) + if ($app->get_id() === self::$DI['oauth2-app-user']->get_id()) $found = true; $this->assertInstanceOf('API_OAuth2_Application', $app); } @@ -60,133 +43,78 @@ class API_OAuth2_ApplicationTest extends \PhraseanetTestCase $this->fail(); } - public function testGet_id() + public function testGettersAndSetters() { - $this->assertTrue(is_int($this->object->get_id())); - } + $this->assertTrue(is_int(self::$DI['oauth2-app-user']->get_id())); + $this->assertInstanceOf('User_Adapter', self::$DI['oauth2-app-user']->get_creator()); + $this->assertEquals(self::$DI['user']->get_id(), self::$DI['oauth2-app-user']->get_creator()->get_id()); - public function testGet_creator() - { - $this->assertInstanceOf('User_Adapter', $this->object->get_creator()); - } + $this->assertTrue(in_array(self::$DI['oauth2-app-user']->get_type(), [API_OAuth2_Application::DESKTOP_TYPE, API_OAuth2_Application::WEB_TYPE])); - public function testGet_type() - { - $this->assertTrue(in_array($this->object->get_type(), [API_OAuth2_Application::DESKTOP_TYPE, API_OAuth2_Application::WEB_TYPE])); - } + $this->assertTrue(is_string(self::$DI['oauth2-app-user']->get_nonce())); + $this->assertTrue(strlen(self::$DI['oauth2-app-user']->get_nonce()) === 6); - public function testGet_nonce() - { - $this->assertTrue(is_string($this->object->get_nonce())); - $this->assertTrue(strlen($this->object->get_nonce()) === 6); - } - - public function testSet_type() - { try { - $this->object->set_type('prout'); + self::$DI['oauth2-app-user']->set_type('prout'); $this->fail(); } catch (Exception_InvalidArgument $e) { } - $this->object->set_type(API_OAuth2_Application::WEB_TYPE); - $this->assertEquals(API_OAuth2_Application::WEB_TYPE, $this->object->get_type()); - $this->object->set_type(API_OAuth2_Application::DESKTOP_TYPE); - $this->assertEquals(API_OAuth2_Application::DESKTOP_TYPE, $this->object->get_type()); - $this->assertEquals(API_OAuth2_Application::NATIVE_APP_REDIRECT_URI, $this->object->get_redirect_uri()); - } + self::$DI['oauth2-app-user']->set_type(API_OAuth2_Application::WEB_TYPE); + $this->assertEquals(API_OAuth2_Application::WEB_TYPE, self::$DI['oauth2-app-user']->get_type()); + self::$DI['oauth2-app-user']->set_type(API_OAuth2_Application::DESKTOP_TYPE); + $this->assertEquals(API_OAuth2_Application::DESKTOP_TYPE, self::$DI['oauth2-app-user']->get_type()); + $this->assertEquals(API_OAuth2_Application::NATIVE_APP_REDIRECT_URI, self::$DI['oauth2-app-user']->get_redirect_uri()); + self::$DI['oauth2-app-user']->set_type(API_OAuth2_Application::WEB_TYPE); - public function testGet_name() - { - $this->assertEquals('test app', $this->object->get_name()); - } - public function testSet_name() - { - $this->object->set_name('prout'); - $this->assertEquals('prout', $this->object->get_name()); - } + self::$DI['oauth2-app-user']->set_name('prout'); + $this->assertEquals('prout', self::$DI['oauth2-app-user']->get_name()); + self::$DI['oauth2-app-user']->set_name('test application for user'); + $this->assertEquals('test application for user', self::$DI['oauth2-app-user']->get_name()); - public function testGet_description() - { - $this->assertEquals('', $this->object->get_description()); - } - public function testSet_description() - { $desc = 'prouti prouto prout prout'; - $this->object->set_description($desc); - $this->assertEquals($desc, $this->object->get_description()); - } + self::$DI['oauth2-app-user']->set_description($desc); + $this->assertEquals($desc, self::$DI['oauth2-app-user']->get_description()); + self::$DI['oauth2-app-user']->set_description(''); + $this->assertEquals('', self::$DI['oauth2-app-user']->get_description()); - public function testGet_website() - { - $this->assertEquals('', $this->object->get_website()); - } - public function testSet_website() - { $site = 'http://www.example.com/'; - $this->object->set_website($site); - $this->assertEquals($site, $this->object->get_website()); - } + self::$DI['oauth2-app-user']->set_website($site); + $this->assertEquals($site, self::$DI['oauth2-app-user']->get_website()); + self::$DI['oauth2-app-user']->set_website(''); + $this->assertEquals('', self::$DI['oauth2-app-user']->get_website()); - public function testGet_created_on() - { - $this->assertInstanceOf('DateTime', $this->object->get_created_on()); - } + $this->assertInstanceOf('DateTime', self::$DI['oauth2-app-user']->get_created_on()); - public function testGet_last_modified() - { - $this->assertInstanceOf('DateTime', $this->object->get_last_modified()); + $this->assertInstanceOf('DateTime', self::$DI['oauth2-app-user']->get_last_modified()); + + $this->assertMd5(self::$DI['oauth2-app-user']->get_client_id()); + + $client_id = md5('prouto'); + self::$DI['oauth2-app-user']->set_client_id($client_id); + $this->assertEquals($client_id, self::$DI['oauth2-app-user']->get_client_id()); + $this->assertMd5(self::$DI['oauth2-app-user']->get_client_id()); + + $this->assertMd5(self::$DI['oauth2-app-user']->get_client_secret()); + + $client_secret = md5('prouto'); + self::$DI['oauth2-app-user']->set_client_secret($client_secret); + $this->assertEquals($client_secret, self::$DI['oauth2-app-user']->get_client_secret()); + $this->assertMd5(self::$DI['oauth2-app-user']->get_client_secret()); + + $uri = 'http://www.example.com/callback/'; + self::$DI['oauth2-app-user']->set_redirect_uri($uri); + $this->assertEquals($uri, self::$DI['oauth2-app-user']->get_redirect_uri()); + + $this->assertInstanceOf('API_OAuth2_Account', self::$DI['oauth2-app-user']->get_user_account(self::$DI['user'])); } private function assertmd5($md5) { $this->assertTrue((count(preg_match('/[a-z0-9]{32}/', $md5)) === 1)); } - - public function testGet_client_id() - { - $this->assertMd5($this->object->get_client_id()); - } - - public function testSet_client_id() - { - $client_id = md5('prouto'); - $this->object->set_client_id($client_id); - $this->assertEquals($client_id, $this->object->get_client_id()); - $this->assertMd5($this->object->get_client_id()); - } - - public function testGet_client_secret() - { - $this->assertMd5($this->object->get_client_secret()); - } - - public function testSet_client_secret() - { - $client_secret = md5('prouto'); - $this->object->set_client_secret($client_secret); - $this->assertEquals($client_secret, $this->object->get_client_secret()); - $this->assertMd5($this->object->get_client_secret()); - } - - public function testGet_redirect_uri() - { - $this->assertEquals('', $this->object->get_redirect_uri()); - } - - public function testSet_redirect_uri() - { - $uri = 'http://www.example.com/callback/'; - $this->object->set_redirect_uri($uri); - $this->assertEquals($uri, $this->object->get_redirect_uri()); - } - - public function testGet_user_account() - { - $this->assertInstanceOf('API_OAuth2_Account', $this->object->get_user_account(self::$DI['user'])); - } } diff --git a/tests/classes/api/oauthv2/api_oauthv2_TokenTest.php b/tests/classes/api/oauthv2/api_oauthv2_TokenTest.php index 01d60773a6..07cbe89d72 100644 --- a/tests/classes/api/oauthv2/api_oauthv2_TokenTest.php +++ b/tests/classes/api/oauthv2/api_oauthv2_TokenTest.php @@ -2,7 +2,6 @@ class API_OAuth2_TokenTest extends \PhraseanetTestCase { - /** * @var API_OAuth2_Token */ @@ -11,8 +10,7 @@ class API_OAuth2_TokenTest extends \PhraseanetTestCase public function setUp() { parent::setUp(); - $this->application = API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); - $account = API_OAuth2_Account::load_with_user(self::$DI['app'], $this->application, self::$DI['user']); + $account = API_OAuth2_Account::load_with_user(self::$DI['app'], self::$DI['oauth2-app-user'], self::$DI['user']); try { new API_OAuth2_Token(self::$DI['app']['phraseanet.appbox'], $account); @@ -26,7 +24,7 @@ class API_OAuth2_TokenTest extends \PhraseanetTestCase public function tearDown() { - $this->application->delete(); + $this->object->delete(); parent::tearDown(); } @@ -35,59 +33,38 @@ class API_OAuth2_TokenTest extends \PhraseanetTestCase $this->assertTrue((count(preg_match('/[a-z0-9]{32}/', $md5)) === 1)); } - public function testGet_value() + public function testGettersAndSetters() { $this->assertmd5($this->object->get_value()); - } - public function testSet_value() - { $value = md5('prout'); $this->object->set_value($value); $this->assertEquals($value, $this->object->get_value()); - } - public function testGet_session_id() - { + $this->object->set_session_id(null); $this->assertNull($this->object->get_session_id()); - } - public function testSet_session_id() - { $this->object->set_session_id(458); $this->assertEquals(458, $this->object->get_session_id()); - } - public function testGet_expires() - { + $expire = time() + 3600; + $this->object->set_expires($expire); $diff = (int) $this->object->get_expires() - time(); - $this->assertInternalType('string', $this->object->get_expires(), "expiration timestamp is string : " . $this->object->get_expires()); + $this->assertSame($expire, $this->object->get_expires(), "expiration timestamp is string : " . $this->object->get_expires()); $this->assertTrue($diff > 3500, "expire value $diff should be more than 3500 seconds "); $this->assertTrue($diff < 3700, "expire value $diff should be less than 3700 seconds "); - } - public function testSet_expires() - { $date = time() + 7200; $this->object->set_expires($date); $this->assertEquals($date, $this->object->get_expires()); - } - public function testGet_scope() - { $this->assertNull($this->object->get_scope()); - } - public function testset_scope() - { $this->assertNull($this->object->get_scope()); $scope = "prout"; $this->object->set_scope($scope); $this->assertEquals($scope, $this->object->get_scope()); - } - public function testGet_account() - { $this->assertInstanceOf('API_OAuth2_Account', $this->object->get_account()); } diff --git a/tests/classes/collectionTest.php b/tests/classes/collectionTest.php index 640435a481..40c271a4b8 100644 --- a/tests/classes/collectionTest.php +++ b/tests/classes/collectionTest.php @@ -40,8 +40,8 @@ class collectionTest extends \PhraseanetAuthenticatedTestCase public static function tearDownAfterClass() { - self::$object->delete(); + self::$object = self::$objectDisable = null; parent::tearDownAfterClass(); } diff --git a/tests/classes/media/Permalink/media_Permalink_AdapterTest.php b/tests/classes/media/Permalink/media_Permalink_AdapterTest.php index 766bea525a..4d75782eb5 100644 --- a/tests/classes/media/Permalink/media_Permalink_AdapterTest.php +++ b/tests/classes/media/Permalink/media_Permalink_AdapterTest.php @@ -5,46 +5,42 @@ class media_Permalink_AdapterTest extends \PhraseanetTestCase /** * @var media_Permalink_Adapter */ - public static $object; - public static $subdef; + private $object; public function setUp() { parent::setUp(); - $databox = self::$DI['record_1']->get_databox(); - self::$subdef = self::$DI['record_1']->get_subdef('document'); - self::$object = media_Permalink_Adapter::getPermalink(self::$DI['app'], $databox, self::$subdef); + $this->object = media_Permalink_Adapter::getPermalink(self::$DI['app'], self::$DI['record_1']->get_databox(), self::$DI['record_1']->get_subdef('document')); } public function testGet_label() { - $this->assertInternalType('string', self::$object->get_label()); - $this->assertEquals('test001', self::$object->get_label()); + $this->assertSame('test001', $this->object->get_label()); } public function testGetPermalink() { - $this->assertTrue((self::$object instanceof media_Permalink_Adapter)); + $this->assertTrue(($this->object instanceof media_Permalink_Adapter)); } public function testSet_is_activated() { - self::$object->set_is_activated(true); - $this->assertTrue(self::$object->get_is_activated()); - self::$object->set_is_activated(false); - $this->assertFalse(self::$object->get_is_activated()); - self::$object->set_is_activated(true); - $this->assertTrue(self::$object->get_is_activated()); + $this->object->set_is_activated(true); + $this->assertTrue($this->object->get_is_activated()); + $this->object->set_is_activated(false); + $this->assertFalse($this->object->get_is_activated()); + $this->object->set_is_activated(true); + $this->assertTrue($this->object->get_is_activated()); } public function testSet_label() { - self::$object->set_label('coucou les chicos'); - $this->assertEquals('coucou-les-chicos', self::$object->get_label()); - self::$object->set_label(''); - $this->assertEquals('untitled', self::$object->get_label()); - self::$object->set_label('JE ANp ra&é"\/,;:!§/.?%µ*ù$]@^\[{#~234567890°+\'(-è_çà'); - $this->assertEquals('JE-ANp-raeu234567890-e_ca', self::$object->get_label()); + $this->object->set_label('coucou les chicos'); + $this->assertEquals('coucou-les-chicos', $this->object->get_label()); + $this->object->set_label(''); + $this->assertEquals('untitled', $this->object->get_label()); + $this->object->set_label('JE ANp ra&é"\/,;:!§/.?%µ*ù$]@^\[{#~234567890°+\'(-è_çà'); + $this->assertEquals('JE-ANp-raeu234567890-e_ca', $this->object->get_label()); } public function testGet_url() @@ -52,11 +48,11 @@ class media_Permalink_AdapterTest extends \PhraseanetTestCase $url = rtrim(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/' . self::$object->get_label() + . '/document/' . $this->object->get_label() . '.' . pathinfo(self::$DI['record_1']->get_subdef('document')->get_file(), PATHINFO_EXTENSION) - . '?token=' . self::$object->get_token(); + . '?token=' . $this->object->get_token(); - $this->assertEquals($url, self::$object->get_url()); + $this->assertEquals($url, $this->object->get_url()); } public function testGet_Previewurl() @@ -81,34 +77,34 @@ class media_Permalink_AdapterTest extends \PhraseanetTestCase . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/document/' - . '?token=' . self::$object->get_token(); + . '?token=' . $this->object->get_token(); - $this->assertEquals($url, self::$object->get_page()); + $this->assertEquals($url, $this->object->get_page()); } public function testGet_id() { - $this->assertInternalType('integer', self::$object->get_id()); + $this->assertInternalType('integer', $this->object->get_id()); } public function testGet_token() { - $this->assertInternalType('string', self::$object->get_token()); + $this->assertInternalType('string', $this->object->get_token()); } public function testGet_is_activated() { - $this->assertInternalType('boolean', self::$object->get_is_activated()); + $this->assertInternalType('boolean', $this->object->get_is_activated()); } public function testGet_created_on() { - $this->assertInstanceOf('DateTime', self::$object->get_created_on()); + $this->assertInstanceOf('DateTime', $this->object->get_created_on()); } public function testGet_last_modified() { - $this->assertInstanceOf('DateTime', self::$object->get_last_modified()); + $this->assertInstanceOf('DateTime', $this->object->get_last_modified()); } /** @@ -116,7 +112,6 @@ class media_Permalink_AdapterTest extends \PhraseanetTestCase */ public function testCreateAPermalinkAlreadyCreated() { - $databox = self::$DI['record_1']->get_databox(); - media_Permalink_Adapter::create(self::$DI['app'], $databox, self::$DI['record_1']->get_subdef('document')); + media_Permalink_Adapter::create(self::$DI['app'], self::$DI['record_1']->get_databox(), self::$DI['record_1']->get_subdef('document')); } } diff --git a/tests/classes/media/media_subdefTest.php b/tests/classes/media/media_subdefTest.php index 99c8d17948..50180b3bbb 100644 --- a/tests/classes/media/media_subdefTest.php +++ b/tests/classes/media/media_subdefTest.php @@ -8,10 +8,6 @@ class media_subdefTest extends \PhraseanetTestCase * @var \media_subdef */ private static $objectPresent; - /** - * @var \media_subdef - */ - private static $storyPresent; /** * @var \media_subdef @@ -46,10 +42,13 @@ class media_subdefTest extends \PhraseanetTestCase } } - $story = \record_adapter::createStory(self::$DI['app'], self::$DI['collection']); - self::$objectNotPresent->remove_file(); - self::$storyPresent = $story->get_subdef('thumbnail'); + } + + public static function tearDownAfterClass() + { + self::$objectPresent = self::$objectNotPresent = self::$recordonbleu = null; + parent::tearDownAfterClass(); } /** @@ -66,7 +65,7 @@ class media_subdefTest extends \PhraseanetTestCase */ public function testStoryIsNotPhysicallyPresent() { - $this->assertFalse(self::$storyPresent->is_physically_present()); + $this->assertFalse(self::$DI['record_story_3']->get_subdef('thumbnail')->is_physically_present()); } /** diff --git a/tests/classes/record/adapterTest.php b/tests/classes/record/adapterTest.php index 52e6bfa7b5..62e7b76b10 100644 --- a/tests/classes/record/adapterTest.php +++ b/tests/classes/record/adapterTest.php @@ -2,21 +2,12 @@ class record_adapterTest extends \PhraseanetAuthenticatedTestCase { - /** - * @var record_adapter - */ - private static $grouping; - private static $initialized; private static $thumbtitled = false; public function setUp() { parent::setUp(); - if (self::$initialized) { - return; - } - /** * Reset thumbtitle in order to have consistent tests (testGet_title) */ @@ -30,6 +21,12 @@ class record_adapterTest extends \PhraseanetAuthenticatedTestCase } } + public static function tearDownAfterClass() + { + self::$thumbtitled = null; + parent::tearDownAfterClass(); + } + /** * Check whether a record is delete from order_elements when * record::delete is call