public function setUp()
 {
     parent::setUp();
     // Set backend
     $adapter = new AssetAdapter(ASSETS_PATH . '/DBFileTest');
     $filesystem = new Filesystem($adapter);
     $filesystem->addPlugin(new FlysystemUrlPlugin());
     $backend = new AssetStoreTest_SpyStore();
     $backend->setFilesystem($filesystem);
     Injector::inst()->registerService($backend, 'AssetStore');
     // Disable legacy
     Config::inst()->remove(get_class(new FlysystemAssetStore()), 'legacy_filenames');
     // Update base url
     Config::inst()->update('Director', 'alternate_base_url', '/mysite/');
 }
 public function tearDown()
 {
     AssetStoreTest_SpyStore::reset();
     SS_Filesystem::removeFolder($this->getBasePath());
     parent::tearDown();
     Config::unnest();
 }
 public function setUp()
 {
     parent::setUp();
     Config::inst()->update('HTMLEditorField_Toolbar', 'fileurl_scheme_whitelist', array('http'));
     Config::inst()->update('HTMLEditorField_Toolbar', 'fileurl_domain_whitelist', array('example.com'));
     // Filesystem mock
     AssetStoreTest_SpyStore::activate(__CLASS__);
     // Load up files
     /** @var File $file1 */
     $file1 = $this->objFromFixture('File', 'example_file');
     $file1->setFromString(str_repeat('x', 1000), $file1->Name);
     $file1->write();
     /** @var Image $image1 */
     $image1 = $this->objFromFixture('Image', 'example_image');
     $image1->setFromLocalFile(__DIR__ . '/images/HTMLEditorFieldTest-example.jpg', 'folder/subfolder/HTMLEditorFieldTest_example.jpg');
     $image1->write();
 }
 public function setUp()
 {
     parent::setUp();
     // Set backend root to /ImageTest
     AssetStoreTest_SpyStore::activate('ProtectedFileControllerTest');
     // Create a test folders for each of the fixture references
     foreach (Folder::get() as $folder) {
         /** @var Folder $folder */
         $filePath = AssetStoreTest_SpyStore::getLocalPath($folder);
         \Filesystem::makeFolder($filePath);
     }
     // Create a test files for each of the fixture references
     foreach (File::get()->exclude('ClassName', 'Folder') as $file) {
         /** @var File $file */
         $path = AssetStoreTest_SpyStore::getLocalPath($file);
         \Filesystem::makeFolder(dirname($path));
         $fh = fopen($path, "w+");
         fwrite($fh, str_repeat('x', 1000000));
         fclose($fh);
         // Create variant for each file
         $this->getAssetStore()->setFromString(str_repeat('y', 100), $file->Filename, $file->Hash, 'variant');
     }
 }
 public function testDeleteResampledImagesOnUpload()
 {
     $tmpFileName = 'UploadTest-testUpload.jpg';
     $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
     $uploadImage = function () use($tmpFileName, $tmpFilePath) {
         copy(__DIR__ . '/gdtest/test_jpg.jpg', $tmpFilePath);
         // emulates the $_FILES array
         $tmpFile = array('name' => $tmpFileName, 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, 'extension' => 'jpg', 'error' => UPLOAD_ERR_OK);
         $v = new UploadTest_Validator();
         // test upload into default folder
         $u = new Upload();
         $u->setReplaceFile(true);
         $u->setValidator($v);
         $u->loadIntoFile($tmpFile);
         return $u->getFile();
     };
     // Image upload and generate a resampled image
     $image = $uploadImage();
     $resampled = $image->ResizedImage(123, 456);
     $resampledPath = AssetStoreTest_SpyStore::getLocalPath($resampled);
     $this->assertFileExists($resampledPath);
     // Re-upload the image, overwriting the original
     // Resampled images should removed when their parent file is overwritten
     $image = $uploadImage();
     $this->assertFileExists($resampledPath);
 }
 /**
  * Test that files can be removed from an existing field
  */
 public function testRemoveFromHasOne()
 {
     $record = $this->objFromFixture('AssetFieldTest_Object', 'object1');
     // Check record exists
     $this->assertTrue($record->File->exists());
     $filePath = AssetStoreTest_SpyStore::getLocalPath($record->File);
     $this->assertFileExists($filePath);
     // Remove from record
     $response = $this->mockUploadFileSave('File', null, null, null);
     $this->assertEmpty($response['errors']);
     // Check file is removed
     $record = AssetFieldTest_Object::get()->byID($record->ID);
     $this->assertFalse($record->File->exists());
     // Check file object itself exists
     // @todo - When assets are removed from a DBFile reference, these files should be archived
     $this->assertFileExists($filePath, 'File is only detached, not deleted from filesystem');
 }
 /**
  * Reset defaults for this store
  */
 public static function reset()
 {
     // Need flushing since it won't have any files left
     CacheGeneratedAssetHandler::flush();
     // Remove all files in this store
     if (self::$basedir) {
         $path = self::base_path();
         if (file_exists($path)) {
             SS_Filesystem::removeFolder($path);
         }
     }
     self::$seekable_override = null;
     self::$basedir = null;
 }
 /**
  * Tests for the bug #5994 - if you don't execute get_by_id prior to the rename or move, it will fail.
  */
 public function testRenameFolderAndCheckTheFile()
 {
     // ID is prefixed in case Folder is subclassed by project/other module.
     $folder1 = DataObject::get_one('Folder', array('"File"."ID"' => $this->idFromFixture('Folder', 'folder1')));
     $folder1->Name = 'FileTest-folder1-changed';
     $folder1->write();
     // Check if the file in the folder moved along
     $file1 = DataObject::get_by_id('File', $this->idFromFixture('File', 'file1-folder1'), false);
     $this->assertFileExists(AssetStoreTest_SpyStore::getLocalPath($file1));
     $this->assertEquals($file1->Filename, 'FileTest-folder1-changed/File1.txt', 'The file DataObject path uses renamed folder');
     // File should be located in new folder
     $this->assertEquals(ASSETS_PATH . '/FolderTest/FileTest-folder1-changed/55b443b601/File1.txt', AssetStoreTest_SpyStore::getLocalPath($file1));
 }
 /**
  * Test fallback to file generation API with enable_static_file disabled
  */
 public function testGeneratedFile()
 {
     Config::inst()->update('ErrorPage', 'enable_static_file', false);
     $this->logInWithPermission('ADMIN');
     $page = new ErrorPage();
     $page->ErrorCode = 405;
     $page->Title = 'Method Not Allowed';
     $page->write();
     $page->doPublish();
     // Error content is available, even though the static file does not exist (only in assetstore)
     $this->assertNotEmpty(ErrorPage::get_content_for_errorcode('405'));
     $expectedErrorPagePath = AssetStoreTest_SpyStore::base_path() . '/error-405.html';
     $this->assertFileNotExists($expectedErrorPagePath, 'Error page is not cached in static location');
 }
 public function tearDown()
 {
     AssetStoreTest_SpyStore::reset();
     parent::tearDown();
 }
 public function testDeleteFile()
 {
     $file = $this->objFromFixture('File', 'asdf');
     $fileID = $file->ID;
     $filePath = AssetStoreTest_SpyStore::getLocalPath($file);
     $file->delete();
     // File is deleted
     $this->assertFileNotExists($filePath);
     $this->assertEmpty(DataObject::get_by_id('File', $fileID));
 }
示例#12
0
 public function testDeleteDatabaseOnly()
 {
     $file = $this->objFromFixture('File', 'asdf');
     $fileID = $file->ID;
     $filePath = AssetStoreTest_SpyStore::getLocalPath($file);
     $file->delete();
     $this->assertFileExists($filePath);
     $this->assertFalse(DataObject::get_by_id('File', $fileID));
 }
 public function tearDown()
 {
     $_SERVER = $this->oldServer;
     AssetStoreTest_SpyStore::reset();
     parent::tearDown();
 }
 /**
  * Test fallback to file generation API with enable_static_file disabled
  */
 public function testGeneratedFile()
 {
     Config::inst()->update('ErrorPage', 'enable_static_file', false);
     $this->logInWithPermission('ADMIN');
     $page = new ErrorPage();
     $page->ErrorCode = 405;
     $page->Title = 'Method Not Allowed';
     $page->write();
     $page->doPublish();
     // Dynamic content is available
     $response = ErrorPage::response_for('405');
     $this->assertNotEmpty($response);
     $this->assertNotEmpty($response->getBody());
     $this->assertEquals(405, (int) $response->getStatusCode());
     // Static content is not available
     $this->assertEmpty(ErrorPage::get_content_for_errorcode('405'));
     $expectedErrorPagePath = AssetStoreTest_SpyStore::base_path() . '/error-405.html';
     $this->assertFileNotExists($expectedErrorPagePath, 'Error page is not cached in static location');
 }
 /**
  * Test that UploadField:overwriteWarning cannot overwrite Upload:replaceFile
  */
 public function testConfigOverwriteWarningCannotRelaceFiles()
 {
     Upload::config()->replaceFile = false;
     UploadField::config()->defaultConfig = array_merge(UploadField::config()->defaultConfig, array('overwriteWarning' => true));
     $tmpFileName = 'testUploadBasic.txt';
     $response = $this->mockFileUpload('NoRelationField', $tmpFileName);
     $this->assertFalse($response->isError());
     $responseData = Convert::json2array($response->getBody());
     $uploadedFile = DataObject::get_by_id('SilverStripe\\Assets\\File', (int) $responseData[0]['id']);
     $this->assertTrue(is_object($uploadedFile), 'The file object is created');
     $this->assertFileExists(AssetStoreTest_SpyStore::getLocalPath($uploadedFile));
     $tmpFileName = 'testUploadBasic.txt';
     $response = $this->mockFileUpload('NoRelationField', $tmpFileName);
     $this->assertFalse($response->isError());
     $responseData = Convert::json2array($response->getBody());
     $uploadedFile2 = DataObject::get_by_id('SilverStripe\\Assets\\File', (int) $responseData[0]['id']);
     $this->assertTrue(is_object($uploadedFile2), 'The file object is created');
     $this->assertFileExists(AssetStoreTest_SpyStore::getLocalPath($uploadedFile2));
     $this->assertTrue($uploadedFile->Filename !== $uploadedFile2->Filename, 'Filename is not the same');
     $this->assertTrue($uploadedFile->ID !== $uploadedFile2->ID, 'File database record is not the same');
 }
 /**
  * Test that files can be removed from an existing field
  */
 public function testRemoveFromHasOne()
 {
     $record = $this->objFromFixture('AssetFieldTest_Object', 'object1');
     // Check record exists
     $this->assertTrue($record->File->exists());
     $filePath = AssetStoreTest_SpyStore::getLocalPath($record->File);
     $this->assertFileExists($filePath);
     // Remove from record
     $response = $this->mockUploadFileSave('File', null, null, null);
     $this->assertEmpty($response['errors']);
     // Check file is removed
     $record = AssetFieldTest_Object::get()->byID($record->ID);
     $this->assertFalse($record->File->exists());
     // Check file object itself exists
     $this->assertFileNotExists($filePath, 'File is deleted once detached');
 }
 public function testBlockedCombinedJavascript()
 {
     $basePath = $this->getCurrentRelativePath();
     $backend = Injector::inst()->create('Requirements_Backend');
     $this->setupCombinedRequirements($backend);
     $combinedFileName = '/_combinedfiles/RequirementsTest_bc-51622b5.js';
     $combinedFilePath = AssetStoreTest_SpyStore::base_path() . $combinedFileName;
     /* BLOCKED COMBINED FILES ARE NOT INCLUDED */
     $backend->block('RequirementsTest_bc.js');
     clearstatcache();
     // needed to get accurate file_exists() results
     $html = $backend->includeInHTML(false, self::$html_template);
     $this->assertFileNotExists($combinedFilePath);
     $this->assertNotRegExp('/src=".*\\/RequirementsTest_bc\\.js/', $html, 'blocked combined files are not included');
     $backend->unblock('RequirementsTest_bc.js');
     /* BLOCKED UNCOMBINED FILES ARE NOT INCLUDED */
     $this->setupCombinedRequirements($backend);
     $backend->block($basePath . '/RequirementsTest_b.js');
     $combinedFileName2 = '/_combinedfiles/RequirementsTest_bc-fc7468e.js';
     // SHA1 without file c included
     $combinedFilePath2 = AssetStoreTest_SpyStore::base_path() . $combinedFileName2;
     clearstatcache();
     // needed to get accurate file_exists() results
     $html = $backend->includeInHTML(false, self::$html_template);
     $this->assertFileExists($combinedFilePath2);
     $this->assertTrue(strpos(file_get_contents($combinedFilePath2), "alert('b')") === false, 'blocked uncombined files are not included');
     $backend->unblock($basePath . '/RequirementsTest_b.js');
     /* A SINGLE FILE CAN'T BE INCLUDED IN TWO COMBINED FILES */
     $this->setupCombinedRequirements($backend);
     clearstatcache();
     // needed to get accurate file_exists() results
     // Exception generated from including invalid file
     $this->setExpectedException('InvalidArgumentException', sprintf("Requirements_Backend::combine_files(): Already included file(s) %s in combined file '%s'", $basePath . '/RequirementsTest_c.js', 'RequirementsTest_bc.js'));
     $backend->combineFiles('RequirementsTest_ac.js', array($basePath . '/RequirementsTest_a.js', $basePath . '/RequirementsTest_c.js'));
 }
 /**
  * Reset defaults for this store
  */
 public static function reset()
 {
     // Remove all files in this store
     if (self::$basedir) {
         $path = self::base_path();
         if (file_exists($path)) {
             \Filesystem::removeFolder($path);
         }
     }
     self::$seekable_override = null;
     self::$basedir = null;
 }
 /**
  * Test internal file Id generation
  */
 public function testGetFileID()
 {
     $store = new AssetStoreTest_SpyStore();
     $this->assertEquals('directory/2a17a9cb4b/file.jpg', $store->getFileID(sha1('puppies'), 'directory/file.jpg'));
     $this->assertEquals('2a17a9cb4b/file.jpg', $store->getFileID(sha1('puppies'), 'file.jpg'));
     $this->assertEquals('dir_ectory/2a17a9cb4b/fil_e.jpg', $store->getFileID(sha1('puppies'), 'dir__ectory/fil__e.jpg'));
     $this->assertEquals('directory/2a17a9cb4b/file_variant.jpg', $store->getFileID(sha1('puppies'), 'directory/file__variant.jpg', null));
     $this->assertEquals('directory/2a17a9cb4b/file__variant.jpg', $store->getFileID(sha1('puppies'), 'directory/file.jpg', 'variant'));
     $this->assertEquals('2a17a9cb4b/file__var__iant.jpg', $store->getFileID(sha1('puppies'), 'file.jpg', 'var__iant'));
 }