From 62133dc1cae7dd222b77a6e37202eb4d82b822ef Mon Sep 17 00:00:00 2001 From: Joshua Sherman Date: Sun, 12 Jan 2014 18:20:30 -0500 Subject: [PATCH] Hacking away to get coverage up Finished up all the low hanging fruit (working the classes I already started that were just shy of 100% coverage). Just shy of 80% coverage at this point. --- classes/Controller.php | 10 +-- tests/bootstrap.php | 3 +- tests/classes/ControllerTest.php | 149 +++++++++++++++++++++++++++++-- tests/classes/DisplayTest.php | 29 ------ 4 files changed, 150 insertions(+), 41 deletions(-) diff --git a/classes/Controller.php b/classes/Controller.php index cde4f37..edee928 100644 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -161,7 +161,7 @@ class Controller extends Object { if (is_array($module_security[$security_level_key])) { - array_merge($module_security_levels, $module_security[$security_level_key]); + $module_security_levels = array_merge($module_security_levels, $module_security[$security_level_key]); } else { @@ -180,7 +180,7 @@ class Controller extends Object { // @todo Thinking of removing this? case 'BETWEEN': - if ($security_level_count >= 2) + if ($security_level_count == 2) { $is_authenticated = Security::betweenLevel($module_security_levels[0], array_pop($module_security_levels)); } @@ -221,10 +221,8 @@ class Controller extends Object // Redirect to login page Browser::redirect('/login'); - // header() updates are a bitch to test, returning - // halts execution so we don't have any output in our - // testing results. - return false; + // Resolves testing error due to undefined $output + $output = ''; } } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 71d4f6e..86ade92 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,7 +11,8 @@ $root = org\bovigo\vfs\vfsStream::setup('site'); if (!defined('SITE_PATH')) { - define('SECURITY_LEVEL_USER', 10); + define('SECURITY_LEVEL_USER', 10); + define('SECURITY_LEVEL_ADMIN', 20); define('SITE_PATH', org\bovigo\vfs\vfsStream::url('site/')); } diff --git a/tests/classes/ControllerTest.php b/tests/classes/ControllerTest.php index 10ac352..f97ed54 100644 --- a/tests/classes/ControllerTest.php +++ b/tests/classes/ControllerTest.php @@ -10,6 +10,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase $this->config->data['pickles']['disabled'] = false; $this->config->data['pickles']['profiler'] = false; $this->config->data['security']['levels'][10] = 'USER'; + $this->config->data['security']['levels'][20] = 'ADMIN'; setUpRequest('home'); @@ -100,7 +101,90 @@ class ControllerTest extends PHPUnit_Framework_TestCase file_put_contents(SITE_MODULE_PATH . 'notauth.php', $module); - @new Controller(); + new Controller(); + + // Compensates for an empty template due to exit() being skipped + $this->expectOutputString('[]'); + + $this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers())); + } + + public function testSecurityArray() + { + setUpRequest('securityarray'); + + $module = ''; + + file_put_contents(SITE_MODULE_PATH . 'securityarray.php', $module); + + new Controller(); + + // Compensates for an empty template due to exit() being skipped + $this->expectOutputString('[]'); + + $this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers())); + } + + public function testSecurityArrayTypeString() + { + setUpRequest('securityarraytypestring'); + + $module = ' "IS", "level" => SECURITY_LEVEL_USER]; } ?>'; + + file_put_contents(SITE_MODULE_PATH . 'securityarraytypestring.php', $module); + + new Controller(); + + // Compensates for an empty template due to exit() being skipped + $this->expectOutputString('[]'); + + $this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers())); + } + + public function testSecurityArrayTypeArray() + { + setUpRequest('securityarraytypearray'); + + $module = ' "IS", "level" => [SECURITY_LEVEL_USER, SECURITY_LEVEL_ADMIN]]; } ?>'; + + file_put_contents(SITE_MODULE_PATH . 'securityarraytypearray.php', $module); + + new Controller(); + + // Compensates for an empty template due to exit() being skipped + $this->expectOutputString('[]'); + + $this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers())); + } + + public function testSecurityArrayTypeBetween() + { + setUpRequest('securityarraytypebetween'); + + $module = ' "BETWEEN", "levels" => [SECURITY_LEVEL_USER, SECURITY_LEVEL_ADMIN]]; } ?>'; + + file_put_contents(SITE_MODULE_PATH . 'securityarraytypebetween.php', $module); + + new Controller(); + + // Compensates for an empty template due to exit() being skipped + $this->expectOutputString('[]'); + + $this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers())); + } + + public function testSecurityArrayTypeHas() + { + setUpRequest('securityarraytypehas'); + + $module = ' "HAS", "level" => SECURITY_LEVEL_USER]; } ?>'; + + file_put_contents(SITE_MODULE_PATH . 'securityarraytypehas.php', $module); + + new Controller(); + + // Compensates for an empty template due to exit() being skipped + $this->expectOutputString('[]'); $this->assertTrue(in_array('Location: http://testsite.com/login', xdebug_get_headers())); } @@ -153,16 +237,32 @@ class ControllerTest extends PHPUnit_Framework_TestCase $this->expectOutputString('{"user":"me"}'); } - public function testBadRequestMethod() + public function testValidRequestMethod() { - setUpRequest('requestmethod'); + setUpRequest('validrequestmethod'); - $module = ' "bar"]; }' + . '} ?>'; + + file_put_contents(SITE_MODULE_PATH . 'validrequestmethod.php', $module); + + new Controller(); + + $this->expectOutputString('{"foo":"bar"}'); + } + + public function testInvalidRequestMethod() + { + setUpRequest('invalidrequestmethod'); + + $module = ' "bar"]; }' . '} ?>'; - file_put_contents(SITE_MODULE_PATH . 'requestmethod.php', $module); + file_put_contents(SITE_MODULE_PATH . 'invalidrequestmethod.php', $module); new Controller(); @@ -215,6 +315,45 @@ class ControllerTest extends PHPUnit_Framework_TestCase new Controller(); } + + public function testTwoValidTemplates() + { + $this->config->data['pickles']['profiler'] = true; + + setUpRequest('validtemplates'); + + $module = ''; + + file_put_contents(SITE_MODULE_PATH . 'validtemplates.php', $module); + + $child_template = SITE_TEMPLATE_PATH . 'validtemplates.phtml'; + file_put_contents($child_template, '
child template
'); + + // Vim syntax highlighting borks unless ----v + $child = 'template; ?' . '>' . "\n"; + + $html = << + + +

parent template

+ {$child} + + +HTML; + + file_put_contents(SITE_TEMPLATE_PATH . '__shared/index.phtml', $html); + + new Controller(); + + $this->expectOutputRegex('/^ + + +

parent template<\/h1> +
child template<\/div> +<\/body> +<\/html>.+