Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 412 lines (358 sloc) 16.55 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
<?php
/**
*
* ThinkUp/tests/TestOfPluginMySQLDAO.php
*
* Copyright (c) 2009-2012 Gina Trapani, Mark Wilkie
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
* @author Mark Wilkie <mark[at]bitterpill[dot]org>
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2012 Gina Trapani, Mark Wilkie
*/
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';

class TestOfPluginMySQLDAO extends ThinkUpUnitTestCase {

    const TEST_TABLE = 'plugins';

    public function setUp() {
        parent::setUp();
        $this->logger = Logger::getInstance();
        $this->config = Config::getInstance();
    }

    public function tearDown() {
        parent::tearDown();
        $this->logger->close();
    }

    /**
* For PHP 5.2 compatibility, this method must be public so that we can call usort($plugins,
* 'TestOfPluginMySQLDAO::pluginSort')
* private/self::pluginSort doesn't work in PHP 5.2
*/
    public static function pluginSort($a, $b) {
        return strcmp($a->name, $b->name);
    }

    public function testGetInstalledPlugins() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();

        $plugins = $dao->getInstalledPlugins();
        $this->assertEqual(count($plugins), 7);

        usort($plugins, 'TestOfPluginMySQLDAO::pluginSort');
        $this->assertEqual($plugins[0]->name,"Expand URLs");
        $this->assertEqual($plugins[0]->folder_name,"expandurls");

        $this->assertEqual($plugins[1]->name,"Facebook");
        $this->assertEqual($plugins[1]->folder_name,"facebook");

        $this->assertEqual($plugins[2]->name, "GeoEncoder");
        $this->assertEqual($plugins[2]->folder_name, "geoencoder");

        $this->assertEqual($plugins[3]->name,"Google+");
        $this->assertEqual($plugins[3]->folder_name,"googleplus");

        $this->assertEqual($plugins[4]->name,"Hello ThinkUp");
        $this->assertEqual($plugins[4]->folder_name,"hellothinkup");

        $this->assertEqual($plugins[5]->name,"Twitter");
        $this->assertEqual($plugins[5]->folder_name,"twitter");

        $this->assertEqual($plugins[6]->name,"Twitter Realtime");
        $this->assertEqual($plugins[6]->folder_name,"twitterrealtime");
    }

    public function testInsertPugin() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();
        // get a plugn data object
        $plugin = $this->createPlugin();

        // bad plugin object
        try {
            $dao->insertPlugin('not a plugin object, just a string');
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // no name
        try {
            $plugin = $this->createPlugin();
            $plugin->name = null;
            $dao->insertPlugin($plugin);
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // no folder
        try {
            $plugin = $this->createPlugin();
            $plugin->folder_name = null;
            $dao->insertPlugin($plugin);
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // no is_active
        try {
            $plugin = $this->createPlugin();
            $plugin->is_active = null;
            $dao->insertPlugin($plugin);
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // a good plugin insert
        $plugin = $this->createPlugin();
        $this->assertTrue($dao->insertPlugin($plugin), 'a successful insert');
        $sql = "select * from " . $this->table_prefix . 'plugins where name = "' . $plugin->name . '"';
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->verifyPluginData($data, $plugin);

        // a good plugin insert no homepage
        $plugin = $this->createPlugin();
        $plugin->name = 'has no home page';
        $plugin->homepage = null;
        $this->assertEqual($dao->insertPlugin($plugin), 8);
        $sql = "select * from " . $this->table_prefix . 'plugins where name = "' . $plugin->name . '"';
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->verifyPluginData($data, $plugin);

        // a good plugin insert not active
        $plugin = $this->createPlugin(array('is_active' => false));
        $plugin->name = 'not active';
        $plugin->homepage = null;
        $this->assertEqual($dao->insertPlugin($plugin), 9);
        $sql = "select * from " . $this->table_prefix . 'plugins where name = "' . $plugin->name . '"';
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->verifyPluginData($data, $plugin);
    }

    public function testUpdatePugin() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();

        // bad plugin object
        try {
            $dao->updatePlugin('not a plugin object, just a string');
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // no name
        try {
            $plugin = $this->createPlugin();
            $plugin->name = null;
            $dao->updatePlugin($plugin);
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // no folder
        try {
            $plugin = $this->createPlugin();
            $plugin->folder_name = null;
            $dao->updatePlugin($plugin);
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // no is_active
        try {
            $plugin = $this->createPlugin();
            $plugin->is_active = null;
            $dao->updatePlugin($plugin);
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // no id
        try {
            $plugin = $this->createPlugin();
            $dao->updatePlugin($plugin);
            $this->fail("should throw BadArgumentException");
        } catch(BadArgumentException $e) {
            $this->assertPattern('/requires a valid plugin data object/', $e->getMessage());
        }

        // get a plugin data object to update
        $plugin = $this->createPlugin(array('name' => 'mojo jojo 2', 'folder_name' => 'awesomer, two!!!',
        'version' => '1.5.1'));

        //no record to update
        $plugin->id = -9999;
        $this->assertFalse($dao->updatePlugin($plugin));

        //valid update
        $test_plugin_records = $builders_array[0]->columns;
        $plugin->id = $test_plugin_records['last_insert_id'];
        $this->assertTrue($dao->updatePlugin($plugin));
        $sql = "select * from " . $this->table_prefix . 'plugins where id = ' . $test_plugin_records['last_insert_id'];
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->verifyPluginData($data, $plugin);

        //valid update no description
        $plugin = $this->createPlugin(array('name' => 'mojo jojo 222', 'folder_name' => 'awesomer, two too!!!',
        'version' => '1.5.1.a', 'description' => null));
        $test_plugin_records = $builders_array[0]->columns;
        $plugin->id = $test_plugin_records['last_insert_id'];
        $this->assertTrue($dao->updatePlugin($plugin));
        $sql = "select * from " . $this->table_prefix . 'plugins where id = ' . $test_plugin_records['last_insert_id'];
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->verifyPluginData($data, $plugin);

        //valid update inactive
        $plugin = $this->createPlugin(array('name' => 'mojo jojo 222', 'folder_name' => 'awesomer, two too!!!',
        'version' => '1.5.1.a', 'description' => null, 'is_active' => false));
        $test_plugin_records = $builders_array[0]->columns;
        $plugin->id = $test_plugin_records['last_insert_id'];
        $this->assertTrue($dao->updatePlugin($plugin));
        $sql = "select * from " . $this->table_prefix . 'plugins where id = ' . $test_plugin_records['last_insert_id'];
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->verifyPluginData($data, $plugin);
    }

    public function testSetActive() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();

        // flip form false to true
        $test_plugin_records = $builders_array[0]->columns;
        $id = $test_plugin_records['last_insert_id'];
        $this->assertTrue($dao->setActive($id, true));
        $sql = "select * from " . $this->table_prefix . 'plugins where id = ' . $test_plugin_records['last_insert_id'];
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->assertEqual($data['is_active'], 1);

        // already true
        $test_plugin_records = $builders_array[1]->columns;
        $id = $test_plugin_records['last_insert_id'];
        // nothing updated, so false
        $this->assertFalse($dao->setActive($id, true));
        $sql = "select * from " . $this->table_prefix . 'plugins where id = ' . $test_plugin_records['last_insert_id'];
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->assertEqual($data['is_active'], 1);

        // flip to false
        $test_plugin_records = $builders_array[1]->columns;
        $id = $test_plugin_records['last_insert_id'];
        $this->assertTrue($dao->setActive($id, false));
        $sql = "select * from " . $this->table_prefix . 'plugins where id = ' . $test_plugin_records['last_insert_id'];
        $stmt = PluginMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch();
        $this->assertEqual($data['is_active'], 0);
    }

    public function testIsPluginActive() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();

        $this->assertTrue($dao->isPluginActive(1));
        $this->assertTrue($dao->isPluginActive(2));
        $this->assertFalse($dao->isPluginActive(15));
    }

    public function testGetPluginId() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();

        $this->assertEqual($dao->getPluginId('twitter'), 1);
        $this->assertEqual($dao->getPluginId('idontexist'), null);
        $this->assertEqual($dao->getPluginId('testpluginact'), 6);
    }

    public function testGetPluginFolder() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();

        $this->assertEqual($dao->getPluginFolder(1), 'twitter');
        $this->assertEqual($dao->getPluginFolder(99), null);
        $this->assertEqual($dao->getPluginFolder(6), 'testpluginact');
    }

    public function testGetAllPlugins() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();

        $plugins = $dao->getAllPlugins();
        $this->assertEqual(count($plugins), 6);
        $this->assertEqual($plugins[4]->name, "My Test Plugin");
        $this->assertEqual($plugins[4]->folder_name, "testplugin");

        // no plugins?
        $builders_array = null;
        $this->assertEqual(sizeof($dao->getAllPlugins()), 0);
    }

    public function testGetActivePlugins() {
        // build our data
        $builders_array = $this->buildData();
        // init our dao
        $dao = new PluginMySQLDAO();
        $plugins = $dao->getActivePlugins();

        $this->assertEqual(count($plugins), 5);
        $this->assertEqual($plugins[0]->name,"Twitter");
        $this->assertEqual($plugins[0]->folder_name,"twitter");

        // no plugins?
        $builders_array = null;
        $this->assertEqual(sizeof($dao->getActivePlugins()), 0);
    }

    protected function verifyPluginData($data, $object) {
        if (isset( $object->id )) {
            $this->assertEqual($data['id'], $object->id);
        }
        $this->assertEqual($data['name'], $object->name);
        $this->assertEqual($data['folder_name'], $object->folder_name);
        $this->assertEqual($data['description'], $object->description);
        $this->assertEqual($data['author'], $object->author);
        $this->assertEqual($data['homepage'], $object->homepage);
        $this->assertEqual($data['version'], $object->version);
        $this->assertEqual($data['is_active'] ? true : false, $object->is_active);
    }

    protected function createPlugin($vars = array()) {
        $plugin = new Plugin( array(
           'name' => isset($vars['name']) ? $vars['name'] : "Awesome Plugin!",
           'folder_name' => isset($vars['folder_name']) ? $vars['folder_name'] : 'awesome_folder',
           'description' => isset($vars['description']) ? $vars['description'] : 'Man, what an awesome plugin',
           'author' => isset($vars['author']) ? $vars['author'] : 'Mojo Jojo',
           'homepage' => isset($vars['homepage']) ? $vars['homepage'] : 'http://mojojojo.example.com',
           'version' => isset($vars['version']) ? $vars['version'] : '1.3',
           'is_active' => isset($vars['is_active']) ? $vars['is_active'] : true,
           'icon' => '/awesome.jpg'
           ));
           return $plugin;
    }

    protected function buildData() {
        //Insert test data into test table
        //The default Twitter plugin is inserted by default
        $plugin1 = array(
            'name' => 'My Test Plugin', 'folder_name' => 'testplugin',
            'description' => 'Proof of concept plugin',
            'author' => 'Gina Trapani', 'homepage' => 'http://thinkupapp.com',
            'version' => '0.01', 'is_active' => '0'
            );
            $builder1 = FixtureBuilder::build(self::TEST_TABLE, $plugin1);

            $plugin2 = array(
            'name' => 'My Test Plugin Activated', 'folder_name' => 'testpluginact',
            'description' => 'Proof of concept plugin',
            'author' => 'Gina Trapani', 'homepage' => 'http://thinkupapp.com',
            'version' => '0.01', 'is_active' => '1'
            );
            $builder2 = FixtureBuilder::build(self::TEST_TABLE, $plugin2);
            return array($builder1, $builder2);
    }

    public function testValidatePluginId() {
        // init our dao
        $dao = new PluginMySQLDAO();
        $builder = FixtureBuilder::build('plugins');
        $this->assertFalse($dao->isValidPluginId(-99));
        $this->assertTrue($dao->isValidPluginId( $builder->columns[ 'last_insert_id' ] ));
    }
}
Something went wrong with that request. Please try again.