Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JErrorPage Testing #6547

Merged
merged 2 commits into from May 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 16 additions & 16 deletions libraries/cms/error/page.php
Expand Up @@ -35,11 +35,9 @@ public static function render(Exception $error)
if (!$document)
{
// We're probably in an CLI environment
exit($error->getMessage());
jexit($error->getMessage());
}

$config = JFactory::getConfig();

// Get the current template from the application
$template = $app->getTemplate();

Expand All @@ -52,26 +50,28 @@ public static function render(Exception $error)
}

$document->setTitle(JText::_('Error') . ': ' . $error->getCode());

$data = $document->render(
false,
array('template' => $template,
'directory' => JPATH_THEMES,
'debug' => $config->get('debug'))
array(
'template' => $template,
'directory' => JPATH_THEMES,
'debug' => JDEBUG
)
);

// Failsafe to get the error displayed.
// Do not allow cache
$app->allowCache(false);

// If nothing was rendered, just use the message from the Exception
if (empty($data))
{
exit($error->getMessage());
$data = $error->getMessage();
}
else
{
// Do not allow cache
$app->allowCache(false);

$app->setBody($data);
echo $app->toString();
}
$app->setBody($data);

echo $app->toString();
}
catch (Exception $e)
{
Expand All @@ -81,7 +81,7 @@ public static function render(Exception $error)
header('HTTP/1.1 500 Internal Server Error');
}

exit('Error displaying the error page: ' . $e->getMessage() . ': ' . $error->getMessage());
jexit('Error displaying the error page: ' . $e->getMessage() . ': ' . $error->getMessage());
}
}
}
55 changes: 55 additions & 0 deletions tests/unit/suites/libraries/cms/error/JErrorPageTest.php
@@ -0,0 +1,55 @@
<?php
/**
* @package Joomla.UnitTest
* @subpackage Error
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

/**
* Test class for JErrorPage.
*/
class JErrorPageTest extends TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
parent::setUp();

$this->saveFactoryState();

JFactory::$application = $this->getMockCmsApp();
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
$this->restoreFactoryState();

parent::tearDown();
}

/**
* @covers JErrorPage::render
*/
public function testEnsureTheErrorPageIsCorrectlyRendered()
{
// Create an Exception to inject into the method
$exception = new RuntimeException('Testing JErrorPage::render()', 500);

// The render method echoes the output, so catch it in a buffer
ob_start();
JErrorPage::render($exception);
$output = ob_get_clean();

// Validate the <title> element was set correctly
$this->assertContains('<title>500 - Testing JErrorPage::render()</title>', $output);
}
}