public function testFileUpdate() { $prophecy = $this->prophesize('League\\Flysystem\\FilesystemInterface'); $prophecy->update('path.txt', 'contents')->willReturn(true); $filesystem = $prophecy->reveal(); $file = new File(null, 'path.txt'); $file->setFilesystem($filesystem); $output = $file->update('contents'); $this->assertTrue($output); }
/** * Save our modified .yml file. * * @param boolean $makebackup Back up the file before commiting changes to it * * @throws \Bolt\Exception\FilesystemException * * @return boolean true if save was successful */ protected function save($makebackup) { if (!$this->verify()) { return false; } // If we're backing up do it, if we can if ($makebackup) { $this->backup(); } // Update the YAML file if we can, or throw an error if (!$this->file->update($this->yaml)) { throw new FilesystemException('Unable to write to file: ' . $this->file->getPath(), FilesystemException::FILE_NOT_WRITEABLE); } return true; }
/** * Handle a file edit POST. * * @param Request $request * @param Form $form * @param File $file * @param string $type * * @return JsonResponse */ private function handleEdit(Request $request, Form $form, File $file, $type) { $form->submit($request->get($form->getName())); if ($form->isValid()) { $data = $form->getData(); $contents = Input::cleanPostedData($data['contents']) . "\n"; $result = ['ok' => true, 'msg' => 'Unhandled state.']; // Before trying to save a yaml file, check if it's valid. if ($type === 'yml') { $yamlparser = new Parser(); try { $yamlparser->parse($contents); } catch (ParseException $e) { $result['ok'] = false; $result['msg'] = Trans::__("File '%s' could not be saved:", ['%s' => $file->getPath()]) . $e->getMessage(); } } if ($result['ok']) { // Remove ^M (or \r) characters from the file. $contents = str_ireplace("\r", '', $contents); if ($file->update($contents)) { $result['msg'] = Trans::__("File '%s' has been saved.", ['%s' => $file->getPath()]); $result['datechanged'] = date_format(new \DateTime('@' . $file->getTimestamp()), 'c'); } else { $result['msg'] = Trans::__("File '%s' could not be saved, for some reason.", ['%s' => $file->getPath()]); } } } else { $result = ['ok' => false, 'msg' => Trans::__("File '%s' could not be saved, because the form wasn't valid.", ['%s' => $file->getPath()])]; } return $this->json($result); }