Source of file AssetAdminTest.php
Size: 15,453 Bytes - Last Modified: 2021-12-23T10:27:34+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Controller/AssetAdminTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 | <?php namespace SilverStripe\AssetAdmin\Tests\Controller; use SilverStripe\AssetAdmin\Forms\FolderFormFactory; use SilverStripe\AssetAdmin\Controller\AssetAdmin; use SilverStripe\AssetAdmin\Tests\Controller\AssetAdminTest\FileExtension; use SilverStripe\AssetAdmin\Tests\Controller\AssetAdminTest\FolderExtension; use SilverStripe\Assets\File; use SilverStripe\Assets\Folder; use Silverstripe\Assets\Dev\TestAssetStore; use SilverStripe\Assets\Upload_Validator; use SilverStripe\Control\Director; use SilverStripe\Control\Session; use SilverStripe\Dev\FunctionalTest; use SilverStripe\Subsites\Extensions\FolderFormFactoryExtension; use SilverStripe\Versioned\Versioned; use SilverStripe\Security\SecurityToken; /** * Tests {@see AssetAdmin} * @skipUpgrade */ class AssetAdminTest extends FunctionalTest { protected static $fixture_file = '../fixtures.yml'; /** * @var Session */ protected $session = null; protected static $illegal_extensions = [ FolderFormFactory::class => [FolderFormFactoryExtension::class], ]; protected function setUp(): void { parent::setUp(); TestAssetStore::activate('AssetAdminTest'); $this->logInWithPermission('ADMIN'); $this->session = $this->mainSession->session(); File::add_extension(FileExtension::class); Folder::add_extension(FolderExtension::class); // Create a test folders for each of the fixture references foreach (File::get()->filter('ClassName', Folder::class) as $folder) { /** @var Folder $folder */ $folder->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); } // Create a test files for each of the fixture references $content = str_repeat('x', 1000000); foreach (File::get()->exclude('ClassName', Folder::class) as $file) { /** @var File $file */ $file->setFromString($content, $file->generateFilename()); $file->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); } // Override FunctionalTest defaults SecurityToken::enable(); $this->session->set('SecurityID', SecurityToken::inst()->getValue()); // Disable is_uploaded_file() in tests Upload_Validator::config()->set('use_is_uploaded_file', false); } protected function tearDown(): void { File::remove_extension(FileExtension::class); Folder::remove_extension(FolderExtension::class); TestAssetStore::reset(); parent::tearDown(); } public function testApiHistory() { $file = $this->objFromFixture(File::class, 'file1'); $response = Director::test( 'admin/assets/api/history?fileId='. $file->ID, null, $this->session, 'GET' ); $this->assertFalse($response->isError()); $body = json_decode($response->getBody(), true); $this->assertArrayHasKey('summary', $body[0]); $this->assertArrayHasKey('versionid', $body[0]); $this->assertArrayHasKey('summary', $body[0]); // test permission filtering and } public function testItCreatesFile() { $folder1 = $this->objFromFixture(Folder::class, 'folder1'); /** @skipUpgrade */ $fileData = array('Upload' => $this->getUploadFile('Upload', 'testItCreatesFile.txt')); $_FILES = $fileData; $postedData = array_merge( $fileData, [ 'ParentID' => $folder1->ID, 'SecurityID' => SecurityToken::inst()->getValue(), ] ); $response = Director::test( 'admin/assets/api/createFile', $postedData, $this->session, 'POST' ); $this->assertFalse($response->isError()); $responseData = json_decode($response->getBody(), true); $newFile = File::get()->byID($responseData[0]['id']); $this->assertNotNull($newFile); $this->assertEquals($folder1->ID, $newFile->ParentID); $this->assertEquals('testItCreatesFile.txt', $newFile->Name); // Test that duplicate uploads are renamed $response = Director::test( 'admin/assets/api/createFile', $postedData, $this->session, 'POST' ); $this->assertFalse($response->isError()); $responseData = json_decode($response->getBody(), true); $newFile2 = File::get()->byID($responseData[0]['id']); $this->assertNotNull($newFile2); $this->assertEquals($folder1->ID, $newFile2->ParentID); $this->assertNotEquals($newFile->ID, $newFile2->ID); $this->assertEquals('testItCreatesFile-v2.txt', $newFile2->Name); } public function testItRestrictsCreateFileOnCanCreate() { $folder = $this->objFromFixture(Folder::class, 'folder1'); $fileData = array('Upload' => $this->getUploadFile('Upload', 'disallowCanCreate.txt')); $_FILES = $fileData; $response = Director::test( 'admin/assets/api/createFile', array_merge( $fileData, [ 'ParentID' => $folder->ID, 'SecurityID' => SecurityToken::inst()->getValue(), ] ), $this->session, 'POST' ); $this->assertTrue($response->isError()); $this->assertEquals(403, $response->getStatusCode()); } public function testItRestrictsCreateFileOnCanAddChildren() { $folder = $this->objFromFixture(Folder::class, 'disallowCanAddChildren'); /** @skipUpgrade */ $fileData = array('Upload' => $this->getUploadFile('Upload', 'test.txt')); $_FILES = $fileData; $response = Director::test( 'admin/assets/api/createFile', array_merge( $fileData, [ 'ParentID' => $folder->ID, 'SecurityID' => SecurityToken::inst()->getValue(), ] ), $this->session, 'POST' ); $this->assertTrue($response->isError()); $this->assertEquals(403, $response->getStatusCode()); } public function testItRestrictsCreateFileOnExtension() { $folder1 = $this->objFromFixture( Folder::class, 'folder1' ); /** @skipUpgrade */ $fileData = array('Upload' => $this->getUploadFile('Upload', 'disallowed.php')); $_FILES = $fileData; $response = Director::test( 'admin/assets/api/createFile', array_merge( $fileData, [ 'ParentID' => $folder1->ID, 'SecurityID' => SecurityToken::inst()->getValue(), ] ), $this->session, 'POST' ); $this->assertTrue($response->isError()); $this->assertEquals(400, $response->getStatusCode()); $responseData = json_decode($response->getBody(), true); $this->assertEquals( [ 'type' => 'error', 'code' => 400, 'value' => "Extension 'php' is not allowed", ], $responseData['errors'][0] ); } public function testItRestrictsUpdateFile() { /** @var File $allowedFile */ $allowedFile = $this->objFromFixture(File::class, 'file1'); /** @var File $disallowedFile */ $disallowedFile = $this->objFromFixture(File::class, 'disallowCanEdit'); $response = Director::test( 'admin/assets/fileEditForm/' . $allowedFile->ID, [ 'action_save' => 1, 'ID' => $allowedFile->ID, 'Name' => 'disallowCanEdit.txt', 'Title' => 'new', 'SecurityID' => SecurityToken::inst()->getValue(), 'CanViewType' => $allowedFile->CanViewType, 'ViewerGroups' => 'unchanged', 'CanEditType' => $allowedFile->CanEditType, 'EditorGroups' => 'unchanged', ], $this->session ); $this->assertFalse($response->isError()); $response = Director::test( 'admin/assets/fileEditForm/' . $disallowedFile->ID, [ 'action_save' => 1, 'ID' => $disallowedFile->ID, 'Title' => 'new', 'SecurityID' => SecurityToken::inst()->getValue(), 'CanViewType' => $disallowedFile->CanViewType, 'ViewerGroups' => 'unchanged', 'CanEditType' => $disallowedFile->CanEditType, 'EditorGroups' => 'unchanged', ], $this->session ); $this->assertTrue($response->isError()); } /** * @param string $paramName * @param string $tmpFileName * @return array Emulating an entry in the $_FILES superglobal */ protected function getUploadFile($paramName, $tmpFileName = 'AssetAdminTest.txt') { $tmpFilePath = TEMP_PATH . DIRECTORY_SEPARATOR . $tmpFileName; $tmpFileContent = ''; for ($i = 0; $i < 10000; $i++) { $tmpFileContent .= '0'; } file_put_contents($tmpFilePath, $tmpFileContent); // emulates the $_FILES array return array( 'name' => $tmpFileName, 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, 'error' => UPLOAD_ERR_OK, ); } public function testSaveOrPublish() { // Test rename folder $folder1ID = $this->idFromFixture(Folder::class, 'folder1'); $response = $this->post( 'admin/assets/fileEditForm/' . $folder1ID, [ 'ID' => $folder1ID, 'action_save' => 1, 'Name' => 'folder1-renamed', 'SecurityID' => SecurityToken::inst()->getValue(), 'CanViewType' => 'Inherit', 'ViewerGroups' => 'unchanged', 'CanEditType' => 'Inherit', 'EditorGroups' => 'unchanged', ] ); $this->assertFalse($response->isError()); $folder1 = Folder::get()->byID($folder1ID); $this->assertEquals('folder1-renamed', $folder1->Name); } public function testGetMinimalistObjectFromData() { /** @var File $file */ $assetAdmin = AssetAdmin::singleton(); $file = $this->objFromFixture(File::class, 'file1'); $data = $assetAdmin->getMinimalistObjectFromData($file); // Thumbnail value is hard to predit, so we'll just check that it's there before unseting it. $this->assertNotEmpty($data['thumbnail']); unset($data['thumbnail']); $expected = [ "id" => $file->ID, "parent" => [ "id" => $file->Parent()->ID, "title" => $file->Parent()->Title, "filename" => $file->Parent()->Filename, ], "title" => $file->Title, "exists" => $file->exists(), "category" => $file->appCategory(), "extension" => $file->Extension, "size" => $file->AbsoluteSize, "published" => $file->isPublished(), "modified" => $file->isModifiedOnDraft(), "draft" => $file->isOnDraftOnly(), "hasRestrictedAccess" => $file->hasRestrictedAccess(), "isTrackedFormUpload" => $file->isTrackedFormUpload(), "visibility" => $file->getVisibility(), ]; $this->assertEquals($expected, $data); } public function testGetObjectFromDataFile() { $assetAdmin = AssetAdmin::singleton(); /** @var File $file */ $file = $this->objFromFixture(File::class, 'file1'); $data = $assetAdmin->getObjectFromData($file); // Thumbnail value is hard to predict, so we'll just check that it's there before unseting it. $this->assertNotEmpty($data['thumbnail']); unset($data['thumbnail']); $expected = [ "id" => $file->ID, "parent" => [ "id" => $file->Parent()->ID, "title" => $file->Parent()->Title, "filename" => $file->Parent()->Filename, ], "title" => $file->Title, "exists" => $file->exists(), "category" => $file->appCategory(), "extension" => $file->Extension, "size" => $file->AbsoluteSize, "published" => $file->isPublished(), "modified" => $file->isModifiedOnDraft(), "draft" => $file->isOnDraftOnly(), "inUseCount" => 1, "created" => $file->Created, "lastUpdated" => $file->LastEdited, "owner" => [ "id" => $file->Owner()->ID, "title" => $file->Owner()->Name ], "type" => $file->FileType, "name" => $file->Name, "filename" => $file->Filename, "url" => $file->AbsoluteURL, "canEdit" => $file->canEdit(), "canDelete" => $file->canDelete(), "hasRestrictedAccess" => $file->hasRestrictedAccess(), "isTrackedFormUpload" => $file->isTrackedFormUpload(), "visibility" => $file->getVisibility(), ]; $this->assertEquals($expected, $data); } public function testGetObjectFromDataFileWithFolder() { $assetAdmin = AssetAdmin::singleton(); /** @var File $file */ $file = $this->objFromFixture(Folder::class, 'folder1'); $data = $assetAdmin->getObjectFromData($file); // Thumbnail value is hard to predit, so we'll just check that it's there before unseting it. $this->assertNotEmpty($data['thumbnail']); unset($data['thumbnail']); $expected = [ "id" => $file->ID, "parent" => [ "id" => $file->Parent()->ID, "title" => $file->Parent()->Title, "filename" => $file->Parent()->Filename, ], "title" => $file->Title, "exists" => $file->exists(), "category" => 'folder', "extension" => $file->Extension, "size" => $file->AbsoluteSize, "published" => $file->isPublished(), "modified" => $file->isModifiedOnDraft(), "draft" => $file->isOnDraftOnly(), "inUseCount" => 0, "created" => $file->Created, "lastUpdated" => $file->LastEdited, "owner" => [ "id" => $file->Owner()->ID, "title" => $file->Owner()->Name ], "type" => 'folder', "name" => $file->Name, "filename" => $file->Filename, "url" => $file->AbsoluteURL, "canEdit" => $file->canEdit(), "canDelete" => $file->canDelete(), "hasRestrictedAccess" => $file->hasRestrictedAccess(), "isTrackedFormUpload" => $file->isTrackedFormUpload(), "visibility" => $file->getVisibility(), ]; $this->assertEquals($expected, $data); } } |