/**
  * Constructor
  * @return AppUpgraderClient
  */
 public function __construct()
 {
     $config = Config::getInstance();
     $is_in_beta = $config->getValue('is_subscribed_to_beta');
     $is_in_beta = isset($is_in_beta) ? $is_in_beta : false;
     if ($is_in_beta) {
         self::$UPDATE_URL = self::$UPDATE_BETA_URL;
     }
 }
 public function testUpdate()
 {
     $upgrade_controller = new UpgradeApplicationController(true);
     $valid_url = AppUpgraderClient::$UPDATE_URL;
     error_reporting(E_ERROR | E_USER_ERROR);
     // turn off warning messages
     $config = Config::getInstance();
     $proper_version = $config->getValue('THINKUP_VERSION');
     $config->setValue('THINKUP_VERSION', 1.0);
     //set a low version num
     // delete index.pho
     $this->assertTrue(unlink($this->test_web_dir . '/index.php'));
     // create a file in out data dir
     touch($this->test_web_dir . '/data/dont_delete_me');
     AppUpgraderClient::$UPDATE_URL = $this->test_web_dir . '/data/valid_json';
     file_put_contents(AppUpgraderClient::$UPDATE_URL, '{"version":"100.1", "url":"' . THINKUP_WEBAPP_PATH . 'test_installer/thinkup.zip"}');
     $update_info = $upgrade_controller->runUpdate($this->test_web_dir);
     $this->assertPattern('/data\\/\\d+\\-v1\\-config\\.inc\\.backup\\.php/', $update_info['config']);
     $this->assertPattern('/data\\/\\d+\\-v1\\-backup\\.zip/', $update_info['backup']);
     $this->assertTrue(file_exists($this->test_web_dir . '/index.php'), "we should have our index file back");
     $data_path = FileDataManager::getDataPath();
     $this->assertFalse(is_dir($data_path . '/data/thinkup'), "our unzipped update deleted");
     $this->assertTrue(file_exists($this->test_web_dir . '/data/dont_delete_me'), "/data/* not deleted");
     unlink(AppUpgraderClient::$UPDATE_URL);
     // reset
     AppUpgraderClient::$UPDATE_URL = $valid_url;
     error_reporting(E_STRICT);
     // reset error reporting
     $config->setValue('THINKUP_VERSION', $proper_version);
 }
 /**
  * Upgrade the application code to the latest version.
  * @throws Exception
  * @param bool $verify_updatable Whether or not to verify if installation is updatable, defaults to false
  * @return array Backup file information
  */
 public function runUpdate($file_path, $verify_updatable = false)
 {
     $app_dir = preg_replace("/\\/_lib\\/controller/", '', $file_path);
     // do we have the disk space we need?
     $disk_util = new AppUpgraderDiskUtil($app_dir);
     $disk_space_megs = $disk_util->getAvailableDiskSpace();
     // do we have the perms to do what we need?
     $disk_util->validateUpdatePermissions($app_dir);
     // do we need to update?
     $update_client = new AppUpgraderClient();
     $update_info = $update_client->getLatestVersionInfo();
     require dirname(__FILE__) . '/../../install/version.php';
     $version = Config::GetInstance()->getvalue('THINKUP_VERSION');
     if ($update_info['version'] < $version) {
         throw new Exception("You are running the latest version of ThinkUp.");
     }
     if ($verify_updatable == true) {
         return array('latest_version' => $update_info['version']);
     }
     // download zip...
     $update_zip_data = $update_client->getLatestVersionZip($update_info['url']);
     $update_zip = $disk_util->writeZip($update_zip_data);
     $zip = new ZipArchive();
     $open_result = $zip->open($update_zip);
     if ($open_result !== true) {
         unlink($update_zip);
         throw new Exception("Unable to extract " . $update_zip . ". ZipArchive::open failed with error code " . $open_result);
     }
     $num_files = $zip->numFiles;
     if ($num_files < 1) {
         unlink($update_zip);
         throw new Exception("Unable to extract " . $update_zip . ". ZipArchive->numFiles is " . $num_files);
     }
     $backup_file_info = array();
     $backup_file_info = $disk_util->backupInstall();
     $disk_util->deleteOldInstall();
     $data_path = FileDataManager::getDataPath();
     if ($zip->extractTo($data_path) !== true) {
         throw new Exception("Unable to extract new files into {$app_dir}: " . $zip->getStatusString());
     } else {
         $new_version_dir = $data_path . 'thinkup';
         $disk_util->recurseCopy($new_version_dir, $app_dir);
         // delete install files
         $disk_util->deleteDir($new_version_dir);
         unlink($update_zip);
     }
     //replace config file
     copy($backup_file_info['config'], "{$app_dir}/config.inc.php");
     return $backup_file_info;
 }