Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 191 lines (170 sloc) 6.951 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
<?php
/**
*
* ThinkUp/tests/TestOfTestController.php
*
* Copyright (c) 2009-2012 Gina Trapani, Guillaume Boudreau, 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/>.
*
* Test TestController class
*
* TestController isn't a real ThinkUp controller, this is just a template for all Controller tests.
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2012 Gina Trapani, Guillaume Boudreau, Mark Wilkie
* @author Gina Trapani <ginatrapani[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';

class TestOfTestController extends ThinkUpUnitTestCase {

    public function setUp(){
        parent::setUp();
        $config = Config::getInstance();
        $config->setValue('debug', true);
    }

    public function tearDown(){
        parent::tearDown();
    }

    /**
* Test constructor
*/
    public function testConstructor() {
        $controller = new TestController(true);
        $this->assertTrue(isset($controller), 'constructor test');
    }

    /**
* Test controller
* @TODO Possibly load the resulting markup as a DOM object and test various children in it;
* this would enforce valid markup
*/
    public function testControl() {
        $config = Config::getInstance();
        $config->setValue("app_title_prefix", "Angelina Jolie's ");
        $controller = new TestController(true);
        $results = $controller->go();

        $this->assertEqual('text/html; charset=UTF-8', $controller->getContentType());
        //test if view variables were set correctly
        $v_mgr = $controller->getViewManager();
        $this->assertEqual($v_mgr->getTemplateDataItem('test'), 'Testing, testing, 123');
        $this->assertEqual($v_mgr->getTemplateDataItem('app_title'), "Angelina Jolie's ThinkUp");
        $this->debug($results);
        $this->assertEqual($results, '<a href="'.$config->getValue('site_root_path').
        '">Angelina Jolie\'s ThinkUp</a>: Testing, testing, 123 | Not logged in', "controller output");
    }

    /**
* Test cache key, no params
* @TODO Possibly load the resulting markup as a DOM object and test various children in it;
* this would enforce valid markup
*/
    public function testCacheKeyNoRequestParams() {
        $config = Config::getInstance();
        $config->setValue('cache_pages', true);
        $this->debug($config->getValue('datadir_path'));
        $controller = new TestController(true);
        $results = $controller->go();

        $this->assertEqual($controller->getCacheKeyString(), '.httestme.tpl-');
    }

    /**
* Test json output
*/
    public function testJsonOutput() {
        $config = Config::getInstance();
        $controller = new TestController(true);
        $_GET['json'] = true;
        $results = $controller->go();
        unset($_GET['json']);
        $obj = json_decode($results);
        $this->assertIsA($obj, 'stdClass');
        $this->assertEqual($obj->aname, 'a value');
        $this->assertIsA($obj->alist, 'Array');
        $this->assertEqual($obj->alink, 'http://thinkupapp.com');
        $this->assertEqual( $controller->getContentType(),'application/json; charset=UTF-8');
    }

    /**
* Test adding script to header
*/
    public function testAddJsScript() {
        $config = Config::getInstance();
        $controller = new TestController(true);
        $controller->addHeaderJavaScript('plugins/hellothinkup/assets/js/test.js');
        $results = $controller->go();

        //test if view javascript variable is set correctly
        $v_mgr = $controller->getViewManager();
        $scripts = $v_mgr->getTemplateDataItem('header_scripts');
        $this->assertEqual($scripts[0], 'plugins/hellothinkup/assets/js/test.js');
    }

    /**
* Test setting content type header
*/
    public function testAddHeader() {
        $config = Config::getInstance();
        $controller = new TestController(true);
        $_GET['text'] = true;

        $results = $controller->go();
        $this->assertEqual( $controller->getContentType(),'text/plain; charset=UTF-8');
    }

    /**
* Test setting content type header
*/
    public function testAddImageContentTypeHeader() {
        $config = Config::getInstance();
        $controller = new TestController(true);
        $_GET['png'] = true;

        $results = $controller->go();
        $this->assertEqual( $controller->getContentType(),'image/png');
    }

    /**
* Test add CSS 2 header
*/
    public function testAddCSS2Header() {
        $config = Config::getInstance();
        $controller = new TestController(true);
        $_GET['css'] = true;
        $results = $controller->go();
        $this->assertEqual(count($controller->getHeaderCSS()), 1);
        $css = $controller->getHeaderCSS();
        $this->assertEqual($css[0], 'assets/css/bla.css');
        $this->assertPattern('/assets\/css\/bla\.css"/', $results);
    }

    /**
* Test exception handling
*/
    public function testExceptionHandling() {
        $_GET['throwexception'] = 'yesindeedy';
        $controller = new TestController(true);
        $results = $controller->go();

        $v_mgr = $controller->getViewManager();
        $config = Config::getInstance();
        $this->assertEqual('Testing exception handling!', $v_mgr->getTemplateDataItem('error_msg'));
        $this->assertPattern('/<html/', $results);

        $_GET['json'] = true;
        $results = $controller->go();
        $this->debug($results);
        $this->assertFalse(strpos($results, '<html'));
        $this->assertPattern('/{/', $results);
        $this->assertPattern('/Testing exception handling/', $results);
        $this->assertEqual('Exception', $v_mgr->getTemplateDataItem('error_type'));
        unset($_GET['json']);

        $_GET['text'] = true;
        $results = $controller->go();
        $this->assertFalse(strpos($results, '<html'));
        $this->assertFalse(strpos($results, '{'));
        $this->assertPattern('/Testing exception handling/', $results);
        $this->assertEqual('Exception', $v_mgr->getTemplateDataItem('error_type'));
        unset($_GET['text']);
    }
}
Something went wrong with that request. Please try again.