2020-01-13 10:28:26 +13:00
< ? php
namespace Tests\E2E\Services\Storage ;
use CURLFile ;
use Tests\E2E\Client ;
trait StorageBase
{
2021-06-21 19:06:28 +12:00
public function testCreateBucketFile () : array
2021-06-17 22:10:58 +12:00
{
/**
* Test for SUCCESS
*/
$bucket = $this -> client -> call ( Client :: METHOD_POST , '/storage/buckets' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2021-06-21 19:06:28 +12:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
2021-06-18 20:38:36 +12:00
], $this -> getHeaders ()), [
2021-06-17 22:10:58 +12:00
'name' => 'Test Bucket' ,
2021-06-21 19:06:28 +12:00
'maximumFileSize' => 2000000 , //2MB
2021-06-23 00:12:46 +12:00
'allowedFileExtensions' => [ " jpg " , " png " ],
2021-06-18 20:38:36 +12:00
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
2021-06-17 22:10:58 +12:00
]);
$this -> assertEquals ( 201 , $bucket [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $bucket [ 'body' ][ '$id' ]);
2021-06-21 19:06:28 +12:00
2021-06-17 22:10:58 +12:00
$bucketId = $bucket [ 'body' ][ '$id' ];
2021-06-21 19:06:28 +12:00
$file = $this -> client -> call ( Client :: METHOD_POST , '/storage/buckets/' . $bucketId . '/files' , array_merge ([
2021-06-17 22:10:58 +12:00
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'file' => new CURLFile ( realpath ( __DIR__ . '/../../../resources/logo.png' ), 'image/png' , 'logo.png' ),
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
]);
2021-06-18 20:38:36 +12:00
$this -> assertEquals ( 201 , $file [ 'headers' ][ 'status-code' ]);
2021-06-17 22:10:58 +12:00
$this -> assertNotEmpty ( $file [ 'body' ][ '$id' ]);
$this -> assertIsInt ( $file [ 'body' ][ 'dateCreated' ]);
$this -> assertEquals ( 'logo.png' , $file [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'image/png' , $file [ 'body' ][ 'mimeType' ]);
$this -> assertEquals ( 47218 , $file [ 'body' ][ 'sizeOriginal' ]);
2021-06-21 20:23:08 +12:00
$this -> assertTrue ( md5_file ( realpath ( __DIR__ . '/../../../resources/logo.png' )) != $file [ 'body' ][ 'signature' ]); // should validate that the file is encrypted
/**
* Test for Large File above 20 MB
* This should also validate the test for when Bucket encryption
* is disabled as we are using same test
*/
$bucket2 = $this -> client -> call ( Client :: METHOD_POST , '/storage/buckets' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], $this -> getHeaders ()), [
'name' => 'Test Bucket 2' ,
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
]);
$this -> assertEquals ( 201 , $bucket2 [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $bucket2 [ 'body' ][ '$id' ]);
$file2 = $this -> client -> call ( Client :: METHOD_POST , '/storage/buckets/' . $bucket2 [ 'body' ][ '$id' ] . '/files' , array_merge ([
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'file' => new CURLFile ( realpath ( __DIR__ . '/../../../resources/disk-a/large-file.mp4' ), 'video/mp4' , 'large-file.mp4' ),
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
]);
$this -> assertEquals ( 201 , $file2 [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $file2 [ 'body' ][ '$id' ]);
$this -> assertIsInt ( $file2 [ 'body' ][ 'dateCreated' ]);
$this -> assertEquals ( 'large-file.mp4' , $file2 [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'video/mp4' , $file2 [ 'body' ][ 'mimeType' ]);
$this -> assertEquals ( 23660615 , $file2 [ 'body' ][ 'sizeOriginal' ]);
$this -> assertEquals ( md5_file ( realpath ( __DIR__ . '/../../../resources/disk-a/large-file.mp4' )), $file2 [ 'body' ][ 'signature' ]); // should validate that the file is not encrypted
2021-06-17 22:10:58 +12:00
/**
2021-06-21 18:58:06 +12:00
* Test for FAILURE unknown Bucket
2021-06-17 22:10:58 +12:00
*/
2021-06-21 18:58:06 +12:00
$res = $this -> client -> call ( Client :: METHOD_POST , '/storage/buckets/empty/files' , array_merge ([
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'file' => new CURLFile ( realpath ( __DIR__ . '/../../../resources/logo.png' ), 'image/png' , 'logo.png' ),
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
]);
$this -> assertEquals ( 404 , $res [ 'headers' ][ 'status-code' ]);
2021-06-21 19:06:28 +12:00
/**
2021-06-23 00:12:46 +12:00
* Test for FAILURE file above bucket ' s file size limit
2021-06-21 19:06:28 +12:00
*/
$res = $this -> client -> call ( Client :: METHOD_POST , '/storage/buckets/' . $bucketId . '/files' , array_merge ([
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'file' => new CURLFile ( realpath ( __DIR__ . '/../../../resources/disk-b/kitten-1.png' ), 'image/png' , 'kitten-1.png' ),
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
]);
$this -> assertEquals ( 400 , $res [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'File size not allowed' , $res [ 'body' ][ 'message' ]);
/**
2021-06-23 00:12:46 +12:00
* Test for FAILURE unsupported bucket file extension
2021-06-21 19:06:28 +12:00
*/
2021-06-23 00:12:46 +12:00
$res = $this -> client -> call ( Client :: METHOD_POST , '/storage/buckets/' . $bucketId . '/files' , array_merge ([
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'file' => new CURLFile ( realpath ( __DIR__ . '/../../../resources/disk-a/kitten-3.gif' ), 'image/gif' , 'kitten-3.gif' ),
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
]);
$this -> assertEquals ( 400 , $res [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'File extension not allowed' , $res [ 'body' ][ 'message' ]);
2021-06-22 20:37:04 +12:00
return [ 'bucketId' => $bucketId , 'fileId' => $file [ 'body' ][ '$id' ], 'largeFileId' => $file2 [ 'body' ][ '$id' ], 'largeBucketId' => $bucket2 [ 'body' ][ '$id' ]];
2021-06-18 21:24:16 +12:00
}
/**
* @ depends testCreateBucketFile
*/
2021-06-21 19:06:28 +12:00
public function testListBucketFiles ( array $data ) : array
2021-06-18 21:24:16 +12:00
{
/**
* Test for SUCCESS
*/
$files = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/' . $data [ 'bucketId' ] . '/files' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $files [ 'headers' ][ 'status-code' ]);
$this -> assertGreaterThan ( 0 , $files [ 'body' ][ 'sum' ]);
$this -> assertGreaterThan ( 0 , count ( $files [ 'body' ][ 'files' ]));
/**
2021-06-21 18:58:06 +12:00
* Test for FAILURE unknown Bucket
2021-06-18 21:24:16 +12:00
*/
2021-06-21 19:06:28 +12:00
2021-06-21 18:58:06 +12:00
$files = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/empty/files' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 404 , $files [ 'headers' ][ 'status-code' ]);
2021-06-18 21:24:16 +12:00
return $data ;
2021-06-17 22:10:58 +12:00
}
2021-06-18 21:33:00 +12:00
/**
* @ depends testCreateBucketFile
*/
2021-06-21 19:06:28 +12:00
public function testGetBucketFile ( array $data ) : array
2021-06-18 21:33:00 +12:00
{
2021-06-21 18:58:06 +12:00
$bucketId = $data [ 'bucketId' ];
2021-06-18 21:33:00 +12:00
/**
* Test for SUCCESS
*/
2021-06-21 18:58:06 +12:00
$file1 = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/' . $bucketId . '/files/' . $data [ 'fileId' ], array_merge ([
2021-06-18 21:33:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $file1 [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $file1 [ 'body' ][ '$id' ]);
$this -> assertIsInt ( $file1 [ 'body' ][ 'dateCreated' ]);
$this -> assertEquals ( 'logo.png' , $file1 [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'image/png' , $file1 [ 'body' ][ 'mimeType' ]);
$this -> assertEquals ( 47218 , $file1 [ 'body' ][ 'sizeOriginal' ]);
//$this->assertEquals(54944, $file1['body']['sizeActual']);
//$this->assertEquals('gzip', $file1['body']['algorithm']);
//$this->assertEquals('1', $file1['body']['fileOpenSSLVersion']);
//$this->assertEquals('aes-128-gcm', $file1['body']['fileOpenSSLCipher']);
//$this->assertNotEmpty($file1['body']['fileOpenSSLTag']);
//$this->assertNotEmpty($file1['body']['fileOpenSSLIV']);
$this -> assertIsArray ( $file1 [ 'body' ][ '$read' ]);
$this -> assertIsArray ( $file1 [ 'body' ][ '$write' ]);
$this -> assertCount ( 1 , $file1 [ 'body' ][ '$read' ]);
$this -> assertCount ( 1 , $file1 [ 'body' ][ '$write' ]);
2021-06-21 18:58:06 +12:00
$file2 = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/' . $bucketId . '/files/' . $data [ 'fileId' ] . '/preview' , array_merge ([
2021-06-18 21:33:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $file2 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'image/png' , $file2 [ 'headers' ][ 'content-type' ]);
$this -> assertNotEmpty ( $file2 [ 'body' ]);
2021-06-21 19:06:28 +12:00
2021-06-18 21:33:00 +12:00
//new image preview features
2021-06-21 18:58:06 +12:00
$file3 = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/' . $bucketId . '/files/' . $data [ 'fileId' ] . '/preview' , array_merge ([
2021-06-18 21:33:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'width' => 300 ,
'height' => 100 ,
'borderRadius' => '50' ,
'opacity' => '0.5' ,
'output' => 'png' ,
'rotation' => '45' ,
]);
$this -> assertEquals ( 200 , $file3 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'image/png' , $file3 [ 'headers' ][ 'content-type' ]);
$this -> assertNotEmpty ( $file3 [ 'body' ]);
$image = new \Imagick ();
$image -> readImageBlob ( $file3 [ 'body' ]);
$original = new \Imagick ( __DIR__ . '/../../../resources/logo-after.png' );
$this -> assertEquals ( $image -> getImageWidth (), $original -> getImageWidth ());
$this -> assertEquals ( $image -> getImageHeight (), $original -> getImageHeight ());
$this -> assertEquals ( 'PNG' , $image -> getImageFormat ());
2021-06-21 18:58:06 +12:00
$file4 = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/' . $bucketId . '/files/' . $data [ 'fileId' ] . '/preview' , array_merge ([
2021-06-18 21:33:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'width' => 200 ,
'height' => 80 ,
'borderWidth' => '5' ,
'borderColor' => 'ff0000' ,
'output' => 'jpg' ,
]);
2021-06-21 19:06:28 +12:00
2021-06-18 21:33:00 +12:00
$this -> assertEquals ( 200 , $file4 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'image/jpeg' , $file4 [ 'headers' ][ 'content-type' ]);
$this -> assertNotEmpty ( $file4 [ 'body' ]);
2021-06-21 19:06:28 +12:00
2021-06-18 21:33:00 +12:00
$image = new \Imagick ();
$image -> readImageBlob ( $file4 [ 'body' ]);
$original = new \Imagick ( __DIR__ . '/../../../resources/logo-after.jpg' );
$this -> assertEquals ( $image -> getImageWidth (), $original -> getImageWidth ());
$this -> assertEquals ( $image -> getImageHeight (), $original -> getImageHeight ());
$this -> assertEquals ( 'JPEG' , $image -> getImageFormat ());
2021-06-21 18:58:06 +12:00
$file5 = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/' . $bucketId . '/files/' . $data [ 'fileId' ] . '/download' , array_merge ([
2021-06-18 21:33:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $file5 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'attachment; filename="logo.png"' , $file5 [ 'headers' ][ 'content-disposition' ]);
$this -> assertEquals ( 'image/png' , $file5 [ 'headers' ][ 'content-type' ]);
$this -> assertNotEmpty ( $file5 [ 'body' ]);
2021-06-21 18:58:06 +12:00
$file6 = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/' . $bucketId . '/files/' . $data [ 'fileId' ] . '/view' , array_merge ([
2021-06-18 21:33:00 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $file6 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'image/png' , $file6 [ 'headers' ][ 'content-type' ]);
$this -> assertNotEmpty ( $file6 [ 'body' ]);
2021-06-22 20:37:04 +12:00
/**
* Test large files decompress successfully
*/
$file7 = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/' . $data [ 'largeBucketId' ] . '/files/' . $data [ 'largeFileId' ] . '/download' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$fileData = $file7 [ 'body' ];
$this -> assertEquals ( 200 , $file7 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'attachment; filename="large-file.mp4"' , $file7 [ 'headers' ][ 'content-disposition' ]);
$this -> assertEquals ( 'video/mp4' , $file7 [ 'headers' ][ 'content-type' ]);
$this -> assertNotEmpty ( $fileData );
$this -> assertEquals ( md5_file ( realpath ( __DIR__ . '/../../../resources/disk-a/large-file.mp4' )), md5 ( $fileData )); // validate the file is downloaded correctly
2021-06-18 21:33:00 +12:00
/**
2021-06-21 18:58:06 +12:00
* Test for FAILURE unknown Bucket
2021-06-18 21:33:00 +12:00
*/
2021-06-22 20:37:04 +12:00
$file8 = $this -> client -> call ( Client :: METHOD_GET , '/storage/buckets/empty/files/' . $data [ 'fileId' ], array_merge ([
2021-06-21 18:58:06 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2021-06-22 20:37:04 +12:00
$this -> assertEquals ( 404 , $file8 [ 'headers' ][ 'status-code' ]);
2021-06-21 18:58:06 +12:00
2021-06-18 21:33:00 +12:00
return $data ;
}
2021-06-20 23:20:35 +12:00
/**
* @ depends testCreateBucketFile
*/
2021-06-21 19:06:28 +12:00
public function testUpdateBucketFile ( array $data ) : array
2021-06-20 23:20:35 +12:00
{
/**
* Test for SUCCESS
*/
$file = $this -> client -> call ( Client :: METHOD_PUT , '/storage/buckets/' . $data [ 'bucketId' ] . '/files/' . $data [ 'fileId' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-06-21 19:06:28 +12:00
'read' => [ 'role:all' , 'user:x' ],
2021-06-20 23:20:35 +12:00
'write' => [ 'role:all' , 'user:x' ],
]);
$this -> assertEquals ( 200 , $file [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $file [ 'body' ][ '$id' ]);
$this -> assertIsInt ( $file [ 'body' ][ 'dateCreated' ]);
$this -> assertEquals ( 'logo.png' , $file [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'image/png' , $file [ 'body' ][ 'mimeType' ]);
$this -> assertEquals ( 47218 , $file [ 'body' ][ 'sizeOriginal' ]);
//$this->assertEquals(54944, $file['body']['sizeActual']);
//$this->assertEquals('gzip', $file['body']['algorithm']);
//$this->assertEquals('1', $file['body']['fileOpenSSLVersion']);
//$this->assertEquals('aes-128-gcm', $file['body']['fileOpenSSLCipher']);
//$this->assertNotEmpty($file['body']['fileOpenSSLTag']);
//$this->assertNotEmpty($file['body']['fileOpenSSLIV']);
$this -> assertIsArray ( $file [ 'body' ][ '$read' ]);
$this -> assertIsArray ( $file [ 'body' ][ '$write' ]);
$this -> assertCount ( 2 , $file [ 'body' ][ '$read' ]);
$this -> assertCount ( 2 , $file [ 'body' ][ '$write' ]);
2021-06-21 19:06:28 +12:00
2021-06-20 23:20:35 +12:00
/**
2021-06-21 18:58:06 +12:00
* Test for FAILURE unknown Bucket
2021-06-20 23:20:35 +12:00
*/
2021-06-21 18:58:06 +12:00
$file = $this -> client -> call ( Client :: METHOD_PUT , '/storage/buckets/empty/files/' . $data [ 'fileId' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-06-21 19:06:28 +12:00
'read' => [ 'role:all' , 'user:x' ],
2021-06-21 18:58:06 +12:00
'write' => [ 'role:all' , 'user:x' ],
]);
2021-06-21 19:06:28 +12:00
2021-06-21 18:58:06 +12:00
$this -> assertEquals ( 404 , $file [ 'headers' ][ 'status-code' ]);
2021-06-21 19:06:28 +12:00
2021-06-20 23:20:35 +12:00
return $data ;
}
/**
* @ depends testUpdateBucketFile
*/
2021-06-21 19:06:28 +12:00
public function testDeleteBucketFile ( array $data ) : array
2021-06-20 23:20:35 +12:00
{
/**
* Test for SUCCESS
*/
$file = $this -> client -> call ( Client :: METHOD_DELETE , '/storage/buckets/' . $data [ 'bucketId' ] . '/files/' . $data [ 'fileId' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 204 , $file [ 'headers' ][ 'status-code' ]);
$this -> assertEmpty ( $file [ 'body' ]);
$file = $this -> client -> call ( Client :: METHOD_GET , '/storage/files/' . $data [ 'fileId' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 404 , $file [ 'headers' ][ 'status-code' ]);
2021-06-21 19:06:28 +12:00
2021-06-20 23:20:35 +12:00
/**
* Test for FAILURE
*/
2021-06-21 19:06:28 +12:00
2021-06-20 23:20:35 +12:00
return $data ;
}
2021-06-21 19:06:28 +12:00
public function testCreateFile () : array
2020-01-13 10:28:26 +13:00
{
/**
* Test for SUCCESS
*/
$file = $this -> client -> call ( Client :: METHOD_POST , '/storage/files' , array_merge ([
'content-type' => 'multipart/form-data' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 10:28:26 +13:00
], $this -> getHeaders ()), [
2020-01-19 21:05:31 +13:00
'file' => new CURLFile ( realpath ( __DIR__ . '/../../../resources/logo.png' ), 'image/png' , 'logo.png' ),
2021-06-12 06:23:16 +12:00
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
2020-01-13 10:28:26 +13:00
]);
$this -> assertEquals ( $file [ 'headers' ][ 'status-code' ], 201 );
2020-02-17 20:16:11 +13:00
$this -> assertNotEmpty ( $file [ 'body' ][ '$id' ]);
2020-01-19 21:05:31 +13:00
$this -> assertIsInt ( $file [ 'body' ][ 'dateCreated' ]);
$this -> assertEquals ( 'logo.png' , $file [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'image/png' , $file [ 'body' ][ 'mimeType' ]);
$this -> assertEquals ( 47218 , $file [ 'body' ][ 'sizeOriginal' ]);
2020-01-13 10:28:26 +13:00
/**
* Test for FAILURE
*/
2020-02-17 20:16:11 +13:00
return [ 'fileId' => $file [ 'body' ][ '$id' ]];
2020-01-13 10:28:26 +13:00
}
2021-06-21 19:06:28 +12:00
2020-01-13 10:28:26 +13:00
/**
* @ depends testCreateFile
*/
2021-06-21 19:06:28 +12:00
public function testGetFile ( array $data ) : array
2020-01-13 10:28:26 +13:00
{
/**
* Test for SUCCESS
*/
$file1 = $this -> client -> call ( Client :: METHOD_GET , '/storage/files/' . $data [ 'fileId' ], array_merge ([
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 10:28:26 +13:00
], $this -> getHeaders ()));
$this -> assertEquals ( $file1 [ 'headers' ][ 'status-code' ], 200 );
2020-02-17 20:16:11 +13:00
$this -> assertNotEmpty ( $file1 [ 'body' ][ '$id' ]);
2020-01-13 10:28:26 +13:00
$this -> assertIsInt ( $file1 [ 'body' ][ 'dateCreated' ]);
$this -> assertEquals ( 'logo.png' , $file1 [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'image/png' , $file1 [ 'body' ][ 'mimeType' ]);
$this -> assertEquals ( 47218 , $file1 [ 'body' ][ 'sizeOriginal' ]);
//$this->assertEquals(54944, $file1['body']['sizeActual']);
//$this->assertEquals('gzip', $file1['body']['algorithm']);
//$this->assertEquals('1', $file1['body']['fileOpenSSLVersion']);
//$this->assertEquals('aes-128-gcm', $file1['body']['fileOpenSSLCipher']);
//$this->assertNotEmpty($file1['body']['fileOpenSSLTag']);
//$this->assertNotEmpty($file1['body']['fileOpenSSLIV']);
2021-05-04 10:20:40 +12:00
$this -> assertIsArray ( $file1 [ 'body' ][ '$read' ]);
$this -> assertIsArray ( $file1 [ 'body' ][ '$write' ]);
$this -> assertCount ( 1 , $file1 [ 'body' ][ '$read' ]);
$this -> assertCount ( 1 , $file1 [ 'body' ][ '$write' ]);
2020-01-13 10:28:26 +13:00
$file2 = $this -> client -> call ( Client :: METHOD_GET , '/storage/files/' . $data [ 'fileId' ] . '/preview' , array_merge ([
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 10:28:26 +13:00
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $file2 [ 'headers' ][ 'status-code' ]);
2020-07-07 08:08:29 +12:00
$this -> assertEquals ( 'image/png' , $file2 [ 'headers' ][ 'content-type' ]);
2020-01-13 10:28:26 +13:00
$this -> assertNotEmpty ( $file2 [ 'body' ]);
2021-06-21 19:06:28 +12:00
2021-03-26 18:42:39 +13:00
//new image preview features
$file3 = $this -> client -> call ( Client :: METHOD_GET , '/storage/files/' . $data [ 'fileId' ] . '/preview' , array_merge ([
2020-01-13 10:28:26 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2021-03-26 18:42:39 +13:00
], $this -> getHeaders ()), [
'width' => 300 ,
'height' => 100 ,
'borderRadius' => '50' ,
'opacity' => '0.5' ,
'output' => 'png' ,
'rotation' => '45' ,
]);
2020-01-13 10:28:26 +13:00
$this -> assertEquals ( 200 , $file3 [ 'headers' ][ 'status-code' ]);
2020-07-07 08:08:29 +12:00
$this -> assertEquals ( 'image/png' , $file3 [ 'headers' ][ 'content-type' ]);
2020-01-13 10:28:26 +13:00
$this -> assertNotEmpty ( $file3 [ 'body' ]);
2021-03-26 18:42:39 +13:00
$image = new \Imagick ();
$image -> readImageBlob ( $file3 [ 'body' ]);
$original = new \Imagick ( __DIR__ . '/../../../resources/logo-after.png' );
$this -> assertEquals ( $image -> getImageWidth (), $original -> getImageWidth ());
$this -> assertEquals ( $image -> getImageHeight (), $original -> getImageHeight ());
$this -> assertEquals ( 'PNG' , $image -> getImageFormat ());
$file4 = $this -> client -> call ( Client :: METHOD_GET , '/storage/files/' . $data [ 'fileId' ] . '/preview' , array_merge ([
2020-01-13 10:28:26 +13:00
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2021-03-26 18:42:39 +13:00
], $this -> getHeaders ()), [
'width' => 200 ,
'height' => 80 ,
'borderWidth' => '5' ,
'borderColor' => 'ff0000' ,
'output' => 'jpg' ,
]);
2021-06-21 19:06:28 +12:00
2020-01-13 10:28:26 +13:00
$this -> assertEquals ( 200 , $file4 [ 'headers' ][ 'status-code' ]);
2021-03-26 18:42:39 +13:00
$this -> assertEquals ( 'image/jpeg' , $file4 [ 'headers' ][ 'content-type' ]);
2020-01-13 10:28:26 +13:00
$this -> assertNotEmpty ( $file4 [ 'body' ]);
2021-06-21 19:06:28 +12:00
2021-03-26 18:42:39 +13:00
$image = new \Imagick ();
$image -> readImageBlob ( $file4 [ 'body' ]);
$original = new \Imagick ( __DIR__ . '/../../../resources/logo-after.jpg' );
$this -> assertEquals ( $image -> getImageWidth (), $original -> getImageWidth ());
$this -> assertEquals ( $image -> getImageHeight (), $original -> getImageHeight ());
$this -> assertEquals ( 'JPEG' , $image -> getImageFormat ());
$file5 = $this -> client -> call ( Client :: METHOD_GET , '/storage/files/' . $data [ 'fileId' ] . '/download' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $file5 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'attachment; filename="logo.png"' , $file5 [ 'headers' ][ 'content-disposition' ]);
$this -> assertEquals ( 'image/png' , $file5 [ 'headers' ][ 'content-type' ]);
$this -> assertNotEmpty ( $file5 [ 'body' ]);
$file6 = $this -> client -> call ( Client :: METHOD_GET , '/storage/files/' . $data [ 'fileId' ] . '/view' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $file6 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'image/png' , $file6 [ 'headers' ][ 'content-type' ]);
$this -> assertNotEmpty ( $file6 [ 'body' ]);
2020-01-13 10:28:26 +13:00
/**
* Test for FAILURE
*/
return $data ;
}
2021-06-21 19:06:28 +12:00
2020-01-13 10:28:26 +13:00
/**
* @ depends testGetFile
*/
2021-06-21 19:06:28 +12:00
public function testListFiles ( array $data ) : array
2020-01-13 10:28:26 +13:00
{
/**
* Test for SUCCESS
*/
$files = $this -> client -> call ( Client :: METHOD_GET , '/storage/files' , array_merge ([
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 10:28:26 +13:00
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $files [ 'headers' ][ 'status-code' ]);
$this -> assertGreaterThan ( 0 , $files [ 'body' ][ 'sum' ]);
$this -> assertGreaterThan ( 0 , count ( $files [ 'body' ][ 'files' ]));
/**
* Test for FAILURE
*/
2021-06-21 19:06:28 +12:00
2020-01-13 10:28:26 +13:00
return $data ;
}
/**
* @ depends testListFiles
*/
2021-06-21 19:06:28 +12:00
public function testUpdateFile ( array $data ) : array
2020-01-13 10:28:26 +13:00
{
/**
* Test for SUCCESS
*/
$file = $this -> client -> call ( Client :: METHOD_PUT , '/storage/files/' . $data [ 'fileId' ], array_merge ([
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 10:28:26 +13:00
], $this -> getHeaders ()), [
2021-06-12 06:23:16 +12:00
'read' => [ 'role:all' ],
'write' => [ 'role:all' ],
2020-01-13 10:28:26 +13:00
]);
$this -> assertEquals ( 200 , $file [ 'headers' ][ 'status-code' ]);
2020-02-17 20:16:11 +13:00
$this -> assertNotEmpty ( $file [ 'body' ][ '$id' ]);
2020-01-13 10:28:26 +13:00
$this -> assertIsInt ( $file [ 'body' ][ 'dateCreated' ]);
$this -> assertEquals ( 'logo.png' , $file [ 'body' ][ 'name' ]);
$this -> assertEquals ( 'image/png' , $file [ 'body' ][ 'mimeType' ]);
$this -> assertEquals ( 47218 , $file [ 'body' ][ 'sizeOriginal' ]);
//$this->assertEquals(54944, $file['body']['sizeActual']);
//$this->assertEquals('gzip', $file['body']['algorithm']);
//$this->assertEquals('1', $file['body']['fileOpenSSLVersion']);
//$this->assertEquals('aes-128-gcm', $file['body']['fileOpenSSLCipher']);
//$this->assertNotEmpty($file['body']['fileOpenSSLTag']);
//$this->assertNotEmpty($file['body']['fileOpenSSLIV']);
2021-05-04 10:20:40 +12:00
$this -> assertIsArray ( $file [ 'body' ][ '$read' ]);
$this -> assertIsArray ( $file [ 'body' ][ '$write' ]);
$this -> assertCount ( 1 , $file [ 'body' ][ '$read' ]);
$this -> assertCount ( 1 , $file [ 'body' ][ '$write' ]);
2021-06-21 19:06:28 +12:00
2020-01-13 10:28:26 +13:00
/**
* Test for FAILURE
*/
return $data ;
}
/**
* @ depends testUpdateFile
*/
2021-06-21 19:06:28 +12:00
public function testDeleteFile ( array $data ) : array
2020-01-13 10:28:26 +13:00
{
/**
* Test for SUCCESS
*/
$file = $this -> client -> call ( Client :: METHOD_DELETE , '/storage/files/' . $data [ 'fileId' ], array_merge ([
'content-type' => 'application/json' ,
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2020-01-13 10:28:26 +13:00
], $this -> getHeaders ()));
$this -> assertEquals ( 204 , $file [ 'headers' ][ 'status-code' ]);
$this -> assertEmpty ( $file [ 'body' ]);
2020-05-07 00:12:52 +12:00
$file = $this -> client -> call ( Client :: METHOD_GET , '/storage/files/' . $data [ 'fileId' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 404 , $file [ 'headers' ][ 'status-code' ]);
2021-06-21 19:06:28 +12:00
2020-01-13 10:28:26 +13:00
/**
* Test for FAILURE
*/
2021-06-21 19:06:28 +12:00
2020-01-13 10:28:26 +13:00
return $data ;
}
2021-06-21 19:06:28 +12:00
}