Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Views/errors/html/error_404.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

<p>
<?php if (! empty($message) && $message !== '(null)') : ?>
<?= esc($message) ?>
<?= esc(nl2br($message)) ?>
<?php else : ?>
Sorry! Cannot seem to find the page you were looking for.
<?php endif ?>
Expand Down
2 changes: 1 addition & 1 deletion app/Views/errors/html/error_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="container">
<h1><?= esc($title), esc($exception->getCode() ? ' #' . $exception->getCode() : '') ?></h1>
<p>
<?= esc($exception->getMessage()) ?>
<?= esc(nl2br($exception->getMessage())) ?>
<a href="https://www.duckduckgo.com/?q=<?= urlencode($title . ' ' . preg_replace('#\'.*\'|".*"#Us', '', $exception->getMessage())) ?>"
rel="noreferrer" target="_blank">search &rarr;</a>
</p>
Expand Down
11 changes: 9 additions & 2 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ public function initialize()
//--------------------------------------------------------------------

$this->connectTime = microtime(true);
$connectionErrors = [];

try
{
Expand All @@ -350,6 +351,7 @@ public function initialize()
}
catch (Throwable $e)
{
$connectionErrors[] = sprintf('Main connection [%s]: %s', $this->DBDriver, $e->getMessage());
log_message('error', 'Error connecting to the database: ' . $e->getMessage());
}

Expand All @@ -360,7 +362,7 @@ public function initialize()
if (! empty($this->failover) && is_array($this->failover))
{
// Go over all the failovers
foreach ($this->failover as $failover)
foreach ($this->failover as $index => $failover)
{
// Replace the current settings with those of the failover
foreach ($failover as $key => $val)
Expand All @@ -378,6 +380,7 @@ public function initialize()
}
catch (Throwable $e)
{
$connectionErrors[] = sprintf('Failover #%d [%s]: %s', ++$index, $this->DBDriver, $e->getMessage());
log_message('error', 'Error connecting to the database: ' . $e->getMessage());
}

Expand All @@ -392,7 +395,11 @@ public function initialize()
// We still don't have a connection?
if (! $this->connID)
{
throw new DatabaseException('Unable to connect to the database.');
throw new DatabaseException(sprintf(
'Unable to connect to the database.%s%s',
PHP_EOL,
implode(PHP_EOL, $connectionErrors)
));
}
}

Expand Down
58 changes: 21 additions & 37 deletions tests/system/Database/BaseConnectionTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php namespace CodeIgniter\Database;
<?php

namespace CodeIgniter\Database;

use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;
use Throwable;

class BaseConnectionTest extends \CodeIgniter\Test\CIUnitTestCase
class BaseConnectionTest extends CIUnitTestCase
{
protected $options = [
'DSN' => '',
Expand Down Expand Up @@ -42,8 +47,6 @@ class BaseConnectionTest extends \CodeIgniter\Test\CIUnitTestCase
'failover' => [],
];

//--------------------------------------------------------------------

public function testSavesConfigOptions()
{
$db = new MockConnection($this->options);
Expand All @@ -64,94 +67,75 @@ public function testSavesConfigOptions()
$this->assertSame([], $db->failover);
}

//--------------------------------------------------------------------

public function testConnectionThrowExceptionWhenCannotConnect()
{
$db = new MockConnection($this->options);

$this->expectException('\CodeIgniter\Database\Exceptions\DatabaseException');
$this->expectExceptionMessage('Unable to connect to the database.');

$db->shouldReturn('connect', false)
->initialize();
try
{
$db = new MockConnection($this->options);
$db->shouldReturn('connect', false)->initialize();
}
catch (Throwable $e)
{
$this->assertInstanceOf(DatabaseException::class, $e);
$this->assertStringContainsString('Unable to connect to the database.', $e->getMessage());
}
}

//--------------------------------------------------------------------

public function testCanConnectAndStoreConnection()
{
$db = new MockConnection($this->options);

$db->shouldReturn('connect', 123)
->initialize();
$db->shouldReturn('connect', 123)->initialize();

$this->assertSame(123, $db->getConnection());
}

//--------------------------------------------------------------------

/**
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
* @group single
* @group single
*/
public function testCanConnectToFailoverWhenNoConnectionAvailable()
{
$options = $this->options;
$options['failover'] = [$this->failoverOptions];

$db = new MockConnection($options);

$db->shouldReturn('connect', [false, 345])
->initialize();
$db->shouldReturn('connect', [false, 345])->initialize();

$this->assertSame(345, $db->getConnection());
$this->assertSame('failover', $db->username);
}

//--------------------------------------------------------------------

public function testStoresConnectionTimings()
{
$start = microtime(true);

$db = new MockConnection($this->options);

$db->initialize();

$this->assertGreaterThan($start, $db->getConnectStart());
$this->assertGreaterThan(0.0, $db->getConnectDuration());
}

//--------------------------------------------------------------------

public function testMagicIssetTrue()
{
$db = new MockConnection($this->options);

$this->assertTrue(isset($db->charset));
}

//--------------------------------------------------------------------

public function testMagicIssetFalse()
{
$db = new MockConnection($this->options);

$this->assertFalse(isset($db->foobar));
}

//--------------------------------------------------------------------

public function testMagicGet()
{
$db = new MockConnection($this->options);

$this->assertEquals('utf8', $db->charset);
$this->assertSame('utf8', $db->charset);
}

//--------------------------------------------------------------------

public function testMagicGetMissing()
{
$db = new MockConnection($this->options);
Expand Down