Skip to content

Commit

Permalink
Web updater code cleanup
Browse files Browse the repository at this point in the history
* Add missing PHP docblocks
* Convertion underscore_style function names to camelCase style (code style guide)
* Fix gitignore data path and version.php current version
* Add new tests to all_tests.php run
* Reword error messages
  • Loading branch information
ginatrapani committed Mar 12, 2012
1 parent 0fa667c commit 6cd03c9
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,7 +1,7 @@
webapp/config.inc.php
webapp/config.inc.bak.php
webapp/data/
./data/
data/
tests/config.tests.inc.php
.*
extras/cron/config
Expand Down
5 changes: 5 additions & 0 deletions tests/TestOfLoader.php
Expand Up @@ -49,6 +49,7 @@ public function testLoaderRegisterDefault() {
// check default lookup path without additionalPath
$this->assertEqual( Loader::getLookupPath(), array(
THINKUP_WEBAPP_PATH . '_lib/model/',
THINKUP_WEBAPP_PATH . "_lib/model/update/",
THINKUP_WEBAPP_PATH . '_lib/controller/',
THINKUP_WEBAPP_PATH . '_lib/model/exceptions/'
));
Expand All @@ -68,6 +69,7 @@ public function testLoaderRegisterWithStringAdditionalPath() {
// check lookup path with single additionalPath
$this->assertEqual( Loader::getLookupPath(), array(
THINKUP_WEBAPP_PATH . '_lib/model/',
THINKUP_WEBAPP_PATH . "_lib/model/update/",
THINKUP_WEBAPP_PATH . '_lib/controller/',
THINKUP_WEBAPP_PATH . '_lib/model/exceptions/',
THINKUP_ROOT_PATH . 'tests/classes'
Expand All @@ -87,6 +89,7 @@ public function testLoaderRegisterWithArrayAdditionalPaths() {
// check lookup path with array additionalPath
$this->assertEqual( Loader::getLookupPath(), array(
THINKUP_WEBAPP_PATH . '_lib/model/',
THINKUP_WEBAPP_PATH . "_lib/model/update/",
THINKUP_WEBAPP_PATH . '_lib/controller/',
THINKUP_WEBAPP_PATH . '_lib/model/exceptions/',
THINKUP_ROOT_PATH . 'tests',
Expand Down Expand Up @@ -123,13 +126,15 @@ public function testAdditionalPathAfterInitialRegister() {
Loader::register();
$this->assertEqual( Loader::getLookupPath(), array(
THINKUP_WEBAPP_PATH . '_lib/model/',
THINKUP_WEBAPP_PATH . "_lib/model/update/",
THINKUP_WEBAPP_PATH . '_lib/controller/',
THINKUP_WEBAPP_PATH . '_lib/model/exceptions/',
));

Loader::addPath(THINKUP_ROOT_PATH . 'tests/classes');
$this->assertEqual( Loader::getLookupPath(), array(
THINKUP_WEBAPP_PATH . '_lib/model/',
THINKUP_WEBAPP_PATH . "_lib/model/update/",
THINKUP_WEBAPP_PATH . '_lib/controller/',
THINKUP_WEBAPP_PATH . '_lib/model/exceptions/',
THINKUP_ROOT_PATH . 'tests/classes'
Expand Down
18 changes: 10 additions & 8 deletions tests/TestOfUpdateController.php
Expand Up @@ -3,7 +3,7 @@
*
* ThinkUp/tests/TestOfUpdateController.php
*
* Copyright (c) 2009-2012 Mark Wilkie
* Copyright (c) 2012 Mark Wilkie
*
* LICENSE:
*
Expand All @@ -20,13 +20,11 @@
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* TestOfUpgradeController
* TestOfUpdateController
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2012 Mark Wilkie
* @copyright 2012 Mark Wilkie
* @author Mark Wilkie <mwilkie[at]gmail[dot]com>
*
*/
require_once dirname(__FILE__).'/init.tests.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
Expand Down Expand Up @@ -62,7 +60,6 @@ public function testControlNotLoggedIn() {
public function testException() {
$this->simulateLogin('me@example.com', true);
$controller = new UpdateController(true);

}

public function testReadyToUpdate() {
Expand Down Expand Up @@ -103,10 +100,15 @@ public function testRanUpdate() {
}
}

/**
* Mock Update class
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*
*/
class Update {
static $current_exception = false;
public function run_update() {
if(self::$current_exception == true) {
public function runUpdate() {
if (self::$current_exception == true) {
throw new Exception(self::$current_exception);
} else {
return array('backup' => '/bla/backup-zipfile.zip', 'config' => '/bla/config.inc.backup.php');
Expand Down
3 changes: 2 additions & 1 deletion tests/all_controller_tests.php
Expand Up @@ -68,7 +68,8 @@
"TestOfUpgradeController",
"TestOfPostAPIController",
"TestOfStreamerAuthController",
"TestOfUpdateNowController"
"TestOfUpdateNowController",
"TestOfUpdateController"
);

if (!getenv("TEST_TIMING")=="1") {
Expand Down
1 change: 1 addition & 0 deletions tests/all_model_tests.php
Expand Up @@ -86,6 +86,7 @@
$model_tests->add(new TestOfURLProcessor());
$model_tests->add(new TestOfTableStatsMySQLDAO());
$model_tests->add(new TestOfShortLinkMySQLDAO());
$model_tests->add(new TestOfUpdate());

$tr = new TextReporter();
list($usec, $sec) = explode(" ", microtime());
Expand Down
42 changes: 22 additions & 20 deletions tests/update/TestOfUpdate.php
@@ -1,9 +1,8 @@
<?php
/**
*
* ThinkUp/tests/update/TestOfUpdate.php
*
* Copyright (c) 2009-2012 Mark Wilkie
* Copyright (c) 2012 Mark Wilkie
*
* LICENSE:
*
Expand All @@ -23,9 +22,7 @@
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2012 Mark Wilkie
* @author Mark Wilkie <mwilkie[at]gmail[dot]com>
*
*/

require_once dirname(__FILE__).'/../init.tests.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
require_once THINKUP_WEBAPP_PATH.'config.inc.php';
Expand All @@ -37,33 +34,39 @@ public function setUp() {
parent::setUp();
}


public function tearDown() {
}

public function testAvailableFileSpace() {
$tu_update = new Update();
try {
$tu_update->runUpdate();
$this->fail("Should throw an exception...");
} catch(Exception $e) {
$this->assertPattern('/You are running the latest version of ThinkUp./', $e->getMessage());
}
}

public function testAvailableFileSPace() {
$this->assertTrue(true);
UpdateDiskUtil::$DISK_SPACE_NEEDED = 107374182400 * 10; // 10,000 megs
public function testNotEnoughAvailableFileSpace() {
UpdateDiskUtil::$DISK_SPACE_NEEDED = 107374182400 * 1024 * 10 ; // 10,000 megs
$tu_update = new Update();
try {
$tu_update->run_update();
$tu_update->runUpdate();
$this->fail("Should throw an exception...");
} catch(Exception $e) {
$this->assertPattern('/There is not enough free disk space to perform an update/', $e->getMessage());
}
UpdateDiskUtil::$DISK_SPACE_NEEDED = 107374182400; // set back to 100 megs
$this->assertFalse(0);
}

public function testPermissions() {
chmod( THINKUP_WEBAPP_PATH . 'index.php', 0444 ); // make a file not writeable
$tu_update = new Update();
try {
$tu_update->run_update();
$tu_update->runUpdate();
$this->fail("Should throw an exception...");
} catch(Exception $e) {
$this->assertPattern('/We do not have permissions to write the file/', $e->getMessage());
$this->assertPattern('/is not writable. Unable to continue update./', $e->getMessage());
}
chmod( THINKUP_WEBAPP_PATH . 'index.php', 0644 ); // make a file writeable
}
Expand All @@ -77,7 +80,7 @@ public function testGetLatestInfo() {
// bad url, or bad response...
UpdateClient::$UPDATE_URL = '/badurl/nofile';
try {
$tu_update->run_update();
$tu_update->runUpdate();
$this->fail("Should throw an exception...");
} catch(Exception $e) {
$this->assertEqual('Unable to load latest update info from /badurl/nofile', $e->getMessage());
Expand All @@ -86,7 +89,7 @@ public function testGetLatestInfo() {
// bad json in response data
UpdateClient::$UPDATE_URL = THINKUP_WEBAPP_PATH . '../tests/data/update/bad_update_info.txt';
try {
$tu_update->run_update();
$tu_update->runUpdate();
$this->fail("Should throw an exception...");
} catch(Exception $e) {
$this->assertEqual('Invalid data received from update server: this is not valid json', $e->getMessage());
Expand All @@ -95,10 +98,10 @@ public function testGetLatestInfo() {
// we do not need to update
UpdateClient::$UPDATE_URL = THINKUP_WEBAPP_PATH . '../tests/data/update/old_update_info.txt';
try {
$tu_update->run_update();
$tu_update->runUpdate();
$this->fail("Should throw an exception...");
} catch(Exception $e) {
$this->assertEqual('Thinkup is up to date', $e->getMessage());
$this->assertEqual('You are running the latest version of ThinkUp.', $e->getMessage());
}
// reset
UpdateClient::$UPDATE_URL = $valid_url;
Expand All @@ -113,7 +116,7 @@ public function testGetLatestUpdateFile() {
// we do need to update, but have an bad download url
UpdateClient::$UPDATE_URL = THINKUP_WEBAPP_PATH . '../tests/data/update/new_update_info_bad_url.txt';
try {
$tu_update->run_update();
$tu_update->runUpdate();
$this->fail("Should throw an exception...");
} catch(Exception $e) {
$this->assertEqual('Unable to download latest update file ./webapp/nothing.not', $e->getMessage());
Expand All @@ -125,16 +128,15 @@ public function testGetLatestUpdateFile() {
file_put_contents(UpdateClient::$UPDATE_URL,
'{"version":"100.1", "url":"' . THINKUP_WEBAPP_PATH . '_lib/model/update/class.Update.php"}');
try {
$tu_update->run_update();
$tu_update->runUpdate();
$this->fail("Should throw an exception...");
} catch(Exception $e) {
$this->assertPattern('/Unable to open update zip file, corrupted zip file?/', $e->getMessage());
$this->assertPattern('/Unable to extract/', $e->getMessage());
}
unlink(UpdateClient::$UPDATE_URL);

// reset
UpdateClient::$UPDATE_URL = $valid_url;
error_reporting( $error_reporting ); // reset error reporting
}

}
4 changes: 2 additions & 2 deletions webapp/_lib/controller/class.ThinkUpAuthController.php
Expand Up @@ -36,7 +36,7 @@ public function __construct($session_started=false) {

public function control() {
$response = $this->preAuthControl();
if (!$response) {
if (!$response) {
if ($this->isLoggedIn()) {
return $this->authControl();
} else {
Expand Down Expand Up @@ -67,7 +67,7 @@ protected function bounce() {
$controller = new DashboardController(true);
return $controller->go();
} else {
throw new ControllerAuthException('You must log in to access this controller: ' . get_class($this));
throw new ControllerAuthException('You must log in to access this controller: ' . get_class($this));
}
}
}
13 changes: 8 additions & 5 deletions webapp/_lib/controller/class.ThinkUpController.php
Expand Up @@ -69,17 +69,20 @@ abstract class ThinkUpController {
* @var array
*/
protected $json_data = null;

/**
*
* For testing
* @var str
*/
protected $content_type = 'text/html; charset=UTF-8'; //default

public $redirect_destination;
/**
*
* @var boolean if true we will pass a CSRF token to the view
* @var str
*/
protected $content_type = 'text/html; charset=UTF-8'; //default
/**
*
* @var boolean if true we will pass a CSRF token to the view
*/
protected $view_csrf_token = false; //default

/**
Expand Down
25 changes: 9 additions & 16 deletions webapp/_lib/controller/class.UpdateController.php
Expand Up @@ -3,7 +3,7 @@
*
* ThinkUp/webapp/_lib/controller/class.UpdateController.php
*
* Copyright (c) 2009-2012 Mark Wilkie
* Copyright (c) 2012 Mark Wilkie
*
* LICENSE:
*
Expand All @@ -20,21 +20,18 @@
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* Upgrade Controller
* Web-based Application Update Controller
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2012 Mark Wilkie
* @copyright 2012 Mark Wilkie
* @author Mark Wilkie <mwilkie[at]gmail[dot]com>
*
*/
class UpdateController extends ThinkUpAuthController {

var $updat_obj = null;
/**
* Constructor
* @param bool $session_started
* @return UpgradeController
* @return UpdateController
*/
public function __construct($session_started=false) {
parent::__construct($session_started);
Expand All @@ -43,22 +40,18 @@ public function __construct($session_started=false) {
}

public function authControl() {


if(isset($_GET['run_update'])) {
$backup_info = $this->update_obj->run_update();
if (isset($_GET['run_update'])) {
$backup_info = $this->update_obj->runUpdate();
//var_dump($backup_info);
$site_root_path = Config::getInstance()->getValue('site_root_path');
$redir = $site_root_path . 'install/update.php?ran_update=1';
$this->redirect($redir);
} else if(isset($_GET['ran_update'])) {
} else if (isset($_GET['ran_update'])) {
$this->addToView('updated',true);
// { ["backup"]=> string(75) "/Users/mark/projects/thinkup_test/thinkup/data/1331256968-v1.0.2-backup.zip"
// ["config"]=> string(86) "/Users/mark/projects/thinkup_test/thinkup/data/1331256968-v1.0.2-config.inc.backup.php" }
} else {
$verify_updatable = true;
$update_info = $this->update_obj->run_update($verify_updatable);
if(isset($update_info['latest_version'])) {
$update_info = $this->update_obj->runUpdate($verify_updatable);
if (isset($update_info['latest_version'])) {
$this->addToView('latest_version',$update_info['latest_version']);
}
$this->addToView('updateable',true);
Expand Down

0 comments on commit 6cd03c9

Please sign in to comment.