public function testPreExecSuccess()
 {
     $this->backupServiceMock->expects($this->once())->method('isBackupExecuting')->will($this->returnValue(false));
     $this->backupServiceMock->expects($this->once())->method('setBackupRunning')->with($this->equalTo(true));
     $this->configServiceMock->expects($this->once())->method('getLogfileName')->will($this->returnValue('/dev/null'));
     $retVal = $this->cut->preExec();
     $this->assertTrue($retVal);
 }
 public function testScheduleBackupPreconditionsNok()
 {
     $this->backupServiceMock->expects($this->never())->method('executeBackup');
     $this->statusContainerMock->expects($this->once())->method('getOverallStatus')->will($this->returnValue(\OCA\EasyBackup\StatusContainer::ERROR));
     $this->backupServiceMock->expects($this->once())->method('createStatusInformation')->will($this->returnValue($this->statusContainerMock));
     $this->responseFactoryMock->expects($this->once())->method('createJSONBadRequestResponse');
     $this->cut->scheduleBackup();
 }
 /**
  * @ControllerManaged
  */
 protected function getLogFileContent()
 {
     $lines = $this->tailFile();
     $wrappedLines = array();
     $maxWidth = $this->configService->getDisplayWidth();
     foreach ($lines as $line) {
         // Get rid of "Could not create directory '/var/www/.ssh'."
         if (preg_match("/\\.ssh'\\./", $line)) {
             continue;
         }
         while (strlen($line) > $maxWidth) {
             $wrappedLines[] = substr($line, 0, $maxWidth) . "\n";
             $line = substr($line, $maxWidth);
         }
         $wrappedLines[] = $line . "\n";
     }
     $data = '';
     $linesToDisplay = array_slice($wrappedLines, 0, $this->configService->getNumberOfLinesToDisplay());
     $increment = count($linesToDisplay) > 0 ? intval(200 / count($linesToDisplay)) : 0;
     $colorCode = 0;
     foreach ($linesToDisplay as $line) {
         $colorCodeHex = dechex($colorCode);
         if (strlen($colorCodeHex) == 1) {
             $colorCodeHex = "0" . $colorCodeHex;
         }
         $data .= "<font color=\"#{$colorCodeHex}{$colorCodeHex}{$colorCodeHex}\">" . str_replace("\n", '<br>', htmlentities($line)) . '</font>';
         $colorCode += $increment;
     }
     return array('html' => $data, 'backupExecutingOrWaitingForRun' => $this->backupService->isExecutingOrWaitingForRun(), 'lastBackupHtml' => $this->renderHtml('lastbackup.inc', array('lastBackupSuccessful' => $this->backupService->isLastBackupSuccessful(), 'lastBackupTime' => $this->backupService->getLastBackupTime())));
 }
 private function getStatusContainer()
 {
     if ($this->statusContainer == null) {
         $this->statusContainer = $this->backupService->createStatusInformation();
     }
     return $this->statusContainer;
 }
 public function testUploadSshKeyMalFormed()
 {
     $keyFileName = __DIR__ . '/../resource/private_key.txt';
     $uploadedFileName = '/tmp/test_keyfile';
     $this->requestMock->expects($this->once())->method('getUploadedFile')->with($this->equalTo('easybackup_sshKeyFile'))->will($this->returnValue(array('tmp_name' => $keyFileName)));
     $this->backupServiceMock->expects($this->once())->method('validatePrivateSshKey')->will($this->returnValue(false));
     $this->responseFactoryMock->expects($this->once())->method('createJSONBadRequestResponse')->with($this->equalTo('Key is not well-formed'));
     $this->cut->uploadSshKey();
 }
 public function testGetPublicSshKeyFromPrivateKeyOk()
 {
     $keyFileName = __DIR__ . '/../resource/private_key.txt';
     $this->shellExecServiceMock->expects($this->at(0))->method('shellExec')->with($this->equalTo('which ssh-keygen'))->will($this->returnValue(new ShellExecResult(0, array())));
     $this->configServiceMock->expects($this->once())->method('getPrivateKeyFilename')->will($this->returnValue($keyFileName));
     $this->shellExecServiceMock->expects($this->at(1))->method('shellExec')->with($this->equalTo("ssh-keygen -P '' -q -y -f '{$keyFileName}'"))->will($this->returnValue(new ShellExecResult(0, array('ssh-rsa AAAAB'))));
     $publicKey = $this->cut->getPublicSshKeyFromPrivateKey();
     $this->assertEquals('ssh-rsa AAAAB', $publicKey);
 }
 public function testIndexConfigurationOkBackupOk()
 {
     $statusContainer = new StatusContainer();
     $statusContainer->addStatus('mockStatus', StatusContainer::OK, '');
     $this->backupServiceMock->expects($this->once())->method('createStatusInformation')->will($this->returnValue($statusContainer));
     $this->backupServiceMock->expects($this->atLeastOnce())->method('isLastBackupSuccessful')->will($this->returnValue(true));
     $this->containerMock->expects($this->once())->method('isAdminUser')->will($this->returnValue(true));
     $this->responseFactoryMock->expects($this->once())->method('createTemplateResponse')->with($this->equalTo('easybackup'), $this->equalTo('index'), $this->callback(function ($params) {
         return $params['subTemplate'] == 'restore.inc';
     }));
     $retVal = $this->cut->index();
 }
 /**
  * @ControllerManaged
  *
  * @param string $config        	
  */
 protected function restoreAction($restoreConfig)
 {
     $this->backupService->scheduleRestoreJob($restoreConfig);
 }
 private function renderPublicKeyHtml()
 {
     $statusContainer = $this->backupService->createStatusInformation();
     $parameters = array('statusContainer' => $statusContainer, 'publicKey' => $this->configService->getPublicKey());
     return $this->renderHtml('publickey.inc', $parameters);
 }