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);
 }
 /**
  * @ControllerManaged
  *
  * @param boolean $scheduled        	
  */
 protected function setBackupScheduled($scheduled)
 {
     $statusContainer = $this->backupService->createStatusInformation();
     if ($statusContainer->getOverallStatus() == StatusContainer::ERROR) {
         throw new EasyBackupException($this->trans->t('Not all preconditions are met, backup cannot be scheduled'));
     }
     $this->configService->setBackupScheduled($scheduled);
     if ($scheduled) {
         $this->backupService->scheduleBackupJob();
     } else {
         $this->backupService->unScheduleBackupJob();
     }
 }
 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);
 }
 /**
  *
  * @return boolean
  */
 public function isExecutingOrWaitingForRun()
 {
     if ($this->isBackupExecuting()) {
         return true;
     }
     if ($this->configService->isRegistered($this->runOnceJob)) {
         return true;
     }
     return false;
 }
 public function testIsToBeExecutedNow11()
 {
     // Wrong schedule parameter
     $this->setExpectedException('\\OCA\\EasyBackup\\EasyBackupException');
     $now = new \DateTime(null, new \DateTimeZone('UTC'));
     $this->configServiceMock->expects($this->once())->method('getAppValue')->with($this->equalTo('SCHEDULED'))->will($this->returnValue('invalidformat'));
     $firstRunAtHour = intval($now->format('H'));
     $twelveHoursAgo = $now->sub(new \DateInterval('PT12H'));
     $retVal = $this->cut->isToBeExecutedNow($firstRunAtHour, $twelveHoursAgo);
 }
 public function testUploadSshKeyOk()
 {
     $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->configServiceMock->expects($this->once())->method('getPrivateKeyFilename')->will($this->returnValue($uploadedFileName));
     $this->configServiceMock->expects($this->once())->method('getPublicKey')->will($this->returnValue('***MockKey***'));
     $this->backupServiceMock->expects($this->once())->method('validatePrivateSshKey')->will($this->returnValue(true));
     $statusContainer = new StatusContainer();
     $statusContainer->addStatus('privateKeyPresent', StatusContainer::OK, '');
     $this->backupServiceMock->expects($this->any())->method('createStatusInformation')->will($this->returnValue($statusContainer));
     $this->responseFactoryMock->expects($this->exactly(2))->method('createTemplateResponse')->will($this->returnValue($this->templateResponseMock));
     $this->responseFactoryMock->expects($this->at(0))->method('createTemplateResponse')->with($this->equalTo('easybackup'), $this->equalTo('preconditions.inc'), $this->equalTo(array('statusContainer' => $statusContainer)))->will($this->returnValue($this->templateResponseMock));
     $this->responseFactoryMock->expects($this->at(1))->method('createTemplateResponse')->with($this->equalTo('easybackup'), $this->equalTo('publickey.inc'), $this->equalTo(array('statusContainer' => $statusContainer, 'publicKey' => '***MockKey***')))->will($this->returnValue($this->templateResponseMock));
     $this->responseFactoryMock->expects($this->once())->method('createJSONSuccessResponse')->with($this->equalTo(array('preconditionsHtml' => null, 'publicKeyHtml' => null)));
     $this->cut->uploadSshKey();
     $this->assertEquals(file_get_contents($keyFileName), file_get_contents($uploadedFileName));
     $stat = stat($uploadedFileName);
     $this->assertEquals("0600", sprintf("%o", $stat['mode'] & 0777));
     // Only the read/write bit for the owner are set (-rw-------)
     unlink($uploadedFileName);
 }
 public function testTailFileLongLine()
 {
     $this->configServiceMock->expects($this->atLeastOnce())->method('getLogfileName')->will($this->returnValue(__DIR__ . '/../resource/longlines.txt'));
     $this->configServiceMock->expects($this->atLeastOnce())->method('getNumberOfLinesToDisplay')->will($this->returnValue(6));
     $this->configServiceMock->expects($this->once())->method('getDisplayWidth')->will($this->returnValue(30));
     $this->responseFactoryMock->expects($this->once())->method('createTemplateResponse')->with($this->equalTo('easybackup'), $this->equalTo('lastbackup.inc'), $this->equalTo(array('lastBackupSuccessful' => null, 'lastBackupTime' => null)))->will($this->returnValue($this->templateResponseMock));
     $expected = '<font color="#000000">4. This is a very long line, i<br></font>';
     $expected .= '<font color="#212121">t has even more than 60 charac<br></font>';
     $expected .= '<font color="#424242">ters<br></font>';
     $expected .= '<font color="#636363">3. This is a short line again<br></font>';
     $expected .= '<font color="#848484">2. This is a very long line, i<br></font>';
     $expected .= '<font color="#a5a5a5">t has more than 30 characters<br></font>';
     $this->responseFactoryMock->expects($this->once())->method('createJSONSuccessResponse')->with($this->equalTo(array('html' => $expected, 'backupExecutingOrWaitingForRun' => null, 'lastBackupHtml' => null)))->will($this->returnValue($this->templateResponseMock));
     $this->cut->getLogFileContent();
 }
 private function renderPublicKeyHtml()
 {
     $statusContainer = $this->backupService->createStatusInformation();
     $parameters = array('statusContainer' => $statusContainer, 'publicKey' => $this->configService->getPublicKey());
     return $this->renderHtml('publickey.inc', $parameters);
 }
 /**
  * @ControllerManaged
  * @NoCSRFRequired
  */
 protected function getCompleteLogfile()
 {
     $file = $this->configService->getLogfileName();
     $data = file_get_contents($file);
     return $this->responseFactory->createPlainTextResponse($data);
 }
 public function testRegisteredClassOfWrongType()
 {
     $this->setExpectedException('\\OCA\\EasyBackup\\EasyBackupException');
     $this->cut->register($this->runOnceJobMock, '\\OCA\\EasyBackup\\Service\\ConfigServiceTest');
 }
 private function createParameters($subTemplate)
 {
     $parameters = array('statusContainer' => $this->getStatusContainer(), 'userName' => $this->configService->getHostUserName(), 'keyUploadUrl' => $this->urlGenerator->linkToRoute('easybackup.config.uploadSshKey'), 'schedule' => $this->scheduleService, 'scheduleTime' => $this->configService->getScheduleTime(), 'isScheduled' => $this->configService->isBackupScheduled(), 'configurationUrl' => $this->urlGenerator->linkToRoute('easybackup.page.configuration'), 'backupUrl' => $this->urlGenerator->linkToRoute('easybackup.page.backup'), 'restoreUrl' => $this->urlGenerator->linkToRoute('easybackup.page.restore'), 'logfileUrl' => $this->urlGenerator->linkToRoute('easybackup.logfileview.getCompleteLogfile'), 'subTemplate' => $subTemplate, 'publicKey' => $this->configService->getPublicKey(), 'isExecuting' => $this->backupService->isExecutingOrWaitingForRun(), 'lastBackupSuccessful' => $this->backupService->isLastBackupSuccessful(), 'lastBackupTime' => $this->backupService->getLastBackupTime());
     return $parameters;
 }