/** * @covers Alchemy\Phrasea\Border\Checker\Sha256::check */ public function testCheck() { $session = self::$DI['app']['EM']->find('Phraseanet:LazaretSession', 1); self::$DI['app']['border-manager']->process($session, File::buildFromPathfile($this->media->getFile()->getPathname(), self::$DI['collection'], self::$DI['app']), null, Manager::FORCE_RECORD); $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', ['getSha256'], [self::$DI['app'], $this->media, self::$DI['collection']]); $mock->expects($this->once())->method('getSha256')->will($this->returnValue('7fad283de349b903c850548cda65cf2d86d24c4e3856cdc2b97e47430494b8c8')); $response = $this->object->check(self::$DI['app']['EM'], $mock); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Checker\\Response', $response); $this->assertFalse($response->isOk()); }
protected function getQuarantineItem() { $lazaretSession = new LazaretSession(); self::$DI['app']['orm.em']->persist($lazaretSession); $quarantineItem = null; $callback = function ($element, $visa, $code) use(&$quarantineItem) { $quarantineItem = $element; }; $tmpname = tempnam(sys_get_temp_dir(), 'test_quarantine'); copy(__DIR__ . '/../../../../files/iphone_pic.jpg', $tmpname); $file = File::buildFromPathfile($tmpname, self::$DI['collection'], self::$DI['app']); self::$DI['app']['border-manager']->process($lazaretSession, $file, $callback, Manager::FORCE_LAZARET); return $quarantineItem; }
private function insertLazaretFiles(EntityManager $em, \Pimple $DI) { $session = new LazaretSession(); $session->setUser($DI['user']); $em->persist($session); $em->flush(); $file = File::buildFromPathfile($this->container['root.path'] . '/tests/files/cestlafete.jpg', $DI['coll'], $this->container); $callback = function ($element) use($DI) { $DI['lazaret_1'] = $element; }; $this->container['border-manager']->process($session, $file, $callback, Manager::FORCE_LAZARET); }
/** * @covers Alchemy\Phrasea\Border\Manager::getVisa */ public function testGetVisa() { $records = []; $postProcessRecord = function ($record) use(&$records) { $records[] = $record; }; self::$DI['app']['phraseanet.SE'] = $this->createSearchEngineMock(); $visa = $this->object->getVisa(File::buildFromPathfile(self::$file1, self::$DI['collection'], self::$DI['app'])); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); $this->assertTrue($visa->isValid()); $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$DI['collection'], self::$DI['app']), $postProcessRecord); $visa = $this->object->getVisa(File::buildFromPathfile(self::$file1, self::$DI['collection'], self::$DI['app'])); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); $this->assertTrue($visa->isValid()); $this->object->registerChecker(new Sha256(self::$DI['app'])); $visa = $this->object->getVisa(File::buildFromPathfile(self::$file1, self::$DI['collection'], self::$DI['app'])); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); $this->assertFalse($visa->isValid()); foreach ($records as $record) { if ($record instanceof \record_adapter) { $record->delete(); } } }
public function testSearchWithRecords() { $this->authenticate(self::$DI['app']); $record = \record_adapter::createFromFile(BorderFile::buildFromPathfile(__DIR__ . '/../../../files/cestlafete.jpg', self::$DI['collection'], self::$DI['app']), self::$DI['app']); $request = new Request(['search_type' => 0], [], [], [], [], ['HTTP_Accept' => 'application/json']); $result = $this->object->search($request); $this->assertEquals(200, $result->get_http_code()); $this->assertEquals('application/json', $result->get_content_type()); $this->assertTrue(is_array(json_decode($result->format(), true))); $data = json_decode($result->format(), true); $this->assertArrayHasKey('records', $data['response']['results']); $this->assertArrayHasKey('stories', $data['response']['results']); $found = false; foreach ($data['response']['results']['records'] as $retRecord) { if ($retRecord['record_id'] == $record->get_record_id() && $retRecord['databox_id'] == $record->get_sbas_id()) { $found = true; break; } } if (!$found) { $this->fail('unable to find the record back'); } }
/** * Add an element to phraseanet * * @param Application $app A Silex application * @param Request $request The current request * @param int $file_id A lazaret element id * * parameters : 'bas_id' int (mandatory) : The id of the destination collection * 'keep_attributes' boolean (optional) : Keep all attributes attached to the lazaret element * 'attributes' array (optional) : Attributes id's to attach to the lazaret element * * @return Response */ public function addElement(Application $app, Request $request, $file_id) { $ret = ['success' => false, 'message' => '', 'result' => []]; //Optional parameter $keepAttributes = !!$request->request->get('keep_attributes', false); $attributesToKeep = $request->request->get('attributes', []); //Mandatory parameter if (null === $request->request->get('bas_id')) { $ret['message'] = $app->trans('You must give a destination collection'); return $app->json($ret); } $lazaretFile = $app['EM']->find('Phraseanet:LazaretFile', $file_id); /* @var $lazaretFile LazaretFile */ if (null === $lazaretFile) { $ret['message'] = $app->trans('File is not present in quarantine anymore, please refresh'); return $app->json($ret); } $lazaretFileName = $app['root.path'] . '/tmp/lazaret/' . $lazaretFile->getFilename(); $lazaretThumbFileName = $app['root.path'] . '/tmp/lazaret/' . $lazaretFile->getThumbFilename(); try { $borderFile = Border\File::buildFromPathfile($lazaretFileName, $lazaretFile->getCollection($app), $app, $lazaretFile->getOriginalName()); $record = null; /* @var $record \record_adapter */ //Post record creation $callBack = function ($element, $visa, $code) use(&$record) { $record = $element; }; //Force creation record $app['border-manager']->process($lazaretFile->getSession(), $borderFile, $callBack, Border\Manager::FORCE_RECORD); $app['phraseanet.SE']->addRecord($record); if ($keepAttributes) { //add attribute $metaFields = new Border\MetaFieldsBag(); $metadataBag = new Border\MetadataBag(); foreach ($lazaretFile->getAttributes() as $attr) { //Check which ones to keep if (!!count($attributesToKeep)) { if (!in_array($attr->getId(), $attributesToKeep)) { continue; } } try { $attribute = Border\Attribute\Factory::getFileAttribute($app, $attr->getName(), $attr->getValue()); } catch (\InvalidArgumentException $e) { continue; } /* @var $attribute AttributeInterface */ switch ($attribute->getName()) { case AttributeInterface::NAME_METADATA: $value = $attribute->getValue(); $metadataBag->set($value->getTag()->getTagname(), new \PHPExiftool\Driver\Metadata\Metadata($value->getTag(), $value->getValue())); break; case AttributeInterface::NAME_STORY: $attribute->getValue()->appendChild($record); break; case AttributeInterface::NAME_STATUS: $record->set_binary_status($attribute->getValue()); break; case AttributeInterface::NAME_METAFIELD: $metaFields->set($attribute->getField()->get_name(), $attribute->getValue()); break; } } $datas = $metadataBag->toMetadataArray($record->get_databox()->get_meta_structure()); $record->set_metadatas($datas); $fields = $metaFields->toMetadataArray($record->get_databox()->get_meta_structure()); $record->set_metadatas($fields); } //Delete lazaret file $app['EM']->remove($lazaretFile); $app['EM']->flush(); $ret['success'] = true; } catch (\Exception $e) { $ret['message'] = $app->trans('An error occured'); } try { $app['filesystem']->remove([$lazaretFileName, $lazaretThumbFileName]); } catch (IOException $e) { } return $app->json($ret); }
/** * @covers Alchemy\Phrasea\Border\File::buildFromPathfile * @expectedException \InvalidArgumentException */ public function testBuildFromWrongPathfile() { File::buildFromPathfile('unexistent.file', self::$DI['collection'], self::$DI['app']); }
/** * Add an element to phraseanet * * @param Request $request The current request * @param int $file_id A lazaret element id * * parameters : 'bas_id' int (mandatory) : The id of the destination collection * 'keep_attributes' boolean (optional) : Keep all attributes attached to the lazaret element * 'attributes' array (optional) : Attributes id's to attach to the lazaret element * * @return Response */ public function addElement(Request $request, $file_id) { $ret = ['success' => false, 'message' => '', 'result' => []]; //Optional parameter $keepAttributes = !!$request->request->get('keep_attributes', false); $attributesToKeep = $request->request->get('attributes', []); //Mandatory parameter if (null === $request->request->get('bas_id')) { $ret['message'] = $this->app->trans('You must give a destination collection'); return $this->app->json($ret); } /* @var LazaretFile $lazaretFile */ $lazaretFile = $this->getLazaretFileRepository()->find($file_id); if (null === $lazaretFile) { $ret['message'] = $this->app->trans('File is not present in quarantine anymore, please refresh'); return $this->app->json($ret); } $path = $this->app['tmp.lazaret.path']; $lazaretFileName = $path . '/' . $lazaretFile->getFilename(); $lazaretThumbFileName = $path . '/' . $lazaretFile->getThumbFilename(); try { $borderFile = Border\File::buildFromPathfile($lazaretFileName, $lazaretFile->getCollection($this->app), $this->app, $lazaretFile->getOriginalName()); //Post record creation /** @var \record_adapter $record */ $record = null; $callBack = function ($element) use(&$record) { $record = $element; }; //Force creation record $this->getBorderManager()->process($lazaretFile->getSession(), $borderFile, $callBack, Border\Manager::FORCE_RECORD); if ($keepAttributes) { //add attribute $metaFields = new Border\MetaFieldsBag(); $metadataBag = new Border\MetadataBag(); foreach ($lazaretFile->getAttributes() as $attr) { //Check which ones to keep if (!!count($attributesToKeep)) { if (!in_array($attr->getId(), $attributesToKeep)) { continue; } } try { $attribute = Border\Attribute\Factory::getFileAttribute($this->app, $attr->getName(), $attr->getValue()); } catch (\InvalidArgumentException $e) { continue; } switch ($attribute->getName()) { case AttributeInterface::NAME_METADATA: /** @var Metadata $value */ $value = $attribute->getValue(); $metadataBag->set($value->getTag()->getTagname(), new Metadata($value->getTag(), $value->getValue())); break; case AttributeInterface::NAME_STORY: /** @var \record_adapter $value */ $value = $attribute->getValue(); $value->appendChild($record); break; case AttributeInterface::NAME_STATUS: $record->set_binary_status($attribute->getValue()); break; case AttributeInterface::NAME_METAFIELD: /** @var Border\Attribute\MetaField $attribute */ $metaFields->set($attribute->getField()->get_name(), $attribute->getValue()); break; } } $data = $metadataBag->toMetadataArray($record->get_databox()->get_meta_structure()); $record->set_metadatas($data); $fields = $metaFields->toMetadataArray($record->get_databox()->get_meta_structure()); $record->set_metadatas($fields); } //Delete lazaret file $manager = $this->getEntityManager(); $manager->remove($lazaretFile); $manager->flush(); $ret['success'] = true; } catch (\Exception $e) { $ret['message'] = $this->app->trans('An error occured'); } try { $this->getFilesystem()->remove([$lazaretFileName, $lazaretThumbFileName]); } catch (IOException $e) { } return $this->app->json($ret); }