Last active
August 29, 2015 13:57
-
-
Save pumatertion/9526873 to your computer and use it in GitHub Desktop.
Testing RestUploads with FunctionalTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BLEICKER\ArtManager\Rest\Tests\Functional\Resource\ResourceManager: | |
properties: | |
statusCache: | |
object: | |
factoryObjectName: TYPO3\Flow\Cache\CacheManager | |
factoryMethodName: getCache | |
arguments: | |
1: | |
value: Flow_Resource_Status | |
TYPO3\Flow\Resource\ResourceTypeConverter: | |
properties: | |
resourceManager: | |
object: 'BLEICKER\ArtManager\Rest\Tests\Functional\Resource\ResourceManager' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* * | |
* This script belongs to the TYPO3 Flow framework. * | |
* * | |
* It is free software; you can redistribute it and/or modify it under * | |
* the terms of the GNU Lesser General Public License, either version 3 * | |
* of the License, or (at your option) any later version. * | |
* * | |
* The TYPO3 project - inspiring people to share! * | |
* */ | |
namespace BLEICKER\ArtManager\Rest\Tests\Functional\Resource; | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Exception; | |
/** | |
* Class ResourceManager | |
* Replacement for testing upload behaviour without failing is_uploaded_file, move_uploaded_file | |
* | |
* @package BLEICKER\ArtManager\Rest\Tests\Functional\Resource | |
* @Flow\Scope("singleton") | |
*/ | |
class ResourceManager extends \TYPO3\Flow\Resource\ResourceManager { | |
/** | |
* @return void | |
*/ | |
public function initializeObject(){ | |
$this->initialize(); | |
} | |
/** | |
* This method is calles somewhere from outsite. | |
* Whe have to proof the calling of this method by introducing initializeObject() wich calls initialize() | |
* | |
* @return void | |
*/ | |
public function initialize() { | |
parent::initialize(); | |
$this->setUp(); | |
} | |
/** | |
* @return void | |
*/ | |
public static function setUp() { | |
$persistentResourcesStorageBaseUri = FLOW_PATH_DATA . 'Persistent/TestingResources/'; | |
\TYPO3\Flow\Utility\Files::createDirectoryRecursively($persistentResourcesStorageBaseUri); | |
} | |
/** | |
* @return void | |
*/ | |
public static function tearDown(){ | |
$persistentResourcesStorageBaseUri = FLOW_PATH_DATA . 'Persistent/TestingResources/'; | |
\TYPO3\Flow\Utility\Files::removeDirectoryRecursively($persistentResourcesStorageBaseUri); | |
} | |
/** | |
* @param array $uploadInfo | |
* @return \TYPO3\Flow\Resource\Resource | |
* @throws \TYPO3\Flow\Exception | |
*/ | |
public function importUploadedResource(array $uploadInfo) { | |
$pathInfo = pathinfo($uploadInfo['name']); | |
if (isset($pathInfo['extension']) && substr(strtolower($pathInfo['extension']), -3, 3) === 'php') { | |
throw new Exception('php file uploads not allowed', 1394715100); | |
} | |
$temporaryTargetPathAndFilename = $uploadInfo['tmp_name']; | |
if (!self::is_uploaded_file($temporaryTargetPathAndFilename)) { | |
throw new Exception($temporaryTargetPathAndFilename . ' does not exists', 1394715101); | |
} | |
$openBasedirEnabled = (boolean)ini_get('open_basedir'); | |
if ($openBasedirEnabled === TRUE) { | |
// Move uploaded file to a readable folder before trying to read sha1 value of file | |
$newTemporaryTargetPathAndFilename = $this->persistentResourcesStorageBaseUri . uniqid(); | |
if (self::move_uploaded_file($temporaryTargetPathAndFilename, $newTemporaryTargetPathAndFilename) === FALSE) { | |
throw new Exception('Moving "' . $temporaryTargetPathAndFilename . '" to "' . $newTemporaryTargetPathAndFilename . '" failed', 1394715102); | |
} | |
$hash = sha1_file($newTemporaryTargetPathAndFilename); | |
$finalTargetPathAndFilename = $this->persistentResourcesStorageBaseUri . $hash; | |
if (rename($newTemporaryTargetPathAndFilename, $finalTargetPathAndFilename) === FALSE) { | |
throw new Exception('Renaming "' . $newTemporaryTargetPathAndFilename . '" to "' . $finalTargetPathAndFilename . '" failed', 1394715103); | |
} | |
} else { | |
$hash = sha1_file($temporaryTargetPathAndFilename); | |
$finalTargetPathAndFilename = $this->persistentResourcesStorageBaseUri . $hash; | |
if (self::move_uploaded_file($temporaryTargetPathAndFilename, $finalTargetPathAndFilename) === FALSE) { | |
throw new Exception('Moving "' . $temporaryTargetPathAndFilename . '" to "' . $finalTargetPathAndFilename . '" failed', 1394715104); | |
} | |
} | |
$this->fixFilePermissions($finalTargetPathAndFilename); | |
$resource = new \TYPO3\Flow\Resource\Resource(); | |
$resource->setFilename($pathInfo['basename']); | |
$resourcePointer = $this->getResourcePointerForHash($hash); | |
$resource->setResourcePointer($resourcePointer); | |
$this->attachImportedResource($resource); | |
return $resource; | |
} | |
/** | |
* @param string $pathAndFilename | |
* @return boolean | |
*/ | |
public static function is_uploaded_file($pathAndFilename) { | |
return is_file($pathAndFilename); | |
} | |
/** | |
* @param string $pathAndFilename | |
* @param string $destinationPathAndFilename | |
* @return boolean | |
*/ | |
public static function move_uploaded_file($pathAndFilename, $destinationPathAndFilename) { | |
return copy($pathAndFilename, $destinationPathAndFilename); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sets up test requirements depending on the enabled tests. | |
* | |
* @return void | |
*/ | |
public function setUp() { | |
parent::setUp(); | |
\BLEICKER\ArtManager\Rest\Tests\Functional\Resource\ResourceManager::setUp(); | |
} | |
/** | |
* Calls Parent Tear Down and also invokes removement of directory holding test uploads resources | |
*/ | |
public function tearDown() { | |
parent::tearDown(); | |
\BLEICKER\ArtManager\Rest\Tests\Functional\Resource\ResourceManager::tearDown(); | |
} | |
/** | |
* @test | |
*/ | |
public function createMediaIncludingAResourceWorks() { | |
$filePath = 'f.e. /foo/bar/baz/UploadTest.jpg' | |
$uri = new Uri('http://localhost/rest/media); | |
$post = array(); | |
$files = array( | |
'resource' => array( | |
'name' => array('resource' => 'UploadTest.jpg'), | |
'type' => array('resource' => 'image/jpg'), | |
'tmp_name' => array('resource' => $filePath), | |
'error' => array('resource' => 0), | |
'size' => array('resource' => filesize($filePath)) | |
) | |
); | |
$request = Request::create($uri, 'POST', $post, $files); | |
$response = $this->browser->sendRequest($request); | |
$responseData = json_decode($response->getContent(), TRUE); | |
$this->assertEquals(200, $response->getStatusCode()); | |
// additional tests f.e. is resource present in json response | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment