/** * Check that the directory can be used for backup. * * @throws \BackupMigrate\Core\Exception\BackupMigrateException */ protected function checkDirectory() { // @TODO: Figure out if the file is or might be accessible via the web. $dir = $this->confGet('directory'); $is_private = strpos($dir, 'private://') === 0; // Attempt to create/prepare the directory if it is in the private directory if ($is_private) { if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY && FILE_MODIFY_PERMISSIONS)) { throw new BackupMigrateException("The backup file could not be saved to '%dir' because the directory could not be created or cannot be written to. Please make sure your private files directory is writable by the web server.", ['%dir' => $dir]); } } else { // If the file is local to the server. $real = \Drupal::service('file_system')->realpath($dir); if ($real) { // If the file is within the docroot. $in_root = strpos($real, DRUPAL_ROOT) === 0; if ($in_root && !$is_private) { throw new BackupMigrateException("The backup file could not be saved to '%dir' because that directory may be publicly accessible via the web. Please save your backups to the private file directory or a directory outside of the web root.", ['%dir' => $dir]); } } } // Do the regular exists/writable checks parent::checkDirectory(); // @TODO: Warn if the realpath cannot be resolved (because we cannot determine if the file is publicly accessible) }
/** * @covers ::saveFile */ public function testMetadata() { // Create with an extension. $file = $this->manager->create('txt'); $file->write('Hello, World 4!'); $file->setName('item4'); $file->setMeta('x-example', '12345'); $this->destination->saveFile($file); $this->assertFileExists($this->destURI . '/item4.txt'); $this->assertEquals('Hello, World 4!', file_get_contents($this->destURI . '/item4.txt')); // Dipping beneath the API to test that the sidecar is created $this->assertFileExists($this->destURI . '/item4.txt.info'); // Load the file again and get the metadata $file = $this->destination->getFile('item4.txt'); $file = $this->destination->loadFileMetadata($file); $this->assertEquals('12345', $file->getMeta('x-example')); // Dipping beneath the API to test that the info file doesn't exist after a delete $this->destination->deleteFile('item4.txt'); $this->assertFileNotExists($this->destURI . '/item4.txt'); $this->assertFileNotExists($this->destURI . '/item4.txt.info'); }