Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #66: Added PluginOptionDAO for, uh, plugin options
- Loading branch information
1 parent
aba56d4
commit 0926a80
Showing
7 changed files
with
328 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
require_once dirname(__FILE__).'/config.tests.inc.php'; | ||
require_once $SOURCE_ROOT_PATH.'extlib/simpletest/autorun.php'; | ||
require_once $SOURCE_ROOT_PATH.'extlib/simpletest/web_tester.php'; | ||
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.$INCLUDE_PATH); | ||
|
||
require_once $SOURCE_ROOT_PATH.'tests/classes/class.ThinkTankUnitTestCase.php'; | ||
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Utils.php'; | ||
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Plugin.php'; | ||
require_once $SOURCE_ROOT_PATH.'tests/fixtures/class.FixtureBuilder.php'; | ||
require_once $SOURCE_ROOT_PATH.'webapp/model/class.DAOFactory.php'; | ||
require_once $SOURCE_ROOT_PATH.'webapp/model/class.PluginOptionMySQLDAO.php'; | ||
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Profiler.php'; | ||
|
||
class TestOfPluginOptionMySQLDAO extends ThinkTankUnitTestCase { | ||
|
||
const TEST_TABLE = 'plugin_options'; | ||
|
||
public function __construct() { | ||
$this->UnitTestCase('PluginMySQLDAO class test'); | ||
} | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
$this->logger = Logger::getInstance(); | ||
$this->config = Config::getInstance(); | ||
$this->prefix = $this->config->getValue('table_prefix'); | ||
} | ||
|
||
public function tearDown() { | ||
parent::tearDown(); | ||
$this->logger->close(); | ||
} | ||
|
||
|
||
public function testDeleteOption() { | ||
# init our dao | ||
$dao = new PluginOptionMySQLDAO(); | ||
|
||
# build some options | ||
$builder1 = $this->buildOptions(1, 'test name', 'test option'); | ||
$builder2 = $this->buildOptions(2, 'test name2', 'test option2'); | ||
$builder3 = $this->buildOptions(3, 'test name3', 'test option3'); | ||
|
||
$insert_id = $builder1->columns[ 'last_insert_id' ]; | ||
$this->assertTrue( $dao->deleteOption( $insert_id ), "delete an option" ); | ||
$sql = "select * from " . $this->prefix . 'plugin_options where id = ' . $insert_id; | ||
$stmt = PluginOptionMysqlDAO::$PDO->query($sql); | ||
$data = $stmt->fetch(); | ||
$this->assertFalse($data, 'should be no plugin option data'); | ||
|
||
$sql = "select count(*) as option_count from " . $this->prefix . 'plugin_options'; | ||
$stmt = PluginOptionMysqlDAO::$PDO->query($sql); | ||
$data = $stmt->fetch(); | ||
$this->assertEqual($data['option_count'], 2, 'we should have two options left'); | ||
|
||
# try and delete a non existent option | ||
$this->assertFalse( $dao->deleteOption( -99 ), "delete an non existent option" ); | ||
|
||
} | ||
|
||
public function testOfInsertOption() { | ||
# init our dao | ||
$dao = new PluginOptionMySQLDAO(); | ||
$this->assertTrue( | ||
$dao->insertOption( 101, 'an option name', 'an option value' ), "added/inserted an option" ); | ||
$sql = "select * from " . $this->prefix . 'plugin_options where plugin_id = 101'; | ||
$stmt = PluginOptionMysqlDAO::$PDO->query($sql); | ||
$data = $stmt->fetch(); | ||
$this->assertEqual($data['id'], 1); | ||
$this->assertEqual($data['plugin_id'], 101); | ||
$this->assertEqual($data['option_name'], 'an option name', 'an option name'); | ||
$this->assertEqual($data['option_value'], 'an option value', 'an option value'); | ||
$sql = "select count(*) as option_count from " . $this->prefix . 'plugin_options'; | ||
$stmt = PluginOptionMysqlDAO::$PDO->query($sql); | ||
$data = $stmt->fetch(); | ||
$this->assertEqual($data['option_count'], 1, 'we should have one option'); | ||
} | ||
|
||
public function testOfUpdateOption() { | ||
# init our dao | ||
$dao = new PluginOptionMySQLDAO(); | ||
$builder1 = $this->buildOptions(1, 'test name', 'test option'); | ||
$insert_id1 = $builder1->columns[ 'last_insert_id' ]; | ||
$builder2 = $this->buildOptions(2, 'test name2', 'test option2'); | ||
$insert_id2 = $builder2->columns[ 'last_insert_id' ]; | ||
|
||
// update with a bad id | ||
$this->assertFalse( $dao->updateOption( -99, 'a name', 'a value', 'nothing updated' ) ); | ||
|
||
// update with valid id | ||
$this->assertTrue( | ||
$dao->updateOption( $insert_id1, 'an option name updated', 'an option value updated' ), | ||
"updated an option" ); | ||
// validate updated data | ||
$sql = "select * from " . $this->prefix . 'plugin_options where id = '. $insert_id1; | ||
$stmt = PluginOptionMysqlDAO::$PDO->query($sql); | ||
$data = $stmt->fetch(); | ||
$this->assertEqual($data['id'], $insert_id1); | ||
$this->assertEqual($data['plugin_id'], $builder1->columns[ 'plugin_id' ]); | ||
$this->assertEqual($data['option_name'], 'an option name updated', 'name updated'); | ||
$this->assertEqual($data['option_value'], 'an option value updated', 'value updated'); | ||
|
||
// make sure we only update data for our id | ||
$sql = "select * from " . $this->prefix . 'plugin_options where id = '. $insert_id2; | ||
$stmt = PluginOptionMysqlDAO::$PDO->query($sql); | ||
$data = $stmt->fetch(); | ||
$this->assertEqual($data['id'], $insert_id2); | ||
$this->assertEqual($data['plugin_id'], $builder2->columns[ 'plugin_id' ]); | ||
$this->assertEqual($data['option_name'], 'test name2', 'name not updated'); | ||
$this->assertEqual($data['option_value'], 'test option2', 'value not updated'); | ||
} | ||
|
||
public function testOfgetOptions() { | ||
# init our dao | ||
$dao = new PluginOptionMySQLDAO(); | ||
$builder1 = $this->buildOptions(1, 'test name', 'test option'); | ||
$insert_id1 = $builder1->columns[ 'last_insert_id' ]; | ||
$builder2 = $this->buildOptions(2, 'test name2', 'test option2'); | ||
$insert_id2 = $builder2->columns[ 'last_insert_id' ]; | ||
$builder3 = $this->buildOptions(2, 'test name3', 'test option3'); | ||
$insert_id3 = $builder3->columns[ 'last_insert_id' ]; | ||
|
||
// bad plugin id | ||
$this->assertNull( $dao->getOptions(-99) ); | ||
|
||
// gets all options if no plugin_id passed | ||
$options = $dao->getOptions(); | ||
$this->assertNotNull( $options ); | ||
$this->assertEqual(3, count($options)); | ||
$this->assertIsA($options[0], 'PluginOption'); | ||
$this->assertEqual($options[0]->plugin_id, 1); | ||
$this->assertEqual($options[1]->plugin_id, 2); | ||
$this->assertEqual($options[2]->plugin_id, 2); | ||
|
||
// gets all options if plugin_id passed | ||
$options = $dao->getOptions(1); | ||
$this->assertNotNull( $options ); | ||
$this->assertEqual(1, count($options)); | ||
$this->assertIsA($options[0], 'PluginOption'); | ||
$this->assertEqual($options[0]->plugin_id, 1); | ||
$this->assertEqual($options[0]->id, 1); | ||
|
||
// gets all options if plugin_id passed one more time | ||
$options = $dao->getOptions(2); | ||
$this->assertNotNull( $options ); | ||
$this->assertEqual(2, count($options)); | ||
$this->assertIsA($options[0], 'PluginOption'); | ||
$this->assertEqual($options[0]->plugin_id, 2); | ||
$this->assertEqual($options[0]->id, 2); | ||
$this->assertIsA($options[1], 'PluginOption'); | ||
$this->assertEqual($options[1]->plugin_id, 2); | ||
$this->assertEqual($options[1]->id, 3); | ||
|
||
} | ||
public function buildOptions($id, $name, $value) { | ||
$plugin_data = array( | ||
'plugin_id' => $id, | ||
'option_name' => $name, | ||
'option_value' => $value | ||
); | ||
$builder = FixtureBuilder::build(self::TEST_TABLE, $plugin_data); | ||
return $builder; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Plugin Option | ||
* | ||
* A ThinkTank plugin option | ||
* | ||
* @author Mark Wilkie <mwilkie[at]gmail[dot]com> | ||
* | ||
*/ | ||
class PluginOption { | ||
/* | ||
* @var int id | ||
*/ | ||
var $id; | ||
|
||
/* | ||
* @var int plugin id | ||
*/ | ||
var $plugin_id; | ||
|
||
/* | ||
* @var str plugin option name | ||
*/ | ||
var $option_name; | ||
|
||
/* | ||
* @var str plugin option value | ||
*/ | ||
var $option_value; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Plugin Option Data Access Object | ||
* The data access object for retrieving and saving plugin option data for thinktank | ||
* @author Mark Wilkie <mwilkie[at]gmail[dot]com> | ||
*/ | ||
|
||
require_once 'model/class.PDODAO.php'; | ||
require_once 'model/interface.PluginOptionDAO.php'; | ||
require_once 'model/class.PluginOption.php'; | ||
require_once 'model/exceptions/class.BadArgumentException.php'; | ||
|
||
class PluginOptionMySQLDAO extends PDODAO implements PluginOptionDAO { | ||
|
||
public function deleteOption($id) { | ||
$q = 'DELETE FROM #prefix#plugin_options WHERE id = :id'; | ||
$stmt = $this->execute($q, array(':id' => $id)); | ||
if ( $this->getUpdateCount($stmt) > 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public function insertOption($plugin_id, $name, $value) { | ||
$q = 'INSERT INTO #prefix#plugin_options | ||
(plugin_id, option_name, option_value) | ||
VALUES | ||
(:plugin_id, :option_name, :option_value)'; | ||
$stmt = $this->execute($q, | ||
array(':plugin_id' => $plugin_id, ':option_name' => $name, ':option_value' => $value) ); | ||
if ( $this->getUpdateCount($stmt) > 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public function updateOption($id, $name, $value) { | ||
$q = 'UPDATE #prefix#plugin_options | ||
SET | ||
option_name = :option_name, | ||
option_value = :option_value | ||
WHERE | ||
id = :id'; | ||
$stmt = $this->execute($q, | ||
array(':id' => $id, ':option_name' => $name, ':option_value' => $value) ); | ||
if ( $this->getUpdateCount($stmt) > 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public function getOptions($plugin_id = null) { | ||
$q = 'SELECT id, plugin_id, option_name, option_value | ||
FROM | ||
#prefix#plugin_options | ||
WHERE '; | ||
$q .= $plugin_id ? 'plugin_id = :plugin_id' : 'TRUE'; | ||
$data = null; | ||
if($plugin_id) { | ||
$data = array(':plugin_id' => $plugin_id); | ||
$stmt = $this->execute($q, $data); | ||
} else { | ||
$stmt = $this->execute($q); | ||
} | ||
$stmt = $this->execute($q, $data); | ||
$options = $this->getDataRowsAsObjects($stmt, 'PluginOption'); | ||
return isset($options[0]) ? $options : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Plugin Data Access Object interface | ||
* | ||
* @author Mark Wilkie <mwilkie[at]gmail[dot]com> | ||
* | ||
*/ | ||
interface PluginOptionDAO { | ||
|
||
/** | ||
* Add/Insert a plugin option by plugin id | ||
* @param int A plugin id | ||
* @param str A plugin option name | ||
* @param int A plugin option value | ||
* @return bool If successful or not | ||
*/ | ||
public function insertOption($option_id, $name, $value); | ||
|
||
/** | ||
* Updates a plugin option by id | ||
* @param int An id | ||
* @param str A plugin option name | ||
* @param int A plugin option value | ||
* @return bool If successful or not | ||
*/ | ||
public function updateOption($id, $name, $value); | ||
|
||
/** | ||
* Gets plugin options | ||
* @param int A plugin id (optional). If not defined returns all options for all plugins | ||
* @return array A list of PluginOption objects | ||
*/ | ||
public function getOptions($plugin_id); | ||
|
||
/** | ||
* Delete a plugin option by id | ||
* @param int A plugin option id | ||
* @return bool If successful or not | ||
*/ | ||
public function deleteOption($option_id); | ||
|
||
} |