Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 345 lines (292 sloc) 15.581 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
<?php
/**
*
* ThinkUp/tests/TestOfOptionMySQLDAO.php
*
* Copyright (c) 2009-2012 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 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 TestOfOptionMySQLDAO extends ThinkUpUnitTestCase {

    const TEST_TABLE = 'options';

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

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

    public function testCreateOption() {
        // add one option
        $optiondao = new OptionMySQLDAO();
        $optiondao->insertOption('test_namespace', 'test_name', 'test_value');
        $sql = "select * from " . $this->table_prefix . 'options where namespace = ' .
            '\'test_namespace\' order by option_id';
        $stmt = PluginOptionMySQLDAO::$PDO->query($sql);
        $data = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            array_push($data, $row);
        }
        $stmt->closeCursor();
        $this->assertEqual(count($data), 1);
        $data = $data[0];
        $this->assertEqual($data['option_id'], 2);
        $this->assertEqual($data['namespace'], 'test_namespace');
        $this->assertEqual($data['option_name'], 'test_name');
        $this->assertEqual($data['option_value'], 'test_value');
        $this->assertTrue( (strtotime($data['created']) + 10) > time());
        $this->assertTrue( (strtotime($data['last_updated']) + 10) > time());

        // add another with different namespace
        $optiondao->insertOption('test_namespace2', 'test_name', 'test_value');

        $sql = "select * from " . $this->table_prefix . 'options where namespace = ' .
            '\'test_namespace2\' order by option_id';
        $stmt = PluginOptionMySQLDAO::$PDO->query($sql);
        $data = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            array_push($data, $row);
        }
        $stmt->closeCursor();
        $this->assertEqual(count($data), 1);
        $data = $data[0];
        $this->assertEqual($data['option_id'], 3);
        $this->assertEqual($data['namespace'], 'test_namespace2');
        $this->assertEqual($data['option_name'], 'test_name');
        $this->assertEqual($data['option_value'], 'test_value');
        # we'll add a few seconds to the date to account for any lag...
        # our dates should be nowish
        $this->assertTrue( (strtotime($data['created']) + 10) > time());
        $this->assertTrue( (strtotime($data['last_updated']) + 10) > time());
    }

    public function testCreateDuplicateOption() {
        // add one option
        $optiondao = new OptionMySQLDAO();
        $optiondao->insertOption('test_namespace', 'test_name', 'test_value');
        try {
            $optiondao->insertOption('test_namespace', 'test_name', 'test_value');
            $this->fail('Should throw DuplicateOptionException');
        } catch(DuplicateOptionException $e) {
            $this->assertIsA($e, 'DuplicateOptionException');
            $this->assertPattern('/namespace test_namespace and name test_name exists/', $e->getMessage());
        }
    }

    public function testDeleteOptionById() {
        // add one option
        $optiondao = new OptionMySQLDAO();

        // no options delete
        $this->assertEqual(0, $optiondao->deleteOption(2));

        // delete only one row
        $builder1 = FixtureBuilder::build(self::TEST_TABLE);
        $builder2 = FixtureBuilder::build(self::TEST_TABLE);
        $this->assertEqual(1, $optiondao->deleteOption($builder1->columns['last_insert_id']));
    }

    public function testDeleteOptionByName() {
        // add one option
        $optiondao = new OptionMySQLDAO();

        // no options delete
        $this->assertEqual(0, $optiondao->deleteOptionByName('nonamespace', 'noname'));

        // delete only one
        $builder1 = FixtureBuilder::build(self::TEST_TABLE, array('namespace'=>'test', 'option_name'=>'testname') );
        $builder2 = FixtureBuilder::build(self::TEST_TABLE, array('namespace'=>'test2', 'option_name'=>'testname2') );
        $this->assertEqual(1, $optiondao->deleteOption($builder1->columns['last_insert_id']));
    }

    public function testUpdateOptionById() {
        // add one option
        $optiondao = new OptionMySQLDAO();

        // no option to update
        $this->assertEqual(0, $optiondao->updateOption(2, 'value'));

        $builder1 = FixtureBuilder::build(self::TEST_TABLE,
        array('namespace' => 'test', 'option_name' => 'testname', 'created' => '-2d', 'last_updated' => '-2d') );
        $builder2 = FixtureBuilder::build(self::TEST_TABLE,
        array('namespace' => 'test2', 'option_name' => 'testname2', 'created' => '-2d', 'last_updated' => '-2d') );
        $this->assertEqual(1, $optiondao->updateOption($builder1->columns['last_insert_id'], 'test_value123'));

        $sql = "select * from " . $this->table_prefix . 'options where option_id = ' . $builder1->columns['last_insert_id'];
        $stmt = PluginOptionMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch(PDO::FETCH_ASSOC);
        $this->assertEqual($data['option_name'], 'testname');
        $this->assertEqual($data['namespace'], 'test');
        $this->assertEqual($data['option_value'], 'test_value123');
        # we'll add or subtract a few seconds from/to the date to account for any lag...
        # date created is still 2 days old
        $this->assertTrue(strtotime($data['created']) < (time() - (24 * 60 * 60 * 2) + 20000 ), '
'.strtotime($data['created']) . '
' . (time() - (24 * 60 * 60 * 2) + 20000 ));
        # last updated is now
        $this->assertTrue(strtotime($data['last_updated']) > (time() - 10) );
    }

    public function testUpdateOptionByIdWithnameUpdate() {
        // add one option
        $optiondao = new OptionMySQLDAO();

        $builder1 = FixtureBuilder::build(self::TEST_TABLE,
        array('namespace' => 'test', 'option_name' => 'testname', 'created' => '-2d', 'last_updated' => '-2d') );
        $builder2 = FixtureBuilder::build(self::TEST_TABLE,
        array('namespace' => 'test2', 'option_name' => 'testname2', 'created' => '-2d', 'last_updated' => '-2d') );

        $this->assertEqual(1, $optiondao->updateOption($builder1->columns['last_insert_id'], 'test_value123', 'newname'));

        $sql = "select * from " . $this->table_prefix . 'options where option_id = ' . $builder1->columns['last_insert_id'];
        $stmt = PluginOptionMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch(PDO::FETCH_ASSOC);
        $this->assertEqual($data['option_name'], 'newname');
        $this->assertEqual($data['namespace'], 'test');
        $this->assertEqual($data['option_value'], 'test_value123');
        # we'll add or subtract a few seconds from/to the date to account for any lag...
        # date created is still 2 days old
        $this->assertTrue(strtotime($data['created']) < (time() - (24 * 60 * 60 * 2) + 20000 ), '
'.strtotime($data['created']) . '
' . (time() - (24 * 60 * 60 * 2) + 20000 ));
        # last updated is now
        $this->assertTrue(strtotime($data['last_updated']) > (time() - 10) );
    }

    public function testUpdateOptionByName() {
        // add one option
        $optiondao = new OptionMySQLDAO();

        // no options to update
        $this->assertEqual(0, $optiondao->updateOptionbyName('nonamespace', 'noname', 'value'));

        $builder1 = FixtureBuilder::build(self::TEST_TABLE, array('namespace'=>'test', 'option_name'=>'testname') );
        $builder2 = FixtureBuilder::build(self::TEST_TABLE, array('namespace'=>'test2', 'option_name'=>'testname2') );
        $this->assertEqual(1, $optiondao->updateOptionByName('test', 'testname', 'test_value123'));

        $sql = "select * from " . $this->table_prefix . 'options where option_id = ' . $builder1->columns['last_insert_id'];
        $stmt = PluginOptionMySQLDAO::$PDO->query($sql);
        $data = $stmt->fetch(PDO::FETCH_ASSOC);
        $this->assertEqual($data['option_name'], 'testname');
        $this->assertEqual($data['namespace'], 'test');
        $this->assertEqual($data['option_value'], 'test_value123');
        # we'll add or subtract a few seconds from/to the date to account for any lag...
        # date created is still 2 days old
        $this->assertTrue(strtotime($data['created']) < (time() - (24 * 60 * 60 * 2) + 10 ));
        # last updated is now
        $this->assertTrue(strtotime($data['last_updated']) > (time() - 10) );
    }

    public function testGetOptionByName() {
        // add one option
        $optiondao = new OptionMySQLDAO();

        // no options
        $this->assertNull($optiondao->getOptionByName('nonamespace', 'noname'));

        // get option
        $data = array('namespace' => 'test', 'option_name' => 'testname', 'option_value' => 'test_value');
        $builder1 = FixtureBuilder::build(self::TEST_TABLE, $data);
        $option = $optiondao->getOptionByName('test', 'testname');
        $this->assertEqual($option->option_id, $builder1->columns['last_insert_id']);
        $this->assertEqual($option->namespace, 'test');
        $this->assertEqual($option->option_name, 'testname');
        $this->assertEqual($option->option_value, 'test_value');
    }

    public function testGetOptionById() {
        // add one option
        $optiondao = new OptionMySQLDAO();

        // no options
        $this->assertNull( $optiondao->getOption(2) );

        // get option
        $data = array('namespace' => 'test', 'option_name' => 'testname', 'option_value' => 'test_value');
        $builder1 = FixtureBuilder::build(self::TEST_TABLE, $data);
        $option = $optiondao->getOption($builder1->columns['last_insert_id']);
        $this->assertEqual($option->option_id, $builder1->columns['last_insert_id']);
        $this->assertEqual($option->namespace, 'test');
        $this->assertEqual($option->option_name, 'testname');
        $this->assertEqual($option->option_value, 'test_value');
    }

    public function testGetOptionsHash() {
        // add one option
        $optiondao = new OptionMySQLDAO();

        // no options
        $this->assertNull($optiondao->getOptions('nonamespace'));

        // get option
        $data1 = array('namespace' => 'test', 'option_name' => 'testname', 'option_value' => 'test_value');
        $builder1 = FixtureBuilder::build(self::TEST_TABLE, $data1);
        $data2 = array('namespace' => 'test', 'option_name' => 'testname2', 'option_value' => 'test_value2');
        $builder2 = FixtureBuilder::build(self::TEST_TABLE, $data2);
        $data3 = array('namespace' => 'testb', 'option_name' => 'testnameb', 'option_value' => 'test_valueb');
        $builder3 = FixtureBuilder::build(self::TEST_TABLE, $data3);

        $options = $optiondao->getOptions('test');
        $this->assertEqual(count($options), 2);
        $this->assertIsa($options['testname'], 'Option');
        $this->assertIsa($options['testname2'], 'Option');

        $data1 = array('option_id' => $builder1->columns['last_insert_id'], 'namespace' => 'test',
        'option_name' => 'testname', 'option_value' => 'test_value');
        $option1 = new Option($data1);
        $this->assertIdentical($options['testname'], $option1);

        $data2 = array('option_id' => $builder2->columns['last_insert_id'], 'namespace' => 'test',
        'option_name' => 'testname2', 'option_value' => 'test_value2');
        $option2 = new Option($data2);
        $this->assertIdentical($options['testname2'], $option2);
        ;
    }

    public function testGetOptionValue() {
        $optiondao = new OptionMySQLDAO();

        // no option value
        $this->assertNull($optiondao->getOptionValue('nonamespace', 'noname'));

        // get option value
        $data1 = array('namespace' => 'test', 'option_name' => 'testname', 'option_value' => 'test_value');
        $builder1 = FixtureBuilder::build(self::TEST_TABLE, $data1);
        $this->assertEqual($optiondao->getOptionValue('test', 'testname'), 'test_value');
    }

    public function testSession() {
        $optiondao = new OptionMySQLDAO();

        $config = Config::getInstance();
        $app_path = $config->getValue('source_root_path');

        // set session data
        $optiondao->setSessionData('bla', array('name' => 'value'));
        $key = 'options_data:bla';
        $this->assertIdentical(array('name' => 'value'), SessionCache::get($key) );

        // clear session data
        $optiondao->clearSessionData('bla');
        $this->assertFalse(SessionCache::isKeySet($key));

        // get session data
        $this->assertFalse($optiondao->getSessionData('bla')); // no data

        // with data
        SessionCache::put($key, array('name' => 'value') );
        $this->assertIdentical(array('name' => 'value'), $optiondao->getSessionData('bla'));

        // test updates
        $data1 = array('namespace' => 'test', 'option_name' => 'testname', 'option_value' => 'test_value');
        $builder1 = FixtureBuilder::build(self::TEST_TABLE, $data1);
        $options = $optiondao->getOptions('test');
        $this->assertNotNull($options);

        # update by name
        $optiondao->updateOptionByName('test', 'testname', 'test_value123');
        $options = $optiondao->getOptions('test');
        $this->assertEqual($options['testname']->option_value, 'test_value123');

        # update by id
        $optiondao->updateOption($options['testname']->option_id, 'test_value1234');
        $options = $optiondao->getOptions('test');
        $this->assertEqual($options['testname']->option_value, 'test_value1234');

        # delete by name
        $optiondao->deleteOptionByName('test', 'testname');
        $options = $optiondao->getOptions('test');
        $this->assertNull($options);

        # delete by id
        $builder1 = null;
        $builder1 = FixtureBuilder::build(self::TEST_TABLE, $data1);
        $optiondao->deleteOption($builder1->columns['last_insert_id']);
        $options = $optiondao->getOptions('test');
        $this->assertNull($options);
    }

    public function testIsOptionsTable() {
        $optiondao = new OptionMySQLDAO();
        $this->assertTrue($optiondao->isOptionsTable(), 'we have an option table');
        PluginOptionMySQLDAO::$PDO->query("drop table " . $this->table_prefix . 'options');
        $this->assertFalse($optiondao->isOptionsTable(), 'we do not have an option table');
    }
}
Something went wrong with that request. Please try again.